Changeset 6 for trunk


Ignore:
Timestamp:
Apr 21, 2016, 10:22:08 AM (8 years ago)
Author:
chronos
Message:
  • Added: Acronyms can be loaded from/saved to different files.
  • Added: Application remembers list of last used files.
  • Added: Show question dialog on exit if acronyms were modified.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/UAcronym.pas

    r5 r6  
    7676
    7777  TAcronymDb = class
     78    FileName: string;
    7879    Sources: TObjectList; // TObjectList<TAcronymSource>
    7980    Acronyms: TAcronyms;
    8081    Contexts: TAcronymContexts;
     82    Modified: Boolean;
    8183    constructor Create;
    8284    destructor Destroy; override;
     
    222224  AcronymContext: TAcronymContext;
    223225begin
     226  Self.FileName := FileName;
    224227  Acronyms.Clear;
    225228  F := TStringList.Create;
     
    261264    Context.Free;
    262265  end;
     266  Modified := False;
    263267end;
    264268
     
    272276  Context: TStringList;
    273277begin
     278  Self.FileName := FileName;
    274279  F := TStringList.Create;
    275280  Line := TStringList.Create;
     
    299304    Context.Free;
    300305  end;
     306  Modified := False;
    301307end;
    302308
     
    336342    Acronym.Meanings.Add(Meaning);
    337343  end;
     344  Modified := True;
    338345end;
    339346
     
    352359    end;
    353360  end;
     361  Modified := True;
    354362end;
    355363
  • trunk/UFormImport.pas

    r2 r6  
    9393    end;
    9494  end;
     95  if AddedCount > 0 then FormMain.AcronymDb.Modified := True;
    9596  ShowMessage(Format('Imported %d new acronyms.', [AddedCount]));
    9697end;
  • trunk/UFormMain.lfm

    r5 r6  
    11object FormMain: TFormMain
    2   Left = 762
     2  Left = 541
    33  Height = 446
    4   Top = 587
     4  Top = 352
    55  Width = 631
    66  Caption = 'Acronym Decoder'
    7   ClientHeight = 446
     7  ClientHeight = 421
    88  ClientWidth = 631
     9  Menu = MainMenu1
    910  OnClose = FormClose
    1011  OnCloseQuery = FormCloseQuery
     
    1516  object ListViewAcronyms: TListView
    1617    Left = 8
    17     Height = 391
    18     Top = 46
     18    Height = 372
     19    Top = 40
    1920    Width = 613
    2021    Anchors = [akTop, akLeft, akRight, akBottom]
     
    43924393      OnExecute = AAcronymRemoveAllExecute
    43934394    end
     4395    object AFileOpen: TAction
     4396      Caption = 'Open'
     4397      OnExecute = AFileOpenExecute
     4398    end
     4399    object AFileNew: TAction
     4400      Caption = 'New'
     4401      OnExecute = AFileNewExecute
     4402    end
     4403    object AFileSave: TAction
     4404      Caption = 'Save'
     4405      OnExecute = AFileSaveExecute
     4406    end
     4407    object AFileClose: TAction
     4408      Caption = 'Close'
     4409      OnExecute = AFileCloseExecute
     4410    end
     4411    object AFileSaveAs: TAction
     4412      Caption = 'Save as...'
     4413      OnExecute = AFileSaveAsExecute
     4414    end
    43944415  end
    43954416  object PersistentForm1: TPersistentForm
     
    43974418    EntireVisible = False
    43984419    left = 304
    4399     top = 128
     4420    top = 112
    44004421  end
    44014422  object PopupMenuAcronym: TPopupMenu
     
    44154436    end
    44164437  end
     4438  object MainMenu1: TMainMenu
     4439    left = 465
     4440    top = 112
     4441    object MenuItem8: TMenuItem
     4442      Caption = 'File'
     4443      object MenuItem10: TMenuItem
     4444        Action = AFileNew
     4445      end
     4446      object MenuItem11: TMenuItem
     4447        Action = AFileOpen
     4448      end
     4449      object MenuItemOpenRecent: TMenuItem
     4450        Caption = 'Open recent'
     4451      end
     4452      object MenuItem12: TMenuItem
     4453        Action = AFileSave
     4454      end
     4455      object MenuItem13: TMenuItem
     4456        Action = AFileSaveAs
     4457      end
     4458      object MenuItem14: TMenuItem
     4459        Action = AFileClose
     4460      end
     4461      object MenuItem15: TMenuItem
     4462        Caption = '-'
     4463      end
     4464      object MenuItem9: TMenuItem
     4465        Action = AExit
     4466      end
     4467    end
     4468  end
     4469  object OpenDialog1: TOpenDialog
     4470    left = 151
     4471    top = 85
     4472  end
     4473  object SaveDialog1: TSaveDialog
     4474    left = 48
     4475    top = 85
     4476  end
     4477  object LastOpenedList1: TLastOpenedList
     4478    MaxCount = 10
     4479    OnChange = LastOpenedList1Change
     4480    left = 509
     4481    top = 267
     4482  end
    44174483end
  • trunk/UFormMain.pas

    r5 r6  
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
    99  ComCtrls, StdCtrls, ExtCtrls, ActnList, UAcronym, UPersistentForm, Contnrs,
    10   URegistry, Registry;
     10  URegistry, ULastOpenedList, Registry;
    1111
    1212type
     
    1919    AAcronymRemove: TAction;
    2020    AAcronymRemoveAll: TAction;
     21    AFileSaveAs: TAction;
     22    AFileClose: TAction;
     23    AFileSave: TAction;
     24    AFileNew: TAction;
     25    AFileOpen: TAction;
    2126    AImport: TAction;
    2227    AShow: TAction;
     
    2429    ActionList1: TActionList;
    2530    EditSearch: TEdit;
     31    LastOpenedList1: TLastOpenedList;
    2632    ListViewAcronyms: TListView;
     33    MainMenu1: TMainMenu;
    2734    MenuItem1: TMenuItem;
     35    MenuItem10: TMenuItem;
     36    MenuItem11: TMenuItem;
     37    MenuItem12: TMenuItem;
     38    MenuItem13: TMenuItem;
     39    MenuItem14: TMenuItem;
     40    MenuItem15: TMenuItem;
     41    MenuItemOpenRecent: TMenuItem;
    2842    MenuItem2: TMenuItem;
    2943    MenuItem3: TMenuItem;
     
    3246    MenuItem6: TMenuItem;
    3347    MenuItem7: TMenuItem;
     48    MenuItem8: TMenuItem;
     49    MenuItem9: TMenuItem;
     50    OpenDialog1: TOpenDialog;
    3451    PersistentForm1: TPersistentForm;
    3552    PopupMenuAcronym: TPopupMenu;
    3653    PopupMenuTryIcon: TPopupMenu;
     54    SaveDialog1: TSaveDialog;
    3755    TrayIcon1: TTrayIcon;
    3856    procedure AAcronymAddExecute(Sender: TObject);
     
    4159    procedure AAcronymRemoveExecute(Sender: TObject);
    4260    procedure AExitExecute(Sender: TObject);
     61    procedure AFileCloseExecute(Sender: TObject);
     62    procedure AFileNewExecute(Sender: TObject);
     63    procedure AFileOpenExecute(Sender: TObject);
     64    procedure AFileSaveAsExecute(Sender: TObject);
     65    procedure AFileSaveExecute(Sender: TObject);
    4366    procedure AImportExecute(Sender: TObject);
    4467    procedure AShowExecute(Sender: TObject);
     
    4972    procedure FormDestroy(Sender: TObject);
    5073    procedure FormShow(Sender: TObject);
     74    procedure LastOpenedList1Change(Sender: TObject);
    5175    procedure ListViewAcronymsData(Sender: TObject; Item: TListItem);
    5276    procedure ListViewAcronymsDblClick(Sender: TObject);
     
    5781  private
    5882    FoundAcronyms: TAcronymMeanings;
     83    RegistryContext: TRegistryContext;
     84    procedure OpenRecentClick(Sender: TObject);
    5985    procedure UpdateAcronymsList;
    6086    procedure UpdateInterface;
     87    procedure LoadConfig;
     88    procedure SaveConfig;
    6189  public
    6290    AcronymDb: TAcronymDb;
     
    74102
    75103const
    76   DefaultFileName = 'Acronyms.csv';
     104  ProjectExt = '.adp';
     105  DefaultFileName = 'Acronyms' + ProjectExt;
     106  DefaultRegKey = '\Software\Acronym Decoder';
    77107
    78108{ TFormMain }
     
    91121procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
    92122begin
    93   AcronymDB.SaveToFile(DefaultFileName);
     123  AFileClose.Execute;
    94124  PersistentForm1.Save(Self);
     125  SaveConfig;
    95126end;
    96127
    97128procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
    98 begin
     129var
     130  ModalResult: TModalResult;
     131begin
     132  if Assigned(AcronymDb) and AcronymDb.Modified then begin
     133    ModalResult := MessageDlg('Application exit',
     134    'Acronyms were modified. Do you want to save them to file before exit?',
     135      mtConfirmation, [mbYes, mbNo, mbCancel], 0);
     136    if ModalResult = mrYes then AFileSave.Execute
     137    else if ModalResult = mrNo then CanClose := True
     138    else CanClose := False;
     139  end;
    99140end;
    100141
     
    102143begin
    103144  Close;
     145end;
     146
     147procedure TFormMain.AFileCloseExecute(Sender: TObject);
     148begin
     149  if Assigned(AcronymDb) then begin
     150    if AcronymDb.Modified then AFileSave.Execute;
     151    FreeAndNil(AcronymDb);
     152    UpdateAcronymsList;
     153    UpdateInterface;
     154  end;
     155end;
     156
     157procedure TFormMain.AFileNewExecute(Sender: TObject);
     158begin
     159  AFileClose.Execute;
     160  AcronymDb := TAcronymDb.Create;
     161  AcronymDb.FileName := DefaultFileName;
     162  AcronymDb.Acronyms.Clear;
     163  UpdateAcronymsList;
     164  UpdateInterface;
     165end;
     166
     167procedure TFormMain.AFileOpenExecute(Sender: TObject);
     168begin
     169  OpenDialog1.DefaultExt := ProjectExt;
     170  OpenDialog1.FileName := AcronymDb.FileName;
     171  if OpenDialog1.Execute then begin
     172    AcronymDb.LoadFromFile(OpenDialog1.FileName);
     173    LastOpenedList1.AddItem(OpenDialog1.FileName);
     174    UpdateAcronymsList;
     175    UpdateInterface;
     176  end;
     177end;
     178
     179procedure TFormMain.AFileSaveAsExecute(Sender: TObject);
     180begin
     181  SaveDialog1.DefaultExt := ProjectExt;
     182  SaveDialog1.FileName := AcronymDb.FileName;
     183  if SaveDialog1.Execute then begin
     184    AcronymDb.SaveToFile(SaveDialog1.FileName);
     185    LastOpenedList1.AddItem(SaveDialog1.FileName);
     186    UpdateInterface;
     187  end;
     188end;
     189
     190procedure TFormMain.AFileSaveExecute(Sender: TObject);
     191begin
     192  if FileExists(AcronymDb.FileName) then begin
     193    AcronymDb.SaveToFile(AcronymDb.FileName);
     194    LastOpenedList1.AddItem(AcronymDb.FileName);
     195    UpdateInterface;
     196  end else AFileSaveAs.Execute;
    104197end;
    105198
     
    133226  TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
    134227    AcronymDb.Acronyms.Clear;
     228    AcronymDb.Modified := True;
    135229    UpdateAcronymsList;
    136230    UpdateInterface;
     
    185279
    186280procedure TFormMain.FormShow(Sender: TObject);
    187 const
    188   DefaultRegKey = '\Software\Acronym Decoder';
    189 begin
    190   PersistentForm1.RegistryContext := RegContext(HKEY_CURRENT_USER, DefaultRegKey);
     281begin
     282  LoadConfig;
    191283  PersistentForm1.Load(Self);
    192   if FileExists(DefaultFileName) then
    193     AcronymDB.LoadFromFile(DefaultFileName);
    194   AcronymDb.FilterList(EditSearch.Text, FoundAcronyms);
     284
     285  if (LastOpenedList1.Items.Count > 0) and  FileExists(LastOpenedList1.Items[0]) then
     286    AcronymDB.LoadFromFile(LastOpenedList1.Items[0]);
    195287  UpdateAcronymsList;
    196288  UpdateInterface;
     289end;
     290
     291procedure TFormMain.LastOpenedList1Change(Sender: TObject);
     292begin
     293  LastOpenedList1.LoadToMenuItem(MenuItemOpenRecent, OpenRecentClick);
    197294end;
    198295
     
    235332end;
    236333
     334procedure TFormMain.OpenRecentClick(Sender: TObject);
     335begin
     336  AFileClose.Execute;
     337  AFileNew.Execute;
     338  AcronymDb.LoadFromFile(TMenuItem(Sender).Caption);
     339  LastOpenedList1.AddItem(TMenuItem(Sender).Caption);
     340  UpdateAcronymsList;
     341  UpdateInterface;
     342end;
     343
    237344procedure TFormMain.UpdateAcronymsList;
    238345begin
    239   AcronymDb.Acronyms.Sort(AcronymComparer);
    240   AcronymDb.FilterList(EditSearch.Text, FoundAcronyms);
     346  if Assigned(AcronymDb) then begin
     347    AcronymDb.Acronyms.Sort(AcronymComparer);
     348    AcronymDb.FilterList(EditSearch.Text, FoundAcronyms);
     349  end else FoundAcronyms.Clear;
    241350  ListViewAcronyms.Items.Count := FoundAcronyms.Count;
    242351  ListViewAcronyms.Refresh;
     
    244353
    245354procedure TFormMain.UpdateInterface;
    246 begin
    247   AAcronymRemove.Enabled := Assigned(ListViewAcronyms.Selected);
     355var
     356  Title: string;
     357begin
     358  ListViewAcronyms.Enabled := Assigned(AcronymDb);
     359  AAcronymRemove.Enabled := Assigned(AcronymDb) and Assigned(ListViewAcronyms.Selected);
     360  AAcronymModify.Enabled := Assigned(AcronymDb) and Assigned(ListViewAcronyms.Selected);
     361  AAcronymAdd.Enabled := Assigned(AcronymDb);
     362  AAcronymRemoveAll.Enabled := Assigned(AcronymDb) and (AcronymDb.Acronyms.Count > 0);
     363  AFileClose.Enabled := Assigned(AcronymDb);
     364  AFileSave.Enabled := Assigned(AcronymDb) and AcronymDb.Modified;
     365  AFileSaveAs.Enabled := Assigned(AcronymDb);
     366  EditSearch.Enabled := Assigned(AcronymDb);
     367
     368  Title := Application.Title;
     369  if Assigned(AcronymDb) then Title := Title + ' - ' + ExtractFileName(AcronymDb.FileName);
     370  if Assigned(AcronymDb) and AcronymDb.Modified then Title := Title + ' (modified)';
     371  Caption := Title;
     372end;
     373
     374procedure TFormMain.LoadConfig;
     375begin
     376  RegistryContext := RegContext(HKEY_CURRENT_USER, DefaultRegKey);
     377  PersistentForm1.RegistryContext := RegistryContext;
     378  LastOpenedList1.LoadFromRegistry(RegistryContext);
     379end;
     380
     381procedure TFormMain.SaveConfig;
     382begin
     383  LastOpenedList1.SaveToRegistry(RegistryContext);
    248384end;
    249385
Note: See TracChangeset for help on using the changeset viewer.