source: tags/1.4.0/Forms/UFormAcronyms.pas

Last change on this file was 148, checked in by chronos, 7 years ago
  • Fixed: Update filtered acronyms in main window if they were changed in acronyms and categories window.
File size: 10.9 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 FormClose(Sender: TObject; var CloseAction: TCloseAction);
39 procedure FormCreate(Sender: TObject);
40 procedure FormShow(Sender: TObject);
41 procedure ListViewAcronymsData(Sender: TObject; Item: TListItem);
42 procedure ListViewAcronymsDblClick(Sender: TObject);
43 procedure ListViewAcronymsKeyPress(Sender: TObject; var Key: char);
44 procedure ListViewAcronymsResize(Sender: TObject);
45 procedure ListViewAcronymsSelectItem(Sender: TObject; Item: TListItem;
46 Selected: Boolean);
47 procedure ListViewFilter1Change(Sender: TObject);
48 procedure ListViewSort1ColumnWidthChanged(Sender: TObject);
49 function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
50 procedure ListViewSort1Filter(ListViewSort: TListViewSort);
51 private
52 MeaningCount: Integer;
53 procedure FilterList(List: TListObject);
54 procedure UpdateAcronymsList;
55 public
56 FocusAcronym: TAcronymMeaning;
57 procedure UpdateInterface;
58 end;
59
60var
61 FormAcronyms: TFormAcronyms;
62
63implementation
64
65{$R *.lfm}
66
67uses
68 UCore, UFormMain, UFormAcronym;
69
70resourcestring
71 SRemoveAcronym = 'Remove acronyms';
72 SRemoveAcronymQuery = 'Do you want to remove selected acronyms?';
73 STotal = 'Total';
74 SFiltered = 'Filtered';
75
76
77{ TFormAcronyms }
78
79procedure TFormAcronyms.ListViewAcronymsData(Sender: TObject; Item: TListItem);
80begin
81 if Item.Index < ListViewSort1.List.Count then
82 with TAcronymMeaning(ListViewSort1.List[Item.Index]) do begin
83 Item.Caption := Acronym.Name;
84 Item.SubItems.Add(Name);
85 Item.Data := TAcronymMeaning(ListViewSort1.List[Item.Index]);
86 Item.SubItems.Add(Categories.GetString);
87 end;
88end;
89
90procedure TFormAcronyms.ListViewAcronymsDblClick(Sender: TObject);
91begin
92 AModify.Execute;
93end;
94
95procedure TFormAcronyms.ListViewAcronymsKeyPress(Sender: TObject; var Key: char
96 );
97begin
98 if Key = #27 then Close;
99end;
100
101procedure TFormAcronyms.ListViewAcronymsResize(Sender: TObject);
102begin
103 //ListViewFilter1.UpdateFromListView(ListViewAcronyms);
104end;
105
106procedure TFormAcronyms.ListViewAcronymsSelectItem(Sender: TObject;
107 Item: TListItem; Selected: Boolean);
108begin
109 UpdateInterface;
110end;
111
112procedure TFormAcronyms.FormShow(Sender: TObject);
113var
114 I: Integer;
115begin
116 Core.PersistentForm1.Load(Self);
117 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
118 UpdateAcronymsList;
119 Core.ScaleDPI1.ScaleControl(ToolBar1, Core.ScaleDPI1.DesignDPI);
120
121 // Focus line with acronym
122 if Assigned(FocusAcronym) then begin
123 I := 0;
124 while (I < ListViewAcronyms.Items.Count) and (ListViewAcronyms.Items[I].Data <> FocusAcronym) do Inc(I);
125 if I < ListViewAcronyms.Items.Count then begin
126 ListViewAcronyms.ItemIndex := I;
127 ListViewAcronyms.Selected.Focused := True;
128 end;
129 if ListViewAcronyms.Selected <> nil then
130 ListViewAcronyms.Selected.MakeVisible(False);
131 end;
132end;
133
134procedure TFormAcronyms.AAddExecute(Sender: TObject);
135var
136 TempEntry: TAcronymEntry;
137 Meaning: TAcronymMeaning;
138 I: Integer;
139begin
140 TempEntry := TAcronymEntry.Create;
141 TempEntry.Name := '';
142 TempEntry.Meaning := '';
143 TempEntry.Description := '';
144 FormAcronym := TFormAcronym.Create(Self);
145 try
146 FormAcronym.Load(TempEntry);
147 if FormAcronym.ShowModal = mrOk then begin
148 FormAcronym.Save(TempEntry);
149 Meaning := Core.AcronymDb.AddAcronym(TempEntry.Name, TempEntry.Meaning);
150 Meaning.Description := TempEntry.Description;
151 Meaning.Categories.AssignFromStrings(TempEntry.Categories);
152
153 // Update reverse references
154 for I := 0 to Meaning.Categories.Count - 1 do
155 if TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.IndexOf(Meaning) = -1 then
156 TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.Add(Meaning);
157
158 Core.AcronymDb.Update;
159 UpdateAcronymsList;
160 UpdateInterface;
161 end;
162 finally
163 FreeAndNil(FormAcronym);
164 end;
165 TempEntry.Free;
166end;
167
168procedure TFormAcronyms.AModifyExecute(Sender: TObject);
169var
170 TempEntry: TAcronymEntry;
171 TempCategories: TStringList;
172 Meaning: TAcronymMeaning;
173 I: Integer;
174begin
175 if Assigned(ListViewAcronyms.Selected) then
176 with TAcronymMeaning(ListViewAcronyms.Selected.Data) do begin
177 TempEntry := TAcronymEntry.Create;
178 TempEntry.Name := Acronym.Name;
179 TempEntry.Meaning := Name;
180 TempEntry.Description := Description;
181 Categories.AssignToStrings(TempEntry.Categories);
182 Sources.AssignToStrings(TempEntry.Sources);
183 TempCategories := TStringList.Create;
184 TempCategories.Assign(TempEntry.Categories);
185 FormAcronym := TFormAcronym.Create(Self);
186 try
187 FormAcronym.Load(TempEntry);
188 if FormAcronym.ShowModal = mrOk then begin
189 FormAcronym.Save(TempEntry);
190 if (TempEntry.Name <> Acronym.Name) or
191 (TempEntry.Meaning <> Name) or
192 (TempEntry.Description <> Description) or
193 not FormMain.CompareStrings(TempEntry.Categories, TempCategories) then begin
194 // TODO: Update item inplace if possible
195 Core.AcronymDb.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Selected.Data));
196 Meaning := Core.AcronymDb.AddAcronym(TempEntry.Name, TempEntry.Meaning);
197 Meaning.Description := TempEntry.Description;
198 Meaning.Categories.AssignFromStrings(TempEntry.Categories);
199
200 // Update reverse references
201 for I := 0 to Meaning.Categories.Count - 1 do
202 if TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.IndexOf(Meaning) = -1 then
203 TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.Add(Meaning);
204 Core.AcronymDb.Update;
205 UpdateAcronymsList;
206 UpdateInterface;
207 end;
208 end;
209 finally
210 FreeAndNil(FormAcronym);
211 FreeAndNil(TempEntry);
212 FreeAndNil(TempCategories);
213 end;
214 end;
215end;
216
217procedure TFormAcronyms.ARemoveExecute(Sender: TObject);
218var
219 I: Integer;
220begin
221 if Assigned(ListViewAcronyms.Selected) then begin
222 if MessageDlg(SRemoveAcronym, SRemoveAcronymQuery,
223 TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
224 Core.AcronymDb.BeginUpdate;
225 for I := ListViewAcronyms.Items.Count - 1 downto 0 do
226 if ListViewAcronyms.Items[I].Selected then begin
227 ListViewAcronyms.Items[I].Selected := False;
228 Core.AcronymDb.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Items[I].Data));
229 end;
230 UpdateAcronymsList;
231 UpdateInterface;
232 Core.AcronymDb.EndUpdate;
233 end;
234 end;
235end;
236
237procedure TFormAcronyms.ASelectAllExecute(Sender: TObject);
238var
239 I: Integer;
240begin
241 for I := 0 to ListViewAcronyms.Items.Count - 1 do
242 ListViewAcronyms.Items[I].Selected := True;
243end;
244
245procedure TFormAcronyms.FormClose(Sender: TObject; var CloseAction: TCloseAction
246 );
247begin
248 Core.PersistentForm1.Save(Self);
249end;
250
251procedure TFormAcronyms.FormCreate(Sender: TObject);
252var
253 I: Integer;
254begin
255 FocusAcronym := nil;
256 MeaningCount := 0;
257 Core.CoolTranslator1.TranslateComponentRecursive(Self);
258 for I := 0 to ToolBar1.ButtonCount - 1 do
259 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
260end;
261
262procedure TFormAcronyms.ListViewFilter1Change(Sender: TObject);
263begin
264 UpdateAcronymsList;
265end;
266
267procedure TFormAcronyms.ListViewSort1ColumnWidthChanged(Sender: TObject);
268begin
269 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
270end;
271
272function TFormAcronyms.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
273begin
274 Result := 0;
275 if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
276 with ListViewSort1 do
277 case Column of
278 0: Result := CompareString(TAcronymMeaning(Item1).Acronym.Name, TAcronymMeaning(Item2).Acronym.Name);
279 1: Result := CompareString(TAcronymMeaning(Item1).Name, TAcronymMeaning(Item2).Name);
280 2: Result := CompareString(TAcronymMeaning(Item1).Categories.GetString, TAcronymMeaning(Item2).Categories.GetString);
281 end;
282 if ListViewSort1.Order = soDown then Result := -Result;
283 end else Result := 0;
284end;
285
286procedure TFormAcronyms.ListViewSort1Filter(ListViewSort: TListViewSort);
287begin
288 Core.AcronymDb.Acronyms.Sort(AcronymComparer);
289 Core.AcronymDb.AssignToList(ListViewSort1.List);
290 MeaningCount := ListViewSort1.List.Count;
291 FilterList(ListViewSort1.List);
292end;
293
294procedure TFormAcronyms.FilterList(List: TListObject);
295var
296 I: Integer;
297 FoundCount: Integer;
298 EnteredCount: Integer;
299begin
300 EnteredCount := ListViewFilter1.TextEnteredCount;
301 for I := List.Count - 1 downto 0 do begin
302 if List.Items[I] is TAcronymMeaning then begin
303 with TAcronymMeaning(List.Items[I]) do begin
304 with ListViewFilter1 do
305 if Visible and (EnteredCount > 0) then begin
306 FoundCount := 0;
307 if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
308 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Acronym.Name)) > 0 then Inc(FoundCount);
309 if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
310 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Name)) > 0 then Inc(FoundCount);
311 if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
312 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
313 if FoundCount <> EnteredCount then List.Delete(I);
314 end;
315 end;
316 end else
317 if TAcronymMeaning(List.Items[I]) is TAcronymMeaning then begin
318 List.Delete(I);
319 end;
320 end;
321end;
322
323procedure TFormAcronyms.UpdateAcronymsList;
324begin
325 ListViewSort1.Refresh;
326 UpdateInterface;
327 StatusBar1.Panels[0].Text := STotal + ': ' + IntToStr(MeaningCount);
328 StatusBar1.Panels[1].Text := SFiltered + ': ' + IntToStr(ListViewAcronyms.Items.Count);
329end;
330
331procedure TFormAcronyms.UpdateInterface;
332begin
333 ARemove.Enabled := Assigned(Core.AcronymDb) and Assigned(ListViewAcronyms.Selected);
334 AModify.Enabled := Assigned(Core.AcronymDb) and Assigned(ListViewAcronyms.Selected);
335 AAdd.Enabled := Assigned(Core.AcronymDb);
336 ASelectAll.Enabled := True;
337end;
338
339end.
340
Note: See TracBrowser for help on using the repository browser.