source: trunk/Forms/FormAcronyms.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: 10.9 KB
Line 
1unit FormAcronyms;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
7 Menus, ActnList, ListViewSort, Acronym, LazUTF8, Generics.Collections,
8 Generics.Defaults, FormEx;
9
10type
11
12 { TFormAcronyms }
13
14 TFormAcronyms = class(TFormEx)
15 AAdd: TAction;
16 ASelectAll: TAction;
17 AModify: TAction;
18 ARemove: TAction;
19 ActionList1: TActionList;
20 ListViewAcronyms: TListView;
21 ListViewFilter1: TListViewFilter;
22 ListViewSort1: TListViewSort;
23 MenuItem4: TMenuItem;
24 MenuItem5: TMenuItem;
25 MenuItem6: TMenuItem;
26 MenuItem7: TMenuItem;
27 PopupMenuAcronym: TPopupMenu;
28 StatusBar1: TStatusBar;
29 ToolBar1: TToolBar;
30 ToolButton1: TToolButton;
31 ToolButton2: TToolButton;
32 ToolButton3: TToolButton;
33 procedure AAddExecute(Sender: TObject);
34 procedure AModifyExecute(Sender: TObject);
35 procedure ARemoveExecute(Sender: TObject);
36 procedure ASelectAllExecute(Sender: TObject);
37 procedure FormActivate(Sender: TObject);
38 procedure FormCreate(Sender: TObject);
39 procedure FormShow(Sender: TObject);
40 procedure ListViewAcronymsData(Sender: TObject; Item: TListItem);
41 procedure ListViewAcronymsDblClick(Sender: TObject);
42 procedure ListViewAcronymsKeyPress(Sender: TObject; var Key: char);
43 procedure ListViewAcronymsResize(Sender: TObject);
44 procedure ListViewAcronymsSelectItem(Sender: TObject; Item: TListItem;
45 Selected: Boolean);
46 procedure ListViewFilter1Change(Sender: TObject);
47 procedure ListViewSort1ColumnWidthChanged(Sender: TObject);
48 function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
49 procedure ListViewSort1Filter(ListViewSort: TListViewSort);
50 private
51 FAcronyms: TAcronyms;
52 MeaningCount: Integer;
53 function CompareStrings(Strings1, Strings2: TStrings): Boolean;
54 function AcronymComparer(constref Item1, Item2: TAcronym): Integer;
55 procedure FilterList(List: TObjectList<TObject>);
56 procedure SetAcronyms(AValue: TAcronyms);
57 procedure UpdateAcronymsList;
58 public
59 AcronymDb: TAcronymDb;
60 FocusAcronym: TAcronymMeaning;
61 property Acronyms: TAcronyms read FAcronyms write SetAcronyms;
62 procedure UpdateInterface;
63 end;
64
65
66implementation
67
68{$R *.lfm}
69
70uses
71 FormAcronym;
72
73resourcestring
74 SRemoveAcronym = 'Remove acronyms';
75 SRemoveAcronymQuery = 'Do you want to remove selected acronyms?';
76 STotal = 'Total';
77 SFiltered = 'Filtered';
78
79
80{ TFormAcronyms }
81
82procedure TFormAcronyms.ListViewAcronymsData(Sender: TObject; Item: TListItem);
83begin
84 if Item.Index < ListViewSort1.List.Count then
85 with TAcronymMeaning(ListViewSort1.List[Item.Index]) do begin
86 Item.Caption := Acronym.Name;
87 Item.SubItems.Add(Name);
88 Item.Data := TAcronymMeaning(ListViewSort1.List[Item.Index]);
89 Item.SubItems.Add(Categories.GetString);
90 end;
91end;
92
93procedure TFormAcronyms.ListViewAcronymsDblClick(Sender: TObject);
94begin
95 AModify.Execute;
96end;
97
98procedure TFormAcronyms.ListViewAcronymsKeyPress(Sender: TObject; var Key: char
99 );
100begin
101 if Key = #27 then Close;
102end;
103
104procedure TFormAcronyms.ListViewAcronymsResize(Sender: TObject);
105begin
106 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
107end;
108
109procedure TFormAcronyms.ListViewAcronymsSelectItem(Sender: TObject;
110 Item: TListItem; Selected: Boolean);
111begin
112 UpdateInterface;
113end;
114
115procedure TFormAcronyms.FormShow(Sender: TObject);
116var
117 I: Integer;
118begin
119 UpdateAcronymsList;
120 ScaleDPI.ScaleControl(ToolBar1, ScaleDPI.DesignDPI);
121
122 // Focus line with acronym
123 if Assigned(FocusAcronym) then begin
124 I := 0;
125 while (I < ListViewAcronyms.Items.Count) and (ListViewAcronyms.Items[I].Data <> FocusAcronym) do Inc(I);
126 if I < ListViewAcronyms.Items.Count then begin
127 ListViewAcronyms.ItemIndex := I;
128 ListViewAcronyms.Selected.Focused := True;
129 end;
130 if ListViewAcronyms.Selected <> nil then
131 ListViewAcronyms.Selected.MakeVisible(False);
132 end;
133end;
134
135function TFormAcronyms.CompareStrings(Strings1, Strings2: TStrings): Boolean;
136var
137 I: Integer;
138begin
139 Result := Strings1.Count = Strings2.Count;
140 if not Result then Exit;
141 for I := 0 to Strings1.Count - 1 do
142 if (Strings1[I] <> Strings2[I]) or (Strings1.Objects[I] <> Strings2.Objects[I]) then begin
143 Result := False;
144 Exit;
145 end;
146end;
147
148procedure TFormAcronyms.AAddExecute(Sender: TObject);
149var
150 TempEntry: TAcronymEntry;
151 Meaning: TAcronymMeaning;
152 FormAcronym: TFormAcronym;
153begin
154 TempEntry := TAcronymEntry.Create;
155 TempEntry.Name := '';
156 TempEntry.Meaning := '';
157 TempEntry.Description := '';
158 FormAcronym := TFormAcronym.Create(Self);
159 try
160 FormAcronym.AcronymDb := AcronymDb;
161 FormAcronym.Load(TempEntry);
162 if FormAcronym.ShowModal = mrOk then begin
163 FormAcronym.Save(TempEntry);
164 Meaning := Acronyms.Db.AddAcronym(TempEntry.Name, TempEntry.Meaning);
165 Meaning.Description := TempEntry.Description;
166 Meaning.Categories.AssignFromStrings(TempEntry.Categories);
167
168 Acronyms.Db.Update;
169 UpdateAcronymsList;
170 UpdateInterface;
171 end;
172 finally
173 FreeAndNil(FormAcronym);
174 end;
175 TempEntry.Free;
176end;
177
178procedure TFormAcronyms.AModifyExecute(Sender: TObject);
179var
180 TempEntry: TAcronymEntry;
181 TempCategories: TStringList;
182 Meaning: TAcronymMeaning;
183 FormAcronym: TFormAcronym;
184begin
185 if Assigned(ListViewAcronyms.Selected) then
186 with TAcronymMeaning(ListViewAcronyms.Selected.Data) do begin
187 TempEntry := TAcronymEntry.Create;
188 TempEntry.Name := Acronym.Name;
189 TempEntry.Meaning := Name;
190 TempEntry.Description := Description;
191 Categories.AssignToStrings(TempEntry.Categories);
192 Sources.AssignToStrings(TempEntry.Sources);
193 TempCategories := TStringList.Create;
194 TempCategories.Assign(TempEntry.Categories);
195 FormAcronym := TFormAcronym.Create(Self);
196 try
197 FormAcronym.AcronymDb := AcronymDb;
198 FormAcronym.Load(TempEntry);
199 if FormAcronym.ShowModal = mrOk then begin
200 FormAcronym.Save(TempEntry);
201 if (TempEntry.Name <> Acronym.Name) or
202 (TempEntry.Meaning <> Name) or
203 (TempEntry.Description <> Description) or
204 not CompareStrings(TempEntry.Categories, TempCategories) then begin
205 // TODO: Update item inplace if possible
206 Acronyms.Db.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Selected.Data));
207 Meaning := Acronyms.Db.AddAcronym(TempEntry.Name, TempEntry.Meaning);
208 Meaning.Description := TempEntry.Description;
209 Meaning.Categories.AssignFromStrings(TempEntry.Categories);
210
211 Acronyms.Db.Update;
212 UpdateAcronymsList;
213 UpdateInterface;
214 end;
215 end;
216 finally
217 FreeAndNil(FormAcronym);
218 FreeAndNil(TempEntry);
219 FreeAndNil(TempCategories);
220 end;
221 end;
222end;
223
224procedure TFormAcronyms.ARemoveExecute(Sender: TObject);
225var
226 I: Integer;
227begin
228 if Assigned(ListViewAcronyms.Selected) then begin
229 if MessageDlg(SRemoveAcronym, SRemoveAcronymQuery,
230 TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
231 Acronyms.Db.BeginUpdate;
232 for I := ListViewAcronyms.Items.Count - 1 downto 0 do
233 if ListViewAcronyms.Items[I].Selected then begin
234 ListViewAcronyms.Items[I].Selected := False;
235 Acronyms.Db.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Items[I].Data));
236 end;
237 UpdateAcronymsList;
238 UpdateInterface;
239 Acronyms.Db.EndUpdate;
240 end;
241 end;
242end;
243
244procedure TFormAcronyms.ASelectAllExecute(Sender: TObject);
245var
246 I: Integer;
247begin
248 for I := 0 to ListViewAcronyms.Items.Count - 1 do
249 ListViewAcronyms.Items[I].Selected := True;
250end;
251
252procedure TFormAcronyms.FormActivate(Sender: TObject);
253begin
254 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
255end;
256
257procedure TFormAcronyms.FormCreate(Sender: TObject);
258var
259 I: Integer;
260begin
261 FocusAcronym := nil;
262 MeaningCount := 0;
263 for I := 0 to ToolBar1.ButtonCount - 1 do
264 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
265end;
266
267procedure TFormAcronyms.ListViewFilter1Change(Sender: TObject);
268begin
269 UpdateAcronymsList;
270end;
271
272procedure TFormAcronyms.ListViewSort1ColumnWidthChanged(Sender: TObject);
273begin
274 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
275end;
276
277function TFormAcronyms.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
278begin
279 Result := 0;
280 if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
281 with ListViewSort1 do
282 case Column of
283 0: Result := CompareString(TAcronymMeaning(Item1).Acronym.Name, TAcronymMeaning(Item2).Acronym.Name);
284 1: Result := CompareString(TAcronymMeaning(Item1).Name, TAcronymMeaning(Item2).Name);
285 2: Result := CompareString(TAcronymMeaning(Item1).Categories.GetString, TAcronymMeaning(Item2).Categories.GetString);
286 end;
287 if ListViewSort1.Order = soDown then Result := -Result;
288 end else Result := 0;
289end;
290
291function TFormAcronyms.AcronymComparer(constref Item1, Item2: TAcronym): Integer;
292begin
293 Result := CompareStr(TAcronym(Item1).Name, TAcronym(Item2).Name);
294end;
295
296procedure TFormAcronyms.ListViewSort1Filter(ListViewSort: TListViewSort);
297begin
298 Acronyms.Db.Acronyms.Sort(TComparer<TAcronym>.Construct(AcronymComparer));
299 Acronyms.Db.AssignToList(ListViewSort1.List);
300 MeaningCount := ListViewSort1.List.Count;
301 FilterList(ListViewSort1.List);
302end;
303
304procedure TFormAcronyms.FilterList(List: TObjectList<TObject>);
305var
306 I: Integer;
307 FoundCount: Integer;
308 EnteredCount: Integer;
309begin
310 EnteredCount := ListViewFilter1.TextEnteredCount;
311 for I := List.Count - 1 downto 0 do begin
312 if List.Items[I] is TAcronymMeaning then begin
313 with TAcronymMeaning(List.Items[I]) do begin
314 with ListViewFilter1 do
315 if Visible and (EnteredCount > 0) then begin
316 FoundCount := 0;
317 if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
318 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Acronym.Name)) > 0 then Inc(FoundCount);
319 if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
320 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Name)) > 0 then Inc(FoundCount);
321 if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
322 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
323 if FoundCount <> EnteredCount then List.Delete(I);
324 end;
325 end;
326 end else
327 if TAcronymMeaning(List.Items[I]) is TAcronymMeaning then begin
328 List.Delete(I);
329 end;
330 end;
331end;
332
333procedure TFormAcronyms.SetAcronyms(AValue: TAcronyms);
334begin
335 if FAcronyms = AValue then Exit;
336 FAcronyms := AValue;
337end;
338
339procedure TFormAcronyms.UpdateAcronymsList;
340begin
341 ListViewSort1.Refresh;
342 UpdateInterface;
343 StatusBar1.Panels[0].Text := STotal + ': ' + IntToStr(MeaningCount);
344 StatusBar1.Panels[1].Text := SFiltered + ': ' + IntToStr(ListViewAcronyms.Items.Count);
345end;
346
347procedure TFormAcronyms.UpdateInterface;
348begin
349 ARemove.Enabled := Assigned(FAcronyms) and Assigned(ListViewAcronyms.Selected);
350 AModify.Enabled := Assigned(FAcronyms) and Assigned(ListViewAcronyms.Selected);
351 AAdd.Enabled := Assigned(FAcronyms);
352 ASelectAll.Enabled := True;
353end;
354
355end.
356
Note: See TracBrowser for help on using the repository browser.