Changeset 513


Ignore:
Timestamp:
Apr 19, 2018, 5:07:40 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Code cleanup.
  • Modified: Center progress dialog to center of screen even with multiple jobs.
Location:
Common
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • Common/Demo/JobProgressView/JobProgressViewDemo.lpi

    r512 r513  
    8080        <Filename Value="UFormMain.pas"/>
    8181        <IsPartOfProject Value="True"/>
    82         <ComponentName Value="Form1"/>
     82        <ComponentName Value="FormMain"/>
    8383        <ResourceBaseClass Value="Form"/>
    8484      </Unit1>
  • Common/Demo/JobProgressView/JobProgressViewDemo.lpr

    r512 r513  
    1616  RequireDerivedFormResource:=True;
    1717  Application.Initialize;
    18   Application.CreateForm(TForm1, Form1);
     18  Application.CreateForm(TFormMain, FormMain);
    1919  Application.Run;
    2020end.
  • Common/Demo/JobProgressView/UFormMain.lfm

    r512 r513  
    1 object Form1: TForm1
    2   Left = 515
     1object FormMain: TFormMain
     2  Left = 501
    33  Height = 480
    4   Top = 252
     4  Top = 296
    55  Width = 743
    66  Caption = 'JobProgressView demo'
     
    8989    ShowDelay = 0
    9090    AutoClose = False
    91     left = 488
    92     top = 72
     91    left = 480
     92    top = 120
    9393  end
    9494end
  • Common/Demo/JobProgressView/UFormMain.pas

    r512 r513  
    1111type
    1212
    13   { TForm1 }
     13  { TFormMain }
    1414
    15   TForm1 = class(TForm)
     15  TFormMain = class(TForm)
    1616    ButtonTest: TButton;
    1717    CheckBoxAutoClose: TCheckBox;
     
    3232
    3333var
    34   Form1: TForm1;
     34  FormMain: TFormMain;
    3535
    3636implementation
     
    3838{$R *.lfm}
    3939
    40 { TForm1 }
     40{ TFormMain }
    4141
    42 procedure TForm1.ButtonTestClick(Sender: TObject);
     42procedure TFormMain.ButtonTestClick(Sender: TObject);
    4343var
    4444  I: Integer;
    4545begin
     46  ButtonTest.Enabled := False;
    4647  with JobProgressView1 do begin
    4748    AutoClose := CheckBoxAutoClose.Checked;
     
    5253    Start;
    5354  end;
     55  ButtonTest.Enabled := True;
    5456end;
    5557
    56 procedure TForm1.JobMethod(Job: TJob);
     58procedure TFormMain.JobMethod(Job: TJob);
    5759var
    5860  Count: Integer;
  • Common/UJobProgressView.pas

    r512 r513  
    129129    TotalStartTime: TDateTime;
    130130    Log: TStringList;
     131    FForm: TFormJobProgressView;
    131132    procedure SetTerminate(const AValue: Boolean);
    132133    procedure UpdateProgress;
    133134    procedure JobProgressChange(Sender: TObject);
    134135  public
    135     Form: TFormJobProgressView;
    136136    Jobs: TJobs;
    137137    CurrentJob: TJob;
     
    140140    destructor Destroy; override;
    141141    procedure Clear;
    142     procedure AddJob(Title: string; Method: TJobProgressViewMethod;
    143       NoThreaded: Boolean = False; WaitFor: Boolean = False);
     142    function AddJob(Title: string; Method: TJobProgressViewMethod;
     143      NoThreaded: Boolean = False; WaitFor: Boolean = False): TJob;
    144144    procedure Start;
    145145    procedure Stop;
    146146    procedure TermSleep(Delay: Integer);
     147    property Form: TFormJobProgressView read FForm;
    147148    property Terminate: Boolean read FTerminate write SetTerminate;
    148149  published
     
    181182end;
    182183
     184{ TJobThread }
     185
    183186procedure TJobThread.Execute;
    184187begin
     
    198201end;
    199202
    200 procedure TJobProgressView.AddJob(Title: string; Method: TJobProgressViewMethod;
    201   NoThreaded: Boolean = False; WaitFor: Boolean = False);
     203{ TFormJobProgressView }
     204
     205procedure TFormJobProgressView.UpdateHeight;
    202206var
    203   NewJob: TJob;
    204 begin
    205   NewJob := TJob.Create;
    206   NewJob.ProgressView := Self;
    207   NewJob.Title := Title;
    208   NewJob.Method := Method;
    209   NewJob.NoThreaded := NoThreaded;
    210   NewJob.WaitFor := WaitFor;
    211   NewJob.Progress.Max := 100;
    212   NewJob.Progress.Reset;
    213   NewJob.Progress.OnChange := JobProgressChange;
    214   Jobs.Add(NewJob);
     207  H: Integer;
     208  PanelOperationsVisible: Boolean;
     209  PanelOperationsHeight: Integer;
     210  PanelProgressVisible: Boolean;
     211  PanelProgressTotalVisible: Boolean;
     212  PanelLogVisible: Boolean;
     213  MemoLogHeight: Integer = 200;
     214  I: Integer;
     215  ItemRect: TRect;
     216  MaxH: Integer;
     217begin
     218    H := PanelOperationsTitle.Height;
     219    PanelOperationsVisible := JobProgressView.Jobs.Count > 0;
     220    if PanelOperationsVisible <> PanelOperations.Visible then
     221      PanelOperations.Visible := PanelOperationsVisible;
     222    if ListViewJobs.Items.Count > 0 then begin
     223      Maxh := 0;
     224      for I := 0 to ListViewJobs.Items.Count - 1 do
     225      begin
     226        ItemRect := ListViewJobs.Items[i].DisplayRect(drBounds);
     227        Maxh := Max(Maxh, ItemRect.Top + (ItemRect.Bottom - ItemRect.Top));
     228      end;
     229      PanelOperationsHeight := Scale96ToScreen(12) + Maxh;
     230    end else PanelOperationsHeight := Scale96ToScreen(8);
     231    if PanelOperationsHeight <> PanelOperations.Height then
     232      PanelOperations.Height := PanelOperationsHeight;
     233    if PanelOperationsVisible then
     234      H := H + PanelOperations.Height;
     235
     236    PanelProgressVisible := (JobProgressView.Jobs.Count > 0) and not JobProgressView.Finished;
     237    if PanelProgressVisible <> PanelProgress.Visible then
     238      PanelProgress.Visible := PanelProgressVisible;
     239    if PanelProgressVisible then
     240      H := H + PanelProgress.Height;
     241    PanelProgressTotalVisible := (JobProgressView.Jobs.Count > 1) and not JobProgressView.Finished;
     242    if PanelProgressTotalVisible <> PanelProgressTotal.Visible then
     243      PanelProgressTotal.Visible := PanelProgressTotalVisible;
     244    if PanelProgressTotalVisible then
     245      H := H + PanelProgressTotal.Height;
     246    Constraints.MinHeight := H;
     247    PanelLogVisible := MemoLog.Lines.Count > 0;
     248    if PanelLogVisible <> PanelLog.Visible then
     249      PanelLog.Visible := PanelLogVisible;
     250    if PanelLogVisible then
     251      H := H + Scale96ToScreen(MemoLogHeight);
     252    if PanelText.Visible then
     253      H := H + PanelText.Height;
     254    if Height <> H then begin
     255      Height := H;
     256      Top := (Screen.Height - H) div 2;
     257    end;
     258end;
     259
     260procedure TFormJobProgressView.TimerUpdateTimer(Sender: TObject);
     261var
     262  ProgressBarPartVisible: Boolean;
     263  ProgressBarTotalVisible: Boolean;
     264begin
     265  JobProgressView.UpdateProgress;
     266  if Visible and (not ProgressBarPart.Visible) and
     267  Assigned(JobProgressView.CurrentJob) and
     268  (JobProgressView.CurrentJob.Progress.Value > 0) then begin
     269    ProgressBarPartVisible := True;
     270    if ProgressBarPartVisible <> ProgressBarPart.Visible then
     271      ProgressBarPart.Visible := ProgressBarPartVisible;
     272    ProgressBarTotalVisible := True;
     273    if ProgressBarTotalVisible <> ProgressBarTotal.Visible then
     274      ProgressBarTotal.Visible := ProgressBarTotalVisible;
     275  end;
     276  if not Visible then begin
     277    TimerUpdate.Interval := UpdateInterval;
     278    if not JobProgressView.OwnerDraw then Show;
     279  end;
     280  if Assigned(JobProgressView.CurrentJob) then begin
     281    LabelText.Caption := JobProgressView.CurrentJob.Progress.Text;
     282    if LabelText.Caption <> '' then begin
     283      PanelText.Visible := True;
     284      UpdateHeight;
     285    end;
     286  end;
     287end;
     288
     289procedure TFormJobProgressView.FormDestroy(Sender:TObject);
     290begin
     291end;
     292
     293procedure TFormJobProgressView.ListViewJobsData(Sender: TObject; Item: TListItem);
     294begin
     295  if (Item.Index >= 0) and (Item.Index < JobProgressView.Jobs.Count) then
     296  with TJob(JobProgressView.Jobs[Item.Index]) do begin
     297    Item.Caption := Title;
     298    if Item.Index = JobProgressView.CurrentJobIndex then Item.ImageIndex := 1
     299      else if Finished then Item.ImageIndex := 0
     300      else Item.ImageIndex := 2;
     301    Item.Data := JobProgressView.Jobs[Item.Index];
     302  end;
     303end;
     304
     305procedure TFormJobProgressView.FormClose(Sender: TObject;
     306  var CloseAction: TCloseAction);
     307begin
     308end;
     309
     310procedure TFormJobProgressView.FormCreate(Sender: TObject);
     311begin
     312  Caption := SPleaseWait;
     313  try
     314    //Animate1.FileName := ExtractFileDir(UTF8Encode(Application.ExeName)) +
     315    //  DirectorySeparator + 'horse.avi';
     316    //Animate1.Active := True;
     317  except
     318
     319  end;
     320end;
     321
     322procedure TFormJobProgressView.ReloadJobList;
     323begin
     324  // Workaround for not showing first line
     325  //Form.ListViewJobs.Items.Count := Jobs.Count + 1;
     326  //Form.ListViewJobs.Refresh;
     327
     328  if ListViewJobs.Items.Count <> JobProgressView.Jobs.Count then
     329    ListViewJobs.Items.Count := JobProgressView.Jobs.Count;
     330  ListViewJobs.Refresh;
     331  Application.ProcessMessages;
     332  UpdateHeight;
     333end;
     334
     335procedure TFormJobProgressView.FormShow(Sender: TObject);
     336begin
     337  ReloadJobList;
     338end;
     339
     340procedure TFormJobProgressView.FormHide(Sender: TObject);
     341begin
     342  JobProgressView.Jobs.Clear;
     343  ReloadJobList;
     344end;
     345
     346procedure TFormJobProgressView.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
     347begin
     348  CanClose := JobProgressView.Finished;
     349  JobProgressView.Terminate := True;
     350  Caption := SPleaseWait + STerminate;
     351end;
     352
     353
     354{ TJobProgressView }
     355
     356function TJobProgressView.AddJob(Title: string; Method: TJobProgressViewMethod;
     357  NoThreaded: Boolean = False; WaitFor: Boolean = False): TJob;
     358begin
     359  Result := TJob.Create;
     360  Result.ProgressView := Self;
     361  Result.Title := Title;
     362  Result.Method := Method;
     363  Result.NoThreaded := NoThreaded;
     364  Result.WaitFor := WaitFor;
     365  Result.Progress.Max := 100;
     366  Result.Progress.Reset;
     367  Result.Progress.OnChange := JobProgressChange;
     368  Jobs.Add(Result);
    215369  //ReloadJobList;
    216370end;
     
    302456      Form.Hide;
    303457    end;
    304     //Clear;
     458    if not Form.Visible then Clear;
    305459    Form.Caption := SFinished;
    306460    //LabelEstimatedTimePart.Visible := False;
     
    311465end;
    312466
    313 procedure TFormJobProgressView.UpdateHeight;
    314 var
    315   H: Integer;
    316   PanelOperationsVisible: Boolean;
    317   PanelOperationsHeight: Integer;
    318   PanelProgressVisible: Boolean;
    319   PanelProgressTotalVisible: Boolean;
    320   PanelLogVisible: Boolean;
    321   MemoLogHeight: Integer = 200;
    322   I: Integer;
    323   ItemRect: TRect;
    324   MaxH: Integer;
    325 begin
    326     H := PanelOperationsTitle.Height;
    327     PanelOperationsVisible := JobProgressView.Jobs.Count > 0;
    328     if PanelOperationsVisible <> PanelOperations.Visible then
    329       PanelOperations.Visible := PanelOperationsVisible;
    330     if ListViewJobs.Items.Count > 0 then begin
    331       Maxh := 0;
    332       for I := 0 to ListViewJobs.Items.Count - 1 do
    333       begin
    334         ItemRect := ListViewJobs.Items[i].DisplayRect(drBounds);
    335         Maxh := Max(Maxh, ItemRect.Top + (ItemRect.Bottom - ItemRect.Top));
    336       end;
    337       PanelOperationsHeight := Scale96ToScreen(12) + Maxh;
    338     end else PanelOperationsHeight := Scale96ToScreen(8);
    339     if PanelOperationsHeight <> PanelOperations.Height then
    340       PanelOperations.Height := PanelOperationsHeight;
    341     if PanelOperationsVisible then
    342       H := H + PanelOperations.Height;
    343 
    344     PanelProgressVisible := (JobProgressView.Jobs.Count > 0) and not JobProgressView.Finished;
    345     if PanelProgressVisible <> PanelProgress.Visible then
    346       PanelProgress.Visible := PanelProgressVisible;
    347     if PanelProgressVisible then
    348       H := H + PanelProgress.Height;
    349     PanelProgressTotalVisible := (JobProgressView.Jobs.Count > 1) and not JobProgressView.Finished;
    350     if PanelProgressTotalVisible <> PanelProgressTotal.Visible then
    351       PanelProgressTotal.Visible := PanelProgressTotalVisible;
    352     if PanelProgressTotalVisible then
    353       H := H + PanelProgressTotal.Height;
    354     Constraints.MinHeight := H;
    355     PanelLogVisible := MemoLog.Lines.Count > 0;
    356     if PanelLogVisible <> PanelLog.Visible then
    357       PanelLog.Visible := PanelLogVisible;
    358     if PanelLogVisible then
    359       H := H + Scale96ToScreen(MemoLogHeight);
    360     if PanelText.Visible then
    361       H := H + PanelText.Height;
    362     if Height <> H then Height := H;
    363 end;
    364 
    365467procedure TJobProgressView.JobProgressChange(Sender: TObject);
    366468begin
    367469  if Assigned(FOnOwnerDraw) then
    368470    FOnOwnerDraw(Self);
    369 end;
    370 
    371 procedure TFormJobProgressView.TimerUpdateTimer(Sender: TObject);
    372 var
    373   ProgressBarPartVisible: Boolean;
    374   ProgressBarTotalVisible: Boolean;
    375 begin
    376   JobProgressView.UpdateProgress;
    377   if Visible and (not ProgressBarPart.Visible) and
    378   Assigned(JobProgressView.CurrentJob) and
    379   (JobProgressView.CurrentJob.Progress.Value > 0) then begin
    380     ProgressBarPartVisible := True;
    381     if ProgressBarPartVisible <> ProgressBarPart.Visible then
    382       ProgressBarPart.Visible := ProgressBarPartVisible;
    383     ProgressBarTotalVisible := True;
    384     if ProgressBarTotalVisible <> ProgressBarTotal.Visible then
    385       ProgressBarTotal.Visible := ProgressBarTotalVisible;
    386   end;
    387   if not Visible then begin
    388     TimerUpdate.Interval := UpdateInterval;
    389     if not JobProgressView.OwnerDraw then Show;
    390   end;
    391   if Assigned(JobProgressView.CurrentJob) then begin
    392     LabelText.Caption := JobProgressView.CurrentJob.Progress.Text;
    393     if LabelText.Caption <> '' then begin
    394       PanelText.Visible := True;
    395       UpdateHeight;
    396     end;
    397   end;
    398 end;
    399 
    400 procedure TFormJobProgressView.FormDestroy(Sender:TObject);
    401 begin
    402 end;
    403 
    404 procedure TFormJobProgressView.ListViewJobsData(Sender: TObject; Item: TListItem);
    405 begin
    406   if (Item.Index >= 0) and (Item.Index < JobProgressView.Jobs.Count) then
    407   with TJob(JobProgressView.Jobs[Item.Index]) do begin
    408     Item.Caption := Title;
    409     if Item.Index = JobProgressView.CurrentJobIndex then Item.ImageIndex := 1
    410       else if Finished then Item.ImageIndex := 0
    411       else Item.ImageIndex := 2;
    412     Item.Data := JobProgressView.Jobs[Item.Index];
    413   end;
    414 end;
    415 
    416 procedure TFormJobProgressView.FormClose(Sender: TObject;
    417   var CloseAction: TCloseAction);
    418 begin
    419 end;
    420 
    421 procedure TFormJobProgressView.FormCreate(Sender: TObject);
    422 begin
    423   Caption := SPleaseWait;
    424   try
    425     //Animate1.FileName := ExtractFileDir(UTF8Encode(Application.ExeName)) +
    426     //  DirectorySeparator + 'horse.avi';
    427     //Animate1.Active := True;
    428   except
    429 
    430   end;
    431 end;
    432 
    433 procedure TFormJobProgressView.ReloadJobList;
    434 begin
    435   // Workaround for not showing first line
    436   //Form.ListViewJobs.Items.Count := Jobs.Count + 1;
    437   //Form.ListViewJobs.Refresh;
    438 
    439   if ListViewJobs.Items.Count <> JobProgressView.Jobs.Count then
    440     ListViewJobs.Items.Count := JobProgressView.Jobs.Count;
    441   ListViewJobs.Refresh;
    442   Application.ProcessMessages;
    443   UpdateHeight;
    444 end;
    445 
    446 procedure TFormJobProgressView.FormShow(Sender: TObject);
    447 begin
    448   ReloadJobList;
    449 end;
    450 
    451 procedure TFormJobProgressView.FormHide(Sender: TObject);
    452 begin
    453   JobProgressView.Jobs.Clear;
    454   ReloadJobList;
    455471end;
    456472
     
    471487    Sleep(Quantum);
    472488  end;
    473 end;
    474 
    475 procedure TFormJobProgressView.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    476 begin
    477   CanClose := JobProgressView.Finished;
    478   JobProgressView.Terminate := True;
    479   Caption := SPleaseWait + STerminate;
    480489end;
    481490
     
    539548  inherited;
    540549  if not (csDesigning in ComponentState) then begin
    541     Form := TFormJobProgressView.Create(Self);
    542     Form.JobProgressView := Self;
     550    FForm := TFormJobProgressView.Create(Self);
     551    FForm.JobProgressView := Self;
    543552  end;
    544553  Jobs := TJobs.Create;
    545554  Log := TStringList.Create;
    546555  //PanelOperationsTitle.Height := 80;
    547   ShowDelay := 0; //1000; // ms
     556  AutoClose := True;
     557  ShowDelay := 0;
    548558end;
    549559
     
    551561begin
    552562  Jobs.Clear;
     563  Log.Clear;
    553564  //ReloadJobList;
    554565end;
     
    560571  inherited;
    561572end;
     573
     574{ TProgress }
    562575
    563576procedure TProgress.SetMax(const AValue: Integer);
     
    606619end;
    607620
    608 { TProgress }
    609 
    610621procedure TProgress.Increment;
    611622begin
Note: See TracChangeset for help on using the changeset viewer.