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