Changeset 2 for trunk/UMainForm.pas


Ignore:
Timestamp:
May 30, 2010, 6:09:55 PM (14 years ago)
Author:
george
Message:
  • Přidáno: Uchování polohy a velikosti oken.
  • Přidáno: Čtení stromu skupin a objektů. Zobrazení seznamů objektů.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        11lib
         2Config.xml
         3chronis
  • trunk/UMainForm.pas

    r1 r2  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
    9   StdCtrls, USqlDatabase;
     9  StdCtrls, USqlDatabase, DOM, XMLRead, XMLWrite, UPersistentForm;
     10
     11const
     12  ConfigFileName = 'Config.xml';
    1013
    1114type
     
    1821    ListView1: TListView;
    1922    TreeView1: TTreeView;
     23    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    2024    procedure FormCreate(Sender: TObject);
     25    procedure FormShow(Sender: TObject);
     26    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    2127  private
    22     { private declarations }
     28    procedure LoadConfiguration;
    2329  public
     30    PersistentForm: TPersistentForm;
    2431    Database: TSQLDatabase;
    2532    procedure LoadTree;
     
    3542{ TMainForm }
    3643
     44procedure TMainForm.LoadConfiguration;
     45var
     46  Config: TXMLDocument;
     47  I1: Integer;
     48  I2: Integer;
     49  I3: Integer;
     50begin
     51  if FileExists(ConfigFileName) then begin
     52    ReadXMLFile(Config, ConfigFileName);
     53    for I1 := 0 to Config.ChildNodes.Count - 1 do
     54    with Config.ChildNodes[I1] do begin
     55      if NodeName = 'configuration' then
     56      for I2 := 0 to ChildNodes.Count - 1 do
     57      with ChildNodes[I2] do begin
     58        if NodeName = 'database' then
     59        for I3 := 0 to ChildNodes.Count - 1 do
     60        with ChildNodes[I3] do begin
     61          if NodeName = 'hostname' then
     62            Database.HostName := TextContent;
     63          if NodeName = 'schema' then
     64            Database.Schema := TextContent;
     65          if NodeName = 'username' then
     66            Database.UserName := TextContent;
     67          if NodeName = 'password' then
     68            Database.Password := TextContent;
     69        end;
     70      end;
     71    end;
     72    Config.Destroy;
     73  end;
     74end;
     75
    3776procedure TMainForm.FormCreate(Sender: TObject);
    3877begin
    3978  Database := TSqlDatabase.Create;
    40   with Database do begin
    41     Hostname := 'localhost';
    42     Schema := 'chronis_system';
    43     UserName := 'root';
     79  LoadConfiguration;
     80  Database.Connect;
     81  PersistentForm := TPersistentForm.Create;
     82end;
    4483
     84procedure TMainForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
     85begin
     86  PersistentForm.Save(Self);
     87end;
     88
     89procedure TMainForm.FormShow(Sender: TObject);
     90begin
     91  PersistentForm.Load(Self);
     92  LoadTree;
     93end;
     94
     95procedure TMainForm.TreeView1Change(Sender: TObject; Node: TTreeNode);
     96var
     97  DbRows: TDbRows;
     98  Properties: TDbRows;
     99  Values: TDbRows;
     100  I: Integer;
     101  C: Integer;
     102  NewItem: TListItem;
     103  NewColumn: TListColumn;
     104begin
     105  if Assigned(TreeView1.Selected) then
     106  with ListView1, Items do begin
     107    Clear;
     108    DbRows := Database.Query('SELECT * FROM `Object` WHERE `Id`=' + IntToStr(TreeView1.Selected.ImageIndex));
     109    if DbRows.Count = 1 then begin
     110      // Load column names
     111      Properties := Database.Query('SELECT * FROM `Property` WHERE `Object`=' +
     112        DbRows[0].Values['Id']);
     113      Columns.Clear;
     114      NewColumn := Columns.Add;
     115      NewColumn.Caption := 'Id';
     116      for I := 0 to Properties.Count - 1 do begin
     117        NewColumn := Columns.Add;
     118        NewColumn.Caption := DbRows[I].Values['Name'];
     119      end;
     120
     121      // Load items
     122      Values := Database.Query('SELECT * FROM `' + DbRows[0].Values['Schema'] + '`.`' +
     123        DbRows[0].Values['Table'] + '`');
     124      for I := 0 to Values.Count - 1 do begin
     125        NewItem := Items.Add;
     126        NewItem.Caption := Values[I].Values['Id'];
     127        for C := 0 to Properties.Count - 1 do begin
     128          NewItem.SubItems.Add(Values[I].Values[Properties[C].Values['ColumnName']]);
     129        end;
     130      end;
     131      Values.Destroy;
     132      Properties.Destroy;
     133    end;
     134    DbRows.Destroy;
    45135  end;
    46136end;
    47137
    48138procedure TMainForm.LoadTree;
     139var
     140  DbRows: TDbRows;
     141  ObjectDbRows: TDbRows;
     142  I: Integer;
     143  O: Integer;
     144  NewNode: TTreeNode;
     145  NewObjectNode: TTreeNode;
    49146begin
    50 
     147  with TreeView1, Items do begin
     148    Clear;
     149    AddChild(nil, 'Skupiny');
     150    DbRows := Database.Query('SELECT * FROM `MenuGroup`');
     151    for I := 0 to DbRows.Count - 1 do begin
     152      NewNode := AddChild(TopItem, DbRows[I].Values['Name']);
     153      ObjectDbRows := Database.Query('SELECT * FROM `Object` WHERE `MenuGroup`=' + DbRows[I].Values['Id']);
     154      for O := 0 to ObjectDbRows.Count - 1 do begin
     155        NewObjectNode := AddChild(NewNode, ObjectDbRows[O].Values['Name']);
     156        NewObjectNode.ImageIndex := StrToInt(ObjectDbRows[O].Values['Id']);
     157      end;
     158      ObjectDbRows.Destroy;
     159    end;
     160    TopItem.Expand(True);
     161    DbRows.Destroy;
     162  end;
    51163end;
    52164
Note: See TracChangeset for help on using the changeset viewer.