1 | unit Template;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
---|
7 | Dialogs, ExtCtrls, StdCtrls, StrUtils,
|
---|
8 | OniDataClass, Functions, Data, Menus, Buttons;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TNewFileSelectedEvent = procedure(fileinfo: TFileInfo) of object;
|
---|
12 |
|
---|
13 | TForm_ToolTemplate = class(TForm)
|
---|
14 | panel_files: TPanel;
|
---|
15 | filelist: TListBox;
|
---|
16 | panel_extension: TPanel;
|
---|
17 | label_ext: TLabel;
|
---|
18 | combo_extension: TComboBox;
|
---|
19 | check_zerobyte: TCheckBox;
|
---|
20 | edit_filtername: TEdit;
|
---|
21 | check_filtername: TCheckBox;
|
---|
22 | Splitter1: TSplitter;
|
---|
23 | content: TPanel;
|
---|
24 | filepopup: TPopupMenu;
|
---|
25 | popup_import: TMenuItem;
|
---|
26 | popup_export: TMenuItem;
|
---|
27 | popup_separator: TMenuItem;
|
---|
28 | importd: TOpenDialog;
|
---|
29 | exportd: TSaveDialog;
|
---|
30 | btn_sort_id_asc: TSpeedButton;
|
---|
31 | btn_sort_id_desc: TSpeedButton;
|
---|
32 | btn_sort_name_asc: TSpeedButton;
|
---|
33 | btn_sort_name_desc: TSpeedButton;
|
---|
34 | btn_sort_ext_asc: TSpeedButton;
|
---|
35 | btn_sort_ext_desc: TSpeedButton;
|
---|
36 | Label1: TLabel;
|
---|
37 | Label2: TLabel;
|
---|
38 | procedure RecreateList;
|
---|
39 | procedure LoadFileNames;
|
---|
40 | procedure SelectFileName(filename: String);
|
---|
41 | procedure SelectFileID(id: Integer);
|
---|
42 | procedure check_filternameClick(Sender: TObject);
|
---|
43 | procedure check_zerobyteClick(Sender: TObject);
|
---|
44 | procedure combo_extensionClick(Sender: TObject);
|
---|
45 | procedure listClick(Sender: TObject);
|
---|
46 | procedure listMouseDown(Sender: TObject; Button: TMouseButton;
|
---|
47 | Shift: TShiftState; X, Y: Integer);
|
---|
48 |
|
---|
49 | procedure FormResize(Sender: TObject);
|
---|
50 | procedure FormCreate(Sender: TObject);
|
---|
51 | procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
52 | procedure popup_importClick(Sender: TObject);
|
---|
53 | procedure popup_exportClick(Sender: TObject);
|
---|
54 | procedure popup_opentool(Sender: TObject);
|
---|
55 | procedure filepopupPopup(Sender: TObject);
|
---|
56 | procedure btn_sortClick(Sender: TObject);
|
---|
57 | private
|
---|
58 | FSortBy: TSortType;
|
---|
59 | FOnNewFileSelected: TNewFileSelectedEvent;
|
---|
60 | FAllowedExts: String;
|
---|
61 | FAllowMultiSelect: Boolean;
|
---|
62 | procedure SetAllowedExts(exts: String);
|
---|
63 | procedure SetMultiSelect(allow: Boolean);
|
---|
64 | public
|
---|
65 | constructor Create(AOwner: TComponent); override;
|
---|
66 | procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean);
|
---|
67 | published
|
---|
68 | property OnNewFileSelected: TNewFileSelectedEvent read FOnNewFileSelected write FOnNewFileSelected;
|
---|
69 | property AllowedExts: String read FAllowedExts write SetAllowedExts;
|
---|
70 | property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect;
|
---|
71 | end;
|
---|
72 |
|
---|
73 | var
|
---|
74 | ToolList: TToolList;
|
---|
75 | procedure AddToolListEntry(context, name, exts: String);
|
---|
76 |
|
---|
77 | implementation
|
---|
78 | {$R *.dfm}
|
---|
79 | uses Main, Exporters;
|
---|
80 |
|
---|
81 |
|
---|
82 | procedure TForm_ToolTemplate.RecreateList;
|
---|
83 | var
|
---|
84 | i: LongWord;
|
---|
85 | exts: TStringArray;
|
---|
86 | f, c1,c2: Int64;
|
---|
87 | time1,time2,time3: Double;
|
---|
88 | begin
|
---|
89 | QueryPerformanceFrequency(f);
|
---|
90 | QueryPerformanceCounter(c1);
|
---|
91 | combo_extension.Items.Clear;
|
---|
92 | combo_extension.Items.Add('_All files_ (' +
|
---|
93 | IntToStr(OniDataConnection.GetFilesCount) + ')');
|
---|
94 | exts := OniDataConnection.GetExtensionsList;
|
---|
95 | for i := 0 to High(exts) do
|
---|
96 | if Length(FAllowedExts) > 0 then
|
---|
97 | begin
|
---|
98 | if Pos(MidStr(exts[i],1,4), FAllowedExts) > 0 then
|
---|
99 | begin
|
---|
100 | combo_extension.Items.Add(exts[i]);
|
---|
101 | end;
|
---|
102 | end else
|
---|
103 | combo_extension.Items.Add(exts[i]);
|
---|
104 | QueryPerformanceCounter(c2);
|
---|
105 | time1 := (c2 - c1) / f;
|
---|
106 | combo_extension.ItemIndex := 0;
|
---|
107 | combo_extensionClick(Self);
|
---|
108 | QueryPerformanceCounter(c1);
|
---|
109 | time2 := (c1 - c2) / f;
|
---|
110 | ShowMessage(FloatToStr(time1) +#13+#10+ FloatToStr(time2));
|
---|
111 | end;
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 |
|
---|
116 | procedure TForm_ToolTemplate.LoadFileNames;
|
---|
117 | var
|
---|
118 | Extension: String;
|
---|
119 | no_zero_bytes: Boolean;
|
---|
120 | pattern: String;
|
---|
121 | files: TStringArray;
|
---|
122 | i: LongWord;
|
---|
123 | begin
|
---|
124 | Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4);
|
---|
125 | no_zero_bytes := not check_zerobyte.Checked;
|
---|
126 | pattern := '';
|
---|
127 | if check_filtername.Checked then
|
---|
128 | pattern := edit_filtername.Text;
|
---|
129 | if Extension = '_All' then
|
---|
130 | if Length(FAllowedExts) > 0 then
|
---|
131 | Extension := FAllowedExts
|
---|
132 | else
|
---|
133 | Extension := '';
|
---|
134 |
|
---|
135 | files := OniDataConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
|
---|
136 |
|
---|
137 | filelist.Visible := False;
|
---|
138 | filelist.Items.Clear;
|
---|
139 | if Length(files) > 0 then
|
---|
140 | for i := 0 to High(files) do
|
---|
141 | filelist.Items.Add(files[i]);
|
---|
142 | filelist.Visible := True;
|
---|
143 | end;
|
---|
144 |
|
---|
145 |
|
---|
146 | procedure TForm_ToolTemplate.popup_exportClick(Sender: TObject);
|
---|
147 | var
|
---|
148 | id: Integer;
|
---|
149 | ext: String;
|
---|
150 | begin
|
---|
151 | id := OniDataConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
|
---|
152 | ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
|
---|
153 | exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*';
|
---|
154 | exportd.DefaultExt := ext;
|
---|
155 | if exportd.Execute then
|
---|
156 | ExportDatFile(id, exportd.FileName);
|
---|
157 | end;
|
---|
158 |
|
---|
159 | procedure TForm_ToolTemplate.popup_importClick(Sender: TObject);
|
---|
160 | var
|
---|
161 | id: Integer;
|
---|
162 | finfo: TFileInfo;
|
---|
163 | fs: TFileStream;
|
---|
164 | data: TData;
|
---|
165 | begin
|
---|
166 | id := OniDataConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
|
---|
167 | finfo := OniDataConnection.GetFileInfo(id);
|
---|
168 |
|
---|
169 | importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' +
|
---|
170 | finfo.Extension + '|All files|*.*';
|
---|
171 | if importd.Execute then
|
---|
172 | begin
|
---|
173 | fs := TFileStream.Create(importd.FileName, fmOpenRead);
|
---|
174 | if fs.Size <> finfo.Size then
|
---|
175 | ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) +
|
---|
176 | ', file has to have same size as file in .dat.' + CrLf +
|
---|
177 | 'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
|
---|
178 | 'Size of chosen file: ' + FormatFileSize(fs.Size))
|
---|
179 | else begin
|
---|
180 | SetLength(data, fs.Size);
|
---|
181 | fs.Read(data[0], fs.Size);
|
---|
182 | OniDataConnection.UpdateDatFile(id, data);
|
---|
183 | Self.listClick(Self);
|
---|
184 | end;
|
---|
185 | fs.Free;
|
---|
186 | end;
|
---|
187 | end;
|
---|
188 |
|
---|
189 | procedure TForm_ToolTemplate.popup_opentool(Sender: TObject);
|
---|
190 | var
|
---|
191 | sender_name, context: String;
|
---|
192 | id: Integer;
|
---|
193 | begin
|
---|
194 | sender_name := TComponent(Sender).Name;
|
---|
195 | id := OniDataConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
|
---|
196 | context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name));
|
---|
197 | Form_Main.open_child(context, id);
|
---|
198 | end;
|
---|
199 |
|
---|
200 | procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject);
|
---|
201 | begin
|
---|
202 | LoadFileNames;
|
---|
203 | end;
|
---|
204 |
|
---|
205 |
|
---|
206 | constructor TForm_ToolTemplate.Create(AOwner: TComponent);
|
---|
207 | var
|
---|
208 | i: Integer;
|
---|
209 | item: TMenuItem;
|
---|
210 | begin
|
---|
211 | inherited;
|
---|
212 | RecreateList;
|
---|
213 | if Length(ToolList) > 0 then
|
---|
214 | begin
|
---|
215 | for i := 0 to High(ToolList) do
|
---|
216 | begin
|
---|
217 | item := TMenuItem.Create(filepopup);
|
---|
218 | item.Name := 'popup_' + ToolList[i].context;
|
---|
219 | item.Caption := 'Open with ' + ToolList[i].name;
|
---|
220 | item.OnClick := Self.popup_opentool;
|
---|
221 | filepopup.Items.Insert(i, item);
|
---|
222 | end;
|
---|
223 | end;
|
---|
224 | end;
|
---|
225 |
|
---|
226 | procedure TForm_ToolTemplate.filepopupPopup(Sender: TObject);
|
---|
227 | var
|
---|
228 | ext: String;
|
---|
229 | i: Integer;
|
---|
230 | begin
|
---|
231 | ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
|
---|
232 | for i := 0 to High(ToolList) do
|
---|
233 | begin
|
---|
234 | filepopup.Items.Items[i].Enabled := True;
|
---|
235 | if Length(ToolList[i].exts) > 0 then
|
---|
236 | if Pos(ext, ToolList[i].exts) = 0 then
|
---|
237 | filepopup.Items.Items[i].Enabled := False;
|
---|
238 | end;
|
---|
239 | end;
|
---|
240 |
|
---|
241 | procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject);
|
---|
242 | begin
|
---|
243 | LoadFileNames;
|
---|
244 | end;
|
---|
245 |
|
---|
246 | procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
|
---|
247 | begin
|
---|
248 | if btn_sort_id_asc.Down then
|
---|
249 | FSortBy := stIDAsc
|
---|
250 | else if btn_sort_id_desc.Down then
|
---|
251 | FSortBy := stIDDesc
|
---|
252 | else if btn_sort_name_asc.Down then
|
---|
253 | FSortBy := stNameAsc
|
---|
254 | else if btn_sort_name_desc.Down then
|
---|
255 | FSortBy := stNameDesc
|
---|
256 | else if btn_sort_ext_asc.Down then
|
---|
257 | FSortBy := stExtAsc
|
---|
258 | else if btn_sort_ext_desc.Down then
|
---|
259 | FSortBy := stExtDesc;
|
---|
260 | LoadFileNames;
|
---|
261 | end;
|
---|
262 |
|
---|
263 | procedure TForm_ToolTemplate.check_filternameClick(Sender: TObject);
|
---|
264 | begin
|
---|
265 | edit_filtername.Enabled := not check_filtername.Checked;
|
---|
266 | LoadFileNames;
|
---|
267 | end;
|
---|
268 |
|
---|
269 | procedure TForm_ToolTemplate.listClick(Sender: TObject);
|
---|
270 | var
|
---|
271 | fileid: Integer;
|
---|
272 | begin
|
---|
273 | if filelist.ItemIndex > -1 then
|
---|
274 | begin
|
---|
275 | fileid := OniDataConnection.ExtractFileID(
|
---|
276 | filelist.Items.Strings[filelist.ItemIndex]);
|
---|
277 | if Assigned(FOnNewFileSelected) then
|
---|
278 | FOnNewFileSelected(OniDataConnection.GetFileInfo(fileid));
|
---|
279 | end;
|
---|
280 | end;
|
---|
281 |
|
---|
282 | procedure TForm_ToolTemplate.listMouseDown(Sender: TObject; Button: TMouseButton;
|
---|
283 | Shift: TShiftState; X, Y: Integer);
|
---|
284 | var
|
---|
285 | pt: TPoint;
|
---|
286 | begin
|
---|
287 | pt.X := x;
|
---|
288 | pt.Y := y;
|
---|
289 | filelist.ItemIndex := filelist.ItemAtPos(pt, true);
|
---|
290 | Self.listClick(Self);
|
---|
291 | end;
|
---|
292 |
|
---|
293 |
|
---|
294 |
|
---|
295 | procedure TForm_ToolTemplate.SelectFileID(id: Integer);
|
---|
296 | var
|
---|
297 | i: Integer;
|
---|
298 | begin
|
---|
299 | filelist.ItemIndex := -1;
|
---|
300 | if filelist.Items.Count > 0 then
|
---|
301 | for i := 0 to filelist.Items.Count - 1 do
|
---|
302 | if OniDataConnection.ExtractFileID(filelist.Items.Strings[i]) = id then
|
---|
303 | begin
|
---|
304 | filelist.ItemIndex := i;
|
---|
305 | Break;
|
---|
306 | end;
|
---|
307 | Self.listClick(Self);
|
---|
308 | end;
|
---|
309 |
|
---|
310 | procedure TForm_ToolTemplate.SelectFileName(filename: String);
|
---|
311 | var
|
---|
312 | i: Integer;
|
---|
313 | begin
|
---|
314 | filelist.ItemIndex := -1;
|
---|
315 | if filelist.Items.Count > 0 then
|
---|
316 | for i := 0 to filelist.Items.Count - 1 do
|
---|
317 | if filelist.Items.Strings[i] = filename then
|
---|
318 | filelist.ItemIndex := i;
|
---|
319 | Self.listClick(Self);
|
---|
320 | end;
|
---|
321 |
|
---|
322 | procedure TForm_ToolTemplate.SetAllowedExts(exts: String);
|
---|
323 | begin
|
---|
324 | FAllowedExts := exts;
|
---|
325 | RecreateList;
|
---|
326 | end;
|
---|
327 |
|
---|
328 | procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String;
|
---|
329 | zerobytes: Boolean);
|
---|
330 | var
|
---|
331 | i: Integer;
|
---|
332 | begin
|
---|
333 | if Length(pattern) > 0 then
|
---|
334 | Self.edit_filtername.Text := pattern;
|
---|
335 | Self.check_filtername.Checked := Length(pattern) > 0;
|
---|
336 | if Length(extension) > 0 then
|
---|
337 | begin
|
---|
338 | for i := 0 to Self.combo_extension.Items.Count - 1 do
|
---|
339 | if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then
|
---|
340 | Break;
|
---|
341 | if i < Self.combo_extension.Items.Count then
|
---|
342 | Self.combo_extension.ItemIndex := i
|
---|
343 | else
|
---|
344 | Self.combo_extension.ItemIndex := -1;
|
---|
345 | end;
|
---|
346 | Self.check_zerobyte.Checked := zerobytes;
|
---|
347 | Self.LoadFileNames;
|
---|
348 | end;
|
---|
349 |
|
---|
350 | procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean);
|
---|
351 | begin
|
---|
352 | FAllowMultiSelect := allow;
|
---|
353 | filelist.MultiSelect := allow;
|
---|
354 | end;
|
---|
355 |
|
---|
356 |
|
---|
357 | procedure TForm_ToolTemplate.FormResize(Sender: TObject);
|
---|
358 | begin
|
---|
359 | if Self.Width < 300 then
|
---|
360 | Self.Width := 300;
|
---|
361 | if Self.Height < 200 then
|
---|
362 | Self.Height := 200;
|
---|
363 | end;
|
---|
364 |
|
---|
365 |
|
---|
366 |
|
---|
367 | procedure TForm_ToolTemplate.FormCreate(Sender: TObject);
|
---|
368 | begin
|
---|
369 | Self.Width := 260;
|
---|
370 | Self.Height := 300;
|
---|
371 | FOnNewFileSelected := nil;
|
---|
372 | FAllowedExts := '';
|
---|
373 | FAllowMultiSelect := False;
|
---|
374 | end;
|
---|
375 |
|
---|
376 | procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
377 | begin
|
---|
378 | Action := caFree;
|
---|
379 | end;
|
---|
380 |
|
---|
381 |
|
---|
382 | procedure AddToolListEntryExt(context, ext: String);
|
---|
383 | var
|
---|
384 | i: Integer;
|
---|
385 | begin
|
---|
386 | for i := 0 to High(ToolList) do
|
---|
387 | if ToolList[i].context = context then
|
---|
388 | begin
|
---|
389 | if Pos(ext, ToolList[i].exts) = 0 then
|
---|
390 | begin
|
---|
391 | if Length(ToolList[i].exts) = 0 then
|
---|
392 | ToolList[i].exts := ext
|
---|
393 | else
|
---|
394 | ToolList[i].exts := ToolList[i].exts + ',' + ext;
|
---|
395 | end;
|
---|
396 | Exit;
|
---|
397 | end;
|
---|
398 | end;
|
---|
399 |
|
---|
400 | procedure AddToolListEntry(context, name, exts: String);
|
---|
401 | var
|
---|
402 | i: Integer;
|
---|
403 | begin
|
---|
404 | if Length(ToolList) > 0 then
|
---|
405 | begin
|
---|
406 | for i := 0 to High(ToolList) do
|
---|
407 | if ToolList[i].context = context then
|
---|
408 | begin
|
---|
409 | if (Length(ToolList[i].name) = 0) and (Length(name) > 0) then
|
---|
410 | ToolList[i].name := name;
|
---|
411 | if Length(exts) > 0 then
|
---|
412 | AddToolListEntryExt(context, exts);
|
---|
413 | Exit;
|
---|
414 | end;
|
---|
415 | end;
|
---|
416 | SetLength(ToolList, Length(ToolList) + 1);
|
---|
417 | for i := High(ToolList) downto 1 do
|
---|
418 | if ToolList[i - 1].name > name then
|
---|
419 | ToolList[i] := ToolList[i - 1]
|
---|
420 | else
|
---|
421 | Break;
|
---|
422 | ToolList[i].context := context;
|
---|
423 | ToolList[i].name := name;
|
---|
424 | ToolList[i].exts := exts;
|
---|
425 | end;
|
---|
426 |
|
---|
427 | end.
|
---|