Changeset 21 for trunk/Packages/Common/JobProgressView.pas
- Timestamp:
- Apr 3, 2025, 10:49:00 PM (2 weeks ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/Packages/Common/JobProgressView.pas ¶
r20 r21 1 unit UJobProgressView; 2 3 {$MODE Delphi} 1 unit JobProgressView; 4 2 5 3 interface … … 7 5 uses 8 6 SysUtils, Variants, Classes, Graphics, Controls, Forms, Syncobjs, 9 Dialogs, ComCtrls, StdCtrls, ExtCtrls, Contnrs, UThreading,7 Dialogs, ComCtrls, StdCtrls, ExtCtrls, Generics.Collections, Threading, Math, 10 8 DateUtils; 11 9 … … 13 11 EstimatedTimeShowTreshold = 4; 14 12 EstimatedTimeShowTresholdTotal = 1; 15 MemoLogHeight = 200;16 13 UpdateInterval = 100; // ms 17 14 … … 24 21 FLock: TCriticalSection; 25 22 FOnChange: TNotifyEvent; 23 FText: string; 26 24 FValue: Integer; 27 25 FMax: Integer; 28 26 procedure SetMax(const AValue: Integer); 27 procedure SetText(AValue: string); 29 28 procedure SetValue(const AValue: Integer); 30 29 public … … 35 34 property Value: Integer read FValue write SetValue; 36 35 property Max: Integer read FMax write SetMax; 36 property Text: string read FText write SetText; 37 37 property OnChange: TNotifyEvent read FOnChange write FOnChange; 38 38 end; … … 69 69 end; 70 70 71 TJobs = class(TObjectList<TJob>) 72 end; 73 71 74 TJobThread = class(TListedThread) 72 75 procedure Execute; override; … … 80 83 TFormJobProgressView = class(TForm) 81 84 ImageList1: TImageList; 85 LabelText: TLabel; 82 86 Label2: TLabel; 83 87 LabelOperation: TLabel; … … 86 90 ListViewJobs: TListView; 87 91 MemoLog: TMemo; 92 PanelText: TPanel; 88 93 PanelProgressTotal: TPanel; 89 94 PanelOperationsTitle: TPanel; … … 94 99 ProgressBarTotal: TProgressBar; 95 100 TimerUpdate: TTimer; 101 procedure FormHide(Sender: TObject); 102 procedure FormShow(Sender: TObject); 103 procedure ReloadJobList; 96 104 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); 97 procedure FormDestroy(Sender: TObject);98 105 procedure ListViewJobsData(Sender: TObject; Item: TListItem); 99 106 procedure TimerUpdateTimer(Sender: TObject); 100 107 procedure FormCreate(Sender: TObject); 101 108 procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); 109 procedure UpdateHeight; 102 110 public 103 111 JobProgressView: TJobProgressView; … … 118 126 TotalStartTime: TDateTime; 119 127 Log: TStringList; 128 FForm: TFormJobProgressView; 120 129 procedure SetTerminate(const AValue: Boolean); 121 130 procedure UpdateProgress; 122 procedure ReloadJobList;123 procedure StartJobs;124 procedure UpdateHeight;125 131 procedure JobProgressChange(Sender: TObject); 126 132 public 127 Form: TFormJobProgressView; 128 Jobs: TObjectList; // TListObject<TJob> 133 Jobs: TJobs; 129 134 CurrentJob: TJob; 130 135 CurrentJobIndex: Integer; … … 132 137 destructor Destroy; override; 133 138 procedure Clear; 134 procedureAddJob(Title: string; Method: TJobProgressViewMethod;135 NoThreaded: Boolean = False; WaitFor: Boolean = False) ;136 procedure Start (AAutoClose: Boolean = True);139 function AddJob(Title: string; Method: TJobProgressViewMethod; 140 NoThreaded: Boolean = False; WaitFor: Boolean = False): TJob; 141 procedure Start; 137 142 procedure Stop; 138 143 procedure TermSleep(Delay: Integer); 144 property Form: TFormJobProgressView read FForm; 139 145 property Terminate: Boolean read FTerminate write SetTerminate; 140 146 published … … 148 154 end; 149 155 150 //var151 // FormJobProgressView: TFormJobProgressView;152 153 156 procedure Register; 154 157 155 158 resourcestring 156 159 SExecuted = 'Executed'; 160 157 161 158 162 implementation … … 166 170 STotalEstimatedTime = 'Total estimated time: %s'; 167 171 SFinished = 'Finished'; 168 SOperations = 'Operations';169 172 170 173 procedure Register; … … 173 176 end; 174 177 178 { TJobThread } 179 175 180 procedure TJobThread.Execute; 176 181 begin 177 182 try 178 183 try 179 //raise Exception.Create('Exception in job');180 184 ProgressView.CurrentJob.Method(Job); 181 185 except … … 190 194 end; 191 195 192 procedure TJobProgressView.AddJob(Title: string; Method: TJobProgressViewMethod; 193 NoThreaded: Boolean = False; WaitFor: Boolean = False); 196 { TFormJobProgressView } 197 198 procedure TFormJobProgressView.UpdateHeight; 194 199 var 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); 200 H: Integer; 201 PanelOperationsVisible: Boolean; 202 PanelOperationsHeight: Integer; 203 PanelProgressVisible: Boolean; 204 PanelProgressTotalVisible: Boolean; 205 PanelLogVisible: Boolean; 206 MemoLogHeight: Integer = 200; 207 I: Integer; 208 ItemRect: TRect; 209 MaxH: Integer; 210 begin 211 H := PanelOperationsTitle.Height; 212 PanelOperationsVisible := JobProgressView.Jobs.Count > 0; 213 if PanelOperationsVisible <> PanelOperations.Visible then 214 PanelOperations.Visible := PanelOperationsVisible; 215 if ListViewJobs.Items.Count > 0 then begin 216 Maxh := 0; 217 for I := 0 to ListViewJobs.Items.Count - 1 do 218 begin 219 ItemRect := ListViewJobs.Items[i].DisplayRect(drBounds); 220 Maxh := Max(Maxh, ItemRect.Top + (ItemRect.Bottom - ItemRect.Top)); 221 end; 222 PanelOperationsHeight := Scale96ToScreen(12) + Maxh; 223 end else PanelOperationsHeight := Scale96ToScreen(8); 224 if PanelOperationsHeight <> PanelOperations.Height then 225 PanelOperations.Height := PanelOperationsHeight; 226 if PanelOperationsVisible then 227 H := H + PanelOperations.Height; 228 229 PanelProgressVisible := (JobProgressView.Jobs.Count > 0) and not JobProgressView.Finished; 230 if PanelProgressVisible <> PanelProgress.Visible then 231 PanelProgress.Visible := PanelProgressVisible; 232 if PanelProgressVisible then 233 H := H + PanelProgress.Height; 234 PanelProgressTotalVisible := (JobProgressView.Jobs.Count > 1) and not JobProgressView.Finished; 235 if PanelProgressTotalVisible <> PanelProgressTotal.Visible then 236 PanelProgressTotal.Visible := PanelProgressTotalVisible; 237 if PanelProgressTotalVisible then 238 H := H + PanelProgressTotal.Height; 239 Constraints.MinHeight := H; 240 PanelLogVisible := MemoLog.Lines.Count > 0; 241 if PanelLogVisible <> PanelLog.Visible then 242 PanelLog.Visible := PanelLogVisible; 243 if PanelLogVisible then 244 H := H + Scale96ToScreen(MemoLogHeight); 245 if PanelText.Visible then 246 H := H + PanelText.Height; 247 if Height <> H then begin 248 Height := H; 249 Top := (Screen.Height - H) div 2; 250 end; 251 end; 252 253 procedure TFormJobProgressView.TimerUpdateTimer(Sender: TObject); 254 var 255 ProgressBarPartVisible: Boolean; 256 ProgressBarTotalVisible: Boolean; 257 begin 258 JobProgressView.UpdateProgress; 259 if Visible and (not ProgressBarPart.Visible) and 260 Assigned(JobProgressView.CurrentJob) and 261 (JobProgressView.CurrentJob.Progress.Value > 0) then begin 262 ProgressBarPartVisible := True; 263 if ProgressBarPartVisible <> ProgressBarPart.Visible then 264 ProgressBarPart.Visible := ProgressBarPartVisible; 265 ProgressBarTotalVisible := True; 266 if ProgressBarTotalVisible <> ProgressBarTotal.Visible then 267 ProgressBarTotal.Visible := ProgressBarTotalVisible; 268 end; 269 if not Visible then begin 270 TimerUpdate.Interval := UpdateInterval; 271 if not JobProgressView.OwnerDraw then Show; 272 end; 273 if Assigned(JobProgressView.CurrentJob) then begin 274 LabelText.Caption := JobProgressView.CurrentJob.Progress.Text; 275 if LabelText.Caption <> '' then begin 276 PanelText.Visible := True; 277 UpdateHeight; 278 end; 279 end; 280 end; 281 282 procedure TFormJobProgressView.ListViewJobsData(Sender: TObject; Item: TListItem); 283 begin 284 if (Item.Index >= 0) and (Item.Index < JobProgressView.Jobs.Count) then 285 with JobProgressView.Jobs[Item.Index] do begin 286 Item.Caption := Title; 287 if Item.Index = JobProgressView.CurrentJobIndex then Item.ImageIndex := 1 288 else if Finished then Item.ImageIndex := 0 289 else Item.ImageIndex := 2; 290 Item.Data := JobProgressView.Jobs[Item.Index]; 291 end; 292 end; 293 294 procedure TFormJobProgressView.FormClose(Sender: TObject; 295 var CloseAction: TCloseAction); 296 begin 297 end; 298 299 procedure TFormJobProgressView.FormCreate(Sender: TObject); 300 begin 301 Caption := SPleaseWait; 302 try 303 //Animate1.FileName := ExtractFileDir(UTF8Encode(Application.ExeName)) + 304 // DirectorySeparator + 'horse.avi'; 305 //Animate1.Active := True; 306 except 307 308 end; 309 end; 310 311 procedure TFormJobProgressView.ReloadJobList; 312 begin 313 // Workaround for not showing first line 314 //Form.ListViewJobs.Items.Count := Jobs.Count + 1; 315 //Form.ListViewJobs.Refresh; 316 317 if ListViewJobs.Items.Count <> JobProgressView.Jobs.Count then 318 ListViewJobs.Items.Count := JobProgressView.Jobs.Count; 319 ListViewJobs.Refresh; 320 Application.ProcessMessages; 321 UpdateHeight; 322 end; 323 324 procedure TFormJobProgressView.FormShow(Sender: TObject); 325 begin 326 ReloadJobList; 327 end; 328 329 procedure TFormJobProgressView.FormHide(Sender: TObject); 330 begin 331 JobProgressView.Jobs.Clear; 332 ReloadJobList; 333 end; 334 335 procedure TFormJobProgressView.FormCloseQuery(Sender: TObject; var CanClose: Boolean); 336 begin 337 CanClose := JobProgressView.Finished; 338 JobProgressView.Terminate := True; 339 Caption := SPleaseWait + STerminate; 340 end; 341 342 343 { TJobProgressView } 344 345 function TJobProgressView.AddJob(Title: string; Method: TJobProgressViewMethod; 346 NoThreaded: Boolean = False; WaitFor: Boolean = False): TJob; 347 begin 348 Result := TJob.Create; 349 Result.ProgressView := Self; 350 Result.Title := Title; 351 Result.Method := Method; 352 Result.NoThreaded := NoThreaded; 353 Result.WaitFor := WaitFor; 354 Result.Progress.Max := 100; 355 Result.Progress.Reset; 356 Result.Progress.OnChange := JobProgressChange; 357 Jobs.Add(Result); 207 358 //ReloadJobList; 208 359 end; 209 360 210 procedure TJobProgressView.Start(AAutoClose: Boolean = True); 211 begin 212 AutoClose := AAutoClose; 213 StartJobs; 214 end; 215 216 procedure TJobProgressView.StartJobs; 361 procedure TJobProgressView.Start; 217 362 var 218 363 I: Integer; … … 229 374 Form.MemoLog.Clear; 230 375 376 Form.PanelText.Visible := False; 231 377 Form.LabelEstimatedTimePart.Visible := False; 232 378 Form.LabelEstimatedTimeTotal.Visible := False; … … 249 395 I := 0; 250 396 while I < Jobs.Count do 251 with TJob(Jobs[I])do begin397 with Jobs[I] do begin 252 398 CurrentJobIndex := I; 253 CurrentJob := TJob(Jobs[I]);399 CurrentJob := Jobs[I]; 254 400 JobProgressChange(Self); 255 401 StartTime := Now; … … 258 404 Form.ProgressBarPart.Visible := False; 259 405 //Show; 260 ReloadJobList;406 Form.ReloadJobList; 261 407 Application.ProcessMessages; 262 408 if NoThreaded then begin … … 264 410 Method(CurrentJob); 265 411 end else begin 412 Thread := TJobThread.Create(True); 266 413 try 267 Thread := TJobThread.Create(True);268 414 with Thread do begin 269 415 FreeOnTerminate := False; … … 296 442 //if Visible then Hide; 297 443 Form.MemoLog.Lines.Assign(Log); 298 if (Form.MemoLog.Lines.Count = 0) and AutoClose then begin444 if (Form.MemoLog.Lines.Count = 0) and FAutoClose then begin 299 445 Form.Hide; 300 446 end; 301 Clear;447 if not Form.Visible then Clear; 302 448 Form.Caption := SFinished; 303 449 //LabelEstimatedTimePart.Visible := False; 304 450 Finished := True; 305 451 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; 452 Form.ReloadJobList; 347 453 end; 348 454 end; … … 352 458 if Assigned(FOnOwnerDraw) then 353 459 FOnOwnerDraw(Self); 354 end;355 356 procedure TFormJobProgressView.TimerUpdateTimer(Sender: TObject);357 var358 ProgressBarPartVisible: Boolean;359 ProgressBarTotalVisible: Boolean;360 begin361 JobProgressView.UpdateProgress;362 if Visible and (not ProgressBarPart.Visible) and363 Assigned(JobProgressView.CurrentJob) and364 (JobProgressView.CurrentJob.Progress.Value > 0) then begin365 ProgressBarPartVisible := True;366 if ProgressBarPartVisible <> ProgressBarPart.Visible then367 ProgressBarPart.Visible := ProgressBarPartVisible;368 ProgressBarTotalVisible := True;369 if ProgressBarTotalVisible <> ProgressBarTotal.Visible then370 ProgressBarTotal.Visible := ProgressBarTotalVisible;371 end;372 if not Visible then begin373 TimerUpdate.Interval := UpdateInterval;374 if not JobProgressView.OwnerDraw then Show;375 end;376 end;377 378 procedure TFormJobProgressView.FormDestroy(Sender:TObject);379 begin380 end;381 382 procedure TFormJobProgressView.ListViewJobsData(Sender: TObject; Item: TListItem);383 begin384 if (Item.Index >= 0) and (Item.Index < JobProgressView.Jobs.Count) then385 with TJob(JobProgressView.Jobs[Item.Index]) do begin386 Item.Caption := Title;387 if Item.Index = JobProgressView.CurrentJobIndex then Item.ImageIndex := 1388 else if Finished then Item.ImageIndex := 0389 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 begin397 ListViewJobs.Clear;398 end;399 400 procedure TFormJobProgressView.FormCreate(Sender: TObject);401 begin402 Caption := SPleaseWait;403 try404 //Animate1.FileName := ExtractFileDir(UTF8Encode(Application.ExeName)) +405 // DirectorySeparator + 'horse.avi';406 //Animate1.Active := True;407 except408 409 end;410 460 end; 411 461 … … 428 478 end; 429 479 430 procedure TFormJobProgressView.FormCloseQuery(Sender: TObject; var CanClose: Boolean);431 begin432 CanClose := JobProgressView.Finished;433 JobProgressView.Terminate := True;434 Caption := SPleaseWait + STerminate;435 end;436 437 480 procedure TJobProgressView.SetTerminate(const AValue: Boolean); 438 481 var … … 441 484 if AValue = FTerminate then Exit; 442 485 for I := 0 to Jobs.Count - 1 do 443 TJob(Jobs[I]).Terminate := AValue;486 Jobs[I].Terminate := AValue; 444 487 FTerminate := AValue; 445 488 end; … … 490 533 end; 491 534 492 procedure TJobProgressView.ReloadJobList;493 begin494 UpdateHeight;495 // Workaround for not showing first line496 Form.ListViewJobs.Items.Count := Jobs.Count + 1;497 Form.ListViewJobs.Refresh;498 499 if Form.ListViewJobs.Items.Count <> Jobs.Count then500 Form.ListViewJobs.Items.Count := Jobs.Count;501 Form.ListViewJobs.Refresh;502 //Application.ProcessMessages;503 end;504 505 535 constructor TJobProgressView.Create(TheOwner: TComponent); 506 536 begin 507 537 inherited; 508 538 if not (csDesigning in ComponentState) then begin 509 F orm := TFormJobProgressView.Create(Self);510 F orm.JobProgressView := Self;511 end; 512 Jobs := T ObjectList.Create;539 FForm := TFormJobProgressView.Create(Self); 540 FForm.JobProgressView := Self; 541 end; 542 Jobs := TJobs.Create; 513 543 Log := TStringList.Create; 514 544 //PanelOperationsTitle.Height := 80; 515 ShowDelay := 0; //1000; // ms 545 AutoClose := True; 546 ShowDelay := 0; 516 547 end; 517 548 … … 519 550 begin 520 551 Jobs.Clear; 552 Log.Clear; 521 553 //ReloadJobList; 522 554 end; … … 528 560 inherited; 529 561 end; 562 563 { TProgress } 530 564 531 565 procedure TProgress.SetMax(const AValue: Integer); … … 536 570 if FMax < 1 then FMax := 1; 537 571 if FValue >= FMax then FValue := FMax; 572 finally 573 FLock.Release; 574 end; 575 end; 576 577 procedure TProgress.SetText(AValue: string); 578 begin 579 try 580 FLock.Acquire; 581 if FText = AValue then Exit; 582 FText := AValue; 538 583 finally 539 584 FLock.Release; … … 563 608 end; 564 609 565 { TProgress }566 567 610 procedure TProgress.Increment; 568 611 begin 569 try570 FLock.Acquire;612 FLock.Acquire; 613 try 571 614 Value := Value + 1; 572 615 finally … … 577 620 procedure TProgress.Reset; 578 621 begin 579 try580 FLock.Acquire;622 FLock.Acquire; 623 try 581 624 FValue := 0; 582 625 finally … … 594 637 begin 595 638 FLock.Free; 596 inherited Destroy;639 inherited; 597 640 end; 598 641 … … 625 668 destructor TJob.Destroy; 626 669 begin 627 Progress.Free;670 FreeAndNil(Progress); 628 671 inherited; 629 672 end;
Note:
See TracChangeset
for help on using the changeset viewer.