Changeset 9 for trunk/UCore.pas


Ignore:
Timestamp:
Jan 25, 2015, 9:41:26 PM (10 years ago)
Author:
chronos
Message:
  • Added: Preferences dialog window.
  • Modified: Currently selected database is set in TCore class in field Database.
  • Added: Application remembers name of previous opened database and reopen it during startup.
  • Modified: FormDatabases is now used as modal form.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r8 r9  
    99
    1010type
    11 
    1211  { TCore }
    1312
    1413  TCore = class(TDataModule)
     14    ADatabaseConnect: TAction;
     15    ADatabaseDisconnect: TAction;
    1516    APreferences: TAction;
    1617    AExit: TAction;
     
    1819    ImageList1: TImageList;
    1920    XMLConfig1: TXMLConfig;
     21    procedure ADatabaseConnectExecute(Sender: TObject);
     22    procedure ADatabaseDisconnectExecute(Sender: TObject);
    2023    procedure AExitExecute(Sender: TObject);
     24    procedure APreferencesExecute(Sender: TObject);
    2125    procedure DataModuleCreate(Sender: TObject);
    2226    procedure DataModuleDestroy(Sender: TObject);
    2327  private
     28    FDatabase: TDatabase;
    2429    Initialized: Boolean;
    2530    procedure InitEngines;
    2631    procedure InitDataTypes;
     32    procedure SetDatabase(AValue: TDatabase);
    2733  public
     34    Preferences: TPreferences;
    2835    Databases: TDatabases;
    2936    Engines: TDatabaseEngines;
     
    3340    procedure Init;
    3441    procedure Done;
     42    procedure UpdateInterface;
     43    property Database: TDatabase read FDatabase write SetDatabase;
    3544  end;
    3645
     
    4251uses
    4352  UEngineXML, UEngineMySQL, UEngineSQLite, UFormTables, UFormConnect,
    44   UFormDatabases, UDataTypes;
     53  UFormDatabases, UDataTypes, UFormPreferences, UFormMain;
    4554
    4655{$R *.lfm}
     
    5362  Engines := TDatabaseEngines.Create;
    5463  DataTypes := TDataTypes.Create;
     64  Preferences := TPreferences.Create;
    5565end;
    5666
     
    6070end;
    6171
     72procedure TCore.ADatabaseDisconnectExecute(Sender: TObject);
     73begin
     74  Database := nil;
     75end;
     76
     77procedure TCore.ADatabaseConnectExecute(Sender: TObject);
     78begin
     79  if FormDatabases.ShowModal = mrOk then begin
     80    Database := FormDatabases.SelectedDatabase;
     81  end;
     82end;
     83
     84procedure TCore.APreferencesExecute(Sender: TObject);
     85begin
     86  FormPreferences.Load(Preferences);
     87  if FormPreferences.ShowModal = mrOk then begin
     88    FormPreferences.Save(Preferences);
     89  end;
     90end;
     91
    6292procedure TCore.DataModuleDestroy(Sender: TObject);
    6393begin
    64   DataTypes.Free;
    65   Engines.Free;
    66   Databases.Free;
     94  FreeAndNil(Preferences);
     95  FreeAndNil(DataTypes);
     96  FreeAndNil(Engines);
     97  FreeAndNil(Databases);
    6798end;
    6899
     
    98129end;
    99130
     131procedure TCore.SetDatabase(AValue: TDatabase);
     132begin
     133  if FDatabase = AValue then Exit;
     134  if Assigned(FDatabase) then begin
     135    FDatabase.Clear;
     136  end;
     137  FDatabase := AValue;
     138  if Assigned(FDatabase) then begin
     139    FDatabase.Load;
     140    Preferences.LastDatabaseName := FDatabase.Name;
     141  end;
     142  FormTables.Database := FDatabase;
     143  UpdateInterface;
     144end;
     145
    100146procedure TCore.LoadConfig;
    101147var
     
    115161  end;
    116162  //Databases.ConnectionString := XMLConfig1.GetValue('ConnectionString', 'file://' + ExtractFileDir(Application.ExeName) + '/data.xml');
     163  Preferences.RememberDatabase := XMLConfig1.GetValue('RememberDatabase', True);
     164  Preferences.LastDatabaseName := XMLConfig1.GetValue('LastDatabaseName', '');
    117165end;
    118166
     
    127175    XMLConfig1.SetValue('Database/Item' + IntToStr(I) + '/Engine', TDatabase(Databases[I]).Engine.Name);
    128176  end;
     177  XMLConfig1.SetValue('RememberDatabase', Preferences.RememberDatabase);
     178  XMLConfig1.SetValue('LastDatabaseName', Preferences.LastDatabaseName);
    129179end;
    130180
     
    138188    LoadConfig;
    139189    FormDatabases.ReloadList;
     190    UpdateInterface;
     191    if Preferences.RememberDatabase then begin
     192      Database := Databases.FindByName(Preferences.LastDatabaseName);
     193    end else ADatabaseConnect.Execute;
    140194  end;
    141195end;
     
    146200end;
    147201
     202procedure TCore.UpdateInterface;
     203begin
     204  ADatabaseDisconnect.Enabled := Assigned(FormTables.Database);
     205  FormMain.UpdateInterface;
     206end;
     207
    148208end.
    149209
Note: See TracChangeset for help on using the changeset viewer.