1 | unit FormCategories;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
---|
7 | ActnList, Menus, Acronym, ListViewSort, FormEx;
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
60 | implementation
|
---|
61 |
|
---|
62 | {$R *.lfm}
|
---|
63 |
|
---|
64 | resourcestring
|
---|
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 |
|
---|
73 | procedure TFormCategories.FormShow(Sender: TObject);
|
---|
74 | begin
|
---|
75 | UpdateList;
|
---|
76 | ScaleDPI.ScaleControl(ToolBar1, ScaleDPI.DesignDPI);
|
---|
77 | end;
|
---|
78 |
|
---|
79 | procedure TFormCategories.ListViewCategoriesChange(Sender: TObject;
|
---|
80 | Item: TListItem; Change: TItemChange);
|
---|
81 | begin
|
---|
82 | if Assigned(Item) and (Change = ctState) then begin
|
---|
83 | TAcronymCategory(Item.Data).Enabled := Item.Checked;
|
---|
84 | AcronymDb.Modified := True;
|
---|
85 | end;
|
---|
86 | end;
|
---|
87 |
|
---|
88 | procedure TFormCategories.AAddExecute(Sender: TObject);
|
---|
89 | var
|
---|
90 | S: string;
|
---|
91 | begin
|
---|
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;
|
---|
101 | end;
|
---|
102 |
|
---|
103 | procedure TFormCategories.ADisableExecute(Sender: TObject);
|
---|
104 | var
|
---|
105 | I: Integer;
|
---|
106 | begin
|
---|
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;
|
---|
113 | end;
|
---|
114 |
|
---|
115 | procedure TFormCategories.AEnableExecute(Sender: TObject);
|
---|
116 | var
|
---|
117 | I: Integer;
|
---|
118 | begin
|
---|
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;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TFormCategories.AModifyExecute(Sender: TObject);
|
---|
128 | var
|
---|
129 | S: string;
|
---|
130 | begin
|
---|
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;
|
---|
142 | end;
|
---|
143 |
|
---|
144 | procedure TFormCategories.ARemoveExecute(Sender: TObject);
|
---|
145 | var
|
---|
146 | I: Integer;
|
---|
147 | begin
|
---|
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;
|
---|
159 | end;
|
---|
160 |
|
---|
161 | procedure TFormCategories.ASelectAllExecute(Sender: TObject);
|
---|
162 | var
|
---|
163 | I: Integer;
|
---|
164 | begin
|
---|
165 | for I := 0 to ListViewCategories.Items.Count - 1 do
|
---|
166 | ListViewCategories.Items[I].Selected := True;
|
---|
167 | end;
|
---|
168 |
|
---|
169 | procedure TFormCategories.FormCreate(Sender: TObject);
|
---|
170 | var
|
---|
171 | I: Integer;
|
---|
172 | begin
|
---|
173 | for I := 0 to ToolBar1.ButtonCount - 1 do
|
---|
174 | ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
|
---|
175 | end;
|
---|
176 |
|
---|
177 | procedure TFormCategories.ListViewCategoriesData(Sender: TObject; Item: TListItem);
|
---|
178 | begin
|
---|
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;
|
---|
186 | end;
|
---|
187 |
|
---|
188 | procedure TFormCategories.ListViewCategoriesDblClick(Sender: TObject);
|
---|
189 | begin
|
---|
190 | AModify.Execute;
|
---|
191 | end;
|
---|
192 |
|
---|
193 | procedure TFormCategories.ListViewCategoriesKeyPress(Sender: TObject;
|
---|
194 | var Key: char);
|
---|
195 | begin
|
---|
196 | if Key = #27 then Close;
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TFormCategories.ListViewCategoriesSelectItem(Sender: TObject; Item: TListItem;
|
---|
200 | Selected: Boolean);
|
---|
201 | begin
|
---|
202 | UpdateInterface;
|
---|
203 | end;
|
---|
204 |
|
---|
205 | function TFormCategories.ListViewSort1CompareItem(Item1, Item2: TObject
|
---|
206 | ): Integer;
|
---|
207 | begin
|
---|
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;
|
---|
217 | end;
|
---|
218 |
|
---|
219 | procedure TFormCategories.ListViewSort1Filter(ListViewSort: TListViewSort);
|
---|
220 | begin
|
---|
221 | AcronymDb.Categories.AssignToList(ListViewSort1.List);
|
---|
222 | end;
|
---|
223 |
|
---|
224 | procedure TFormCategories.UpdateList;
|
---|
225 | begin
|
---|
226 | ListViewSort1.Refresh;
|
---|
227 | ListViewCategories.Items.Count := AcronymDb.Categories.Count;
|
---|
228 | ListViewCategories.Refresh;
|
---|
229 | UpdateInterface;
|
---|
230 | end;
|
---|
231 |
|
---|
232 | procedure TFormCategories.UpdateInterface;
|
---|
233 | begin
|
---|
234 | ARemove.Enabled := Assigned(ListViewCategories.Selected);
|
---|
235 | AModify.Enabled := Assigned(ListViewCategories.Selected);
|
---|
236 | end;
|
---|
237 |
|
---|
238 | end.
|
---|
239 |
|
---|