source: tags/1.1.0/Forms/UFormAcronyms.pas

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