source: trunk/Forms/FormCategories.pas

Last change on this file was 227, checked in by chronos, 44 hours ago
  • Modified: Do not reference global Core object if possible.
File size: 6.9 KB
Line 
1unit FormCategories;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
7 ActnList, Menus, Acronym, ListViewSort, FormEx;
8
9type
10
11 { TFormCategories }
12
13 TFormCategories = class(TFormEx)
14 AAdd: TAction;
15 AEnable: TAction;
16 ADisable: TAction;
17 ASelectAll: TAction;
18 ARemove: TAction;
19 AModify: TAction;
20 ActionList1: TActionList;
21 ListViewCategories: TListView;
22 ListViewSort1: TListViewSort;
23 MenuItem1: TMenuItem;
24 MenuItem2: TMenuItem;
25 MenuItem3: TMenuItem;
26 MenuItem4: TMenuItem;
27 MenuItem5: TMenuItem;
28 MenuItem6: TMenuItem;
29 MenuItem7: TMenuItem;
30 PopupMenuCategory: TPopupMenu;
31 ToolBar1: TToolBar;
32 ToolButton1: TToolButton;
33 ToolButton2: TToolButton;
34 ToolButton3: TToolButton;
35 procedure AAddExecute(Sender: TObject);
36 procedure ADisableExecute(Sender: TObject);
37 procedure AEnableExecute(Sender: TObject);
38 procedure AModifyExecute(Sender: TObject);
39 procedure ARemoveExecute(Sender: TObject);
40 procedure ASelectAllExecute(Sender: TObject);
41 procedure FormCreate(Sender: TObject);
42 procedure FormShow(Sender: TObject);
43 procedure ListViewCategoriesChange(Sender: TObject; Item: TListItem;
44 Change: TItemChange);
45 procedure ListViewCategoriesData(Sender: TObject; Item: TListItem);
46 procedure ListViewCategoriesDblClick(Sender: TObject);
47 procedure ListViewCategoriesKeyPress(Sender: TObject; var Key: char);
48 procedure ListViewCategoriesSelectItem(Sender: TObject; Item: TListItem;
49 Selected: Boolean);
50 function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
51 procedure ListViewSort1Filter(ListViewSort: TListViewSort);
52 public
53 AcronymDb: TAcronymDb;
54 Categories: TAcronymCategories;
55 procedure UpdateList;
56 procedure UpdateInterface;
57 end;
58
59
60implementation
61
62{$R *.lfm}
63
64resourcestring
65 SCategory = 'Category';
66 SCategoryQuery = 'Enter name of category';
67 SRemoveCategory = 'Remove categories';
68 SRemoveCategoryQuery = 'Do you really want to remove selected categories?';
69 SCategoryAlreadyExists = 'Category %s already exists!';
70
71{ TFormCategories }
72
73procedure TFormCategories.FormShow(Sender: TObject);
74begin
75 UpdateList;
76 ScaleDPI.ScaleControl(ToolBar1, ScaleDPI.DesignDPI);
77end;
78
79procedure TFormCategories.ListViewCategoriesChange(Sender: TObject;
80 Item: TListItem; Change: TItemChange);
81begin
82 if Assigned(Item) and (Change = ctState) then begin
83 TAcronymCategory(Item.Data).Enabled := Item.Checked;
84 AcronymDb.Modified := True;
85 end;
86end;
87
88procedure TFormCategories.AAddExecute(Sender: TObject);
89var
90 S: string;
91begin
92 S := InputBox(SCategory, SCategoryQuery, '');
93 if S <> '' then begin
94 if not Assigned(AcronymDb.Categories.SearchByName(S)) then begin;
95 TAcronymCategory(AcronymDb.Categories[AcronymDb.Categories.Add(TAcronymCategory.Create)]).Name := S;
96 AcronymDb.Modified := True;
97 AcronymDb.Update;
98 UpdateList;
99 end else ShowMessage(Format(SCategoryAlreadyExists, [S]));
100 end;
101end;
102
103procedure TFormCategories.ADisableExecute(Sender: TObject);
104var
105 I: Integer;
106begin
107 for I := ListViewCategories.Items.Count - 1 downto 0 do
108 if ListViewCategories.Items[I].Selected then begin
109 TAcronymCategory(ListViewCategories.Items[I].Data).Enabled := False;
110 AcronymDb.Modified := True;
111 end;
112 UpdateList;
113end;
114
115procedure TFormCategories.AEnableExecute(Sender: TObject);
116var
117 I: Integer;
118begin
119 for I := ListViewCategories.Items.Count - 1 downto 0 do
120 if ListViewCategories.Items[I].Selected then begin
121 TAcronymCategory(ListViewCategories.Items[I].Data).Enabled := True;
122 AcronymDb.Modified := True;
123 end;
124 UpdateList;
125end;
126
127procedure TFormCategories.AModifyExecute(Sender: TObject);
128var
129 S: string;
130begin
131 if Assigned(ListViewCategories.Selected) then begin
132 S := InputBox(SCategory, SCategoryQuery, ListViewCategories.Selected.Caption);
133 if S <> ListViewCategories.Selected.Caption then begin
134 if not Assigned(AcronymDb.Categories.SearchByName(S)) then begin;
135 TAcronymCategory(ListViewCategories.Selected.Data).Name := S;
136 AcronymDb.Modified := True;
137 AcronymDb.Update;
138 UpdateList;
139 end else ShowMessage(Format(SCategoryAlreadyExists, [S]));
140 end;
141 end;
142end;
143
144procedure TFormCategories.ARemoveExecute(Sender: TObject);
145var
146 I: Integer;
147begin
148 if Assigned(ListViewCategories.Selected) then begin
149 if MessageDlg(SRemoveCategory, SRemoveCategoryQuery,
150 TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
151 Categories.Db.BeginUpdate;
152 for I := ListViewCategories.Items.Count - 1 downto 0 do
153 if ListViewCategories.Items[I].Selected then
154 Categories.Remove(ListViewCategories.Items[I].Data);
155 Categories.Db.EndUpdate;
156 UpdateList;
157 end;
158 end;
159end;
160
161procedure TFormCategories.ASelectAllExecute(Sender: TObject);
162var
163 I: Integer;
164begin
165 for I := 0 to ListViewCategories.Items.Count - 1 do
166 ListViewCategories.Items[I].Selected := True;
167end;
168
169procedure TFormCategories.FormCreate(Sender: TObject);
170var
171 I: Integer;
172begin
173 for I := 0 to ToolBar1.ButtonCount - 1 do
174 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
175end;
176
177procedure TFormCategories.ListViewCategoriesData(Sender: TObject; Item: TListItem);
178begin
179 if Item.Index < ListViewSort1.List.Count then
180 with TAcronymCategory(ListViewSort1.List[Item.Index]) do begin
181 Item.Caption := Name;
182 Item.Data := ListViewSort1.List[Item.Index];
183 Item.SubItems.Add(IntToStr(AcronymMeanings.Count));
184 Item.Checked := Enabled;
185 end;
186end;
187
188procedure TFormCategories.ListViewCategoriesDblClick(Sender: TObject);
189begin
190 AModify.Execute;
191end;
192
193procedure TFormCategories.ListViewCategoriesKeyPress(Sender: TObject;
194 var Key: char);
195begin
196 if Key = #27 then Close;
197end;
198
199procedure TFormCategories.ListViewCategoriesSelectItem(Sender: TObject; Item: TListItem;
200 Selected: Boolean);
201begin
202 UpdateInterface;
203end;
204
205function TFormCategories.ListViewSort1CompareItem(Item1, Item2: TObject
206 ): Integer;
207begin
208 Result := 0;
209 if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
210 with ListViewSort1 do
211 case Column of
212 0: Result := CompareString(TAcronymCategory(Item1).Name, TAcronymCategory(Item2).Name);
213 1: Result := CompareInteger(TAcronymCategory(Item1).AcronymMeanings.Count, TAcronymCategory(Item2).AcronymMeanings.Count);
214 end;
215 if ListViewSort1.Order = soDown then Result := -Result;
216 end else Result := 0;
217end;
218
219procedure TFormCategories.ListViewSort1Filter(ListViewSort: TListViewSort);
220begin
221 AcronymDb.Categories.AssignToList(ListViewSort1.List);
222end;
223
224procedure TFormCategories.UpdateList;
225begin
226 ListViewSort1.Refresh;
227 ListViewCategories.Items.Count := AcronymDb.Categories.Count;
228 ListViewCategories.Refresh;
229 UpdateInterface;
230end;
231
232procedure TFormCategories.UpdateInterface;
233begin
234 ARemove.Enabled := Assigned(ListViewCategories.Selected);
235 AModify.Enabled := Assigned(ListViewCategories.Selected);
236end;
237
238end.
239
Note: See TracBrowser for help on using the repository browser.