| 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 | ToolButton4: TToolButton;
|
|---|
| 36 | ToolButton5: TToolButton;
|
|---|
| 37 | procedure AAddExecute(Sender: TObject);
|
|---|
| 38 | procedure ADisableExecute(Sender: TObject);
|
|---|
| 39 | procedure AEnableExecute(Sender: TObject);
|
|---|
| 40 | procedure AModifyExecute(Sender: TObject);
|
|---|
| 41 | procedure ARemoveExecute(Sender: TObject);
|
|---|
| 42 | procedure ASelectAllExecute(Sender: TObject);
|
|---|
| 43 | procedure FormCreate(Sender: TObject);
|
|---|
| 44 | procedure FormShow(Sender: TObject);
|
|---|
| 45 | procedure ListViewCategoriesChange(Sender: TObject; Item: TListItem;
|
|---|
| 46 | Change: TItemChange);
|
|---|
| 47 | procedure ListViewCategoriesData(Sender: TObject; Item: TListItem);
|
|---|
| 48 | procedure ListViewCategoriesDblClick(Sender: TObject);
|
|---|
| 49 | procedure ListViewCategoriesKeyPress(Sender: TObject; var Key: char);
|
|---|
| 50 | procedure ListViewCategoriesSelectItem(Sender: TObject; Item: TListItem;
|
|---|
| 51 | Selected: Boolean);
|
|---|
| 52 | function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
|---|
| 53 | procedure ListViewSort1Filter(ListViewSort: TListViewSort);
|
|---|
| 54 | public
|
|---|
| 55 | AcronymDb: TAcronymDb;
|
|---|
| 56 | Categories: TAcronymCategories;
|
|---|
| 57 | procedure UpdateList;
|
|---|
| 58 | procedure UpdateInterface;
|
|---|
| 59 | end;
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | implementation
|
|---|
| 63 |
|
|---|
| 64 | {$R *.lfm}
|
|---|
| 65 |
|
|---|
| 66 | resourcestring
|
|---|
| 67 | SCategory = 'Category';
|
|---|
| 68 | SCategoryQuery = 'Enter name of category';
|
|---|
| 69 | SRemoveCategory = 'Remove categories';
|
|---|
| 70 | SRemoveCategoryQuery = 'Do you really want to remove selected categories?';
|
|---|
| 71 | SCategoryAlreadyExists = 'Category %s already exists!';
|
|---|
| 72 |
|
|---|
| 73 | { TFormCategories }
|
|---|
| 74 |
|
|---|
| 75 | procedure TFormCategories.FormShow(Sender: TObject);
|
|---|
| 76 | begin
|
|---|
| 77 | UpdateList;
|
|---|
| 78 | ScaleDPI.ScaleControl(ToolBar1, ScaleDPI.DesignDPI);
|
|---|
| 79 | end;
|
|---|
| 80 |
|
|---|
| 81 | procedure TFormCategories.ListViewCategoriesChange(Sender: TObject;
|
|---|
| 82 | Item: TListItem; Change: TItemChange);
|
|---|
| 83 | begin
|
|---|
| 84 | if Assigned(Item) and (Change = ctState) then begin
|
|---|
| 85 | TAcronymCategory(Item.Data).Enabled := Item.Checked;
|
|---|
| 86 | AcronymDb.Modified := True;
|
|---|
| 87 | end;
|
|---|
| 88 | end;
|
|---|
| 89 |
|
|---|
| 90 | procedure TFormCategories.AAddExecute(Sender: TObject);
|
|---|
| 91 | var
|
|---|
| 92 | S: string;
|
|---|
| 93 | NewCategory: TAcronymCategory;
|
|---|
| 94 | begin
|
|---|
| 95 | S := InputBox(SCategory, SCategoryQuery, '');
|
|---|
| 96 | if S <> '' then begin
|
|---|
| 97 | if not Assigned(AcronymDb.Categories.SearchByName(S)) then begin;
|
|---|
| 98 | NewCategory := TAcronymCategory.Create;
|
|---|
| 99 | NewCategory.AcronymDb := Categories.Db;
|
|---|
| 100 | NewCategory.Name := S;
|
|---|
| 101 | Categories.Add(NewCategory);
|
|---|
| 102 |
|
|---|
| 103 | AcronymDb.Modified := True;
|
|---|
| 104 | AcronymDb.Update;
|
|---|
| 105 | UpdateList;
|
|---|
| 106 | end else ShowMessage(Format(SCategoryAlreadyExists, [S]));
|
|---|
| 107 | end;
|
|---|
| 108 | end;
|
|---|
| 109 |
|
|---|
| 110 | procedure TFormCategories.ADisableExecute(Sender: TObject);
|
|---|
| 111 | var
|
|---|
| 112 | I: Integer;
|
|---|
| 113 | begin
|
|---|
| 114 | for I := ListViewCategories.Items.Count - 1 downto 0 do
|
|---|
| 115 | if ListViewCategories.Items[I].Selected then begin
|
|---|
| 116 | TAcronymCategory(ListViewCategories.Items[I].Data).Enabled := False;
|
|---|
| 117 | AcronymDb.Modified := True;
|
|---|
| 118 | end;
|
|---|
| 119 | UpdateList;
|
|---|
| 120 | end;
|
|---|
| 121 |
|
|---|
| 122 | procedure TFormCategories.AEnableExecute(Sender: TObject);
|
|---|
| 123 | var
|
|---|
| 124 | I: Integer;
|
|---|
| 125 | begin
|
|---|
| 126 | for I := ListViewCategories.Items.Count - 1 downto 0 do
|
|---|
| 127 | if ListViewCategories.Items[I].Selected then begin
|
|---|
| 128 | TAcronymCategory(ListViewCategories.Items[I].Data).Enabled := True;
|
|---|
| 129 | AcronymDb.Modified := True;
|
|---|
| 130 | end;
|
|---|
| 131 | UpdateList;
|
|---|
| 132 | end;
|
|---|
| 133 |
|
|---|
| 134 | procedure TFormCategories.AModifyExecute(Sender: TObject);
|
|---|
| 135 | var
|
|---|
| 136 | S: string;
|
|---|
| 137 | begin
|
|---|
| 138 | if Assigned(ListViewCategories.Selected) then begin
|
|---|
| 139 | S := InputBox(SCategory, SCategoryQuery, ListViewCategories.Selected.Caption);
|
|---|
| 140 | if S <> ListViewCategories.Selected.Caption then begin
|
|---|
| 141 | if not Assigned(AcronymDb.Categories.SearchByName(S)) then begin;
|
|---|
| 142 | TAcronymCategory(ListViewCategories.Selected.Data).Name := S;
|
|---|
| 143 | AcronymDb.Modified := True;
|
|---|
| 144 | AcronymDb.Update;
|
|---|
| 145 | UpdateList;
|
|---|
| 146 | end else ShowMessage(Format(SCategoryAlreadyExists, [S]));
|
|---|
| 147 | end;
|
|---|
| 148 | end;
|
|---|
| 149 | end;
|
|---|
| 150 |
|
|---|
| 151 | procedure TFormCategories.ARemoveExecute(Sender: TObject);
|
|---|
| 152 | var
|
|---|
| 153 | I: Integer;
|
|---|
| 154 | begin
|
|---|
| 155 | if Assigned(ListViewCategories.Selected) then begin
|
|---|
| 156 | if MessageDlg(SRemoveCategory, SRemoveCategoryQuery,
|
|---|
| 157 | TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
|
|---|
| 158 | Categories.Db.BeginUpdate;
|
|---|
| 159 | for I := ListViewCategories.Items.Count - 1 downto 0 do
|
|---|
| 160 | if ListViewCategories.Items[I].Selected then
|
|---|
| 161 | Categories.Remove(ListViewCategories.Items[I].Data);
|
|---|
| 162 | Categories.Db.EndUpdate;
|
|---|
| 163 | UpdateList;
|
|---|
| 164 | end;
|
|---|
| 165 | end;
|
|---|
| 166 | end;
|
|---|
| 167 |
|
|---|
| 168 | procedure TFormCategories.ASelectAllExecute(Sender: TObject);
|
|---|
| 169 | var
|
|---|
| 170 | I: Integer;
|
|---|
| 171 | begin
|
|---|
| 172 | for I := 0 to ListViewCategories.Items.Count - 1 do
|
|---|
| 173 | ListViewCategories.Items[I].Selected := True;
|
|---|
| 174 | end;
|
|---|
| 175 |
|
|---|
| 176 | procedure TFormCategories.FormCreate(Sender: TObject);
|
|---|
| 177 | var
|
|---|
| 178 | I: Integer;
|
|---|
| 179 | begin
|
|---|
| 180 | for I := 0 to ToolBar1.ButtonCount - 1 do
|
|---|
| 181 | ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
|
|---|
| 182 | {$IFDEF LCLQt5}
|
|---|
| 183 | ListViewCategories.Checkboxes := False;
|
|---|
| 184 | ListViewCategories.StateImages := ActionList1.Images;
|
|---|
| 185 | {$ENDIF}
|
|---|
| 186 | end;
|
|---|
| 187 |
|
|---|
| 188 | procedure TFormCategories.ListViewCategoriesData(Sender: TObject; Item: TListItem);
|
|---|
| 189 | begin
|
|---|
| 190 | if Item.Index < ListViewSort1.List.Count then
|
|---|
| 191 | with TAcronymCategory(ListViewSort1.List[Item.Index]) do begin
|
|---|
| 192 | Item.Caption := Name;
|
|---|
| 193 | Item.Data := ListViewSort1.List[Item.Index];
|
|---|
| 194 | Item.SubItems.Add(IntToStr(GetAcronymMeaningsCount));
|
|---|
| 195 | Item.SubItems.Add(IntToStr(GetImportSourcesCount));
|
|---|
| 196 | Item.Checked := Enabled;
|
|---|
| 197 | if Enabled then Item.StateIndex := 24 else
|
|---|
| 198 | Item.StateIndex := 23;
|
|---|
| 199 | end;
|
|---|
| 200 | end;
|
|---|
| 201 |
|
|---|
| 202 | procedure TFormCategories.ListViewCategoriesDblClick(Sender: TObject);
|
|---|
| 203 | begin
|
|---|
| 204 | AModify.Execute;
|
|---|
| 205 | end;
|
|---|
| 206 |
|
|---|
| 207 | procedure TFormCategories.ListViewCategoriesKeyPress(Sender: TObject;
|
|---|
| 208 | var Key: char);
|
|---|
| 209 | begin
|
|---|
| 210 | if Key = #27 then Close;
|
|---|
| 211 | end;
|
|---|
| 212 |
|
|---|
| 213 | procedure TFormCategories.ListViewCategoriesSelectItem(Sender: TObject; Item: TListItem;
|
|---|
| 214 | Selected: Boolean);
|
|---|
| 215 | begin
|
|---|
| 216 | UpdateInterface;
|
|---|
| 217 | end;
|
|---|
| 218 |
|
|---|
| 219 | function TFormCategories.ListViewSort1CompareItem(Item1, Item2: TObject
|
|---|
| 220 | ): Integer;
|
|---|
| 221 | begin
|
|---|
| 222 | Result := 0;
|
|---|
| 223 | if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
|
|---|
| 224 | with ListViewSort1 do
|
|---|
| 225 | case Column of
|
|---|
| 226 | 0: Result := CompareString(TAcronymCategory(Item1).Name, TAcronymCategory(Item2).Name);
|
|---|
| 227 | 1: Result := CompareInteger(TAcronymCategory(Item1).GetAcronymMeaningsCount,
|
|---|
| 228 | TAcronymCategory(Item2).GetAcronymMeaningsCount);
|
|---|
| 229 | 2: Result := CompareInteger(TAcronymCategory(Item1).GetImportSourcesCount,
|
|---|
| 230 | TAcronymCategory(Item2).GetImportSourcesCount);
|
|---|
| 231 | end;
|
|---|
| 232 | if ListViewSort1.Order = soDown then Result := -Result;
|
|---|
| 233 | end else Result := 0;
|
|---|
| 234 | end;
|
|---|
| 235 |
|
|---|
| 236 | procedure TFormCategories.ListViewSort1Filter(ListViewSort: TListViewSort);
|
|---|
| 237 | begin
|
|---|
| 238 | AcronymDb.Categories.AssignToList(ListViewSort1.List);
|
|---|
| 239 | end;
|
|---|
| 240 |
|
|---|
| 241 | procedure TFormCategories.UpdateList;
|
|---|
| 242 | begin
|
|---|
| 243 | ListViewSort1.Refresh;
|
|---|
| 244 | ListViewCategories.Items.Count := AcronymDb.Categories.Count;
|
|---|
| 245 | ListViewCategories.Refresh;
|
|---|
| 246 | UpdateInterface;
|
|---|
| 247 | end;
|
|---|
| 248 |
|
|---|
| 249 | procedure TFormCategories.UpdateInterface;
|
|---|
| 250 | begin
|
|---|
| 251 | ARemove.Enabled := Assigned(ListViewCategories.Selected);
|
|---|
| 252 | AModify.Enabled := Assigned(ListViewCategories.Selected);
|
|---|
| 253 | end;
|
|---|
| 254 |
|
|---|
| 255 | end.
|
|---|
| 256 |
|
|---|