source: tags/1.3.0/Forms/UFormAcronyms.pas

Last change on this file was 89, checked in by chronos, 8 years ago
  • Added: Show from which imports acronym meanings comes from.

This would easy correcting wrong acronyms directly in source.

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