source: tags/1.0.0/Forms/UFormMain.pas

Last change on this file was 33, checked in by chronos, 8 years ago
  • Added: Now import sources have categories which are merged to new imported acronym meanings.
File size: 16.7 KB
Line 
1unit UFormMain;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
9 ComCtrls, StdCtrls, ExtCtrls, ActnList, UAcronym, UPersistentForm, URegistry,
10 ULastOpenedList, UListViewSort, UCoolTranslator, Registry,
11 SpecializedList, LazUTF8;
12
13type
14
15 { TFormMain }
16
17 TFormMain = class(TForm)
18 AProcessImports: TAction;
19 AShowImportFormats: TAction;
20 AShowAbout: TAction;
21 AShowImportSources: TAction;
22 AShowAcronyms: TAction;
23 AShowCategories: TAction;
24 ASettings: TAction;
25 AFileSaveAs: TAction;
26 AFileClose: TAction;
27 AFileSave: TAction;
28 AFileNew: TAction;
29 AFileOpen: TAction;
30 AManualImport: TAction;
31 AShow: TAction;
32 AExit: TAction;
33 ActionList1: TActionList;
34 CheckBoxExactMath: TCheckBox;
35 CoolTranslator1: TCoolTranslator;
36 ImageList1: TImageList;
37 LastOpenedList1: TLastOpenedList;
38 ListViewAcronyms: TListView;
39 ListViewFilter1: TListViewFilter;
40 ListViewSort1: TListViewSort;
41 MainMenu1: TMainMenu;
42 MenuItem1: TMenuItem;
43 MenuItem10: TMenuItem;
44 MenuItem11: TMenuItem;
45 MenuItem12: TMenuItem;
46 MenuItem13: TMenuItem;
47 MenuItem14: TMenuItem;
48 MenuItem15: TMenuItem;
49 MenuItem16: TMenuItem;
50 MenuItem17: TMenuItem;
51 MenuItem18: TMenuItem;
52 MenuItem19: TMenuItem;
53 MenuItem20: TMenuItem;
54 MenuItem21: TMenuItem;
55 MenuItem22: TMenuItem;
56 MenuItem23: TMenuItem;
57 MenuItem24: TMenuItem;
58 MenuItemToolbar: TMenuItem;
59 MenuItem4: TMenuItem;
60 MenuItem5: TMenuItem;
61 MenuItem6: TMenuItem;
62 MenuItem7: TMenuItem;
63 MenuItemOpenRecent: TMenuItem;
64 MenuItem2: TMenuItem;
65 MenuItem3: TMenuItem;
66 MenuItem8: TMenuItem;
67 MenuItem9: TMenuItem;
68 OpenDialog1: TOpenDialog;
69 Panel1: TPanel;
70 PersistentForm1: TPersistentForm;
71 PopupMenuTryIcon: TPopupMenu;
72 SaveDialog1: TSaveDialog;
73 ToolBar1: TToolBar;
74 ToolButton1: TToolButton;
75 ToolButton2: TToolButton;
76 ToolButton3: TToolButton;
77 ToolButton4: TToolButton;
78 ToolButton5: TToolButton;
79 ToolButton6: TToolButton;
80 ToolButton7: TToolButton;
81 ToolButton8: TToolButton;
82 TrayIcon1: TTrayIcon;
83 procedure AExitExecute(Sender: TObject);
84 procedure AFileCloseExecute(Sender: TObject);
85 procedure AFileNewExecute(Sender: TObject);
86 procedure AFileOpenExecute(Sender: TObject);
87 procedure AFileSaveAsExecute(Sender: TObject);
88 procedure AFileSaveExecute(Sender: TObject);
89 procedure AManualImportExecute(Sender: TObject);
90 procedure AProcessImportsExecute(Sender: TObject);
91 procedure ASettingsExecute(Sender: TObject);
92 procedure AShowAboutExecute(Sender: TObject);
93 procedure AShowAcronymsExecute(Sender: TObject);
94 procedure AShowCategoriesExecute(Sender: TObject);
95 procedure AShowExecute(Sender: TObject);
96 procedure AShowImportFormatsExecute(Sender: TObject);
97 procedure AShowImportSourcesExecute(Sender: TObject);
98 procedure CheckBoxExactMathChange(Sender: TObject);
99 procedure EditSearchChange(Sender: TObject);
100 procedure FormShow(Sender: TObject);
101 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
102 procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
103 procedure FormCreate(Sender: TObject);
104 procedure FormDestroy(Sender: TObject);
105 procedure LastOpenedList1Change(Sender: TObject);
106 procedure ListViewAcronymsData(Sender: TObject; Item: TListItem);
107 procedure ListViewAcronymsSelectItem(Sender: TObject; Item: TListItem;
108 Selected: Boolean);
109 procedure ListViewFilter1Change(Sender: TObject);
110 function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
111 procedure ListViewSort1Filter(ListViewSort: TListViewSort);
112 procedure MenuItemToolbarClick(Sender: TObject);
113 procedure TrayIcon1Click(Sender: TObject);
114 private
115 FAlwaysOnTop: Boolean;
116 RegistryContext: TRegistryContext;
117 ProjectClosed: Boolean;
118 procedure SetAlwaysOnTop(AValue: Boolean);
119 procedure FilterList(List: TListObject);
120 procedure OpenRecentClick(Sender: TObject);
121 procedure UpdateAcronymsList;
122 procedure UpdateInterface;
123 procedure LoadConfig;
124 procedure SaveConfig;
125 public
126 AcronymDb: TAcronymDb;
127 function CompareStrings(Strings1, Strings2: TStrings): Boolean;
128 property AlwaysOnTop: Boolean read FAlwaysOnTop write SetAlwaysOnTop;
129 end;
130
131var
132 FormMain: TFormMain;
133
134resourcestring
135 SAddedCount = 'Imported %d acronyms';
136
137
138implementation
139
140{$R *.lfm}
141
142uses
143 UFormImport, UFormSettings, UFormCategories, UFormAcronyms,
144 UFormImportSources, UFormAbout, UFormImportFormats;
145
146resourcestring
147 SModified = 'modified';
148 SAppExit = 'Application exit';
149 SAppExitQuery = 'Acronyms were modified. Do you want to save them to file before exit?';
150
151const
152 ProjectExt = '.adp';
153 DefaultFileName = 'Acronyms' + ProjectExt;
154 DefaultRegKey = '\Software\Acronym Decoder';
155
156{ TFormMain }
157
158procedure TFormMain.FormCreate(Sender: TObject);
159begin
160 AcronymDb := TAcronymDb.Create;
161end;
162
163procedure TFormMain.EditSearchChange(Sender: TObject);
164begin
165 UpdateAcronymsList;
166end;
167
168procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
169begin
170 PersistentForm1.Save(Self);
171 SaveConfig;
172end;
173
174procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
175begin
176 AFileClose.Execute;
177 CanClose := ProjectClosed;
178end;
179
180procedure TFormMain.AExitExecute(Sender: TObject);
181begin
182 Close;
183end;
184
185procedure TFormMain.AFileCloseExecute(Sender: TObject);
186var
187 ModalResult: TModalResult;
188 DoClose: Boolean;
189begin
190 DoClose := False;
191 if Assigned(AcronymDb) then begin
192 if AcronymDb.Modified then begin
193 ModalResult := MessageDlg(SAppExit, SAppExitQuery,
194 mtConfirmation, [mbYes, mbNo, mbCancel], 0);
195 if ModalResult = mrYes then begin
196 AFileSave.Execute;
197 DoClose := True;
198 end
199 else if ModalResult = mrNo then begin
200 DoClose := True;
201 end else ProjectClosed := False;
202 end else DoClose := True;
203 end;
204 if DoClose then begin
205 FreeAndNil(AcronymDb);
206 UpdateAcronymsList;
207 UpdateInterface;
208 ProjectClosed := True;
209 end;
210end;
211
212procedure TFormMain.AFileNewExecute(Sender: TObject);
213begin
214 AFileClose.Execute;
215 if ProjectClosed then begin
216 AcronymDb := TAcronymDb.Create;
217 AcronymDb.FileName := DefaultFileName;
218 AcronymDb.Acronyms.Clear;
219 UpdateAcronymsList;
220 UpdateInterface;
221 end;
222end;
223
224procedure TFormMain.AFileOpenExecute(Sender: TObject);
225begin
226 OpenDialog1.DefaultExt := ProjectExt;
227 if Assigned(AcronymDb) then
228 OpenDialog1.FileName := AcronymDb.FileName;
229 if OpenDialog1.Execute then begin
230 AFileClose.Execute;
231 if ProjectClosed then begin
232 AFileNew.Execute;
233 AcronymDb.LoadFromFile(OpenDialog1.FileName);
234 LastOpenedList1.AddItem(OpenDialog1.FileName);
235 UpdateAcronymsList;
236 UpdateInterface;
237 end;
238 end;
239end;
240
241procedure TFormMain.AFileSaveAsExecute(Sender: TObject);
242begin
243 SaveDialog1.DefaultExt := ProjectExt;
244 SaveDialog1.FileName := AcronymDb.FileName;
245 if SaveDialog1.Execute then begin
246 AcronymDb.SaveToFile(SaveDialog1.FileName);
247 LastOpenedList1.AddItem(SaveDialog1.FileName);
248 UpdateInterface;
249 end;
250end;
251
252procedure TFormMain.AFileSaveExecute(Sender: TObject);
253begin
254 if FileExists(AcronymDb.FileName) then begin
255 AcronymDb.SaveToFile(AcronymDb.FileName);
256 LastOpenedList1.AddItem(AcronymDb.FileName);
257 UpdateInterface;
258 end else AFileSaveAs.Execute;
259end;
260
261procedure TFormMain.AManualImportExecute(Sender: TObject);
262begin
263 FormImport.ShowModal;
264 UpdateAcronymsList;
265 UpdateInterface;
266end;
267
268procedure TFormMain.AProcessImportsExecute(Sender: TObject);
269var
270 I: Integer;
271 TotalItemCount: Integer;
272begin
273 TotalItemCount := 0;
274 for I := 0 to AcronymDb.ImportSources.Count - 1 do
275 with TImportSource(AcronymDb.ImportSources[I]) do
276 if Enabled then begin
277 Process;
278 TotalItemCount := TotalItemCount + ItemCount;
279 end;
280 ShowMessage(Format(SAddedCount, [TotalItemCount]));
281 UpdateAcronymsList;
282 UpdateInterface;
283end;
284
285procedure TFormMain.ASettingsExecute(Sender: TObject);
286begin
287 FormSettings.Load;
288 if FormSettings.ShowModal = mrOk then
289 FormSettings.Save;
290end;
291
292procedure TFormMain.AShowAboutExecute(Sender: TObject);
293begin
294 FormAbout.ShowModal;
295end;
296
297procedure TFormMain.AShowAcronymsExecute(Sender: TObject);
298begin
299 FormAcronyms.ShowModal;
300 UpdateInterface;
301end;
302
303procedure TFormMain.AShowCategoriesExecute(Sender: TObject);
304begin
305 FormCategories.Categories := AcronymDb.Categories;
306 FormCategories.ShowModal;
307 UpdateAcronymsList;
308 UpdateInterface;
309end;
310
311procedure TFormMain.AShowExecute(Sender: TObject);
312begin
313 Show;
314end;
315
316procedure TFormMain.AShowImportFormatsExecute(Sender: TObject);
317begin
318 FormImportFormats.ImportFormats := AcronymDb.ImportFormats;
319 FormImportFormats.ShowModal;
320 UpdateInterface;
321end;
322
323procedure TFormMain.AShowImportSourcesExecute(Sender: TObject);
324begin
325 FormImportSources.ImportSources := AcronymDb.ImportSources;
326 FormImportSources.ShowModal;
327 UpdateInterface;
328end;
329
330procedure TFormMain.CheckBoxExactMathChange(Sender: TObject);
331begin
332 UpdateAcronymsList;
333end;
334
335procedure TFormMain.FormDestroy(Sender: TObject);
336begin
337 FreeAndNil(AcronymDb);
338end;
339
340procedure TFormMain.FormShow(Sender: TObject);
341var
342 I: Integer;
343begin
344 LoadConfig;
345 PersistentForm1.Load(Self);
346
347 if ParamCount > 0 then begin
348 AcronymDB.LoadFromFile(ParamStr(1));
349 LastOpenedList1.AddItem(OpenDialog1.FileName);
350 end else
351 if (LastOpenedList1.Items.Count > 0) and FileExists(LastOpenedList1.Items[0]) then
352 AcronymDB.LoadFromFile(LastOpenedList1.Items[0]);
353 UpdateAcronymsList;
354 ListViewFilter1.UpdateFromListView(ListViewAcronyms);
355 UpdateInterface;
356 for I := 0 to ToolBar1.ButtonCount - 1 do
357 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
358end;
359
360procedure TFormMain.LastOpenedList1Change(Sender: TObject);
361begin
362 LastOpenedList1.LoadToMenuItem(MenuItemOpenRecent, OpenRecentClick);
363end;
364
365procedure TFormMain.ListViewAcronymsData(Sender: TObject; Item: TListItem);
366begin
367 if Item.Index < ListViewSort1.List.Count then
368 with TAcronymMeaning(ListViewSort1.List[Item.Index]) do begin
369 Item.Caption := Acronym.Name;
370 Item.SubItems.Add(Name);
371 Item.Data := TAcronymMeaning(ListViewSort1.List[Item.Index]);
372 Item.SubItems.Add(Categories.GetString);
373 end;
374end;
375
376procedure TFormMain.ListViewAcronymsSelectItem(Sender: TObject;
377 Item: TListItem; Selected: Boolean);
378begin
379 UpdateInterface;
380end;
381
382procedure TFormMain.ListViewFilter1Change(Sender: TObject);
383begin
384 UpdateAcronymsList;
385end;
386
387function TFormMain.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
388begin
389 Result := 0;
390 if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
391 with ListViewSort1 do
392 case Column of
393 0: Result := CompareString(TAcronymMeaning(Item1).Acronym.Name, TAcronymMeaning(Item2).Acronym.Name);
394 1: Result := CompareString(TAcronymMeaning(Item1).Description, TAcronymMeaning(Item2).Description);
395 2: Result := CompareString(TAcronymMeaning(Item1).Categories.GetString, TAcronymMeaning(Item2).Categories.GetString);
396 end;
397 if ListViewSort1.Order = soDown then Result := -Result;
398 end else Result := 0;
399end;
400
401procedure TFormMain.ListViewSort1Filter(ListViewSort: TListViewSort);
402begin
403 AcronymDb.Acronyms.Sort(AcronymComparer);
404 AcronymDb.AssignToList(ListViewSort1.List);
405 FilterList(ListViewSort1.List);
406end;
407
408procedure TFormMain.MenuItemToolbarClick(Sender: TObject);
409begin
410 MenuItemToolbar.Checked := not MenuItemToolbar.Checked;
411 UpdateInterface;
412end;
413
414procedure TFormMain.TrayIcon1Click(Sender: TObject);
415begin
416 Visible := not Visible;
417end;
418
419function TFormMain.CompareStrings(Strings1, Strings2: TStrings): Boolean;
420var
421 I: Integer;
422begin
423 Result := Strings1.Count = Strings2.Count;
424 if not Result then Exit;
425 for I := 0 to Strings1.Count - 1 do
426 if (Strings1[I] <> Strings2[I]) or (Strings1.Objects[I] <> Strings2.Objects[I]) then begin
427 Result := False;
428 Exit;
429 end;
430end;
431
432procedure TFormMain.SetAlwaysOnTop(AValue: Boolean);
433begin
434 if FAlwaysOnTop = AValue then Exit;
435 FAlwaysOnTop := AValue;
436 if FAlwaysOnTop then FormStyle := fsSystemStayOnTop
437 else FormStyle := fsNormal;
438end;
439
440procedure TFormMain.FilterList(List: TListObject);
441var
442 I: Integer;
443 FoundCount: Integer;
444 EnteredCount: Integer;
445begin
446 EnteredCount := ListViewFilter1.TextEnteredCount;
447 for I := List.Count - 1 downto 0 do begin
448 if List.Items[I] is TAcronymMeaning then begin
449 with TAcronymMeaning(List.Items[I]) do begin
450 with ListViewFilter1 do
451 if Visible and (EnteredCount > 0) then begin
452 FoundCount := 0;
453 if CheckBoxExactMath.Checked then begin
454 if TextEnteredColumn(0) and (StringGrid.Cells[0, 0] =
455 TAcronymMeaning(List.Items[I]).Acronym.Name) then Inc(FoundCount);
456 if TextEnteredColumn(1) and (StringGrid.Cells[1, 0] =
457 TAcronymMeaning(List.Items[I]).Name) then Inc(FoundCount);
458 if TextEnteredColumn(2) and (StringGrid.Cells[2, 0] =
459 TAcronymMeaning(List.Items[I]).Categories.GetString) then Inc(FoundCount);
460 end else begin
461 if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
462 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Acronym.Name)) > 0 then Inc(FoundCount);
463 if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
464 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Name)) > 0 then Inc(FoundCount);
465 if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
466 UTF8LowerCase(TAcronymMeaning(List.Items[I]).Categories.GetString)) > 0 then Inc(FoundCount);
467 end;
468 if FoundCount <> EnteredCount then List.Delete(I);
469 end else List.Delete(I);
470 end;
471 end else
472 if TAcronymMeaning(List.Items[I]) is TAcronymMeaning then begin
473 List.Delete(I);
474 end;
475 end;
476end;
477
478procedure TFormMain.OpenRecentClick(Sender: TObject);
479begin
480 AFileClose.Execute;
481 if ProjectClosed then begin
482 AFileNew.Execute;
483 AcronymDb.LoadFromFile(TMenuItem(Sender).Caption);
484 LastOpenedList1.AddItem(TMenuItem(Sender).Caption);
485 UpdateAcronymsList;
486 UpdateInterface;
487 end;
488end;
489
490procedure TFormMain.UpdateAcronymsList;
491begin
492 if Assigned(AcronymDb) then begin
493 ListViewSort1.Refresh;
494 end else ListViewSort1.List.Clear;
495end;
496
497procedure TFormMain.UpdateInterface;
498var
499 Title: string;
500begin
501 ListViewAcronyms.Enabled := Assigned(AcronymDb);
502 AFileClose.Enabled := Assigned(AcronymDb);
503 AFileSave.Enabled := Assigned(AcronymDb) and AcronymDb.Modified;
504 AFileSaveAs.Enabled := Assigned(AcronymDb);
505 AManualImport.Enabled := Assigned(AcronymDb);
506 AShowCategories.Enabled := Assigned(AcronymDb);
507 AShowAcronyms.Enabled := Assigned(AcronymDb);
508 AShowImportSources.Enabled := Assigned(AcronymDb);
509 ToolBar1.Visible := MenuItemToolbar.Checked;
510
511 Title := Application.Title;
512 if Assigned(AcronymDb) then Title := Title + ' - ' + ExtractFileName(AcronymDb.FileName);
513 if Assigned(AcronymDb) and AcronymDb.Modified then Title := Title + ' (' + SModified + ')';
514 Caption := Title;
515end;
516
517procedure TFormMain.LoadConfig;
518begin
519 RegistryContext := RegContext(HKEY_CURRENT_USER, DefaultRegKey);
520 PersistentForm1.RegistryContext := RegistryContext;
521 LastOpenedList1.LoadFromRegistry(RegistryContext);
522
523 with TRegistryEx.Create do
524 try
525 RootKey := HKEY_CURRENT_USER;
526 OpenKey(DefaultRegKey, True);
527 if ValueExists('LanguageCode') then
528 CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode(ReadStringWithDefault('LanguageCode', ''))
529 else CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode('');
530 AlwaysOnTop := ReadBoolWithDefault('AlwaysOnTop', False);
531 CheckBoxExactMath.Checked := ReadBoolWithDefault('ExactMatch', False);
532 MenuItemToolbar.Checked := ReadBoolWithDefault('ToolBarVisible', True);
533 finally
534 Free;
535 end;
536end;
537
538procedure TFormMain.SaveConfig;
539begin
540 LastOpenedList1.SaveToRegistry(RegistryContext);
541
542 with TRegistryEx.Create do
543 try
544 RootKey := HKEY_CURRENT_USER;
545 OpenKey(DefaultRegKey, True);
546 if Assigned(CoolTranslator1.Language) and (CoolTranslator1.Language.Code <> '') then
547 WriteString('LanguageCode', CoolTranslator1.Language.Code)
548 else DeleteValue('LanguageCode');
549 WriteBool('AlwaysOnTop', AlwaysOnTop);
550 WriteBool('ExactMatch', CheckBoxExactMath.Checked);
551 WriteBool('ToolBarVisible', MenuItemToolbar.Checked);
552 finally
553 Free;
554 end;
555end;
556
557end.
558
Note: See TracBrowser for help on using the repository browser.