source: trunk/Forms/FormImportSources.pas

Last change on this file was 234, checked in by chronos, 5 months ago
  • Fixed: Workaround to show checkboxes on Qt5 in import sources and acronym categories.
File size: 12.5 KB
Line 
1unit FormImportSources;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
7 ActnList, Menus, Acronym, JobProgressView, ListViewSort, LazUTF8, FormEx,
8 Generics.Collections;
9
10type
11
12 { TFormImportSources }
13
14 TFormImportSources = class(TFormEx)
15 AAdd: TAction;
16 APreview: TAction;
17 AEnable: TAction;
18 ADisable: TAction;
19 AProcess: TAction;
20 ActionList1: TActionList;
21 AModify: TAction;
22 ARemove: TAction;
23 JobProgressView1: TJobProgressView;
24 ListView1: TListView;
25 ListViewFilter1: TListViewFilter;
26 ListViewSort1: TListViewSort;
27 MenuItem1: TMenuItem;
28 MenuItem2: TMenuItem;
29 MenuItem3: TMenuItem;
30 MenuItem4: TMenuItem;
31 MenuItem5: TMenuItem;
32 MenuItem6: TMenuItem;
33 MenuItem7: TMenuItem;
34 MenuItem8: TMenuItem;
35 PopupMenuImportSource: TPopupMenu;
36 ToolBar1: TToolBar;
37 ToolButton1: TToolButton;
38 ToolButton2: TToolButton;
39 ToolButton3: TToolButton;
40 ToolButton4: TToolButton;
41 ToolButton5: TToolButton;
42 ToolButton6: TToolButton;
43 ToolButton7: TToolButton;
44 procedure AAddExecute(Sender: TObject);
45 procedure ADisableExecute(Sender: TObject);
46 procedure AEnableExecute(Sender: TObject);
47 procedure AModifyExecute(Sender: TObject);
48 procedure APreviewExecute(Sender: TObject);
49 procedure AProcessExecute(Sender: TObject);
50 procedure ARemoveExecute(Sender: TObject);
51 procedure FormActivate(Sender: TObject);
52 procedure FormCreate(Sender: TObject);
53 procedure FormShow(Sender: TObject);
54 procedure ListView1Change(Sender: TObject; Item: TListItem;
55 Change: TItemChange);
56 procedure ListView1Data(Sender: TObject; Item: TListItem);
57 procedure ListView1DblClick(Sender: TObject);
58 procedure ListView1KeyPress(Sender: TObject; var Key: char);
59 procedure ListView1Resize(Sender: TObject);
60 procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
61 Selected: Boolean);
62 procedure ListViewFilter1Change(Sender: TObject);
63 procedure ListViewSort1ColumnWidthChanged(Sender: TObject);
64 function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
65 procedure ListViewSort1Filter(ListViewSort: TListViewSort);
66 private
67 PreviewAcronymDb: TAcronymDb;
68 procedure ProcessImportJob(Job: TJob);
69 procedure PreviewImportJob(Job: TJob);
70 procedure FilterList(List: TObjectList<TObject>);
71 public
72 ImportSources: TImportSources;
73 AcronymDb: TAcronymDb;
74 procedure UpdateList;
75 procedure UpdateInterface;
76 end;
77
78
79implementation
80
81{$R *.lfm}
82
83uses
84 FormMain, FormImportSource, FormAcronyms;
85
86resourcestring
87 SRemoveImportSource = 'Remove import sources';
88 SRemoveImportSourceQuery = 'Do you really want to remove selected import sources?';
89 SImportSourceAlreadyExists = 'Import source %s already exists!';
90 SProcessSelectedSource = 'Process selected import source';
91 SPreviewSelectedSource = 'Preview selected import source';
92
93
94{ TFormImportSources }
95
96procedure TFormImportSources.ListView1Data(Sender: TObject; Item: TListItem);
97begin
98 if Item.Index < ListViewSort1.List.Count then
99 with TImportSource(ListViewSort1.List[Item.Index]) do begin
100 Item.Caption := Name;
101 Item.Data := ListViewSort1.List[Item.Index];
102 Item.SubItems.Add(URL);
103 Item.SubItems.Add(Categories.GetString);
104 Item.SubItems.Add(IntToStr(ItemCount));
105 if LastImportTime <> 0 then
106 Item.SubItems.Add(DateToStr(LastImportTime))
107 else Item.SubItems.Add('');
108 Item.SubItems.Add(Format.Name);
109 Item.Checked := Enabled;
110 if Enabled then Item.StateIndex := 24 else
111 Item.StateIndex := 23;
112 end;
113end;
114
115procedure TFormImportSources.ListView1DblClick(Sender: TObject);
116begin
117 AModify.Execute;
118end;
119
120procedure TFormImportSources.ListView1KeyPress(Sender: TObject; var Key: char);
121begin
122 if Key = #27 then Close;
123end;
124
125procedure TFormImportSources.ListView1Resize(Sender: TObject);
126begin
127 ListViewFilter1.UpdateFromListView(ListView1);
128end;
129
130procedure TFormImportSources.ListView1SelectItem(Sender: TObject;
131 Item: TListItem; Selected: Boolean);
132begin
133 UpdateInterface;
134end;
135
136procedure TFormImportSources.ListViewFilter1Change(Sender: TObject);
137begin
138 UpdateList;
139end;
140
141procedure TFormImportSources.ListViewSort1ColumnWidthChanged(Sender: TObject);
142begin
143 ListViewFilter1.UpdateFromListView(ListView1);
144end;
145
146function TFormImportSources.ListViewSort1CompareItem(Item1, Item2: TObject
147 ): Integer;
148var
149 ImportSource1, ImportSource2: TImportSource;
150begin
151 ImportSource1 := TImportSource(Item1);
152 ImportSource2 := TImportSource(Item2);
153 Result := 0;
154 if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
155 with ListViewSort1 do
156 case Column of
157 0: Result := CompareString(ImportSource1.Name, ImportSource2.Name);
158 1: Result := CompareString(ImportSource1.URL, ImportSource2.URL);
159 2: Result := CompareString(ImportSource1.Categories.GetString, ImportSource2.Categories.GetString);
160 3: Result := CompareInteger(ImportSource1.ItemCount, ImportSource2.ItemCount);
161 4: Result := CompareTime(ImportSource1.LastImportTime, ImportSource2.LastImportTime);
162 5: Result := CompareString(ImportSource1.Format.Name, ImportSource2.Format.Name);
163 end;
164 if ListViewSort1.Order = soDown then Result := -Result;
165 end else Result := 0;
166end;
167
168procedure TFormImportSources.ListViewSort1Filter(ListViewSort: TListViewSort);
169begin
170 ImportSources.AssignToList(ListViewSort1.List);
171 FilterList(ListViewSort1.List);
172end;
173
174procedure TFormImportSources.UpdateList;
175begin
176 ListViewSort1.Refresh;
177 UpdateInterface;
178end;
179
180procedure TFormImportSources.UpdateInterface;
181begin
182 ARemove.Enabled := Assigned(ListView1.Selected);
183 AModify.Enabled := Assigned(ListView1.Selected);
184 AProcess.Enabled := Assigned(ListView1.Selected);
185 APreview.Enabled := Assigned(ListView1.Selected);
186end;
187
188procedure TFormImportSources.FormShow(Sender: TObject);
189begin
190 UpdateList;
191 ScaleDPI.ScaleControl(ToolBar1, ScaleDPI.DesignDPI);
192end;
193
194procedure TFormImportSources.ListView1Change(Sender: TObject; Item: TListItem;
195 Change: TItemChange);
196begin
197 if Assigned(Item) and (Change = ctState) then begin
198 TImportSource(Item.Data).Enabled := Item.Checked;
199 AcronymDb.Modified := True;
200 end;
201end;
202
203procedure TFormImportSources.AAddExecute(Sender: TObject);
204var
205 NewImportSource: TImportSource;
206 FormImportSource: TFormImportSource;
207begin
208 NewImportSource := TImportSource.Create;
209 NewImportSource.Sources := ImportSources;
210 FormImportSource := TFormImportSource.Create(Self);
211 try
212 FormImportSource.AcronymDb := AcronymDb;
213 FormImportSource.Load(NewImportSource);
214 if FormImportSource.ShowModal = mrOk then begin
215 FormImportSource.Save(NewImportSource);
216 if not Assigned(ImportSources.SearchByName(NewImportSource.Name)) then begin;
217 ImportSources.Add(NewImportSource);
218
219 NewImportSource := nil;
220 AcronymDb.Modified := True;
221 UpdateList;
222 end else ShowMessage(Format(SImportSourceAlreadyExists, [NewImportSource.Name]));
223 end;
224 if Assigned(NewImportSource) then NewImportSource.Free;
225 finally
226 FreeAndNil(FormImportSource);
227 end;
228end;
229
230procedure TFormImportSources.ADisableExecute(Sender: TObject);
231var
232 I: Integer;
233begin
234 for I := ListView1.Items.Count - 1 downto 0 do
235 if ListView1.Items[I].Selected then begin
236 TImportSource(ListView1.Items[I].Data).Enabled := False;
237 AcronymDb.Modified := True;
238 end;
239 UpdateList;
240end;
241
242procedure TFormImportSources.AEnableExecute(Sender: TObject);
243var
244 I: Integer;
245begin
246 for I := ListView1.Items.Count - 1 downto 0 do
247 if ListView1.Items[I].Selected then begin
248 TImportSource(ListView1.Items[I].Data).Enabled := True;
249 AcronymDb.Modified := True;
250 end;
251 UpdateList;
252end;
253
254procedure TFormImportSources.AModifyExecute(Sender: TObject);
255var
256 NewImportSource: TImportSource;
257 FormImportSource: TFormImportSource;
258begin
259 if Assigned(ListView1.Selected) then begin
260 NewImportSource := TImportSource.Create;
261 NewImportSource.Assign(ListView1.Selected.Data);
262 FormImportSource := TFormImportSource.Create(Self);
263 try
264 FormImportSource.AcronymDb := AcronymDb;
265 FormImportSource.Load(NewImportSource);
266 if FormImportSource.ShowModal = mrOk then begin
267 FormImportSource.Save(NewImportSource);
268 if (NewImportSource.Name <> TImportSource(ListView1.Selected.Data).Name) then begin
269 if not Assigned(ImportSources.SearchByName(NewImportSource.Name)) then begin;
270 TImportSource(ListView1.Selected.Data).Assign(NewImportSource);
271 AcronymDb.Modified := True;
272 end else ShowMessage(Format(SImportSourceAlreadyExists, [NewImportSource.Name]));
273 end else begin
274 TImportSource(ListView1.Selected.Data).Assign(NewImportSource);
275 AcronymDb.Modified := True;
276 end;
277
278 UpdateList;
279 end;
280 if Assigned(NewImportSource) then NewImportSource.Free;
281 finally
282 FreeAndNil(FormImportSource);
283 end;
284 end;
285end;
286
287procedure TFormImportSources.APreviewExecute(Sender: TObject);
288var
289 FormAcronyms: TFormAcronyms;
290begin
291 if Assigned(ListView1.Selected) then begin
292 PreviewAcronymDb := TAcronymDb.Create;
293 try
294 JobProgressView1.AddJob(SPreviewSelectedSource, PreviewImportJob);
295 JobProgressView1.Start;
296 FormAcronyms := TFormAcronyms.Create(nil);
297 try
298 FormAcronyms.AcronymDb := PreviewAcronymDb;
299 FormAcronyms.Acronyms := PreviewAcronymDb.Acronyms;
300 FormAcronyms.ShowModal;
301 finally
302 FormAcronyms.Free;
303 end;
304 finally
305 FreeAndNil(PreviewAcronymDb);
306 end;
307 end;
308end;
309
310procedure TFormImportSources.AProcessExecute(Sender: TObject);
311begin
312 if Assigned(ListView1.Selected) then begin
313 AcronymDb.AddedCount := 0;
314 JobProgressView1.AddJob(SProcessSelectedSource, ProcessImportJob);
315 JobProgressView1.Start;
316 ShowMessage(Format(SAddedCount, [TImportSource(ListView1.Selected.Data).ItemCount,
317 AcronymDb.AddedCount]));
318 end;
319end;
320
321procedure TFormImportSources.ProcessImportJob(Job: TJob);
322begin
323 TImportSource(ListView1.Selected.Data).Process(AcronymDb);
324end;
325
326procedure TFormImportSources.PreviewImportJob(Job: TJob);
327begin
328 TImportSource(ListView1.Selected.Data).Process(PreviewAcronymDb);
329end;
330
331procedure TFormImportSources.FilterList(List: TObjectList<TObject>);
332var
333 I: Integer;
334 FoundCount: Integer;
335 EnteredCount: Integer;
336begin
337 EnteredCount := ListViewFilter1.TextEnteredCount;
338 for I := List.Count - 1 downto 0 do begin
339 if List.Items[I] is TImportSource then begin
340 with TImportSource(List.Items[I]) do begin
341 with ListViewFilter1 do
342 if Visible and (EnteredCount > 0) then begin
343 FoundCount := 0;
344 if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
345 UTF8LowerCase(TImportSource(List.Items[I]).Name)) > 0 then Inc(FoundCount);
346 if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
347 UTF8LowerCase(TImportSource(List.Items[I]).URL)) > 0 then Inc(FoundCount);
348 if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
349 UTF8LowerCase(TImportSource(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
350 if Pos(UTF8LowerCase(StringGrid.Cells[3, 0]),
351 UTF8LowerCase(IntToStr(TImportSource(List.Items[I]).ItemCount))) > 0 then Inc(FoundCount);
352 if Pos(UTF8LowerCase(StringGrid.Cells[4, 0]),
353 UTF8LowerCase(DateTimeToStr(TImportSource(List.Items[I]).LastImportTime))) > 0 then Inc(FoundCount);
354 if Pos(UTF8LowerCase(StringGrid.Cells[5, 0]),
355 UTF8LowerCase(TImportSource(List.Items[I]).Format.Name)) > 0 then Inc(FoundCount);
356 if FoundCount <> EnteredCount then List.Delete(I);
357 end;
358 end;
359 end else
360 if TImportSource(List.Items[I]) is TImportSource then begin
361 List.Delete(I);
362 end;
363 end;
364end;
365
366procedure TFormImportSources.ARemoveExecute(Sender: TObject);
367var
368 I: Integer;
369begin
370 if Assigned(ListView1.Selected) then begin
371 if MessageDlg(SRemoveImportSource, SRemoveImportSourceQuery,
372 TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
373 for I := ListView1.Items.Count - 1 downto 0 do
374 if ListView1.Items[I].Selected then
375 ImportSources.Remove(ListView1.Items[I].Data);
376 UpdateList;
377 end;
378 end;
379end;
380
381procedure TFormImportSources.FormActivate(Sender: TObject);
382begin
383 ListViewFilter1.UpdateFromListView(ListView1);
384end;
385
386procedure TFormImportSources.FormCreate(Sender: TObject);
387var
388 I: Integer;
389begin
390 for I := 0 to ToolBar1.ButtonCount - 1 do
391 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
392 {$IFDEF LCLQt5}
393 ListView1.Checkboxes := False;
394 ListView1.StateImages := ActionList1.Images;
395 {$ENDIF}
396end;
397
398end.
399
Note: See TracBrowser for help on using the repository browser.