source: tags/1.3.1/Forms/UFormAcronyms.pas

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