-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathDN.Progress.pas
114 lines (95 loc) · 2.66 KB
/
DN.Progress.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
unit DN.Progress;
interface
uses
DN.Progress.Intf;
type
TDNProgress = class(TInterfacedObject, IDNProgress)
private
FOnProgress: TDNProgressEvent;
FTaskSteps: Int64;
FFinishedTasks: Integer;
FTasks: array of string;
function GetOnProgress: TDNProgressEvent;
function GetTaskSteps: Int64;
procedure SetOnProgress(const Value: TDNProgressEvent);
procedure SetTaskSteps(const Value: Int64);
protected
procedure DoProgress(const ATask, AItem: string; AProgress, AMax: Int64);
public
constructor Create;
procedure SetTasks(ATasks: array of string);
procedure AddTask(ATask: string);
procedure SetTaskProgress(const AItem: string; AProgress, AMax: Int64);
procedure NextTask;
procedure Reset;
procedure Completed;
property TaskSteps: Int64 read GetTaskSteps write SetTaskSteps;
property OnProgress: TDNProgressEvent read GetOnProgress write SetOnProgress;
end;
implementation
{ TProgress }
procedure TDNProgress.AddTask(ATask: string);
begin
SetLength(FTasks, Length(FTasks) + 1);
FTasks[High(FTasks)] := ATask;
end;
procedure TDNProgress.Completed;
begin
DoProgress('Completed', '', Length(FTasks)*FTaskSteps, Length(FTasks)*TaskSteps);
end;
constructor TDNProgress.Create;
begin
inherited;
FTaskSteps := 100;
end;
procedure TDNProgress.DoProgress(const ATask, AItem: string; AProgress,
AMax: Int64);
begin
if Assigned(FOnProgress) then
FOnProgress(ATask, AItem, AProgress, AMax);
end;
function TDNProgress.GetOnProgress: TDNProgressEvent;
begin
Result := FOnProgress;
end;
function TDNProgress.GetTaskSteps: Int64;
begin
Result := FTaskSteps;
end;
procedure TDNProgress.NextTask;
begin
if FFinishedTasks < Length(FTasks) - 1 then
begin
Inc(FFinishedTasks);
DoProgress(FTasks[FFinishedTasks], '', FFinishedTasks*FTaskSteps, Length(FTasks)*FTaskSteps);
end;
end;
procedure TDNProgress.Reset;
begin
FFinishedTasks := 0;
end;
procedure TDNProgress.SetOnProgress(const Value: TDNProgressEvent);
begin
FOnProgress := Value;
end;
procedure TDNProgress.SetTaskProgress(const AItem: string; AProgress,
AMax: Int64);
begin
DoProgress(FTasks[FFinishedTasks], AItem, FFinishedTasks*FTaskSteps + Round(AProgress / AMax * FTaskSteps), Length(FTasks)*FTaskSteps);
end;
procedure TDNProgress.SetTasks(ATasks: array of string);
var
i: Integer;
begin
SetLength(FTasks, Length(ATasks));
for i := Low(FTasks) to High(FTasks) do
FTasks[i] := ATasks[i];
Reset();
if Length(FTasks) > 0 then
DoProgress(FTasks[FFinishedTasks], '', FFinishedTasks*FTaskSteps, Length(FTasks) * FTaskSteps);
end;
procedure TDNProgress.SetTaskSteps(const Value: Int64);
begin
FTaskSteps := Value;
end;
end.