| 1 | unit FormAcronyms;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
|---|
| 7 | Menus, ActnList, ListViewSort, Acronym, LazUTF8, Generics.Collections,
|
|---|
| 8 | Generics.Defaults, FormEx;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 |
|
|---|
| 12 | { TFormAcronyms }
|
|---|
| 13 |
|
|---|
| 14 | TFormAcronyms = class(TFormEx)
|
|---|
| 15 | AAdd: TAction;
|
|---|
| 16 | ASelectAll: TAction;
|
|---|
| 17 | AModify: TAction;
|
|---|
| 18 | ARemove: TAction;
|
|---|
| 19 | ActionList1: TActionList;
|
|---|
| 20 | ListViewAcronyms: TListView;
|
|---|
| 21 | ListViewFilter1: TListViewFilter;
|
|---|
| 22 | ListViewSort1: TListViewSort;
|
|---|
| 23 | MenuItem4: TMenuItem;
|
|---|
| 24 | MenuItem5: TMenuItem;
|
|---|
| 25 | MenuItem6: TMenuItem;
|
|---|
| 26 | MenuItem7: TMenuItem;
|
|---|
| 27 | PopupMenuAcronym: TPopupMenu;
|
|---|
| 28 | StatusBar1: TStatusBar;
|
|---|
| 29 | ToolBar1: TToolBar;
|
|---|
| 30 | ToolButton1: TToolButton;
|
|---|
| 31 | ToolButton2: TToolButton;
|
|---|
| 32 | ToolButton3: TToolButton;
|
|---|
| 33 | procedure AAddExecute(Sender: TObject);
|
|---|
| 34 | procedure AModifyExecute(Sender: TObject);
|
|---|
| 35 | procedure ARemoveExecute(Sender: TObject);
|
|---|
| 36 | procedure ASelectAllExecute(Sender: TObject);
|
|---|
| 37 | procedure FormActivate(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 | FAcronyms: TAcronyms;
|
|---|
| 52 | MeaningCount: Integer;
|
|---|
| 53 | function CompareStrings(Strings1, Strings2: TStrings): Boolean;
|
|---|
| 54 | function AcronymComparer(constref Item1, Item2: TAcronym): Integer;
|
|---|
| 55 | procedure FilterList(List: TObjectList<TObject>);
|
|---|
| 56 | procedure SetAcronyms(AValue: TAcronyms);
|
|---|
| 57 | procedure UpdateAcronymsList;
|
|---|
| 58 | public
|
|---|
| 59 | AcronymDb: TAcronymDb;
|
|---|
| 60 | FocusAcronym: TAcronymMeaning;
|
|---|
| 61 | property Acronyms: TAcronyms read FAcronyms write SetAcronyms;
|
|---|
| 62 | procedure UpdateInterface;
|
|---|
| 63 | end;
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | implementation
|
|---|
| 67 |
|
|---|
| 68 | {$R *.lfm}
|
|---|
| 69 |
|
|---|
| 70 | uses
|
|---|
| 71 | FormAcronym;
|
|---|
| 72 |
|
|---|
| 73 | resourcestring
|
|---|
| 74 | SRemoveAcronym = 'Remove acronyms';
|
|---|
| 75 | SRemoveAcronymQuery = 'Do you want to remove selected acronyms?';
|
|---|
| 76 | STotal = 'Total';
|
|---|
| 77 | SFiltered = 'Filtered';
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | { TFormAcronyms }
|
|---|
| 81 |
|
|---|
| 82 | procedure TFormAcronyms.ListViewAcronymsData(Sender: TObject; Item: TListItem);
|
|---|
| 83 | begin
|
|---|
| 84 | if Item.Index < ListViewSort1.List.Count then
|
|---|
| 85 | with TAcronymMeaning(ListViewSort1.List[Item.Index]) do begin
|
|---|
| 86 | Item.Caption := Acronym.Name;
|
|---|
| 87 | Item.SubItems.Add(Name);
|
|---|
| 88 | Item.Data := TAcronymMeaning(ListViewSort1.List[Item.Index]);
|
|---|
| 89 | Item.SubItems.Add(Categories.GetString);
|
|---|
| 90 | end;
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | procedure TFormAcronyms.ListViewAcronymsDblClick(Sender: TObject);
|
|---|
| 94 | begin
|
|---|
| 95 | AModify.Execute;
|
|---|
| 96 | end;
|
|---|
| 97 |
|
|---|
| 98 | procedure TFormAcronyms.ListViewAcronymsKeyPress(Sender: TObject; var Key: char
|
|---|
| 99 | );
|
|---|
| 100 | begin
|
|---|
| 101 | if Key = #27 then Close;
|
|---|
| 102 | end;
|
|---|
| 103 |
|
|---|
| 104 | procedure TFormAcronyms.ListViewAcronymsResize(Sender: TObject);
|
|---|
| 105 | begin
|
|---|
| 106 | ListViewFilter1.UpdateFromListView(ListViewAcronyms);
|
|---|
| 107 | end;
|
|---|
| 108 |
|
|---|
| 109 | procedure TFormAcronyms.ListViewAcronymsSelectItem(Sender: TObject;
|
|---|
| 110 | Item: TListItem; Selected: Boolean);
|
|---|
| 111 | begin
|
|---|
| 112 | UpdateInterface;
|
|---|
| 113 | end;
|
|---|
| 114 |
|
|---|
| 115 | procedure TFormAcronyms.FormShow(Sender: TObject);
|
|---|
| 116 | var
|
|---|
| 117 | I: Integer;
|
|---|
| 118 | begin
|
|---|
| 119 | UpdateAcronymsList;
|
|---|
| 120 | ScaleDPI.ScaleControl(ToolBar1, ScaleDPI.DesignDPI);
|
|---|
| 121 |
|
|---|
| 122 | // Focus line with acronym
|
|---|
| 123 | if Assigned(FocusAcronym) then begin
|
|---|
| 124 | I := 0;
|
|---|
| 125 | while (I < ListViewAcronyms.Items.Count) and (ListViewAcronyms.Items[I].Data <> FocusAcronym) do Inc(I);
|
|---|
| 126 | if I < ListViewAcronyms.Items.Count then begin
|
|---|
| 127 | ListViewAcronyms.ItemIndex := I;
|
|---|
| 128 | ListViewAcronyms.Selected.Focused := True;
|
|---|
| 129 | end;
|
|---|
| 130 | if ListViewAcronyms.Selected <> nil then
|
|---|
| 131 | ListViewAcronyms.Selected.MakeVisible(False);
|
|---|
| 132 | end;
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | function TFormAcronyms.CompareStrings(Strings1, Strings2: TStrings): Boolean;
|
|---|
| 136 | var
|
|---|
| 137 | I: Integer;
|
|---|
| 138 | begin
|
|---|
| 139 | Result := Strings1.Count = Strings2.Count;
|
|---|
| 140 | if not Result then Exit;
|
|---|
| 141 | for I := 0 to Strings1.Count - 1 do
|
|---|
| 142 | if (Strings1[I] <> Strings2[I]) or (Strings1.Objects[I] <> Strings2.Objects[I]) then begin
|
|---|
| 143 | Result := False;
|
|---|
| 144 | Exit;
|
|---|
| 145 | end;
|
|---|
| 146 | end;
|
|---|
| 147 |
|
|---|
| 148 | procedure TFormAcronyms.AAddExecute(Sender: TObject);
|
|---|
| 149 | var
|
|---|
| 150 | TempEntry: TAcronymEntry;
|
|---|
| 151 | Meaning: TAcronymMeaning;
|
|---|
| 152 | FormAcronym: TFormAcronym;
|
|---|
| 153 | begin
|
|---|
| 154 | TempEntry := TAcronymEntry.Create;
|
|---|
| 155 | TempEntry.Name := '';
|
|---|
| 156 | TempEntry.Meaning := '';
|
|---|
| 157 | TempEntry.Description := '';
|
|---|
| 158 | FormAcronym := TFormAcronym.Create(Self);
|
|---|
| 159 | try
|
|---|
| 160 | FormAcronym.AcronymDb := AcronymDb;
|
|---|
| 161 | FormAcronym.Load(TempEntry);
|
|---|
| 162 | if FormAcronym.ShowModal = mrOk then begin
|
|---|
| 163 | FormAcronym.Save(TempEntry);
|
|---|
| 164 | Meaning := Acronyms.Db.AddAcronym(TempEntry.Name, TempEntry.Meaning);
|
|---|
| 165 | Meaning.Description := TempEntry.Description;
|
|---|
| 166 | Meaning.Categories.AssignFromStrings(TempEntry.Categories);
|
|---|
| 167 |
|
|---|
| 168 | Acronyms.Db.Update;
|
|---|
| 169 | UpdateAcronymsList;
|
|---|
| 170 | UpdateInterface;
|
|---|
| 171 | end;
|
|---|
| 172 | finally
|
|---|
| 173 | FreeAndNil(FormAcronym);
|
|---|
| 174 | end;
|
|---|
| 175 | TempEntry.Free;
|
|---|
| 176 | end;
|
|---|
| 177 |
|
|---|
| 178 | procedure TFormAcronyms.AModifyExecute(Sender: TObject);
|
|---|
| 179 | var
|
|---|
| 180 | TempEntry: TAcronymEntry;
|
|---|
| 181 | TempCategories: TStringList;
|
|---|
| 182 | Meaning: TAcronymMeaning;
|
|---|
| 183 | FormAcronym: TFormAcronym;
|
|---|
| 184 | begin
|
|---|
| 185 | if Assigned(ListViewAcronyms.Selected) then
|
|---|
| 186 | with TAcronymMeaning(ListViewAcronyms.Selected.Data) do begin
|
|---|
| 187 | TempEntry := TAcronymEntry.Create;
|
|---|
| 188 | TempEntry.Name := Acronym.Name;
|
|---|
| 189 | TempEntry.Meaning := Name;
|
|---|
| 190 | TempEntry.Description := Description;
|
|---|
| 191 | Categories.AssignToStrings(TempEntry.Categories);
|
|---|
| 192 | Sources.AssignToStrings(TempEntry.Sources);
|
|---|
| 193 | TempCategories := TStringList.Create;
|
|---|
| 194 | TempCategories.Assign(TempEntry.Categories);
|
|---|
| 195 | FormAcronym := TFormAcronym.Create(Self);
|
|---|
| 196 | try
|
|---|
| 197 | FormAcronym.AcronymDb := AcronymDb;
|
|---|
| 198 | FormAcronym.Load(TempEntry);
|
|---|
| 199 | if FormAcronym.ShowModal = mrOk then begin
|
|---|
| 200 | FormAcronym.Save(TempEntry);
|
|---|
| 201 | if (TempEntry.Name <> Acronym.Name) or
|
|---|
| 202 | (TempEntry.Meaning <> Name) or
|
|---|
| 203 | (TempEntry.Description <> Description) or
|
|---|
| 204 | not CompareStrings(TempEntry.Categories, TempCategories) then begin
|
|---|
| 205 | // TODO: Update item inplace if possible
|
|---|
| 206 | Acronyms.Db.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Selected.Data));
|
|---|
| 207 | Meaning := Acronyms.Db.AddAcronym(TempEntry.Name, TempEntry.Meaning);
|
|---|
| 208 | Meaning.Description := TempEntry.Description;
|
|---|
| 209 | Meaning.Categories.AssignFromStrings(TempEntry.Categories);
|
|---|
| 210 |
|
|---|
| 211 | Acronyms.Db.Update;
|
|---|
| 212 | UpdateAcronymsList;
|
|---|
| 213 | UpdateInterface;
|
|---|
| 214 | end;
|
|---|
| 215 | end;
|
|---|
| 216 | finally
|
|---|
| 217 | FreeAndNil(FormAcronym);
|
|---|
| 218 | FreeAndNil(TempEntry);
|
|---|
| 219 | FreeAndNil(TempCategories);
|
|---|
| 220 | end;
|
|---|
| 221 | end;
|
|---|
| 222 | end;
|
|---|
| 223 |
|
|---|
| 224 | procedure TFormAcronyms.ARemoveExecute(Sender: TObject);
|
|---|
| 225 | var
|
|---|
| 226 | I: Integer;
|
|---|
| 227 | begin
|
|---|
| 228 | if Assigned(ListViewAcronyms.Selected) then begin
|
|---|
| 229 | if MessageDlg(SRemoveAcronym, SRemoveAcronymQuery,
|
|---|
| 230 | TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
|
|---|
| 231 | Acronyms.Db.BeginUpdate;
|
|---|
| 232 | for I := ListViewAcronyms.Items.Count - 1 downto 0 do
|
|---|
| 233 | if ListViewAcronyms.Items[I].Selected then begin
|
|---|
| 234 | ListViewAcronyms.Items[I].Selected := False;
|
|---|
| 235 | Acronyms.Db.RemoveMeaning(TAcronymMeaning(ListViewAcronyms.Items[I].Data));
|
|---|
| 236 | end;
|
|---|
| 237 | UpdateAcronymsList;
|
|---|
| 238 | UpdateInterface;
|
|---|
| 239 | Acronyms.Db.EndUpdate;
|
|---|
| 240 | end;
|
|---|
| 241 | end;
|
|---|
| 242 | end;
|
|---|
| 243 |
|
|---|
| 244 | procedure TFormAcronyms.ASelectAllExecute(Sender: TObject);
|
|---|
| 245 | var
|
|---|
| 246 | I: Integer;
|
|---|
| 247 | begin
|
|---|
| 248 | for I := 0 to ListViewAcronyms.Items.Count - 1 do
|
|---|
| 249 | ListViewAcronyms.Items[I].Selected := True;
|
|---|
| 250 | end;
|
|---|
| 251 |
|
|---|
| 252 | procedure TFormAcronyms.FormActivate(Sender: TObject);
|
|---|
| 253 | begin
|
|---|
| 254 | ListViewFilter1.UpdateFromListView(ListViewAcronyms);
|
|---|
| 255 | end;
|
|---|
| 256 |
|
|---|
| 257 | procedure TFormAcronyms.FormCreate(Sender: TObject);
|
|---|
| 258 | var
|
|---|
| 259 | I: Integer;
|
|---|
| 260 | begin
|
|---|
| 261 | FocusAcronym := nil;
|
|---|
| 262 | MeaningCount := 0;
|
|---|
| 263 | for I := 0 to ToolBar1.ButtonCount - 1 do
|
|---|
| 264 | ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
|
|---|
| 265 | end;
|
|---|
| 266 |
|
|---|
| 267 | procedure TFormAcronyms.ListViewFilter1Change(Sender: TObject);
|
|---|
| 268 | begin
|
|---|
| 269 | UpdateAcronymsList;
|
|---|
| 270 | end;
|
|---|
| 271 |
|
|---|
| 272 | procedure TFormAcronyms.ListViewSort1ColumnWidthChanged(Sender: TObject);
|
|---|
| 273 | begin
|
|---|
| 274 | ListViewFilter1.UpdateFromListView(ListViewAcronyms);
|
|---|
| 275 | end;
|
|---|
| 276 |
|
|---|
| 277 | function TFormAcronyms.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
|---|
| 278 | begin
|
|---|
| 279 | Result := 0;
|
|---|
| 280 | if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
|
|---|
| 281 | with ListViewSort1 do
|
|---|
| 282 | case Column of
|
|---|
| 283 | 0: Result := CompareString(TAcronymMeaning(Item1).Acronym.Name, TAcronymMeaning(Item2).Acronym.Name);
|
|---|
| 284 | 1: Result := CompareString(TAcronymMeaning(Item1).Name, TAcronymMeaning(Item2).Name);
|
|---|
| 285 | 2: Result := CompareString(TAcronymMeaning(Item1).Categories.GetString, TAcronymMeaning(Item2).Categories.GetString);
|
|---|
| 286 | end;
|
|---|
| 287 | if ListViewSort1.Order = soDown then Result := -Result;
|
|---|
| 288 | end else Result := 0;
|
|---|
| 289 | end;
|
|---|
| 290 |
|
|---|
| 291 | function TFormAcronyms.AcronymComparer(constref Item1, Item2: TAcronym): Integer;
|
|---|
| 292 | begin
|
|---|
| 293 | Result := CompareStr(TAcronym(Item1).Name, TAcronym(Item2).Name);
|
|---|
| 294 | end;
|
|---|
| 295 |
|
|---|
| 296 | procedure TFormAcronyms.ListViewSort1Filter(ListViewSort: TListViewSort);
|
|---|
| 297 | begin
|
|---|
| 298 | Acronyms.Db.Acronyms.Sort(TComparer<TAcronym>.Construct(AcronymComparer));
|
|---|
| 299 | Acronyms.Db.AssignToList(ListViewSort1.List);
|
|---|
| 300 | MeaningCount := ListViewSort1.List.Count;
|
|---|
| 301 | FilterList(ListViewSort1.List);
|
|---|
| 302 | end;
|
|---|
| 303 |
|
|---|
| 304 | procedure TFormAcronyms.FilterList(List: TObjectList<TObject>);
|
|---|
| 305 | var
|
|---|
| 306 | I: Integer;
|
|---|
| 307 | FoundCount: Integer;
|
|---|
| 308 | EnteredCount: Integer;
|
|---|
| 309 | begin
|
|---|
| 310 | EnteredCount := ListViewFilter1.TextEnteredCount;
|
|---|
| 311 | for I := List.Count - 1 downto 0 do begin
|
|---|
| 312 | if List.Items[I] is TAcronymMeaning then begin
|
|---|
| 313 | with TAcronymMeaning(List.Items[I]) do begin
|
|---|
| 314 | with ListViewFilter1 do
|
|---|
| 315 | if Visible and (EnteredCount > 0) then begin
|
|---|
| 316 | FoundCount := 0;
|
|---|
| 317 | if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
|
|---|
| 318 | UTF8LowerCase(TAcronymMeaning(List.Items[I]).Acronym.Name)) > 0 then Inc(FoundCount);
|
|---|
| 319 | if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
|
|---|
| 320 | UTF8LowerCase(TAcronymMeaning(List.Items[I]).Name)) > 0 then Inc(FoundCount);
|
|---|
| 321 | if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
|
|---|
| 322 | UTF8LowerCase(TAcronymMeaning(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
|
|---|
| 323 | if FoundCount <> EnteredCount then List.Delete(I);
|
|---|
| 324 | end;
|
|---|
| 325 | end;
|
|---|
| 326 | end else
|
|---|
| 327 | if TAcronymMeaning(List.Items[I]) is TAcronymMeaning then begin
|
|---|
| 328 | List.Delete(I);
|
|---|
| 329 | end;
|
|---|
| 330 | end;
|
|---|
| 331 | end;
|
|---|
| 332 |
|
|---|
| 333 | procedure TFormAcronyms.SetAcronyms(AValue: TAcronyms);
|
|---|
| 334 | begin
|
|---|
| 335 | if FAcronyms = AValue then Exit;
|
|---|
| 336 | FAcronyms := AValue;
|
|---|
| 337 | end;
|
|---|
| 338 |
|
|---|
| 339 | procedure TFormAcronyms.UpdateAcronymsList;
|
|---|
| 340 | begin
|
|---|
| 341 | ListViewSort1.Refresh;
|
|---|
| 342 | UpdateInterface;
|
|---|
| 343 | StatusBar1.Panels[0].Text := STotal + ': ' + IntToStr(MeaningCount);
|
|---|
| 344 | StatusBar1.Panels[1].Text := SFiltered + ': ' + IntToStr(ListViewAcronyms.Items.Count);
|
|---|
| 345 | end;
|
|---|
| 346 |
|
|---|
| 347 | procedure TFormAcronyms.UpdateInterface;
|
|---|
| 348 | begin
|
|---|
| 349 | ARemove.Enabled := Assigned(FAcronyms) and Assigned(ListViewAcronyms.Selected);
|
|---|
| 350 | AModify.Enabled := Assigned(FAcronyms) and Assigned(ListViewAcronyms.Selected);
|
|---|
| 351 | AAdd.Enabled := Assigned(FAcronyms);
|
|---|
| 352 | ASelectAll.Enabled := True;
|
|---|
| 353 | end;
|
|---|
| 354 |
|
|---|
| 355 | end.
|
|---|
| 356 |
|
|---|