Changeset 368 for Common


Ignore:
Timestamp:
May 29, 2012, 7:46:31 AM (12 years ago)
Author:
chronos
Message:
  • Modified: TJobProgressView is now TComponent descendant registred in component pallete group Samples. Component placed by user to form or datamodule replace original global variable JobProgressView. Usage of global variable wasn't proper object oriented approach.
Location:
Common
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • Common/Common.lpk

    r364 r368  
    7474      <Item13>
    7575        <Filename Value="UJobProgressView.pas"/>
     76        <HasRegisterProc Value="True"/>
    7677        <UnitName Value="UJobProgressView"/>
    7778      </Item13>
  • Common/Common.pas

    r364 r368  
    1717begin
    1818  RegisterUnit('UDebugLog', @UDebugLog.Register);
     19  RegisterUnit('UJobProgressView', @UJobProgressView.Register);
    1920end;
    2021
  • Common/UJobProgressView.lfm

    r364 r368  
    1 object JobProgressView: TJobProgressView
     1object FormJobProgressView: TFormJobProgressView
    22  Left = 466
    33  Height = 248
  • Common/UJobProgressView.pas

    r364 r368  
    3838  end;
    3939
     40  TFormJobProgressView = class;
    4041  TJobProgressView = class;
    4142  TJobThread = class;
    4243  TJob = class;
    4344
    44   TJobProgressViewMethod = procedure of object;
     45  TJobProgressViewMethod = procedure(Job: TJob) of object;
    4546
    4647  { TJob }
     
    7677  end;
    7778
    78   { TJobProgressView }
    79 
    80   TJobProgressView = class(TForm)
     79  { TFormJobProgressView }
     80
     81  TFormJobProgressView = class(TForm)
    8182    ImageList1: TImageList;
    8283    Label2: TLabel;
     
    100101    procedure FormCreate(Sender: TObject);
    101102    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
     103  public
     104    JobProgressView: TJobProgressView;
     105  end;
     106
     107  { TJobProgressView }
     108
     109  TJobProgressView = class(TComponent)
    102110  private
     111    FAutoClose: Boolean;
    103112    Finished: Boolean;
    104113    FOnJobFinish: TJobProgressViewMethod;
     114    FShowDelay: Integer;
    105115    FTerminate: Boolean;
    106116    FormList: TList;
     117    TotalStartTime: TDateTime;
    107118    Log: TStringList;
     119    Form: TFormJobProgressView;
    108120    procedure SetTerminate(const AValue: Boolean);
    109121    procedure UpdateProgress;
     
    112124    procedure UpdateHeight;
    113125  public
    114     TotalStartTime: TDateTime;
    115126    Jobs: TObjectList; // TListObject<TJob>
    116127    CurrentJob: TJob;
    117128    CurrentJobIndex: Integer;
    118     ShowDelay: Integer;
    119     AutoClose: Boolean;
    120129    constructor Create(TheOwner: TComponent); override;
    121130    destructor Destroy; override;
     
    127136    procedure TermSleep(Delay: Integer);
    128137    property Terminate: Boolean read FTerminate write SetTerminate;
     138  published
     139    property ShowDelay: Integer read FShowDelay write FShowDelay;
     140    property AutoClose: Boolean read FAutoClose write FAutoClose;
    129141    property OnJobFinish: TJobProgressViewMethod read FOnJobFinish
    130142      write FOnJobFinish;
    131143  end;
    132144
    133   var
    134     JobProgressView: TJobProgressView;
     145  //var
     146  //  FormJobProgressView: TFormJobProgressView;
     147
     148procedure Register;
    135149
    136150resourcestring
     
    159173    try
    160174      //raise Exception.Create('Exception in job');
    161       JobProgressView.CurrentJob.Method;
     175      ProgressView.CurrentJob.Method(Job);
    162176    except
    163177      on E: Exception do begin
     
    201215  Terminate := False;
    202216
    203   BringToFront;
     217  Form.BringToFront;
    204218
    205219  Finished := False;
    206   Caption := SPleaseWait;
     220  Form.Caption := SPleaseWait;
    207221  try
    208     FormList := Screen.DisableForms(Self);
     222    FormList := Screen.DisableForms(Form);
    209223    Log.Clear;
    210     MemoLog.Clear;
    211 
    212     LabelEstimatedTimePart.Visible := False;
    213     LabelEstimatedTimeTotal.Visible := False;
     224    Form.MemoLog.Clear;
     225
     226    Form.LabelEstimatedTimePart.Visible := False;
     227    Form.LabelEstimatedTimeTotal.Visible := False;
    214228
    215229    CurrentJob := nil;
    216230    if ShowDelay = 0 then begin
    217       TimerUpdate.Interval := UpdateInterval;
    218       TimerUpdate.Enabled := True;
    219       TimerUpdateTimer(Self);
     231      Form.TimerUpdate.Interval := UpdateInterval;
     232      Form.TimerUpdate.Enabled := True;
     233      Form.TimerUpdateTimer(Self);
    220234    end else begin
    221       TimerUpdate.Interval := ShowDelay;
    222       TimerUpdate.Enabled := True;
     235      Form.TimerUpdate.Interval := ShowDelay;
     236      Form.TimerUpdate.Enabled := True;
    223237    end;
    224238
    225239    TotalStartTime := Now;
    226     ProgressBarTotal.Position := 0;
    227     ProgressBarTotal.Visible := False;
     240    Form.ProgressBarTotal.Position := 0;
     241    Form.ProgressBarTotal.Visible := False;
    228242    //UpdateHeight;
    229243
     
    234248      CurrentJob := TJob(Jobs[I]);
    235249      StartTime := Now;
    236       LabelEstimatedTimePart.Caption := Format(SEstimatedTime, ['']);
    237       ProgressBarPart.Position := 0;
    238       ProgressBarPart.Visible := False;
     250      Form.LabelEstimatedTimePart.Caption := Format(SEstimatedTime, ['']);
     251      Form.ProgressBarPart.Position := 0;
     252      Form.ProgressBarPart.Visible := False;
    239253      //Show;
    240254      ReloadJobList;
     
    242256      if NoThreaded then begin
    243257        Thread := nil;
    244         Method;
     258        Method(CurrentJob);
    245259      end else begin
    246260        try
     
    262276        end;
    263277      end;
    264       ProgressBarPart.Hide;
     278      Form.ProgressBarPart.Hide;
    265279      if Terminate then Break;
    266280      EndTime := Now;
    267281      Finished := True;
    268282      if Assigned(FOnJobFinish) then
    269         FOnJobFinish;
     283        FOnJobFinish(CurrentJob);
    270284      Inc(I);
    271285    end;
    272286  finally
    273287    CurrentJob := nil;
    274     TimerUpdate.Enabled := False;
     288    Form.TimerUpdate.Enabled := False;
    275289    Screen.EnableForms(FormList);
    276290    //if Visible then Hide;
    277     MemoLog.Lines.Assign(Log);
    278     if (MemoLog.Lines.Count = 0) and AutoClose then begin
    279       Hide;
     291    Form.MemoLog.Lines.Assign(Log);
     292    if (Form.MemoLog.Lines.Count = 0) and AutoClose then begin
     293      Form.Hide;
    280294    end;
    281295    Clear;
    282     Caption := SFinished;
     296    Form.Caption := SFinished;
    283297    //LabelEstimatedTimePart.Visible := False;
    284298    Finished := True;
     
    297311  PanelLogVisible: Boolean;
    298312begin
     313  with Form do begin
    299314  H := PanelOperationsTitle.Height;
    300315  PanelOperationsVisible := Jobs.Count > 0;
     
    324339    H := H + MemoLogHeight;
    325340  if Height <> H then Height := H;
    326 end;
    327 
    328 procedure TJobProgressView.TimerUpdateTimer(Sender: TObject);
     341  end;
     342end;
     343
     344procedure TFormJobProgressView.TimerUpdateTimer(Sender: TObject);
    329345var
    330346  ProgressBarPartVisible: Boolean;
    331347  ProgressBarTotalVisible: Boolean;
    332348begin
    333   UpdateProgress;
    334   if Visible and (not ProgressBarPart.Visible) and Assigned(CurrentJob) and
    335   (CurrentJob.Progress.Value > 0) then begin
     349  JobProgressView.UpdateProgress;
     350  if Visible and (not ProgressBarPart.Visible) and
     351  Assigned(JobProgressView.CurrentJob) and
     352  (JobProgressView.CurrentJob.Progress.Value > 0) then begin
    336353    ProgressBarPartVisible := True;
    337354    if ProgressBarPartVisible <> ProgressBarPart.Visible then
     
    347364end;
    348365
    349 procedure TJobProgressView.FormDestroy(Sender:TObject);
    350 begin
    351 end;
    352 
    353 procedure TJobProgressView.ListViewJobsData(Sender: TObject; Item: TListItem);
    354 begin
    355   if (Item.Index >= 0) and (Item.Index < Jobs.Count) then
    356   with TJob(Jobs[Item.Index]) do begin
     366procedure TFormJobProgressView.FormDestroy(Sender:TObject);
     367begin
     368end;
     369
     370procedure TFormJobProgressView.ListViewJobsData(Sender: TObject; Item: TListItem);
     371begin
     372  if (Item.Index >= 0) and (Item.Index < JobProgressView.Jobs.Count) then
     373  with TJob(JobProgressView.Jobs[Item.Index]) do begin
    357374    Item.Caption := Title;
    358     if Item.Index = CurrentJobIndex then Item.ImageIndex := 1
     375    if Item.Index = JobProgressView.CurrentJobIndex then Item.ImageIndex := 1
    359376      else if Finished then Item.ImageIndex := 0
    360377      else Item.ImageIndex := 2;
    361     Item.Data := Jobs[Item.Index];
    362   end;
    363 end;
    364 
    365 procedure TJobProgressView.FormClose(Sender: TObject;
     378    Item.Data := JobProgressView.Jobs[Item.Index];
     379  end;
     380end;
     381
     382procedure TFormJobProgressView.FormClose(Sender: TObject;
    366383  var CloseAction: TCloseAction);
    367384begin
     
    369386end;
    370387
    371 procedure TJobProgressView.FormCreate(Sender: TObject);
     388procedure TFormJobProgressView.FormCreate(Sender: TObject);
    372389begin
    373390  Caption := SPleaseWait;
     
    399416end;
    400417
    401 procedure TJobProgressView.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    402 begin
    403   CanClose := Finished;
    404   Terminate := True;
     418procedure TFormJobProgressView.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
     419begin
     420  CanClose := JobProgressView.Finished;
     421  JobProgressView.Terminate := True;
    405422  Caption := SPleaseWait + STerminate;
    406423end;
     
    427444begin
    428445  if Assigned(CurrentJob) then
    429   with CurrentJob do begin
     446  with CurrentJob, Form do begin
    430447    // Part progress
    431448    ProgressBarPart.Max := Progress.Max;
     
    468485  UpdateHeight;
    469486  // Workaround for not showing first line
    470   ListViewJobs.Items.Count := Jobs.Count + 1;
    471   ListViewJobs.Refresh;
    472 
    473   if ListViewJobs.Items.Count <> Jobs.Count then
    474     ListViewJobs.Items.Count := Jobs.Count;
    475   ListViewJobs.Refresh;
     487  Form.ListViewJobs.Items.Count := Jobs.Count + 1;
     488  Form.ListViewJobs.Refresh;
     489
     490  if Form.ListViewJobs.Items.Count <> Jobs.Count then
     491    Form.ListViewJobs.Items.Count := Jobs.Count;
     492  Form.ListViewJobs.Refresh;
    476493  //Application.ProcessMessages;
    477494end;
     
    480497begin
    481498  inherited;
     499  if not (csDesigning in ComponentState) then begin
     500    Form := TFormJobProgressView.Create(Self);
     501    Form.JobProgressView := Self;
     502  end;
    482503  Jobs := TObjectList.Create;
    483504  Log := TStringList.Create;
     
    501522procedure TProgress.SetMax(const AValue: Integer);
    502523begin
    503   FMax := AValue;
    504   if FValue >= FMax then FValue := FMax;
     524  try
     525    FLock.Acquire;
     526    FMax := AValue;
     527    if FValue >= FMax then FValue := FMax;
     528  finally
     529    FLock.Release;
     530  end;
    505531end;
    506532
     
    509535  Change: Boolean;
    510536begin
    511   if AValue < Max then begin
    512     Change := AValue <> FValue;
    513     FValue := AValue;
    514     if Change and Assigned(FOnChange) then FOnChange(Self);
     537  try
     538    FLock.Acquire;
     539    if AValue < Max then begin
     540      Change := AValue <> FValue;
     541      FValue := AValue;
     542      if Change and Assigned(FOnChange) then
     543      try
     544        FLock.Release;
     545        FOnChange(Self);
     546      finally
     547        FLock.Acquire;
     548      end;
     549    end;
     550  finally
     551    FLock.Release;
    515552  end;
    516553end;
Note: See TracChangeset for help on using the changeset viewer.