source: trunk/Forms/FormAcronyms.pas@ 230

Last change on this file since 230 was 230, checked in by chronos, 6 weeks ago
  • Modified: Fixed Import sources modification.
  • Modified: Catogires don't have direct object references to import sources and acronym meanings.
  • Modified: Updated acronyms example file.
File size: 10.9 KB
Line 
1unit FormAcronyms;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
7 Menus, ActnList, ListViewSort, Acronym, LazUTF8, Generics.Collections,
8 Generics.Defaults, FormEx;
9
10type
11
12 { TFormAcronyms }
13
14 TFormAcronyms = class(TFormEx)
15 AAdd: TAction;
16 ASelectAll: TAction;
17 AModify: TAction;
18 ARemove: TAction;
19 ActionList1: TActionList;
20 ListViewAcronyms: TListView;
21 ListViewFilter1: TListViewFilter;
22 ListViewSort1: TListViewSort;
23 MenuItem4: TMenuItem;
24 MenuItem5: TMenuItem;
25 MenuItem6: TMenuItem;
26 MenuItem7: TMenuItem;
27 PopupMenuAcronym: TPopupMenu;
28 StatusBar1: TStatusBar;
29 ToolBar1: TToolBar;
30 ToolButton1: TToolButton;
31 ToolButton2: TToolButton;
32 ToolButton3: TToolButton;
33 procedure AAddExecute(Sender: TObject);
34 procedure AModifyExecute(Sender: TObject);
35 procedure ARemoveExecute(Sender: TObject);
36 procedure ASelectAllExecute(Sender: TObject);
37 procedure FormActivate(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 procedure ListViewSort1ColumnWidthChanged(Sender: TObject);
48 function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
49 procedure ListViewSort1Filter(ListViewSort: TListViewSort);
50 private
51 FAcronyms: TAcronyms;
52 MeaningCount: Integer;
53 function CompareStrings(Strings1, Strings2: TStrings): Boolean;
54 function AcronymComparer(constref Item1, Item2: TAcronym): Integer;
55 procedure FilterList(List: TObjectList<TObject>);
56 procedure SetAcronyms(AValue: TAcronyms);
57 procedure UpdateAcronymsList;
58 public
59 AcronymDb: TAcronymDb;
60 FocusAcronym: TAcronymMeaning;
61 property Acronyms: TAcronyms read FAcronyms write SetAcronyms;
62 procedure UpdateInterface;
63 end;
64
65
66implementation
67
68{$R *.lfm}
69
70uses
71 FormAcronym;
72
73resourcestring
74 SRemoveAcronym = 'Remove acronyms';
75 SRemoveAcronymQuery = 'Do you want to remove selected acronyms?';
76 STotal = 'Total';
77 SFiltered = 'Filtered';
78
79
80{ TFormAcronyms }
81
82procedure TFormAcronyms.ListViewAcronymsData(Sender: TObject; Item: TListItem);
83begin
84 if Item.Index < ListViewSort1.List.Count then
85 with TAcronymMeaning(ListViewSort1.List[Item.Index]) do begin
86 Item.Caption := Acronym.Name;
87 Item.SubItems.Add(Name);
88 Item.Data := TAcronymMeaning(ListViewSort1.List[Item.Index]);
89 Item.SubItems.Add(Categories.GetString);
90 end;
91end;
92
93procedure TFormAcronyms.ListViewAcronymsDblClick(Sender: TObject);
94begin
95 AModify.Execute;
96end;
97
98procedure TFormAcronyms.ListViewAcronymsKeyPress(Sender: TObject; var Key: char
99 );
100begin
101 if Key = #27 then Close;
102end;
103
104procedure TFormAcronyms.ListViewAcronymsResize(Sender: TObject);
105begin
106 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
107end;
108
109procedure TFormAcronyms.ListViewAcronymsSelectItem(Sender: TObject;
110 Item: TListItem; Selected: Boolean);
111begin
112 UpdateInterface;
113end;
114
115procedure TFormAcronyms.FormShow(Sender: TObject);
116var
117 I: Integer;
118begin
119 UpdateAcronymsList;
120 ScaleDPI.ScaleControl(ToolBar1, ScaleDPI.DesignDPI);
121
122 // Focus line with acronym
123 if Assigned(FocusAcronym) then begin
124 I := 0;
125 while (I < ListViewAcronyms.Items.Count) and (ListViewAcronyms.Items[I].Data <> FocusAcronym) do Inc(I);
126 if I < ListViewAcronyms.Items.Count then begin
127 ListViewAcronyms.ItemIndex := I;
128 ListViewAcronyms.Selected.Focused := True;
129 end;
130 if ListViewAcronyms.Selected <> nil then
131 ListViewAcronyms.Selected.MakeVisible(False);
132 end;
133end;
134
135function TFormAcronyms.CompareStrings(Strings1, Strings2: TStrings): Boolean;
136var
137 I: Integer;
138begin
139 Result := Strings1.Count = Strings2.Count;
140 if not Result then Exit;
141 for I := 0 to Strings1.Count - 1 do
142 if (Strings1[I] <> Strings2[I]) or (Strings1.Objects[I] <> Strings2.Objects[I]) then begin
143 Result := False;
144 Exit;
145 end;
146end;
147
148procedure TFormAcronyms.AAddExecute(Sender: TObject);
149var
150 TempEntry: TAcronymEntry;
151 Meaning: TAcronymMeaning;
152 I: Integer;
153 FormAcronym: TFormAcronym;
154begin
155 TempEntry := TAcronymEntry.Create;
156 TempEntry.Name := '';
157 TempEntry.Meaning := '';
158 TempEntry.Description := '';
159 FormAcronym := TFormAcronym.Create(Self);
160 try
161 FormAcronym.AcronymDb := AcronymDb;
162 FormAcronym.Load(TempEntry);
163 if FormAcronym.ShowModal = mrOk then begin
164 FormAcronym.Save(TempEntry);
165 Meaning := Acronyms.Db.AddAcronym(TempEntry.Name, TempEntry.Meaning);
166 Meaning.Description := TempEntry.Description;
167 Meaning.Categories.AssignFromStrings(TempEntry.Categories);
168
169 Acronyms.Db.Update;
170 UpdateAcronymsList;
171 UpdateInterface;
172 end;
173 finally
174 FreeAndNil(FormAcronym);
175 end;
176 TempEntry.Free;
177end;
178
179procedure TFormAcronyms.AModifyExecute(Sender: TObject);
180var
181 TempEntry: TAcronymEntry;
182 TempCategories: TStringList;
183 Meaning: TAcronymMeaning;
184 I: Integer;
185 FormAcronym: TFormAcronym;
186begin
187 if Assigned(ListViewAcronyms.Selected) then
188 with TAcronymMeaning(ListViewAcronyms.Selected.Data) do begin
189 TempEntry := TAcronymEntry.Create;
190 TempEntry.Name := Acronym.Name;
191 TempEntry.Meaning := Name;
192 TempEntry.Description := Description;
193 Categories.AssignToStrings(TempEntry.Categories);
194 Sources.AssignToStrings(TempEntry.Sources);
195 TempCategories := TStringList.Create;
196 TempCategories.Assign(TempEntry.Categories);
197 FormAcronym := TFormAcronym.Create(Self);
198 try
199 FormAcronym.AcronymDb := AcronymDb;
200 FormAcronym.Load(TempEntry);
201 if FormAcronym.ShowModal = mrOk then begin
202 FormAcronym.Save(TempEntry);
203 if (TempEntry.Name <> Acronym.Name) or
204 (TempEntry.Meaning <> Name) or
205 (TempEntry.Description <> Description) or
206 not CompareStrings(TempEntry.Categories, TempCategories) then begin
207 // TODO: Update item inplace if possible
208 Acronyms.Db.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Selected.Data));
209 Meaning := Acronyms.Db.AddAcronym(TempEntry.Name, TempEntry.Meaning);
210 Meaning.Description := TempEntry.Description;
211 Meaning.Categories.AssignFromStrings(TempEntry.Categories);
212
213 Acronyms.Db.Update;
214 UpdateAcronymsList;
215 UpdateInterface;
216 end;
217 end;
218 finally
219 FreeAndNil(FormAcronym);
220 FreeAndNil(TempEntry);
221 FreeAndNil(TempCategories);
222 end;
223 end;
224end;
225
226procedure TFormAcronyms.ARemoveExecute(Sender: TObject);
227var
228 I: Integer;
229begin
230 if Assigned(ListViewAcronyms.Selected) then begin
231 if MessageDlg(SRemoveAcronym, SRemoveAcronymQuery,
232 TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
233 Acronyms.Db.BeginUpdate;
234 for I := ListViewAcronyms.Items.Count - 1 downto 0 do
235 if ListViewAcronyms.Items[I].Selected then begin
236 ListViewAcronyms.Items[I].Selected := False;
237 Acronyms.Db.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Items[I].Data));
238 end;
239 UpdateAcronymsList;
240 UpdateInterface;
241 Acronyms.Db.EndUpdate;
242 end;
243 end;
244end;
245
246procedure TFormAcronyms.ASelectAllExecute(Sender: TObject);
247var
248 I: Integer;
249begin
250 for I := 0 to ListViewAcronyms.Items.Count - 1 do
251 ListViewAcronyms.Items[I].Selected := True;
252end;
253
254procedure TFormAcronyms.FormActivate(Sender: TObject);
255begin
256 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
257end;
258
259procedure TFormAcronyms.FormCreate(Sender: TObject);
260var
261 I: Integer;
262begin
263 FocusAcronym := nil;
264 MeaningCount := 0;
265 for I := 0 to ToolBar1.ButtonCount - 1 do
266 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
267end;
268
269procedure TFormAcronyms.ListViewFilter1Change(Sender: TObject);
270begin
271 UpdateAcronymsList;
272end;
273
274procedure TFormAcronyms.ListViewSort1ColumnWidthChanged(Sender: TObject);
275begin
276 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
277end;
278
279function TFormAcronyms.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
280begin
281 Result := 0;
282 if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
283 with ListViewSort1 do
284 case Column of
285 0: Result := CompareString(TAcronymMeaning(Item1).Acronym.Name, TAcronymMeaning(Item2).Acronym.Name);
286 1: Result := CompareString(TAcronymMeaning(Item1).Name, TAcronymMeaning(Item2).Name);
287 2: Result := CompareString(TAcronymMeaning(Item1).Categories.GetString, TAcronymMeaning(Item2).Categories.GetString);
288 end;
289 if ListViewSort1.Order = soDown then Result := -Result;
290 end else Result := 0;
291end;
292
293function TFormAcronyms.AcronymComparer(constref Item1, Item2: TAcronym): Integer;
294begin
295 Result := CompareStr(TAcronym(Item1).Name, TAcronym(Item2).Name);
296end;
297
298procedure TFormAcronyms.ListViewSort1Filter(ListViewSort: TListViewSort);
299begin
300 Acronyms.Db.Acronyms.Sort(TComparer<TAcronym>.Construct(AcronymComparer));
301 Acronyms.Db.AssignToList(ListViewSort1.List);
302 MeaningCount := ListViewSort1.List.Count;
303 FilterList(ListViewSort1.List);
304end;
305
306procedure TFormAcronyms.FilterList(List: TObjectList<TObject>);
307var
308 I: Integer;
309 FoundCount: Integer;
310 EnteredCount: Integer;
311begin
312 EnteredCount := ListViewFilter1.TextEnteredCount;
313 for I := List.Count - 1 downto 0 do begin
314 if List.Items[I] is TAcronymMeaning then begin
315 with TAcronymMeaning(List.Items[I]) do begin
316 with ListViewFilter1 do
317 if Visible and (EnteredCount > 0) then begin
318 FoundCount := 0;
319 if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
320 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Acronym.Name)) > 0 then Inc(FoundCount);
321 if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
322 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Name)) > 0 then Inc(FoundCount);
323 if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
324 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
325 if FoundCount <> EnteredCount then List.Delete(I);
326 end;
327 end;
328 end else
329 if TAcronymMeaning(List.Items[I]) is TAcronymMeaning then begin
330 List.Delete(I);
331 end;
332 end;
333end;
334
335procedure TFormAcronyms.SetAcronyms(AValue: TAcronyms);
336begin
337 if FAcronyms = AValue then Exit;
338 FAcronyms := AValue;
339end;
340
341procedure TFormAcronyms.UpdateAcronymsList;
342begin
343 ListViewSort1.Refresh;
344 UpdateInterface;
345 StatusBar1.Panels[0].Text := STotal + ': ' + IntToStr(MeaningCount);
346 StatusBar1.Panels[1].Text := SFiltered + ': ' + IntToStr(ListViewAcronyms.Items.Count);
347end;
348
349procedure TFormAcronyms.UpdateInterface;
350begin
351 ARemove.Enabled := Assigned(FAcronyms) and Assigned(ListViewAcronyms.Selected);
352 AModify.Enabled := Assigned(FAcronyms) and Assigned(ListViewAcronyms.Selected);
353 AAdd.Enabled := Assigned(FAcronyms);
354 ASelectAll.Enabled := True;
355end;
356
357end.
358
Note: See TracBrowser for help on using the repository browser.