1 | unit UFormAcronyms;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
---|
9 | Menus, ActnList, UListViewSort, UAcronym, LazUTF8, SpecializedList;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TFormAcronyms }
|
---|
14 |
|
---|
15 | TFormAcronyms = class(TForm)
|
---|
16 | AAdd: TAction;
|
---|
17 | ASelectAll: TAction;
|
---|
18 | AModify: TAction;
|
---|
19 | ARemove: TAction;
|
---|
20 | ActionList1: TActionList;
|
---|
21 | ListViewAcronyms: TListView;
|
---|
22 | ListViewFilter1: TListViewFilter;
|
---|
23 | ListViewSort1: TListViewSort;
|
---|
24 | MenuItem4: TMenuItem;
|
---|
25 | MenuItem5: TMenuItem;
|
---|
26 | MenuItem6: TMenuItem;
|
---|
27 | MenuItem7: TMenuItem;
|
---|
28 | PopupMenuAcronym: TPopupMenu;
|
---|
29 | StatusBar1: TStatusBar;
|
---|
30 | ToolBar1: TToolBar;
|
---|
31 | ToolButton1: TToolButton;
|
---|
32 | ToolButton2: TToolButton;
|
---|
33 | ToolButton3: TToolButton;
|
---|
34 | procedure AAddExecute(Sender: TObject);
|
---|
35 | procedure AModifyExecute(Sender: TObject);
|
---|
36 | procedure ARemoveExecute(Sender: TObject);
|
---|
37 | procedure ASelectAllExecute(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 | MeaningCount: Integer;
|
---|
52 | procedure FilterList(List: TListObject);
|
---|
53 | procedure UpdateAcronymsList;
|
---|
54 | public
|
---|
55 | procedure UpdateInterface;
|
---|
56 | end;
|
---|
57 |
|
---|
58 | var
|
---|
59 | FormAcronyms: TFormAcronyms;
|
---|
60 |
|
---|
61 | implementation
|
---|
62 |
|
---|
63 | {$R *.lfm}
|
---|
64 |
|
---|
65 | uses
|
---|
66 | UCore, UFormMain, UFormAcronym;
|
---|
67 |
|
---|
68 | resourcestring
|
---|
69 | SRemoveAcronym = 'Remove acronyms';
|
---|
70 | SRemoveAcronymQuery = 'Do you want to remove selected acronyms?';
|
---|
71 | STotal = 'Total';
|
---|
72 | SFiltered = 'Filtered';
|
---|
73 |
|
---|
74 |
|
---|
75 | { TFormAcronyms }
|
---|
76 |
|
---|
77 | procedure TFormAcronyms.ListViewAcronymsData(Sender: TObject; Item: TListItem);
|
---|
78 | begin
|
---|
79 | if Item.Index < ListViewSort1.List.Count then
|
---|
80 | with TAcronymMeaning(ListViewSort1.List[Item.Index]) do begin
|
---|
81 | Item.Caption := Acronym.Name;
|
---|
82 | Item.SubItems.Add(Name);
|
---|
83 | Item.Data := TAcronymMeaning(ListViewSort1.List[Item.Index]);
|
---|
84 | Item.SubItems.Add(Categories.GetString);
|
---|
85 | end;
|
---|
86 | end;
|
---|
87 |
|
---|
88 | procedure TFormAcronyms.ListViewAcronymsDblClick(Sender: TObject);
|
---|
89 | begin
|
---|
90 | AModify.Execute;
|
---|
91 | end;
|
---|
92 |
|
---|
93 | procedure TFormAcronyms.ListViewAcronymsKeyPress(Sender: TObject; var Key: char
|
---|
94 | );
|
---|
95 | begin
|
---|
96 | if Key = #27 then Close;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | procedure TFormAcronyms.ListViewAcronymsResize(Sender: TObject);
|
---|
100 | begin
|
---|
101 | //ListViewFilter1.UpdateFromListView(ListViewAcronyms);
|
---|
102 | end;
|
---|
103 |
|
---|
104 | procedure TFormAcronyms.ListViewAcronymsSelectItem(Sender: TObject;
|
---|
105 | Item: TListItem; Selected: Boolean);
|
---|
106 | begin
|
---|
107 | UpdateInterface;
|
---|
108 | end;
|
---|
109 |
|
---|
110 | procedure TFormAcronyms.FormShow(Sender: TObject);
|
---|
111 | begin
|
---|
112 | ListViewFilter1.UpdateFromListView(ListViewAcronyms);
|
---|
113 | UpdateAcronymsList;
|
---|
114 | Core.ScaleDPI1.ScaleControl(ToolBar1, Core.ScaleDPI1.DesignDPI);
|
---|
115 | end;
|
---|
116 |
|
---|
117 | procedure TFormAcronyms.AAddExecute(Sender: TObject);
|
---|
118 | var
|
---|
119 | TempEntry: TAcronymEntry;
|
---|
120 | Meaning: TAcronymMeaning;
|
---|
121 | I: Integer;
|
---|
122 | begin
|
---|
123 | TempEntry := TAcronymEntry.Create;
|
---|
124 | TempEntry.Name := '';
|
---|
125 | TempEntry.Meaning := '';
|
---|
126 | TempEntry.Description := '';
|
---|
127 | FormAcronym := TFormAcronym.Create(Self);
|
---|
128 | try
|
---|
129 | FormAcronym.Load(TempEntry);
|
---|
130 | if FormAcronym.ShowModal = mrOk then begin
|
---|
131 | FormAcronym.Save(TempEntry);
|
---|
132 | Meaning := Core.AcronymDb.AddAcronym(TempEntry.Name, TempEntry.Meaning);
|
---|
133 | Meaning.Description := TempEntry.Description;
|
---|
134 | Meaning.Categories.AssignFromStrings(TempEntry.Categories);
|
---|
135 |
|
---|
136 | // Update reverse references
|
---|
137 | for I := 0 to Meaning.Categories.Count - 1 do
|
---|
138 | if TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.IndexOf(Meaning) = -1 then
|
---|
139 | TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.Add(Meaning);
|
---|
140 |
|
---|
141 | UpdateAcronymsList;
|
---|
142 | UpdateInterface;
|
---|
143 | end;
|
---|
144 | finally
|
---|
145 | FreeAndNil(FormAcronym);
|
---|
146 | end;
|
---|
147 | TempEntry.Free;
|
---|
148 | end;
|
---|
149 |
|
---|
150 | procedure TFormAcronyms.AModifyExecute(Sender: TObject);
|
---|
151 | var
|
---|
152 | TempEntry: TAcronymEntry;
|
---|
153 | TempCategories: TStringList;
|
---|
154 | Meaning: TAcronymMeaning;
|
---|
155 | I: Integer;
|
---|
156 | begin
|
---|
157 | if Assigned(ListViewAcronyms.Selected) then
|
---|
158 | with TAcronymMeaning(ListViewAcronyms.Selected.Data) do begin
|
---|
159 | TempEntry := TAcronymEntry.Create;
|
---|
160 | TempEntry.Name := Acronym.Name;
|
---|
161 | TempEntry.Meaning := Name;
|
---|
162 | TempEntry.Description := Description;
|
---|
163 | Categories.AssignToStrings(TempEntry.Categories);
|
---|
164 | Sources.AssignToStrings(TempEntry.Sources);
|
---|
165 | TempCategories := TStringList.Create;
|
---|
166 | TempCategories.Assign(TempEntry.Categories);
|
---|
167 | FormAcronym := TFormAcronym.Create(Self);
|
---|
168 | try
|
---|
169 | FormAcronym.Load(TempEntry);
|
---|
170 | if FormAcronym.ShowModal = mrOk then begin
|
---|
171 | FormAcronym.Save(TempEntry);
|
---|
172 | if (TempEntry.Name <> Acronym.Name) or
|
---|
173 | (TempEntry.Meaning <> Name) or
|
---|
174 | (TempEntry.Description <> Description) or
|
---|
175 | not FormMain.CompareStrings(TempEntry.Categories, TempCategories) then begin
|
---|
176 | // TODO: Update item inplace if possible
|
---|
177 | Core.AcronymDb.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Selected.Data));
|
---|
178 | Meaning := Core.AcronymDb.AddAcronym(TempEntry.Name, TempEntry.Meaning);
|
---|
179 | Meaning.Description := TempEntry.Description;
|
---|
180 | Meaning.Categories.AssignFromStrings(TempEntry.Categories);
|
---|
181 |
|
---|
182 | // Update reverse references
|
---|
183 | for I := 0 to Meaning.Categories.Count - 1 do
|
---|
184 | if TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.IndexOf(Meaning) = -1 then
|
---|
185 | TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.Add(Meaning);
|
---|
186 |
|
---|
187 | UpdateAcronymsList;
|
---|
188 | UpdateInterface;
|
---|
189 | end;
|
---|
190 | end;
|
---|
191 | finally
|
---|
192 | FreeAndNil(FormAcronym);
|
---|
193 | FreeAndNil(TempEntry);
|
---|
194 | FreeAndNil(TempCategories);
|
---|
195 | end;
|
---|
196 | end;
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TFormAcronyms.ARemoveExecute(Sender: TObject);
|
---|
200 | var
|
---|
201 | I: Integer;
|
---|
202 | begin
|
---|
203 | if Assigned(ListViewAcronyms.Selected) then begin
|
---|
204 | if MessageDlg(SRemoveAcronym, SRemoveAcronymQuery,
|
---|
205 | TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
|
---|
206 | for I := ListViewAcronyms.Items.Count - 1 downto 0 do
|
---|
207 | if ListViewAcronyms.Items[I].Selected then begin
|
---|
208 | ListViewAcronyms.Items[I].Selected := False;
|
---|
209 | Core.AcronymDb.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Items[I].Data));
|
---|
210 | end;
|
---|
211 | UpdateAcronymsList;
|
---|
212 | UpdateInterface;
|
---|
213 | end;
|
---|
214 | end;
|
---|
215 | end;
|
---|
216 |
|
---|
217 | procedure TFormAcronyms.ASelectAllExecute(Sender: TObject);
|
---|
218 | var
|
---|
219 | I: Integer;
|
---|
220 | begin
|
---|
221 | for I := 0 to ListViewAcronyms.Items.Count - 1 do
|
---|
222 | ListViewAcronyms.Items[I].Selected := True;
|
---|
223 | end;
|
---|
224 |
|
---|
225 | procedure TFormAcronyms.FormCreate(Sender: TObject);
|
---|
226 | var
|
---|
227 | I: Integer;
|
---|
228 | begin
|
---|
229 | Core.CoolTranslator1.TranslateComponentRecursive(Self);
|
---|
230 | for I := 0 to ToolBar1.ButtonCount - 1 do
|
---|
231 | ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
|
---|
232 | end;
|
---|
233 |
|
---|
234 | procedure TFormAcronyms.ListViewFilter1Change(Sender: TObject);
|
---|
235 | begin
|
---|
236 | UpdateAcronymsList;
|
---|
237 | end;
|
---|
238 |
|
---|
239 | procedure TFormAcronyms.ListViewSort1ColumnWidthChanged(Sender: TObject);
|
---|
240 | begin
|
---|
241 | ListViewFilter1.UpdateFromListView(ListViewAcronyms);
|
---|
242 | end;
|
---|
243 |
|
---|
244 | function TFormAcronyms.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
---|
245 | begin
|
---|
246 | Result := 0;
|
---|
247 | if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
|
---|
248 | with ListViewSort1 do
|
---|
249 | case Column of
|
---|
250 | 0: Result := CompareString(TAcronymMeaning(Item1).Acronym.Name, TAcronymMeaning(Item2).Acronym.Name);
|
---|
251 | 1: Result := CompareString(TAcronymMeaning(Item1).Name, TAcronymMeaning(Item2).Name);
|
---|
252 | 2: Result := CompareString(TAcronymMeaning(Item1).Categories.GetString, TAcronymMeaning(Item2).Categories.GetString);
|
---|
253 | end;
|
---|
254 | if ListViewSort1.Order = soDown then Result := -Result;
|
---|
255 | end else Result := 0;
|
---|
256 | end;
|
---|
257 |
|
---|
258 | procedure TFormAcronyms.ListViewSort1Filter(ListViewSort: TListViewSort);
|
---|
259 | begin
|
---|
260 | Core.AcronymDb.Acronyms.Sort(AcronymComparer);
|
---|
261 | Core.AcronymDb.AssignToList(ListViewSort1.List);
|
---|
262 | MeaningCount := ListViewSort1.List.Count;
|
---|
263 | FilterList(ListViewSort1.List);
|
---|
264 | end;
|
---|
265 |
|
---|
266 | procedure TFormAcronyms.FilterList(List: TListObject);
|
---|
267 | var
|
---|
268 | I: Integer;
|
---|
269 | FoundCount: Integer;
|
---|
270 | EnteredCount: Integer;
|
---|
271 | begin
|
---|
272 | EnteredCount := ListViewFilter1.TextEnteredCount;
|
---|
273 | for I := List.Count - 1 downto 0 do begin
|
---|
274 | if List.Items[I] is TAcronymMeaning then begin
|
---|
275 | with TAcronymMeaning(List.Items[I]) do begin
|
---|
276 | with ListViewFilter1 do
|
---|
277 | if Visible and (EnteredCount > 0) then begin
|
---|
278 | FoundCount := 0;
|
---|
279 | if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
|
---|
280 | UTF8LowerCase(TAcronymMeaning(List.Items[I]).Acronym.Name)) > 0 then Inc(FoundCount);
|
---|
281 | if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
|
---|
282 | UTF8LowerCase(TAcronymMeaning(List.Items[I]).Name)) > 0 then Inc(FoundCount);
|
---|
283 | if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
|
---|
284 | UTF8LowerCase(TAcronymMeaning(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
|
---|
285 | if FoundCount <> EnteredCount then List.Delete(I);
|
---|
286 | end;
|
---|
287 | end;
|
---|
288 | end else
|
---|
289 | if TAcronymMeaning(List.Items[I]) is TAcronymMeaning then begin
|
---|
290 | List.Delete(I);
|
---|
291 | end;
|
---|
292 | end;
|
---|
293 | end;
|
---|
294 |
|
---|
295 | procedure TFormAcronyms.UpdateAcronymsList;
|
---|
296 | begin
|
---|
297 | ListViewSort1.Refresh;
|
---|
298 | UpdateInterface;
|
---|
299 | StatusBar1.Panels[0].Text := STotal + ': ' + IntToStr(MeaningCount);
|
---|
300 | StatusBar1.Panels[1].Text := SFiltered + ': ' + IntToStr(ListViewAcronyms.Items.Count);
|
---|
301 | end;
|
---|
302 |
|
---|
303 | procedure TFormAcronyms.UpdateInterface;
|
---|
304 | begin
|
---|
305 | ARemove.Enabled := Assigned(Core.AcronymDb) and Assigned(ListViewAcronyms.Selected);
|
---|
306 | AModify.Enabled := Assigned(Core.AcronymDb) and Assigned(ListViewAcronyms.Selected);
|
---|
307 | AAdd.Enabled := Assigned(Core.AcronymDb);
|
---|
308 | ASelectAll.Enabled := True;
|
---|
309 | end;
|
---|
310 |
|
---|
311 | end.
|
---|
312 |
|
---|