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 ListViewAcronymsResize(Sender: TObject);
|
---|
43 | procedure ListViewAcronymsSelectItem(Sender: TObject; Item: TListItem;
|
---|
44 | Selected: Boolean);
|
---|
45 | procedure ListViewFilter1Change(Sender: TObject);
|
---|
46 | function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
---|
47 | procedure ListViewSort1Filter(ListViewSort: TListViewSort);
|
---|
48 | private
|
---|
49 | MeaningCount: Integer;
|
---|
50 | procedure FilterList(List: TListObject);
|
---|
51 | procedure UpdateAcronymsList;
|
---|
52 | public
|
---|
53 | procedure UpdateInterface;
|
---|
54 | end;
|
---|
55 |
|
---|
56 | var
|
---|
57 | FormAcronyms: TFormAcronyms;
|
---|
58 |
|
---|
59 | implementation
|
---|
60 |
|
---|
61 | {$R *.lfm}
|
---|
62 |
|
---|
63 | uses
|
---|
64 | UFormMain, UFormAcronym;
|
---|
65 |
|
---|
66 | resourcestring
|
---|
67 | SRemoveAcronym = 'Remove acronyms';
|
---|
68 | SRemoveAcronymQuery = 'Do you want to remove selected acronyms?';
|
---|
69 | STotal = 'Total';
|
---|
70 | SFiltered = 'Filtered';
|
---|
71 |
|
---|
72 |
|
---|
73 | { TFormAcronyms }
|
---|
74 |
|
---|
75 | procedure TFormAcronyms.ListViewAcronymsData(Sender: TObject; Item: TListItem);
|
---|
76 | begin
|
---|
77 | if Item.Index < ListViewSort1.List.Count then
|
---|
78 | with TAcronymMeaning(ListViewSort1.List[Item.Index]) do begin
|
---|
79 | Item.Caption := Acronym.Name;
|
---|
80 | Item.SubItems.Add(Name);
|
---|
81 | Item.Data := TAcronymMeaning(ListViewSort1.List[Item.Index]);
|
---|
82 | Item.SubItems.Add(Categories.GetString);
|
---|
83 | end;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TFormAcronyms.ListViewAcronymsDblClick(Sender: TObject);
|
---|
87 | begin
|
---|
88 | AModify.Execute;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | procedure TFormAcronyms.ListViewAcronymsResize(Sender: TObject);
|
---|
92 | begin
|
---|
93 | //ListViewFilter1.UpdateFromListView(ListViewAcronyms);
|
---|
94 | end;
|
---|
95 |
|
---|
96 | procedure TFormAcronyms.ListViewAcronymsSelectItem(Sender: TObject;
|
---|
97 | Item: TListItem; Selected: Boolean);
|
---|
98 | begin
|
---|
99 | UpdateInterface;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TFormAcronyms.FormShow(Sender: TObject);
|
---|
103 | begin
|
---|
104 | ListViewFilter1.UpdateFromListView(ListViewAcronyms);
|
---|
105 | UpdateAcronymsList;
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TFormAcronyms.AAddExecute(Sender: TObject);
|
---|
109 | var
|
---|
110 | TempEntry: TAcronymEntry;
|
---|
111 | Meaning: TAcronymMeaning;
|
---|
112 | I: Integer;
|
---|
113 | begin
|
---|
114 | TempEntry := TAcronymEntry.Create;
|
---|
115 | TempEntry.Name := '';
|
---|
116 | TempEntry.Meaning := '';
|
---|
117 | TempEntry.Description := '';
|
---|
118 | FormAcronym.Load(TempEntry);
|
---|
119 | if FormAcronym.ShowModal = mrOk then begin
|
---|
120 | FormAcronym.Save(TempEntry);
|
---|
121 | Meaning := FormMain.AcronymDb.AddAcronym(TempEntry.Name, TempEntry.Meaning);
|
---|
122 | Meaning.Description := TempEntry.Description;
|
---|
123 | Meaning.Categories.AssignFromStrings(TempEntry.Categories);
|
---|
124 | for I := 0 to Meaning.Categories.Count - 1 do
|
---|
125 | TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.Add(Meaning);
|
---|
126 | UpdateAcronymsList;
|
---|
127 | UpdateInterface;
|
---|
128 | end;
|
---|
129 | TempEntry.Free;
|
---|
130 | end;
|
---|
131 |
|
---|
132 | procedure TFormAcronyms.AModifyExecute(Sender: TObject);
|
---|
133 | var
|
---|
134 | TempEntry: TAcronymEntry;
|
---|
135 | TempCategories: TStringList;
|
---|
136 | Meaning: TAcronymMeaning;
|
---|
137 | I: Integer;
|
---|
138 | begin
|
---|
139 | if Assigned(ListViewAcronyms.Selected) then
|
---|
140 | with TAcronymMeaning(ListViewAcronyms.Selected.Data) do begin
|
---|
141 | TempEntry := TAcronymEntry.Create;
|
---|
142 | TempEntry.Name := Acronym.Name;
|
---|
143 | TempEntry.Meaning := Name;
|
---|
144 | TempEntry.Description := Description;
|
---|
145 | Categories.AssignToStrings(TempEntry.Categories);
|
---|
146 | TempCategories := TStringList.Create;
|
---|
147 | TempCategories.Assign(TempEntry.Categories);
|
---|
148 | FormAcronym.Load(TempEntry);
|
---|
149 | if FormAcronym.ShowModal = mrOk then begin
|
---|
150 | FormAcronym.Save(TempEntry);
|
---|
151 | if (TempEntry.Name <> Acronym.Name) or
|
---|
152 | (TempEntry.Meaning <> Name) or
|
---|
153 | (TempEntry.Description <> Description) or
|
---|
154 | not FormMain.CompareStrings(TempEntry.Categories, TempCategories) then begin
|
---|
155 | // TODO: Update item inplace if possible
|
---|
156 | FormMain.AcronymDb.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Selected.Data));
|
---|
157 | Meaning := FormMain.AcronymDb.AddAcronym(TempEntry.Name, TempEntry.Meaning);
|
---|
158 | Meaning.Description := TempEntry.Description;
|
---|
159 | Meaning.Categories.AssignFromStrings(TempEntry.Categories);
|
---|
160 | for I := 0 to Meaning.Categories.Count - 1 do
|
---|
161 | TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.Add(Meaning);
|
---|
162 | UpdateAcronymsList;
|
---|
163 | UpdateInterface;
|
---|
164 | end;
|
---|
165 | end;
|
---|
166 | TempEntry.Free;
|
---|
167 | TempCategories.Free;
|
---|
168 | end;
|
---|
169 | end;
|
---|
170 |
|
---|
171 | procedure TFormAcronyms.ARemoveExecute(Sender: TObject);
|
---|
172 | var
|
---|
173 | I: Integer;
|
---|
174 | begin
|
---|
175 | if Assigned(ListViewAcronyms.Selected) then begin
|
---|
176 | if MessageDlg(SRemoveAcronym, SRemoveAcronymQuery,
|
---|
177 | TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
|
---|
178 | for I := ListViewAcronyms.Items.Count - 1 downto 0 do
|
---|
179 | if ListViewAcronyms.Items[I].Selected then begin
|
---|
180 | ListViewAcronyms.Items[I].Selected := False;
|
---|
181 | FormMain.AcronymDb.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Items[I].Data));
|
---|
182 | end;
|
---|
183 | UpdateAcronymsList;
|
---|
184 | UpdateInterface;
|
---|
185 | end;
|
---|
186 | end;
|
---|
187 | end;
|
---|
188 |
|
---|
189 | procedure TFormAcronyms.ASelectAllExecute(Sender: TObject);
|
---|
190 | var
|
---|
191 | I: Integer;
|
---|
192 | begin
|
---|
193 | for I := 0 to ListViewAcronyms.Items.Count - 1 do
|
---|
194 | ListViewAcronyms.Items[I].Selected := True;
|
---|
195 | end;
|
---|
196 |
|
---|
197 | procedure TFormAcronyms.FormCreate(Sender: TObject);
|
---|
198 | var
|
---|
199 | I: Integer;
|
---|
200 | begin
|
---|
201 | for I := 0 to ToolBar1.ButtonCount - 1 do
|
---|
202 | ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
|
---|
203 | end;
|
---|
204 |
|
---|
205 | procedure TFormAcronyms.ListViewFilter1Change(Sender: TObject);
|
---|
206 | begin
|
---|
207 | UpdateAcronymsList;
|
---|
208 | end;
|
---|
209 |
|
---|
210 | function TFormAcronyms.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
---|
211 | begin
|
---|
212 | Result := 0;
|
---|
213 | if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
|
---|
214 | with ListViewSort1 do
|
---|
215 | case Column of
|
---|
216 | 0: Result := CompareString(TAcronymMeaning(Item1).Acronym.Name, TAcronymMeaning(Item2).Acronym.Name);
|
---|
217 | 1: Result := CompareString(TAcronymMeaning(Item1).Name, TAcronymMeaning(Item2).Name);
|
---|
218 | 2: Result := CompareString(TAcronymMeaning(Item1).Categories.GetString, TAcronymMeaning(Item2).Categories.GetString);
|
---|
219 | end;
|
---|
220 | if ListViewSort1.Order = soDown then Result := -Result;
|
---|
221 | end else Result := 0;
|
---|
222 | end;
|
---|
223 |
|
---|
224 | procedure TFormAcronyms.ListViewSort1Filter(ListViewSort: TListViewSort);
|
---|
225 | begin
|
---|
226 | FormMain.AcronymDb.Acronyms.Sort(AcronymComparer);
|
---|
227 | FormMain.AcronymDb.AssignToList(ListViewSort1.List);
|
---|
228 | MeaningCount := ListViewSort1.List.Count;
|
---|
229 | FilterList(ListViewSort1.List);
|
---|
230 | end;
|
---|
231 |
|
---|
232 | procedure TFormAcronyms.FilterList(List: TListObject);
|
---|
233 | var
|
---|
234 | I: Integer;
|
---|
235 | FoundCount: Integer;
|
---|
236 | EnteredCount: Integer;
|
---|
237 | begin
|
---|
238 | EnteredCount := ListViewFilter1.TextEnteredCount;
|
---|
239 | for I := List.Count - 1 downto 0 do begin
|
---|
240 | if List.Items[I] is TAcronymMeaning then begin
|
---|
241 | with TAcronymMeaning(List.Items[I]) do begin
|
---|
242 | with ListViewFilter1 do
|
---|
243 | if Visible and (EnteredCount > 0) then begin
|
---|
244 | FoundCount := 0;
|
---|
245 | if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
|
---|
246 | UTF8LowerCase(TAcronymMeaning(List.Items[I]).Acronym.Name)) > 0 then Inc(FoundCount);
|
---|
247 | if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
|
---|
248 | UTF8LowerCase(TAcronymMeaning(List.Items[I]).Name)) > 0 then Inc(FoundCount);
|
---|
249 | if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
|
---|
250 | UTF8LowerCase(TAcronymMeaning(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
|
---|
251 | if FoundCount <> EnteredCount then List.Delete(I);
|
---|
252 | end;
|
---|
253 | end;
|
---|
254 | end else
|
---|
255 | if TAcronymMeaning(List.Items[I]) is TAcronymMeaning then begin
|
---|
256 | List.Delete(I);
|
---|
257 | end;
|
---|
258 | end;
|
---|
259 | end;
|
---|
260 |
|
---|
261 | procedure TFormAcronyms.UpdateAcronymsList;
|
---|
262 | begin
|
---|
263 | ListViewSort1.Refresh;
|
---|
264 | UpdateInterface;
|
---|
265 | StatusBar1.Panels[0].Text := STotal + ': ' + IntToStr(MeaningCount);
|
---|
266 | StatusBar1.Panels[1].Text := SFiltered + ': ' + IntToStr(ListViewAcronyms.Items.Count);
|
---|
267 | end;
|
---|
268 |
|
---|
269 | procedure TFormAcronyms.UpdateInterface;
|
---|
270 | begin
|
---|
271 | ARemove.Enabled := Assigned(FormMain.AcronymDb) and Assigned(ListViewAcronyms.Selected);
|
---|
272 | AModify.Enabled := Assigned(FormMain.AcronymDb) and Assigned(ListViewAcronyms.Selected);
|
---|
273 | AAdd.Enabled := Assigned(FormMain.AcronymDb);
|
---|
274 | ASelectAll.Enabled := True;
|
---|
275 | end;
|
---|
276 |
|
---|
277 | end.
|
---|
278 |
|
---|