Changeset 16 for trunk/Forms


Ignore:
Timestamp:
Nov 15, 2012, 2:01:43 PM (12 years ago)
Author:
chronos
Message:
  • Přidáno: Vkládání, úprava a mazání položek jsou nyní funkční.
Location:
trunk/Forms
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormEdit.lfm

    r15 r16  
    3838  end
    3939  object PanelControls: TPanel
    40     Left = 0
    41     Height = 290
    42     Top = 0
    43     Width = 553
     40    Left = 8
     41    Height = 282
     42    Top = 8
     43    Width = 537
    4444    Align = alTop
    4545    Anchors = [akTop, akLeft, akRight, akBottom]
     46    BorderSpacing.Around = 8
    4647    BevelOuter = bvNone
    4748    TabOrder = 2
  • trunk/Forms/UFormEdit.pas

    r15 r16  
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    99  Buttons, ExtCtrls, Spin, ComCtrls, EditBtn, SpecializedList,
    10   SpecializedDictionary, UDataView, USqlDatabase;
     10  SpecializedDictionary, UDataView, USqlDatabase, UTimeEdit;
    1111
    1212type
     
    1919    Visible: Boolean;
    2020    Rect: TRect;
     21    Control: TControl; // Used by TFormEdit
    2122    ControlType: TControlType;
    2223    TitlePlacement: TAlign;
     
    4849    ButtonOk: TButton;
    4950    ButtonCancel: TButton;
    50     CalcEdit1: TCalcEdit;
    5151    PanelControls: TPanel;
    5252    procedure FormCreate(Sender: TObject);
    5353    procedure FormDestroy(Sender: TObject);
    54     procedure SpeedButton1Click(Sender: TObject);
    5554  private
    5655    FView: TDataViewForm;
    5756    procedure SetView(AValue: TDataViewForm);
    58     { private declarations }
     57    procedure RebuildControls;
    5958  public
    60     RuntimeControls: TListObject;
    6159    Values: TDictionaryStringString;
    6260    ItemId: Integer;
    6361    property View: TDataViewForm read FView write SetView;
    64     procedure UpdateData;
     62    procedure ClearData;
     63    procedure LoadFromDatabase;
     64    procedure SaveToDatabase;
     65    procedure LoadFromControls;
    6566  end;
    6667
     
    7273uses
    7374  UCore, UFormMain;
     75
     76resourcestring
     77  SItemNotFound = 'Item not found';
    7478
    7579{ TDataViewForm }
     
    108112begin
    109113  Values := TDictionaryStringString.Create;
    110   RuntimeControls := TListObject.Create;
    111114end;
    112115
    113116procedure TFormEdit.FormDestroy(Sender: TObject);
    114117begin
    115   Values.Free;
    116   RuntimeControls.Free;
    117 end;
    118 
    119 procedure TFormEdit.SpeedButton1Click(Sender: TObject);
    120 begin
    121 
     118  FreeAndNil(Values);
    122119end;
    123120
     
    126123  if FView=AValue then Exit;
    127124  FView := AValue;
    128   if Assigned(AValue) then UpdateData
     125  if Assigned(AValue) then ClearData
    129126    else begin
    130127      Values.Clear;
    131       RuntimeControls.Clear;
    132     end;
    133 end;
    134 
    135 procedure TFormEdit.UpdateData;
     128    end;
     129end;
     130
     131procedure TFormEdit.RebuildControls;
    136132var
    137133  NewControl: TControl;
    138134  I: Integer;
    139135  TitleRect: TRect;
    140   DbRows: TDbRows;
    141136  W: Integer;
    142137  H: Integer;
     
    144139  W := 130;
    145140  H := 24;
    146   try
    147     DbRows := TDbRows.Create;
    148     Core.Database.Query(DbRows, 'SELECT * FROM ' + View.Name + ' WHERE Id=' +
    149       IntToStr(ItemId));
    150     if DbRows.Count > 0 then Values.Assign(DbRows[0])
    151       else begin
    152         Values.Free;
    153         Exit;
    154       end;
    155   finally
    156     DbRows.Free;
    157   end;
    158 
    159   RuntimeControls.Clear;
    160141  for I := 0 to View.Items.Count - 1 do
    161142  with TFormItem(View.Items[I]) do begin
     
    200181        (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    201182      NewControl.Show;
     183      Control := NewControl;
    202184    end else
    203185    if ControlType = ctEdit then begin
     
    209191       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    210192      NewControl.Show;
     193      Control := NewControl;
    211194    end else
    212195    if ControlType = ctMemo then begin
     
    217200       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    218201      NewControl.Show;
     202      Control := NewControl;
    219203    end else
    220204    if ControlType = ctCheckBox then begin
    221205      NewControl := TCheckBox.Create(Self);
    222206      NewControl.Parent := PanelControls;
    223       TCheckBox(NewControl).Enabled :=  Values.Values[Name] = '1';
    224       NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
    225        (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    226       NewControl.Show;
     207      TCheckBox(NewControl).Checked := Values.Values[Name] = '1';
     208      NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
     209       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
     210      NewControl.Show;
     211      Control := NewControl;
    227212    end else
    228213    if ControlType = ctComboBox then begin
     
    233218       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    234219      NewControl.Show;
     220      Control := NewControl;
    235221    end else
    236222    if ControlType = ctSpinEdit then begin
     
    241227       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    242228      NewControl.Show;
     229      Control := NewControl;
    243230    end else
    244231    if ControlType = ctDate then begin
     
    249236       (Rect.Right - Rect.Left) * W - TEditButton(NewControl).Button.Width, (Rect.Bottom - Rect.Top) * H);
    250237      NewControl.Show;
     238      Control := NewControl;
    251239    end else
    252240    if ControlType = ctTime then begin
    253       NewControl := TEdit.Create(Self);
    254       NewControl.Parent := PanelControls;
    255       TEdit(NewControl).Text := TimeToStr(SQLToTime(Values.Values[Name]));
    256       NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
    257        (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    258       NewControl.Show;
     241      NewControl := TTimeEdit.Create(Self);
     242      NewControl.Parent := PanelControls;
     243      TTimeEdit(NewControl).Time := SQLToTime(Values.Values[Name]);
     244      NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
     245       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
     246      NewControl.Show;
     247      Control := NewControl;
    259248    end else
    260249    if ControlType = ctReference then begin
     
    267256       (Rect.Bottom - Rect.Top) * H);
    268257      NewControl.Show;
     258      Control := NewControl;
     259    end;
     260  end;
     261end;
     262
     263procedure TFormEdit.ClearData;
     264var
     265  I: Integer;
     266begin
     267  Values.Clear;
     268  for I := 0 to View.Items.Count - 1 do
     269  with TFormItem(View.Items[I]) do begin
     270    case ControlType of
     271      ctSpinEdit, ctDate, ctTime, ctDateTime, ctReference: Values.Add(Name, '0');
     272      else Values.Add(Name, '');
     273    end;
     274  end;
     275  RebuildControls;
     276end;
     277
     278procedure TFormEdit.LoadFromDatabase;
     279var
     280  DbRows: TDbRows;
     281  Columns: string;
     282  I: Integer;
     283begin
     284  Columns := '';
     285  for I := 0 to View.Items.Count - 1 do
     286  with TFormItem(View.Items[I]) do
     287  if Visible then
     288    Columns := Columns + ', ' + Name;
     289  Delete(Columns, 1, 2);
     290  try
     291    DbRows := TDbRows.Create;
     292    Core.Database.Query(DbRows, 'SELECT ' + Columns + ' FROM `' + View.Name + '` WHERE `Id`=' +
     293      IntToStr(ItemId));
     294    if DbRows.Count > 0 then Values.Assign(DbRows[0])
     295      else raise Exception.Create(SItemNotFound);
     296  finally
     297    DbRows.Free;
     298  end;
     299  RebuildControls;
     300end;
     301
     302procedure TFormEdit.SaveToDatabase;
     303var
     304  DbRows: TDbRows;
     305begin
     306  LoadFromControls;
     307  try
     308    DbRows := TDbRows.Create;
     309    if ItemId < 1 then Core.Database.Insert(View.Name, Values)
     310      else Core.Database.Update(View.Name, Values, '`Id`=' + IntToStr(ItemId));
     311  finally
     312    DbRows.Free;
     313  end;
     314end;
     315
     316procedure TFormEdit.LoadFromControls;
     317var
     318  I: Integer;
     319begin
     320  for I := 0 to View.Items.Count - 1 do
     321  with TFormItem(View.Items[I]) do begin
     322    if ControlType = ctLabel then begin
     323      Values.Values[Name] := TLabel(Control).Caption;
     324    end else
     325    if ControlType = ctEdit then begin
     326      Values.Values[Name] := TEdit(Control).Text;
     327    end else
     328    if ControlType = ctMemo then begin
     329      Values.Values[Name] := TMemo(Control).Lines.Text;
     330    end else
     331    if ControlType = ctCheckBox then begin
     332      Values.Values[Name] := BoolToStr(TCheckBox(Control).Checked);
     333    end else
     334    if ControlType = ctComboBox then begin
     335      Values.Values[Name] := TComboBox(Control).Text;
     336    end else
     337    if ControlType = ctSpinEdit then begin
     338      Values.Values[Name] := IntToStr(TSpinEdit(Control).Value);
     339    end else
     340    if ControlType = ctDate then begin
     341      Values.Values[Name] := DateToSQL(TDateEdit(Control).Date);
     342    end else
     343    if ControlType = ctTime then begin
     344      Values.Values[Name] := TimeToSQL(TTimeEdit(Control).Time);
     345    end else
     346    if ControlType = ctReference then begin
     347      Values.Values[Name] := TEditButton(Control).Text;
    269348    end;
    270349  end;
  • trunk/Forms/UFormList.lfm

    r14 r16  
    3030    end
    3131    object ToolButton2: TToolButton
    32       Left = 49
     32      Left = 45
    3333      Top = 2
    3434      Action = ADelete
    3535    end
    3636    object ToolButton3: TToolButton
    37       Left = 97
     37      Left = 89
    3838      Top = 2
    3939      Action = ADuplicate
    4040    end
    4141    object ToolButton4: TToolButton
    42       Left = 149
     42      Left = 141
    4343      Top = 2
    4444      Action = AModify
    4545    end
    4646    object ToolButton5: TToolButton
    47       Left = 197
     47      Left = 185
    4848      Top = 2
    4949      Action = AReload
    5050    end
    5151    object ToolButton6: TToolButton
    52       Left = 245
     52      Left = 229
    5353      Top = 2
    5454      Action = APrint
    5555    end
    5656    object ToolButton7: TToolButton
    57       Left = 293
     57      Left = 273
    5858      Top = 2
    5959      Action = AExport
     
    128128      Hint = 'Reload data'
    129129      ImageIndex = 5
     130      OnExecute = AReloadExecute
    130131    end
    131132    object APrint: TAction
  • trunk/Forms/UFormList.pas

    r9 r16  
    6969    procedure ADuplicateExecute(Sender: TObject);
    7070    procedure AModifyExecute(Sender: TObject);
     71    procedure AReloadExecute(Sender: TObject);
    7172    procedure FormCreate(Sender: TObject);
    7273    procedure FormDestroy(Sender: TObject);
     
    99100  SDoYouWantToDeleteItem = 'Do you want to really delete item?';
    100101  SItemCount = 'Item count: %s';
     102  SModifyItem = 'Modify item';
     103  SAddItem = 'Add item';
    101104
    102105{ TDataViewList }
     
    149152  Index: Integer;
    150153begin
    151   Index := FormMain.DataViewLists.IndexOf(View);
    152   FormEdit := TFormEdit.Create(FormMain);
    153   FormEdit.ItemId := Integer(ListView1.Selected.Data);
    154   FormEdit.View := TDataViewForm(FormMain.DataViewForms[Index]);
    155   Core.CoolTranslator1.TranslateComponentRecursive(FormEdit);
    156   if FormEdit.ShowModal = mrOk then begin
    157   end;
    158   FormEdit.Free;
     154  try
     155    FormEdit := TFormEdit.Create(FormMain);
     156    Index := FormMain.DataViewLists.IndexOf(View);
     157    FormEdit.ItemId := Integer(ListView1.Selected.Data);
     158    FormEdit.View := TDataViewForm(FormMain.DataViewForms[Index]);
     159    FormEdit.LoadFromDatabase;
     160    FormEdit.Caption := SModifyItem;
     161    Core.CoolTranslator1.TranslateComponentRecursive(FormEdit);
     162    if FormEdit.ShowModal = mrOk then begin
     163      FormEdit.SaveToDatabase;
     164      UpdateData;
     165    end;
     166  finally
     167    FormEdit.Free;
     168  end;
     169end;
     170
     171procedure TFormList.AReloadExecute(Sender: TObject);
     172begin
     173  UpdateData;
    159174end;
    160175
    161176procedure TFormList.AAddExecute(Sender: TObject);
    162 begin
    163   if FormEdit.ShowModal = mrOk then begin
     177var
     178  Index: Integer;
     179begin
     180  try
     181    FormEdit := TFormEdit.Create(FormMain);
     182    Index := FormMain.DataViewLists.IndexOf(View);
     183    FormEdit.ItemId := 0;
     184    FormEdit.View := TDataViewForm(FormMain.DataViewForms[Index]);
     185    FormEdit.ClearData;
     186    FormEdit.Caption := SAddItem;
     187    Core.CoolTranslator1.TranslateComponentRecursive(FormEdit);
     188    if FormEdit.ShowModal = mrOk then begin
     189      FormEdit.SaveToDatabase;
     190      UpdateData;
     191    end;
     192  finally
     193    FormEdit.Free;
    164194  end;
    165195end;
     
    167197procedure TFormList.ADeleteExecute(Sender: TObject);
    168198begin
    169   if FormEdit.ShowModal = mrOk then begin
    170     if MessageDlg(SItemDeletion, SDoYouWantToDeleteItem, mtConfirmation,
    171       [mbYes, mbNo], 0) = mrYes then begin
    172       Core.Database.Query(nil, 'DELETE FROM `' + View.Name +
    173         '` WHERE `Id` = ' + IntToStr(Integer(ListView1.Selected.Data)));
    174     end;
     199  if MessageDlg(SItemDeletion, SDoYouWantToDeleteItem, mtConfirmation,
     200    [mbYes, mbNo], 0) = mrYes then begin
     201    Core.Database.Delete(View.Name, '`Id` = ' + IntToStr(Integer(ListView1.Selected.Data)));
     202    UpdateData;
    175203  end;
    176204end;
     
    200228    for I := 0 to View.Columns.Count - 1 do begin
    201229      Item.Data := Pointer(StrToInt(DbRows[Item.Index].Values['Id']));
    202       if I = 0 then Item.Caption := DbRows[Item.Index].Items[0].Value
     230      if I = 0 then Item.Caption := DbRows[Item.Index].Values[TDbColumn(View.Columns[I]).Name]
    203231        else Item.SubItems.Add(DbRows[Item.Index].Values[TDbColumn(View.Columns[I]).Name]);
    204232    end;
Note: See TracChangeset for help on using the changeset viewer.