source: tags/1.1.0/Forms/UFormMain.pas

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