Ignore:
Timestamp:
Sep 10, 2022, 10:21:48 PM (2 years ago)
Author:
chronos
Message:
  • Modified: Do not create all application forms at initialization phase but dynamically.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormTables.pas

    r26 r28  
    1818    ARemove: TAction;
    1919    ActionList1: TActionList;
    20     ListView1: TListView;
     20    ListViewTables: TListView;
    2121    MenuItem1: TMenuItem;
    2222    MenuItem2: TMenuItem;
     
    3939    procedure FormDestroy(Sender: TObject);
    4040    procedure FormShow(Sender: TObject);
    41     procedure ListView1Data(Sender: TObject; Item: TListItem);
    42     procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
     41    procedure ListViewTablesData(Sender: TObject; Item: TListItem);
     42    procedure ListViewTablesSelectItem(Sender: TObject; Item: TListItem;
    4343      Selected: Boolean);
    4444  private
     
    5353  end;
    5454
    55 var
    56   FormTables: TFormTables;
    57 
    5855
    5956implementation
     
    7168{ TFormTables }
    7269
    73 procedure TFormTables.ListView1Data(Sender: TObject; Item: TListItem);
     70procedure TFormTables.ListViewTablesData(Sender: TObject; Item: TListItem);
    7471begin
    7572  if (Item.Index >= 0) and (Item.Index < FTables.Count) then
    7673  with TTable(FTables[Item.Index]) do begin
    7774    Item.Caption := Caption;
    78     Item.SubItems.Add(IntToStr(RecordsCount));
     75    Item.SubItems.Add(IntToStr(RecordsCount) + ' ' + IntToStr(Fields.Count));
    7976    Item.Data := FTables[Item.Index];
    8077  end
    8178end;
    8279
    83 procedure TFormTables.ListView1SelectItem(Sender: TObject; Item: TListItem;
     80procedure TFormTables.ListViewTablesSelectItem(Sender: TObject; Item: TListItem;
    8481  Selected: Boolean);
    8582begin
     
    9794procedure TFormTables.UpdateInterface;
    9895begin
    99   ListView1.Enabled := Assigned(Tables);
    100   AAdd.Enabled := Assigned(Tables);
    101   AModify.Enabled := Assigned(Tables) and Assigned(ListView1.Selected);
    102   ARemove.Enabled := Assigned(Tables) and Assigned(ListView1.Selected);
    103   AShowRecords.Enabled := Assigned(Tables) and Assigned(ListView1.Selected);
    104   AShowFields.Enabled := Assigned(Tables) and Assigned(ListView1.Selected);
     96  ListViewTables.Enabled := Assigned(Tables) and Assigned(FDbClient);
     97  AAdd.Enabled := Assigned(Tables) and Assigned(FDbClient);
     98  AModify.Enabled := Assigned(Tables) and Assigned(ListViewTables.Selected) and Assigned(FDbClient);
     99  ARemove.Enabled := Assigned(Tables) and Assigned(ListViewTables.Selected) and Assigned(FDbClient);
     100  AShowRecords.Enabled := Assigned(Tables) and Assigned(ListViewTables.Selected) and Assigned(FDbClient);
     101  AShowFields.Enabled := Assigned(Tables) and Assigned(ListViewTables.Selected) and Assigned(FDbClient);
    105102end;
    106103
     
    112109  NewTable.Name := SNewTable;
    113110  NewTable.DbClient := Tables.DbClient;
    114   FormTable.Load(NewTable);
    115   if FormTable.ShowModal = mrOk then begin
    116     FormTable.Save(NewTable);
    117     Tables.DbClient.Query('INSERT INTO Model ( Name , Caption ) VALUES ( ' +
    118       NewTable.Name + ' , ' + NewTable.Caption + ' )');
    119     ReloadList;
    120   end else NewTable.Free;
     111  with TFormTable.Create(nil) do
     112  try
     113    Load(NewTable);
     114    if ShowModal = mrOk then begin
     115      Save(NewTable);
     116      Tables.DbClient.Query('INSERT INTO Model ( Name , Caption ) VALUES ( ' +
     117        NewTable.Name + ' , ' + NewTable.Caption + ' )');
     118      ReloadList;
     119    end else NewTable.Free;
     120  finally
     121    Free;
     122  end;
    121123end;
    122124
    123125procedure TFormTables.AModifyExecute(Sender: TObject);
    124126begin
    125   if Assigned(ListView1.Selected) then begin
    126     FormTable.Load(TTable(ListView1.Selected.Data));
    127     if FormTable.ShowModal = mrOk then begin
    128       FormTable.Save(TTable(ListView1.Selected.Data));
    129       DbClient.Query('UPDATE Model SET Caption = ' + TTable(ListView1.Selected.Data).Caption + ' WHERE Name = ' + TTable(ListView1.Selected.Data).Name);
    130       ReloadList;
     127  if Assigned(ListViewTables.Selected) then begin
     128    with TFormTable.Create(nil) do
     129    try
     130      Load(TTable(ListViewTables.Selected.Data));
     131      if ShowModal = mrOk then begin
     132        Save(TTable(ListViewTables.Selected.Data));
     133        DbClient.Query('UPDATE Model SET Caption = ' + TTable(ListViewTables.Selected.Data).Caption + ' WHERE Name = ' + TTable(ListViewTables.Selected.Data).Name);
     134        ReloadList;
     135      end;
     136    finally
     137      Free;
    131138    end;
    132139  end;
     
    135142procedure TFormTables.ARemoveExecute(Sender: TObject);
    136143begin
    137   if Assigned(ListView1.Selected) then begin
    138     if MessageDlg(SRemoveTable, Format(SRemoveTableConfirm, [TTable(ListView1.Selected.Data).Caption]),
     144  if Assigned(ListViewTables.Selected) then begin
     145    if MessageDlg(SRemoveTable, Format(SRemoveTableConfirm, [TTable(ListViewTables.Selected.Data).Caption]),
    139146    mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
    140       Tables.DbClient.Query('DELETE FROM Model WHERE Name = ' + TTable(ListView1.Selected.Data).Name);
     147      Tables.DbClient.Query('DELETE FROM Model WHERE Name = ' + TTable(ListViewTables.Selected.Data).Name);
    141148      ReloadList;
    142149    end;
     
    152159  OldField: TField;
    153160  OldTable: TTable;
    154 begin
    155   if Assigned(ListView1.Selected) then begin
    156     OldTable := TTable(ListView1.Selected.Data);
     161  FormFields: TFormFields;
     162begin
     163  if Assigned(ListViewTables.Selected) then begin
     164    OldTable := TTable(ListViewTables.Selected.Data);
     165    FormFields := TFormFields.Create(nil);
    157166    FormFields.TableName := OldTable.Caption;
    158167    FormFields.Fields := TFields.Create;
     
    160169    FormFields.Fields.Assign(OldTable.Fields);
    161170    for I := 0 to FormFields.Fields.Count - 1 do
    162       TField(FormFields.Fields[I]).Table := TField(OldTable.Fields[I]).Table;
     171      FormFields.Fields[I].Table := OldTable.Fields[I].Table;
    163172    if FormFields.ShowModal = mrOk then begin
    164173      // Inefficient way to update table data. Copy original columns to new records and
     
    168177      for I := 0 to NewRecords.Count - 1 do begin
    169178        NewRecords.Items[I] := TRecord.Create;
    170         TRecord(NewRecords.Items[I]).Values.Count := FormFields.Fields.Count;
     179        NewRecords[I].Values.Count := FormFields.Fields.Count;
    171180        for C := 0 to FormFields.Fields.Count - 1 do
    172           TRecord(NewRecords.Items[I]).Values[C] := TField(FormFields.Fields[C]).GetValueClass.Create;
     181          NewRecords[I].Values[C] := FormFields.Fields[C].GetValueClass.Create;
    173182      end;
    174183      for C := 0 to FormFields.Fields.Count - 1 do begin
    175         OldField := OldTable.Fields.SearchByName(TField(FormFields.Fields[C]).Name);
     184        OldField := OldTable.Fields.SearchByName(FormFields.Fields[C].Name);
    176185        if Assigned(OldField) then begin
    177186          FI := OldTable.Fields.IndexOf(OldField);
    178187          for I := 0 to NewRecords.Count - 1 do
    179             TValue(TRecord(NewRecords[I]).Values[C]).Assign(TValue(TRecord(OldTable.Records[I]).Values[FI]));
     188            NewRecords[I].Values[C].Assign(OldTable.Records[I].Values[FI]);
    180189        end;
    181190      end;
     
    186195    end;
    187196    FormFields.Fields.Free;
     197    FormFields.Free;
    188198  end;
    189199end;
     
    191201procedure TFormTables.AShowRecordsExecute(Sender: TObject);
    192202begin
    193   if Assigned(ListView1.Selected) then begin
    194     FormRecords.Table := TTable(ListView1.Selected.Data);
    195     FormRecords.ShowModal;
     203  if Assigned(ListViewTables.Selected) then begin
     204    with TFormRecords.Create(nil) do
     205    try
     206      Table := TTable(ListViewTables.Selected.Data);
     207      ShowModal;
     208    finally
     209      Free;
     210    end;
    196211    ReloadList;
    197212  end;
     
    227242  if Assigned(DbClient) then begin
    228243    DbRows := TDbRows.Create;
    229     DbClient.Query('SELECT * FROM Model', DbRows);
    230     for I := 0 to DbRows.Count - 1 do begin
    231       NewTable := Tables.AddNew(DbRows[I].Items['Name']);
    232       NewTable.Caption := DbRows[I].Items['Caption'];
    233     end;
    234     DbRows.Free;
     244    try
     245      DbClient.Query('SELECT * FROM Model', DbRows);
     246      for I := 0 to DbRows.Count - 1 do begin
     247        NewTable := Tables.AddNew(DbRows[I].Items['Name']);
     248        NewTable.Caption := DbRows[I].Items['Caption'];
     249      end;
     250    finally
     251      DbRows.Free;
     252    end;
    235253  end;
    236254
    237255  for I := 0 to Tables.Count - 1 do
    238     TTable(Tables[I]).LoadRecordsCount;
     256    Tables[I].LoadRecordsCount;
    239257  if Assigned(Tables) then begin
    240     ListView1.Items.Count := Tables.Count;
    241     ListView1.Repaint;
    242   end else ListView1.Items.Count := 0;
     258    ListViewTables.Items.Count := Tables.Count;
     259    ListViewTables.Repaint;
     260  end else ListViewTables.Items.Count := 0;
    243261  UpdateInterface;
    244262end;
Note: See TracChangeset for help on using the changeset viewer.