source: tags/1.3.1/Forms/UFormImportSources.pas

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