source: tags/1.0.0/Forms/UFormAcronyms.pas

Last change on this file was 34, checked in by chronos, 8 years ago
  • Fixed: Error during removing acronyms in case that multiple acronyms with same name exists.
File size: 8.5 KB
Line 
1unit UFormAcronyms;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
9 Menus, ActnList, UListViewSort, UAcronym, LazUTF8, SpecializedList;
10
11type
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
51var
52 FormAcronyms: TFormAcronyms;
53
54implementation
55
56{$R *.lfm}
57
58uses
59 UFormMain, UFormAcronym;
60
61resourcestring
62 SRemoveAcronym = 'Remove acronym';
63 SRemoveAcronymQuery = 'Do you want to remove selected acronym?';
64
65
66{ TFormAcronyms }
67
68procedure TFormAcronyms.ListViewAcronymsData(Sender: TObject; Item: TListItem);
69begin
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;
77end;
78
79procedure TFormAcronyms.ListViewAcronymsDblClick(Sender: TObject);
80begin
81 AModify.Execute;
82end;
83
84procedure TFormAcronyms.ListViewAcronymsResize(Sender: TObject);
85begin
86 //ListViewFilter1.UpdateFromListView(ListViewAcronyms);
87end;
88
89procedure TFormAcronyms.ListViewAcronymsSelectItem(Sender: TObject;
90 Item: TListItem; Selected: Boolean);
91begin
92 UpdateInterface;
93end;
94
95procedure TFormAcronyms.FormShow(Sender: TObject);
96begin
97 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
98 UpdateAcronymsList;
99end;
100
101procedure TFormAcronyms.AAddExecute(Sender: TObject);
102var
103 TempEntry: TAcronymEntry;
104 Meaning: TAcronymMeaning;
105 I: Integer;
106begin
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;
123end;
124
125procedure TFormAcronyms.AModifyExecute(Sender: TObject);
126var
127 TempEntry: TAcronymEntry;
128 TempCategories: TStringList;
129 Meaning: TAcronymMeaning;
130 I: Integer;
131begin
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;
162end;
163
164procedure TFormAcronyms.ARemoveExecute(Sender: TObject);
165var
166 I: Integer;
167begin
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;
180end;
181
182procedure TFormAcronyms.ASelectAllExecute(Sender: TObject);
183var
184 I: Integer;
185begin
186 for I := 0 to ListViewAcronyms.Items.Count - 1 do
187 ListViewAcronyms.Items[I].Selected := True;
188end;
189
190procedure TFormAcronyms.ListViewFilter1Change(Sender: TObject);
191begin
192 UpdateAcronymsList;
193end;
194
195function TFormAcronyms.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
196begin
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;
207end;
208
209procedure TFormAcronyms.ListViewSort1Filter(ListViewSort: TListViewSort);
210begin
211 FormMain.AcronymDb.Acronyms.Sort(AcronymComparer);
212 FormMain.AcronymDb.AssignToList(ListViewSort1.List);
213 MeaningCount := ListViewSort1.List.Count;
214 FilterList(ListViewSort1.List);
215end;
216
217procedure TFormAcronyms.FilterList(List: TListObject);
218var
219 I: Integer;
220 FoundCount: Integer;
221 EnteredCount: Integer;
222begin
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;
244end;
245
246procedure TFormAcronyms.UpdateAcronymsList;
247begin
248 ListViewSort1.Refresh;
249 UpdateInterface;
250 StatusBar1.Panels[0].Text := 'Total: ' + IntToStr(MeaningCount);
251 StatusBar1.Panels[1].Text := 'Filtered: ' + IntToStr(ListViewAcronyms.Items.Count);
252end;
253
254procedure TFormAcronyms.UpdateInterface;
255begin
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;
260end;
261
262end.
263
Note: See TracBrowser for help on using the repository browser.