Ignore:
Timestamp:
May 8, 2019, 12:11:40 PM (6 years ago)
Author:
chronos
Message:
  • Fixed: Build under Lazarus 2.0.
  • Modified: Used .lrj files instead of .lrt files.
  • Removed: TemplateGenerics package.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/UJobProgressView.pas

    r1 r21  
    77uses
    88  SysUtils, Variants, Classes, Graphics, Controls, Forms, Syncobjs,
    9   Dialogs, ComCtrls, StdCtrls, ExtCtrls, Contnrs, UThreading,
     9  Dialogs, ComCtrls, StdCtrls, ExtCtrls, Contnrs, UThreading, Math,
    1010  DateUtils;
    1111
     
    1313  EstimatedTimeShowTreshold = 4;
    1414  EstimatedTimeShowTresholdTotal = 1;
    15   MemoLogHeight = 200;
    1615  UpdateInterval = 100; // ms
    1716
     
    2423    FLock: TCriticalSection;
    2524    FOnChange: TNotifyEvent;
     25    FText: string;
    2626    FValue: Integer;
    2727    FMax: Integer;
    2828    procedure SetMax(const AValue: Integer);
     29    procedure SetText(AValue: string);
    2930    procedure SetValue(const AValue: Integer);
    3031  public
     
    3536    property Value: Integer read FValue write SetValue;
    3637    property Max: Integer read FMax write SetMax;
     38    property Text: string read FText write SetText;
    3739    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    3840  end;
     
    6971  end;
    7072
     73  TJobs = class(TObjectList)
     74  end;
     75
    7176  TJobThread = class(TListedThread)
    7277    procedure Execute; override;
     
    8085  TFormJobProgressView = class(TForm)
    8186    ImageList1: TImageList;
     87    LabelText: TLabel;
    8288    Label2: TLabel;
    8389    LabelOperation: TLabel;
     
    8692    ListViewJobs: TListView;
    8793    MemoLog: TMemo;
     94    PanelText: TPanel;
    8895    PanelProgressTotal: TPanel;
    8996    PanelOperationsTitle: TPanel;
     
    94101    ProgressBarTotal: TProgressBar;
    95102    TimerUpdate: TTimer;
     103    procedure FormHide(Sender: TObject);
     104    procedure FormShow(Sender: TObject);
     105    procedure ReloadJobList;
    96106    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    97107    procedure FormDestroy(Sender: TObject);
     
    100110    procedure FormCreate(Sender: TObject);
    101111    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
     112    procedure UpdateHeight;
    102113  public
    103114    JobProgressView: TJobProgressView;
     
    118129    TotalStartTime: TDateTime;
    119130    Log: TStringList;
     131    FForm: TFormJobProgressView;
    120132    procedure SetTerminate(const AValue: Boolean);
    121133    procedure UpdateProgress;
    122     procedure ReloadJobList;
    123     procedure StartJobs;
    124     procedure UpdateHeight;
    125134    procedure JobProgressChange(Sender: TObject);
    126135  public
    127     Form: TFormJobProgressView;
    128     Jobs: TObjectList; // TListObject<TJob>
     136    Jobs: TJobs;
    129137    CurrentJob: TJob;
    130138    CurrentJobIndex: Integer;
     
    132140    destructor Destroy; override;
    133141    procedure Clear;
    134     procedure AddJob(Title: string; Method: TJobProgressViewMethod;
    135       NoThreaded: Boolean = False; WaitFor: Boolean = False);
    136     procedure Start(AAutoClose: Boolean = True);
     142    function AddJob(Title: string; Method: TJobProgressViewMethod;
     143      NoThreaded: Boolean = False; WaitFor: Boolean = False): TJob;
     144    procedure Start;
    137145    procedure Stop;
    138146    procedure TermSleep(Delay: Integer);
     147    property Form: TFormJobProgressView read FForm;
    139148    property Terminate: Boolean read FTerminate write SetTerminate;
    140149  published
     
    166175  STotalEstimatedTime = 'Total estimated time: %s';
    167176  SFinished = 'Finished';
    168   SOperations = 'Operations';
    169177
    170178procedure Register;
     
    172180  RegisterComponents('Common', [TJobProgressView]);
    173181end;
     182
     183{ TJobThread }
    174184
    175185procedure TJobThread.Execute;
     
    190200end;
    191201
    192 procedure TJobProgressView.AddJob(Title: string; Method: TJobProgressViewMethod;
    193   NoThreaded: Boolean = False; WaitFor: Boolean = False);
     202{ TFormJobProgressView }
     203
     204procedure TFormJobProgressView.UpdateHeight;
    194205var
    195   NewJob: TJob;
    196 begin
    197   NewJob := TJob.Create;
    198   NewJob.ProgressView := Self;
    199   NewJob.Title := Title;
    200   NewJob.Method := Method;
    201   NewJob.NoThreaded := NoThreaded;
    202   NewJob.WaitFor := WaitFor;
    203   NewJob.Progress.Max := 100;
    204   NewJob.Progress.Reset;
    205   NewJob.Progress.OnChange := JobProgressChange;
    206   Jobs.Add(NewJob);
     206  H: Integer;
     207  PanelOperationsVisible: Boolean;
     208  PanelOperationsHeight: Integer;
     209  PanelProgressVisible: Boolean;
     210  PanelProgressTotalVisible: Boolean;
     211  PanelLogVisible: Boolean;
     212  MemoLogHeight: Integer = 200;
     213  I: Integer;
     214  ItemRect: TRect;
     215  MaxH: Integer;
     216begin
     217    H := PanelOperationsTitle.Height;
     218    PanelOperationsVisible := JobProgressView.Jobs.Count > 0;
     219    if PanelOperationsVisible <> PanelOperations.Visible then
     220      PanelOperations.Visible := PanelOperationsVisible;
     221    if ListViewJobs.Items.Count > 0 then begin
     222      Maxh := 0;
     223      for I := 0 to ListViewJobs.Items.Count - 1 do
     224      begin
     225        ItemRect := ListViewJobs.Items[i].DisplayRect(drBounds);
     226        Maxh := Max(Maxh, ItemRect.Top + (ItemRect.Bottom - ItemRect.Top));
     227      end;
     228      PanelOperationsHeight := Scale96ToScreen(12) + Maxh;
     229    end else PanelOperationsHeight := Scale96ToScreen(8);
     230    if PanelOperationsHeight <> PanelOperations.Height then
     231      PanelOperations.Height := PanelOperationsHeight;
     232    if PanelOperationsVisible then
     233      H := H + PanelOperations.Height;
     234
     235    PanelProgressVisible := (JobProgressView.Jobs.Count > 0) and not JobProgressView.Finished;
     236    if PanelProgressVisible <> PanelProgress.Visible then
     237      PanelProgress.Visible := PanelProgressVisible;
     238    if PanelProgressVisible then
     239      H := H + PanelProgress.Height;
     240    PanelProgressTotalVisible := (JobProgressView.Jobs.Count > 1) and not JobProgressView.Finished;
     241    if PanelProgressTotalVisible <> PanelProgressTotal.Visible then
     242      PanelProgressTotal.Visible := PanelProgressTotalVisible;
     243    if PanelProgressTotalVisible then
     244      H := H + PanelProgressTotal.Height;
     245    Constraints.MinHeight := H;
     246    PanelLogVisible := MemoLog.Lines.Count > 0;
     247    if PanelLogVisible <> PanelLog.Visible then
     248      PanelLog.Visible := PanelLogVisible;
     249    if PanelLogVisible then
     250      H := H + Scale96ToScreen(MemoLogHeight);
     251    if PanelText.Visible then
     252      H := H + PanelText.Height;
     253    if Height <> H then begin
     254      Height := H;
     255      Top := (Screen.Height - H) div 2;
     256    end;
     257end;
     258
     259procedure TFormJobProgressView.TimerUpdateTimer(Sender: TObject);
     260var
     261  ProgressBarPartVisible: Boolean;
     262  ProgressBarTotalVisible: Boolean;
     263begin
     264  JobProgressView.UpdateProgress;
     265  if Visible and (not ProgressBarPart.Visible) and
     266  Assigned(JobProgressView.CurrentJob) and
     267  (JobProgressView.CurrentJob.Progress.Value > 0) then begin
     268    ProgressBarPartVisible := True;
     269    if ProgressBarPartVisible <> ProgressBarPart.Visible then
     270      ProgressBarPart.Visible := ProgressBarPartVisible;
     271    ProgressBarTotalVisible := True;
     272    if ProgressBarTotalVisible <> ProgressBarTotal.Visible then
     273      ProgressBarTotal.Visible := ProgressBarTotalVisible;
     274  end;
     275  if not Visible then begin
     276    TimerUpdate.Interval := UpdateInterval;
     277    if not JobProgressView.OwnerDraw then Show;
     278  end;
     279  if Assigned(JobProgressView.CurrentJob) then begin
     280    LabelText.Caption := JobProgressView.CurrentJob.Progress.Text;
     281    if LabelText.Caption <> '' then begin
     282      PanelText.Visible := True;
     283      UpdateHeight;
     284    end;
     285  end;
     286end;
     287
     288procedure TFormJobProgressView.FormDestroy(Sender:TObject);
     289begin
     290end;
     291
     292procedure TFormJobProgressView.ListViewJobsData(Sender: TObject; Item: TListItem);
     293begin
     294  if (Item.Index >= 0) and (Item.Index < JobProgressView.Jobs.Count) then
     295  with TJob(JobProgressView.Jobs[Item.Index]) do begin
     296    Item.Caption := Title;
     297    if Item.Index = JobProgressView.CurrentJobIndex then Item.ImageIndex := 1
     298      else if Finished then Item.ImageIndex := 0
     299      else Item.ImageIndex := 2;
     300    Item.Data := JobProgressView.Jobs[Item.Index];
     301  end;
     302end;
     303
     304procedure TFormJobProgressView.FormClose(Sender: TObject;
     305  var CloseAction: TCloseAction);
     306begin
     307end;
     308
     309procedure TFormJobProgressView.FormCreate(Sender: TObject);
     310begin
     311  Caption := SPleaseWait;
     312  try
     313    //Animate1.FileName := ExtractFileDir(UTF8Encode(Application.ExeName)) +
     314    //  DirectorySeparator + 'horse.avi';
     315    //Animate1.Active := True;
     316  except
     317
     318  end;
     319end;
     320
     321procedure TFormJobProgressView.ReloadJobList;
     322begin
     323  // Workaround for not showing first line
     324  //Form.ListViewJobs.Items.Count := Jobs.Count + 1;
     325  //Form.ListViewJobs.Refresh;
     326
     327  if ListViewJobs.Items.Count <> JobProgressView.Jobs.Count then
     328    ListViewJobs.Items.Count := JobProgressView.Jobs.Count;
     329  ListViewJobs.Refresh;
     330  Application.ProcessMessages;
     331  UpdateHeight;
     332end;
     333
     334procedure TFormJobProgressView.FormShow(Sender: TObject);
     335begin
     336  ReloadJobList;
     337end;
     338
     339procedure TFormJobProgressView.FormHide(Sender: TObject);
     340begin
     341  JobProgressView.Jobs.Clear;
     342  ReloadJobList;
     343end;
     344
     345procedure TFormJobProgressView.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
     346begin
     347  CanClose := JobProgressView.Finished;
     348  JobProgressView.Terminate := True;
     349  Caption := SPleaseWait + STerminate;
     350end;
     351
     352
     353{ TJobProgressView }
     354
     355function TJobProgressView.AddJob(Title: string; Method: TJobProgressViewMethod;
     356  NoThreaded: Boolean = False; WaitFor: Boolean = False): TJob;
     357begin
     358  Result := TJob.Create;
     359  Result.ProgressView := Self;
     360  Result.Title := Title;
     361  Result.Method := Method;
     362  Result.NoThreaded := NoThreaded;
     363  Result.WaitFor := WaitFor;
     364  Result.Progress.Max := 100;
     365  Result.Progress.Reset;
     366  Result.Progress.OnChange := JobProgressChange;
     367  Jobs.Add(Result);
    207368  //ReloadJobList;
    208369end;
    209370
    210 procedure TJobProgressView.Start(AAutoClose: Boolean = True);
    211 begin
    212   AutoClose := AAutoClose;
    213   StartJobs;
    214 end;
    215 
    216 procedure TJobProgressView.StartJobs;
     371procedure TJobProgressView.Start;
    217372var
    218373  I: Integer;
     
    229384    Form.MemoLog.Clear;
    230385
     386    Form.PanelText.Visible := False;
    231387    Form.LabelEstimatedTimePart.Visible := False;
    232388    Form.LabelEstimatedTimeTotal.Visible := False;
     
    258414      Form.ProgressBarPart.Visible := False;
    259415      //Show;
    260       ReloadJobList;
     416      Form.ReloadJobList;
    261417      Application.ProcessMessages;
    262418      if NoThreaded then begin
     
    296452    //if Visible then Hide;
    297453    Form.MemoLog.Lines.Assign(Log);
    298     if (Form.MemoLog.Lines.Count = 0) and AutoClose then begin
     454    if (Form.MemoLog.Lines.Count = 0) and FAutoClose then begin
    299455      Form.Hide;
    300456    end;
    301     Clear;
     457    if not Form.Visible then Clear;
    302458    Form.Caption := SFinished;
    303459    //LabelEstimatedTimePart.Visible := False;
    304460    Finished := True;
    305461    CurrentJobIndex := -1;
    306     ReloadJobList;
    307   end;
    308 end;
    309 
    310 procedure TJobProgressView.UpdateHeight;
    311 var
    312   H: Integer;
    313   PanelOperationsVisible: Boolean;
    314   PanelOperationsHeight: Integer;
    315   PanelProgressVisible: Boolean;
    316   PanelProgressTotalVisible: Boolean;
    317   PanelLogVisible: Boolean;
    318 begin
    319   with Form do begin
    320   H := PanelOperationsTitle.Height;
    321   PanelOperationsVisible := Jobs.Count > 0;
    322   if PanelOperationsVisible <> PanelOperations.Visible then
    323     PanelOperations.Visible := PanelOperationsVisible;
    324   PanelOperationsHeight := 8 + 18 * Jobs.Count;
    325   if PanelOperationsHeight <> PanelOperations.Height then
    326     PanelOperations.Height := PanelOperationsHeight;
    327   if PanelOperationsVisible then
    328     H := H + PanelOperations.Height;
    329 
    330   PanelProgressVisible := (Jobs.Count > 0) and not Finished;
    331   if PanelProgressVisible <> PanelProgress.Visible then
    332     PanelProgress.Visible := PanelProgressVisible;
    333   if PanelProgressVisible then
    334     H := H + PanelProgress.Height;
    335   PanelProgressTotalVisible := (Jobs.Count > 1) and not Finished;
    336   if PanelProgressTotalVisible <> PanelProgressTotal.Visible then
    337     PanelProgressTotal.Visible := PanelProgressTotalVisible;
    338   if PanelProgressTotalVisible then
    339     H := H + PanelProgressTotal.Height;
    340   Constraints.MinHeight := H;
    341   PanelLogVisible := MemoLog.Lines.Count > 0;
    342   if PanelLogVisible <> PanelLog.Visible then
    343     PanelLog.Visible := PanelLogVisible;
    344   if PanelLogVisible then
    345     H := H + MemoLogHeight;
    346   if Height <> H then Height := H;
     462    Form.ReloadJobList;
    347463  end;
    348464end;
     
    352468  if Assigned(FOnOwnerDraw) then
    353469    FOnOwnerDraw(Self);
    354 end;
    355 
    356 procedure TFormJobProgressView.TimerUpdateTimer(Sender: TObject);
    357 var
    358   ProgressBarPartVisible: Boolean;
    359   ProgressBarTotalVisible: Boolean;
    360 begin
    361   JobProgressView.UpdateProgress;
    362   if Visible and (not ProgressBarPart.Visible) and
    363   Assigned(JobProgressView.CurrentJob) and
    364   (JobProgressView.CurrentJob.Progress.Value > 0) then begin
    365     ProgressBarPartVisible := True;
    366     if ProgressBarPartVisible <> ProgressBarPart.Visible then
    367       ProgressBarPart.Visible := ProgressBarPartVisible;
    368     ProgressBarTotalVisible := True;
    369     if ProgressBarTotalVisible <> ProgressBarTotal.Visible then
    370       ProgressBarTotal.Visible := ProgressBarTotalVisible;
    371   end;
    372   if not Visible then begin
    373     TimerUpdate.Interval := UpdateInterval;
    374     if not JobProgressView.OwnerDraw then Show;
    375   end;
    376 end;
    377 
    378 procedure TFormJobProgressView.FormDestroy(Sender:TObject);
    379 begin
    380 end;
    381 
    382 procedure TFormJobProgressView.ListViewJobsData(Sender: TObject; Item: TListItem);
    383 begin
    384   if (Item.Index >= 0) and (Item.Index < JobProgressView.Jobs.Count) then
    385   with TJob(JobProgressView.Jobs[Item.Index]) do begin
    386     Item.Caption := Title;
    387     if Item.Index = JobProgressView.CurrentJobIndex then Item.ImageIndex := 1
    388       else if Finished then Item.ImageIndex := 0
    389       else Item.ImageIndex := 2;
    390     Item.Data := JobProgressView.Jobs[Item.Index];
    391   end;
    392 end;
    393 
    394 procedure TFormJobProgressView.FormClose(Sender: TObject;
    395   var CloseAction: TCloseAction);
    396 begin
    397   ListViewJobs.Clear;
    398 end;
    399 
    400 procedure TFormJobProgressView.FormCreate(Sender: TObject);
    401 begin
    402   Caption := SPleaseWait;
    403   try
    404     //Animate1.FileName := ExtractFileDir(UTF8Encode(Application.ExeName)) +
    405     //  DirectorySeparator + 'horse.avi';
    406     //Animate1.Active := True;
    407   except
    408 
    409   end;
    410470end;
    411471
     
    426486    Sleep(Quantum);
    427487  end;
    428 end;
    429 
    430 procedure TFormJobProgressView.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    431 begin
    432   CanClose := JobProgressView.Finished;
    433   JobProgressView.Terminate := True;
    434   Caption := SPleaseWait + STerminate;
    435488end;
    436489
     
    490543end;
    491544
    492 procedure TJobProgressView.ReloadJobList;
    493 begin
    494   UpdateHeight;
    495   // Workaround for not showing first line
    496   Form.ListViewJobs.Items.Count := Jobs.Count + 1;
    497   Form.ListViewJobs.Refresh;
    498 
    499   if Form.ListViewJobs.Items.Count <> Jobs.Count then
    500     Form.ListViewJobs.Items.Count := Jobs.Count;
    501   Form.ListViewJobs.Refresh;
    502   //Application.ProcessMessages;
    503 end;
    504 
    505545constructor TJobProgressView.Create(TheOwner: TComponent);
    506546begin
    507547  inherited;
    508548  if not (csDesigning in ComponentState) then begin
    509     Form := TFormJobProgressView.Create(Self);
    510     Form.JobProgressView := Self;
    511   end;
    512   Jobs := TObjectList.Create;
     549    FForm := TFormJobProgressView.Create(Self);
     550    FForm.JobProgressView := Self;
     551  end;
     552  Jobs := TJobs.Create;
    513553  Log := TStringList.Create;
    514554  //PanelOperationsTitle.Height := 80;
    515   ShowDelay := 0; //1000; // ms
     555  AutoClose := True;
     556  ShowDelay := 0;
    516557end;
    517558
     
    519560begin
    520561  Jobs.Clear;
     562  Log.Clear;
    521563  //ReloadJobList;
    522564end;
     
    528570  inherited;
    529571end;
     572
     573{ TProgress }
    530574
    531575procedure TProgress.SetMax(const AValue: Integer);
     
    536580    if FMax < 1 then FMax := 1;
    537581    if FValue >= FMax then FValue := FMax;
     582  finally
     583    FLock.Release;
     584  end;
     585end;
     586
     587procedure TProgress.SetText(AValue: string);
     588begin
     589  try
     590    FLock.Acquire;
     591    if FText = AValue then Exit;
     592    FText := AValue;
    538593  finally
    539594    FLock.Release;
     
    563618end;
    564619
    565 { TProgress }
    566 
    567620procedure TProgress.Increment;
    568621begin
Note: See TracChangeset for help on using the changeset viewer.