Ignore:
Timestamp:
Jun 29, 2018, 11:44:07 PM (6 years ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
  • Modified: Updated IPTV prices.
  • Added: deb package build script.
File:
1 edited

Legend:

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

    r89 r122  
    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;
     
    111122    Finished: Boolean;
    112123    FOnJobFinish: TJobProgressViewMethod;
     124    FOnOwnerDraw: TNotifyEvent;
     125    FOwnerDraw: Boolean;
    113126    FShowDelay: Integer;
    114127    FTerminate: Boolean;
     
    116129    TotalStartTime: TDateTime;
    117130    Log: TStringList;
    118     Form: TFormJobProgressView;
     131    FForm: TFormJobProgressView;
    119132    procedure SetTerminate(const AValue: Boolean);
    120133    procedure UpdateProgress;
    121     procedure ReloadJobList;
    122     procedure StartJobs;
    123     procedure UpdateHeight;
     134    procedure JobProgressChange(Sender: TObject);
    124135  public
    125     Jobs: TObjectList; // TListObject<TJob>
     136    Jobs: TJobs;
    126137    CurrentJob: TJob;
    127138    CurrentJobIndex: Integer;
     
    129140    destructor Destroy; override;
    130141    procedure Clear;
    131     procedure AddJob(Title: string; Method: TJobProgressViewMethod;
    132       NoThreaded: Boolean = False; WaitFor: Boolean = False);
    133     procedure Start(AAutoClose: Boolean = True);
     142    function AddJob(Title: string; Method: TJobProgressViewMethod;
     143      NoThreaded: Boolean = False; WaitFor: Boolean = False): TJob;
     144    procedure Start;
    134145    procedure Stop;
    135146    procedure TermSleep(Delay: Integer);
     147    property Form: TFormJobProgressView read FForm;
    136148    property Terminate: Boolean read FTerminate write SetTerminate;
    137149  published
     150    property OwnerDraw: Boolean read FOwnerDraw write FOwnerDraw;
    138151    property ShowDelay: Integer read FShowDelay write FShowDelay;
    139152    property AutoClose: Boolean read FAutoClose write FAutoClose;
    140153    property OnJobFinish: TJobProgressViewMethod read FOnJobFinish
    141154      write FOnJobFinish;
     155    property OnOwnerDraw: TNotifyEvent read FOnOwnerDraw
     156      write FOnOwnerDraw;
    142157  end;
    143158
     
    160175  STotalEstimatedTime = 'Total estimated time: %s';
    161176  SFinished = 'Finished';
     177  SOperations = 'Operations:';
    162178
    163179procedure Register;
    164180begin
    165   RegisterComponents('Samples', [TJobProgressView]);
    166 end;
     181  RegisterComponents('Common', [TJobProgressView]);
     182end;
     183
     184{ TJobThread }
    167185
    168186procedure TJobThread.Execute;
     
    183201end;
    184202
    185 procedure TJobProgressView.AddJob(Title: string; Method: TJobProgressViewMethod;
    186   NoThreaded: Boolean = False; WaitFor: Boolean = False);
     203{ TFormJobProgressView }
     204
     205procedure TFormJobProgressView.UpdateHeight;
    187206var
    188   NewJob: TJob;
    189 begin
    190   NewJob := TJob.Create;
    191   NewJob.ProgressView := Self;
    192   NewJob.Title := Title;
    193   NewJob.Method := Method;
    194   NewJob.NoThreaded := NoThreaded;
    195   NewJob.WaitFor := WaitFor;
    196   NewJob.Progress.Max := 100;
    197   NewJob.Progress.Reset;
    198   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);
    199369  //ReloadJobList;
    200370end;
    201371
    202 procedure TJobProgressView.Start(AAutoClose: Boolean = True);
    203 begin
    204   AutoClose := AAutoClose;
    205   StartJobs;
    206 end;
    207 
    208 procedure TJobProgressView.StartJobs;
     372procedure TJobProgressView.Start;
    209373var
    210374  I: Integer;
     
    212376  Terminate := False;
    213377
    214   Form.BringToFront;
     378  if not OwnerDraw then Form.BringToFront;
    215379
    216380  Finished := False;
     
    221385    Form.MemoLog.Clear;
    222386
     387    Form.PanelText.Visible := False;
    223388    Form.LabelEstimatedTimePart.Visible := False;
    224389    Form.LabelEstimatedTimeTotal.Visible := False;
     
    244409      CurrentJobIndex := I;
    245410      CurrentJob := TJob(Jobs[I]);
     411      JobProgressChange(Self);
    246412      StartTime := Now;
    247413      Form.LabelEstimatedTimePart.Caption := Format(SEstimatedTime, ['']);
     
    249415      Form.ProgressBarPart.Visible := False;
    250416      //Show;
    251       ReloadJobList;
     417      Form.ReloadJobList;
    252418      Application.ProcessMessages;
    253419      if NoThreaded then begin
     
    287453    //if Visible then Hide;
    288454    Form.MemoLog.Lines.Assign(Log);
    289     if (Form.MemoLog.Lines.Count = 0) and AutoClose then begin
     455    if (Form.MemoLog.Lines.Count = 0) and FAutoClose then begin
    290456      Form.Hide;
    291457    end;
    292     Clear;
     458    if not Form.Visible then Clear;
    293459    Form.Caption := SFinished;
    294460    //LabelEstimatedTimePart.Visible := False;
    295461    Finished := True;
    296462    CurrentJobIndex := -1;
    297     ReloadJobList;
    298   end;
    299 end;
    300 
    301 procedure TJobProgressView.UpdateHeight;
    302 var
    303   H: Integer;
    304   PanelOperationsVisible: Boolean;
    305   PanelOperationsHeight: Integer;
    306   PanelProgressVisible: Boolean;
    307   PanelProgressTotalVisible: Boolean;
    308   PanelLogVisible: Boolean;
    309 begin
    310   with Form do begin
    311   H := PanelOperationsTitle.Height;
    312   PanelOperationsVisible := Jobs.Count > 0;
    313   if PanelOperationsVisible <> PanelOperations.Visible then
    314     PanelOperations.Visible := PanelOperationsVisible;
    315   PanelOperationsHeight := 8 + 18 * Jobs.Count;
    316   if PanelOperationsHeight <> PanelOperations.Height then
    317     PanelOperations.Height := PanelOperationsHeight;
    318   if PanelOperationsVisible then
    319     H := H + PanelOperations.Height;
    320 
    321   PanelProgressVisible := (Jobs.Count > 0) and not Finished;
    322   if PanelProgressVisible <> PanelProgress.Visible then
    323     PanelProgress.Visible := PanelProgressVisible;
    324   if PanelProgressVisible then
    325     H := H + PanelProgress.Height;
    326   PanelProgressTotalVisible := (Jobs.Count > 1) and not Finished;
    327   if PanelProgressTotalVisible <> PanelProgressTotal.Visible then
    328     PanelProgressTotal.Visible := PanelProgressTotalVisible;
    329   if PanelProgressTotalVisible then
    330     H := H + PanelProgressTotal.Height;
    331   Constraints.MinHeight := H;
    332   PanelLogVisible := MemoLog.Lines.Count > 0;
    333   if PanelLogVisible <> PanelLog.Visible then
    334     PanelLog.Visible := PanelLogVisible;
    335   if PanelLogVisible then
    336     H := H + MemoLogHeight;
    337   if Height <> H then Height := H;
    338   end;
    339 end;
    340 
    341 procedure TFormJobProgressView.TimerUpdateTimer(Sender: TObject);
    342 var
    343   ProgressBarPartVisible: Boolean;
    344   ProgressBarTotalVisible: Boolean;
    345 begin
    346   JobProgressView.UpdateProgress;
    347   if Visible and (not ProgressBarPart.Visible) and
    348   Assigned(JobProgressView.CurrentJob) and
    349   (JobProgressView.CurrentJob.Progress.Value > 0) then begin
    350     ProgressBarPartVisible := True;
    351     if ProgressBarPartVisible <> ProgressBarPart.Visible then
    352       ProgressBarPart.Visible := ProgressBarPartVisible;
    353     ProgressBarTotalVisible := True;
    354     if ProgressBarTotalVisible <> ProgressBarTotal.Visible then
    355       ProgressBarTotal.Visible := ProgressBarTotalVisible;
    356   end;
    357   if not Visible then begin
    358     TimerUpdate.Interval := UpdateInterval;
    359     Show;
    360   end;
    361 end;
    362 
    363 procedure TFormJobProgressView.FormDestroy(Sender:TObject);
    364 begin
    365 end;
    366 
    367 procedure TFormJobProgressView.ListViewJobsData(Sender: TObject; Item: TListItem);
    368 begin
    369   if (Item.Index >= 0) and (Item.Index < JobProgressView.Jobs.Count) then
    370   with TJob(JobProgressView.Jobs[Item.Index]) do begin
    371     Item.Caption := Title;
    372     if Item.Index = JobProgressView.CurrentJobIndex then Item.ImageIndex := 1
    373       else if Finished then Item.ImageIndex := 0
    374       else Item.ImageIndex := 2;
    375     Item.Data := JobProgressView.Jobs[Item.Index];
    376   end;
    377 end;
    378 
    379 procedure TFormJobProgressView.FormClose(Sender: TObject;
    380   var CloseAction: TCloseAction);
    381 begin
    382   ListViewJobs.Clear;
    383 end;
    384 
    385 procedure TFormJobProgressView.FormCreate(Sender: TObject);
    386 begin
    387   Caption := SPleaseWait;
    388   try
    389     //Animate1.FileName := ExtractFileDir(UTF8Encode(Application.ExeName)) +
    390     //  DirectorySeparator + 'horse.avi';
    391     //Animate1.Active := True;
    392   except
    393 
    394   end;
     463    Form.ReloadJobList;
     464  end;
     465end;
     466
     467procedure TJobProgressView.JobProgressChange(Sender: TObject);
     468begin
     469  if Assigned(FOnOwnerDraw) then
     470    FOnOwnerDraw(Self);
    395471end;
    396472
     
    411487    Sleep(Quantum);
    412488  end;
    413 end;
    414 
    415 procedure TFormJobProgressView.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    416 begin
    417   CanClose := JobProgressView.Finished;
    418   JobProgressView.Terminate := True;
    419   Caption := SPleaseWait + STerminate;
    420489end;
    421490
     
    475544end;
    476545
    477 procedure TJobProgressView.ReloadJobList;
    478 begin
    479   UpdateHeight;
    480   // Workaround for not showing first line
    481   Form.ListViewJobs.Items.Count := Jobs.Count + 1;
    482   Form.ListViewJobs.Refresh;
    483 
    484   if Form.ListViewJobs.Items.Count <> Jobs.Count then
    485     Form.ListViewJobs.Items.Count := Jobs.Count;
    486   Form.ListViewJobs.Refresh;
    487   //Application.ProcessMessages;
    488 end;
    489 
    490546constructor TJobProgressView.Create(TheOwner: TComponent);
    491547begin
    492548  inherited;
    493549  if not (csDesigning in ComponentState) then begin
    494     Form := TFormJobProgressView.Create(Self);
    495     Form.JobProgressView := Self;
    496   end;
    497   Jobs := TObjectList.Create;
     550    FForm := TFormJobProgressView.Create(Self);
     551    FForm.JobProgressView := Self;
     552  end;
     553  Jobs := TJobs.Create;
    498554  Log := TStringList.Create;
    499555  //PanelOperationsTitle.Height := 80;
    500   ShowDelay := 0; //1000; // ms
     556  AutoClose := True;
     557  ShowDelay := 0;
    501558end;
    502559
     
    504561begin
    505562  Jobs.Clear;
     563  Log.Clear;
    506564  //ReloadJobList;
    507565end;
     
    509567destructor TJobProgressView.Destroy;
    510568begin
    511   Log.Free;
    512   Jobs.Free;
    513   inherited Destroy;
    514 end;
     569  FreeAndNil(Log);
     570  FreeAndNil(Jobs);
     571  inherited;
     572end;
     573
     574{ TProgress }
    515575
    516576procedure TProgress.SetMax(const AValue: Integer);
     
    519579    FLock.Acquire;
    520580    FMax := AValue;
     581    if FMax < 1 then FMax := 1;
    521582    if FValue >= FMax then FValue := FMax;
     583  finally
     584    FLock.Release;
     585  end;
     586end;
     587
     588procedure TProgress.SetText(AValue: string);
     589begin
     590  try
     591    FLock.Acquire;
     592    if FText = AValue then Exit;
     593    FText := AValue;
    522594  finally
    523595    FLock.Release;
     
    547619end;
    548620
    549 { TProgress }
    550 
    551621procedure TProgress.Increment;
    552622begin
     
    610680begin
    611681  Progress.Free;
    612   inherited Destroy;
     682  inherited;
    613683end;
    614684
Note: See TracChangeset for help on using the changeset viewer.