source: trunk/Forms/UFormAcronyms.pas

Last change on this file was 207, checked in by chronos, 3 years ago
  • Modified: Updated Common package.
  • Modified: CoolTranslator package merged into Common package.
  • Fixed: Build with Lazarus 2.0.12
File size: 11.2 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, fgl;
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 FAcronyms: TAcronyms;
53 MeaningCount: Integer;
54 procedure FilterList(List: TFPGObjectList<TObject>);
55 procedure SetAcronyms(AValue: TAcronyms);
56 procedure UpdateAcronymsList;
57 public
58 FocusAcronym: TAcronymMeaning;
59 property Acronyms: TAcronyms read FAcronyms write SetAcronyms;
60 procedure UpdateInterface;
61 end;
62
63var
64 FormAcronyms: TFormAcronyms;
65
66implementation
67
68{$R *.lfm}
69
70uses
71 UCore, UFormMain, UFormAcronym;
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 Core.PersistentForm1.Load(Self);
120 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
121 UpdateAcronymsList;
122 Core.ScaleDPI1.ScaleControl(ToolBar1, Core.ScaleDPI1.DesignDPI);
123
124 // Focus line with acronym
125 if Assigned(FocusAcronym) then begin
126 I := 0;
127 while (I < ListViewAcronyms.Items.Count) and (ListViewAcronyms.Items[I].Data <> FocusAcronym) do Inc(I);
128 if I < ListViewAcronyms.Items.Count then begin
129 ListViewAcronyms.ItemIndex := I;
130 ListViewAcronyms.Selected.Focused := True;
131 end;
132 if ListViewAcronyms.Selected <> nil then
133 ListViewAcronyms.Selected.MakeVisible(False);
134 end;
135end;
136
137procedure TFormAcronyms.AAddExecute(Sender: TObject);
138var
139 TempEntry: TAcronymEntry;
140 Meaning: TAcronymMeaning;
141 I: Integer;
142begin
143 TempEntry := TAcronymEntry.Create;
144 TempEntry.Name := '';
145 TempEntry.Meaning := '';
146 TempEntry.Description := '';
147 FormAcronym := TFormAcronym.Create(Self);
148 try
149 FormAcronym.Load(TempEntry);
150 if FormAcronym.ShowModal = mrOk then begin
151 FormAcronym.Save(TempEntry);
152 Meaning := Acronyms.Db.AddAcronym(TempEntry.Name, TempEntry.Meaning);
153 Meaning.Description := TempEntry.Description;
154 Meaning.Categories.AssignFromStrings(TempEntry.Categories);
155
156 // Update reverse references
157 for I := 0 to Meaning.Categories.Count - 1 do
158 if TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.IndexOf(Meaning) = -1 then
159 TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.Add(Meaning);
160
161 Acronyms.Db.Update;
162 UpdateAcronymsList;
163 UpdateInterface;
164 end;
165 finally
166 FreeAndNil(FormAcronym);
167 end;
168 TempEntry.Free;
169end;
170
171procedure TFormAcronyms.AModifyExecute(Sender: TObject);
172var
173 TempEntry: TAcronymEntry;
174 TempCategories: TStringList;
175 Meaning: TAcronymMeaning;
176 I: Integer;
177begin
178 if Assigned(ListViewAcronyms.Selected) then
179 with TAcronymMeaning(ListViewAcronyms.Selected.Data) do begin
180 TempEntry := TAcronymEntry.Create;
181 TempEntry.Name := Acronym.Name;
182 TempEntry.Meaning := Name;
183 TempEntry.Description := Description;
184 Categories.AssignToStrings(TempEntry.Categories);
185 Sources.AssignToStrings(TempEntry.Sources);
186 TempCategories := TStringList.Create;
187 TempCategories.Assign(TempEntry.Categories);
188 FormAcronym := TFormAcronym.Create(Self);
189 try
190 FormAcronym.Load(TempEntry);
191 if FormAcronym.ShowModal = mrOk then begin
192 FormAcronym.Save(TempEntry);
193 if (TempEntry.Name <> Acronym.Name) or
194 (TempEntry.Meaning <> Name) or
195 (TempEntry.Description <> Description) or
196 not FormMain.CompareStrings(TempEntry.Categories, TempCategories) then begin
197 // TODO: Update item inplace if possible
198 Acronyms.Db.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Selected.Data));
199 Meaning := Acronyms.Db.AddAcronym(TempEntry.Name, TempEntry.Meaning);
200 Meaning.Description := TempEntry.Description;
201 Meaning.Categories.AssignFromStrings(TempEntry.Categories);
202
203 // Update reverse references
204 for I := 0 to Meaning.Categories.Count - 1 do
205 if TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.IndexOf(Meaning) = -1 then
206 TAcronymCategory(Meaning.Categories.Items[I]).AcronymMeanings.Add(Meaning);
207 Acronyms.Db.Update;
208 UpdateAcronymsList;
209 UpdateInterface;
210 end;
211 end;
212 finally
213 FreeAndNil(FormAcronym);
214 FreeAndNil(TempEntry);
215 FreeAndNil(TempCategories);
216 end;
217 end;
218end;
219
220procedure TFormAcronyms.ARemoveExecute(Sender: TObject);
221var
222 I: Integer;
223begin
224 if Assigned(ListViewAcronyms.Selected) then begin
225 if MessageDlg(SRemoveAcronym, SRemoveAcronymQuery,
226 TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
227 Acronyms.Db.BeginUpdate;
228 for I := ListViewAcronyms.Items.Count - 1 downto 0 do
229 if ListViewAcronyms.Items[I].Selected then begin
230 ListViewAcronyms.Items[I].Selected := False;
231 Acronyms.Db.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Items[I].Data));
232 end;
233 UpdateAcronymsList;
234 UpdateInterface;
235 Acronyms.Db.EndUpdate;
236 end;
237 end;
238end;
239
240procedure TFormAcronyms.ASelectAllExecute(Sender: TObject);
241var
242 I: Integer;
243begin
244 for I := 0 to ListViewAcronyms.Items.Count - 1 do
245 ListViewAcronyms.Items[I].Selected := True;
246end;
247
248procedure TFormAcronyms.FormClose(Sender: TObject; var CloseAction: TCloseAction
249 );
250begin
251 Core.PersistentForm1.Save(Self);
252end;
253
254procedure TFormAcronyms.FormCreate(Sender: TObject);
255var
256 I: Integer;
257begin
258 FocusAcronym := nil;
259 MeaningCount := 0;
260 Core.Translator.TranslateComponentRecursive(Self);
261 Core.ThemeManager.UseTheme(Self);
262 for I := 0 to ToolBar1.ButtonCount - 1 do
263 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
264end;
265
266procedure TFormAcronyms.ListViewFilter1Change(Sender: TObject);
267begin
268 UpdateAcronymsList;
269end;
270
271procedure TFormAcronyms.ListViewSort1ColumnWidthChanged(Sender: TObject);
272begin
273 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
274end;
275
276function TFormAcronyms.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
277begin
278 Result := 0;
279 if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
280 with ListViewSort1 do
281 case Column of
282 0: Result := CompareString(TAcronymMeaning(Item1).Acronym.Name, TAcronymMeaning(Item2).Acronym.Name);
283 1: Result := CompareString(TAcronymMeaning(Item1).Name, TAcronymMeaning(Item2).Name);
284 2: Result := CompareString(TAcronymMeaning(Item1).Categories.GetString, TAcronymMeaning(Item2).Categories.GetString);
285 end;
286 if ListViewSort1.Order = soDown then Result := -Result;
287 end else Result := 0;
288end;
289
290procedure TFormAcronyms.ListViewSort1Filter(ListViewSort: TListViewSort);
291begin
292 Acronyms.Db.Acronyms.Sort(AcronymComparer);
293 Acronyms.Db.AssignToList(ListViewSort1.List);
294 MeaningCount := ListViewSort1.List.Count;
295 FilterList(ListViewSort1.List);
296end;
297
298procedure TFormAcronyms.FilterList(List: TFPGObjectList<TObject>);
299var
300 I: Integer;
301 FoundCount: Integer;
302 EnteredCount: Integer;
303begin
304 EnteredCount := ListViewFilter1.TextEnteredCount;
305 for I := List.Count - 1 downto 0 do begin
306 if List.Items[I] is TAcronymMeaning then begin
307 with TAcronymMeaning(List.Items[I]) do begin
308 with ListViewFilter1 do
309 if Visible and (EnteredCount > 0) then begin
310 FoundCount := 0;
311 if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
312 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Acronym.Name)) > 0 then Inc(FoundCount);
313 if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
314 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Name)) > 0 then Inc(FoundCount);
315 if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
316 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
317 if FoundCount <> EnteredCount then List.Delete(I);
318 end;
319 end;
320 end else
321 if TAcronymMeaning(List.Items[I]) is TAcronymMeaning then begin
322 List.Delete(I);
323 end;
324 end;
325end;
326
327procedure TFormAcronyms.SetAcronyms(AValue: TAcronyms);
328begin
329 if FAcronyms = AValue then Exit;
330 FAcronyms := AValue;
331end;
332
333procedure TFormAcronyms.UpdateAcronymsList;
334begin
335 ListViewSort1.Refresh;
336 UpdateInterface;
337 StatusBar1.Panels[0].Text := STotal + ': ' + IntToStr(MeaningCount);
338 StatusBar1.Panels[1].Text := SFiltered + ': ' + IntToStr(ListViewAcronyms.Items.Count);
339end;
340
341procedure TFormAcronyms.UpdateInterface;
342begin
343 ARemove.Enabled := Assigned(FAcronyms) and Assigned(ListViewAcronyms.Selected);
344 AModify.Enabled := Assigned(FAcronyms) and Assigned(ListViewAcronyms.Selected);
345 AAdd.Enabled := Assigned(FAcronyms);
346 ASelectAll.Enabled := True;
347end;
348
349end.
350
Note: See TracBrowser for help on using the repository browser.