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