-
Notifications
You must be signed in to change notification settings - Fork 3
/
ufmMain.pas
238 lines (199 loc) · 5.71 KB
/
ufmMain.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
unit ufmMain;
interface
uses
{ EnterCriticalSection }
Winapi.Windows,
System.SysUtils, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.StdCtrls, Vcl.ExtCtrls,
System.Generics.Collections,
{ IViewControl }
uIViews;
type
TfmMain = class(TForm, IViewControl)
scrollBoxImages: TScrollBox;
btnLoadImagesFromFolder: TButton;
btnFreeImages: TButton;
lblLoadingImagesProgress: TLabel;
lblThreadsCompleted: TLabel;
btnStopLoading: TButton;
memHelp: TMemo;
procedure btnLoadImagesFromFolderClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnFreeImagesClick(Sender: TObject);
procedure btnStopLoadingClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
private
{$REGION 'Implements interface "IViewControl" '}
procedure CreateControls;
procedure UpdateImage(ABitmap: TBitmap);
procedure ImageThreadComleted(const AThread: TThread);
procedure UpdateLoadProgress(AValue: Integer);
function GetImageViewsControl: TWinControl;
{$ENDREGION}
procedure EnDsControls(AStatus: Boolean);
public
end;
var
fmMain: TfmMain;
implementation
uses
{ CriticalSection }
uCore.CriticalSection,
uConsts,
{ TaskImages }
uTask.Images,
{ WM_VSCROLL }
Winapi.Messages;
{$R *.dfm}
procedure TfmMain.btnLoadImagesFromFolderClick(Sender: TObject);
begin
EnDsControls(False);
TaskImages.LoadAndDisplayImages('F:\Pictures\My Photos\Y2016', CONST_FILE_TYPES);
end;
procedure TfmMain.btnStopLoadingClick(Sender: TObject);
begin
TaskImages.StopTasks;
end;
procedure TfmMain.btnFreeImagesClick(Sender: TObject);
begin
TaskImages.FreeImages;
end;
procedure TfmMain.EnDsControls(AStatus: Boolean);
begin
btnLoadImagesFromFolder.Enabled := AStatus;
btnFreeImages.Enabled := AStatus;
end;
procedure TfmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
TaskImages.StopTasks;
with CriticalSection do
if FImagesThreadPool.LockList.Count > 0 then
begin
Action := caNone;
EnterCriticalSection(Section);
try
FPostponedCloseOnceAllTaskCompleted := True;
finally
LeaveCriticalSection(Section);
end;
end;
end;
procedure TfmMain.FormDestroy(Sender: TObject);
begin
TaskImages.FreeImages;
end;
procedure TfmMain.FormMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
const
CONST_SCROLL_LINES = 2;
var
I, J: Integer;
LScrollBox: TScrollBox;
LControl: TWinControl;
begin
LControl := FindVCLWindow(Mouse.CursorPos);
Handled := LControl is TScrollBox;
if not Handled then
Exit;
LScrollBox := LControl as TScrollBox;
for I := 1 to Mouse.WheelScrollLines do
try
for J := 1 to CONST_SCROLL_LINES do
if WheelDelta > 0 then
LScrollBox.Perform(WM_VSCROLL, SB_LINEUP, 0)
else
LScrollBox.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
finally
LScrollBox.Perform(WM_VSCROLL, SB_ENDSCROLL, 0);
end;
end;
{$REGION 'Implements interface "IViewControl" '}
procedure TfmMain.CreateControls;
begin
end;
procedure TfmMain.UpdateImage(ABitmap: TBitmap);
begin
with CriticalSection do
begin
EnterCriticalSection(Section);
try
if FImages.Count > 0 then
begin
FImages[FDisplayIndex].Picture.Assign(ABitmap);
Inc(FDisplayIndex);
end;
finally
LeaveCriticalSection(Section);
end;
end;
end;
procedure TfmMain.ImageThreadComleted(const AThread: TThread);
function FormatProgressLabel(AValue: Integer): String;
begin
Result := Format('There are %d images loaded', [AValue]);
end;
var
LFileName: String;
LIndex: Integer;
LActionRequired: Boolean;
LPostponedCloseOnceAllTaskCompleted: Boolean;
LThreadsCompleted: Integer;
begin
{ Remove thread from pool list }
LIndex := CriticalSection.FImagesThreadPool.LockList.IndexOf(AThread);
if LIndex = -1 then
Exit;
CriticalSection.FImagesThreadPool.LockList.Remove(
CriticalSection.FImagesThreadPool.LockList.Items[LIndex]);
if CriticalSection.FImagesThreadPool.LockList.Count = 0 then
EnDsControls(True);
EnterCriticalSection(CriticalSection.Section);
try
LPostponedCloseOnceAllTaskCompleted :=
CriticalSection.FPostponedCloseOnceAllTaskCompleted;
finally
LeaveCriticalSection(CriticalSection.Section);
end;
if LPostponedCloseOnceAllTaskCompleted then
Close;
{ Should we run next thread? }
EnterCriticalSection(CriticalSection.Section);
try
LActionRequired :=
(CriticalSection.FImagesThreadPool.LockList.Count < CONST_MAX_THREADS)
and (CriticalSection.FPendingFiles.Count > 0);
Inc(CriticalSection.FThreadsCompleted);
LThreadsCompleted := CriticalSection.FThreadsCompleted;
finally
LeaveCriticalSection(CriticalSection.Section);
end;
{ GUI }
lblThreadsCompleted.Caption := FormatProgressLabel(LThreadsCompleted);
if not LActionRequired then
Exit;
with CriticalSection do
begin
EnterCriticalSection(CriticalSection.Section);
try
LFileName := FPendingFiles[FPendingFiles.Count - 1];
FPendingFiles.Delete(FPendingFiles.Count - 1);
finally
LeaveCriticalSection(Section);
end;
end;
{ Run next thread }
TaskImages.RunImageViewThread(LFileName, tpLowest);
end;
procedure TfmMain.UpdateLoadProgress(AValue: Integer);
begin
lblLoadingImagesProgress.Caption := Format('There are %d images in folder',
[AValue])
end;
function TfmMain.GetImageViewsControl: TWinControl;
begin
Result := scrollBoxImages;
end;
{$ENDREGION}
end.