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í.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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;
Note: See TracChangeset for help on using the changeset viewer.