source: oup/current/Tools/Template.pas @ 51

Last change on this file since 51 was 51, checked in by alloc, 16 years ago
File size: 11.8 KB
Line 
1unit Template;
2
3interface
4
5uses
6  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7  Dialogs, ExtCtrls, StdCtrls, StrUtils,
8  OniDataClass, Functions, Data, Menus, Buttons;
9
10type
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
73var
74  ToolList: TToolList;
75procedure AddToolListEntry(context, name, exts: String);
76
77implementation
78{$R *.dfm}
79uses Main, Exporters;
80
81
82procedure TForm_ToolTemplate.RecreateList;
83var
84  i:    LongWord;
85  exts: TStringArray;
86f, c1,c2: Int64;
87time1,time2,time3: Double;
88begin
89QueryPerformanceFrequency(f);
90QueryPerformanceCounter(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]);
104QueryPerformanceCounter(c2);
105time1 := (c2 - c1) / f;
106  combo_extension.ItemIndex := 0;
107  combo_extensionClick(Self);
108QueryPerformanceCounter(c1);
109time2 := (c1 - c2) / f;
110ShowMessage(FloatToStr(time1) +#13+#10+ FloatToStr(time2));
111end;
112
113
114
115
116procedure TForm_ToolTemplate.LoadFileNames;
117var
118  Extension: String;
119  no_zero_bytes: Boolean;
120  pattern: String;
121  files: TStringArray;
122  i: LongWord;
123begin
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;
143end;
144
145
146procedure TForm_ToolTemplate.popup_exportClick(Sender: TObject);
147var
148  id: Integer;
149  ext: String;
150begin
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);
157end;
158
159procedure TForm_ToolTemplate.popup_importClick(Sender: TObject);
160var
161  id: Integer;
162  finfo: TFileInfo;
163  fs: TFileStream;
164  data: TData;
165begin
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;
187end;
188
189procedure TForm_ToolTemplate.popup_opentool(Sender: TObject);
190var
191  sender_name, context: String;
192  id: Integer;
193begin
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);
198end;
199
200procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject);
201begin
202  LoadFileNames;
203end;
204
205
206constructor TForm_ToolTemplate.Create(AOwner: TComponent);
207var
208  i: Integer;
209  item: TMenuItem;
210begin
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;
224end;
225
226procedure TForm_ToolTemplate.filepopupPopup(Sender: TObject);
227var
228  ext: String;
229  i: Integer;
230begin
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;
239end;
240
241procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject);
242begin
243  LoadFileNames;
244end;
245
246procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
247begin
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;
261end;
262
263procedure TForm_ToolTemplate.check_filternameClick(Sender: TObject);
264begin
265  edit_filtername.Enabled := not check_filtername.Checked;
266  LoadFileNames;
267end;
268
269procedure TForm_ToolTemplate.listClick(Sender: TObject);
270var
271  fileid: Integer;
272begin
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;
280end;
281
282procedure TForm_ToolTemplate.listMouseDown(Sender: TObject; Button: TMouseButton;
283  Shift: TShiftState; X, Y: Integer);
284var
285  pt: TPoint;
286begin
287  pt.X := x;
288  pt.Y := y;
289  filelist.ItemIndex := filelist.ItemAtPos(pt, true);
290  Self.listClick(Self);
291end;
292
293
294
295procedure TForm_ToolTemplate.SelectFileID(id: Integer);
296var
297  i: Integer;
298begin
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);
308end;
309
310procedure TForm_ToolTemplate.SelectFileName(filename: String);
311var
312  i: Integer;
313begin
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);
320end;
321
322procedure TForm_ToolTemplate.SetAllowedExts(exts: String);
323begin
324  FAllowedExts := exts;
325  RecreateList;
326end;
327
328procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String;
329  zerobytes: Boolean);
330var
331  i: Integer;
332begin
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;
348end;
349
350procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean);
351begin
352  FAllowMultiSelect := allow;
353  filelist.MultiSelect := allow;
354end;
355
356
357procedure TForm_ToolTemplate.FormResize(Sender: TObject);
358begin
359  if Self.Width < 300 then
360    Self.Width := 300;
361  if Self.Height < 200 then
362    Self.Height := 200;
363end;
364
365
366
367procedure TForm_ToolTemplate.FormCreate(Sender: TObject);
368begin
369  Self.Width  := 260;
370  Self.Height := 300;
371  FOnNewFileSelected := nil;
372  FAllowedExts := '';
373  FAllowMultiSelect := False;
374end;
375
376procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction);
377begin
378  Action := caFree;
379end;
380
381
382procedure AddToolListEntryExt(context, ext: String);
383var
384  i: Integer;
385begin
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;
398end;
399
400procedure AddToolListEntry(context, name, exts: String);
401var
402  i: Integer;
403begin
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;
425end;
426
427end.
Note: See TracBrowser for help on using the repository browser.