Ignore:
Timestamp:
Nov 14, 2012, 4:08:53 PM (12 years ago)
Author:
chronos
Message:
  • Upraveno: Zprovozněno zobrazování formulářového pohledu pro uživatele.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormEdit.pas

    r8 r9  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    9   Buttons, ExtCtrls, SpecializedList, SpecializedDictionary;
     9  Buttons, ExtCtrls, Spin, SpecializedList, SpecializedDictionary, UDataView,
     10  USqlDatabase;
    1011
    1112type
    1213  TControlType = (ctLabel, ctCheckBox, ctEdit, ctMemo, ctDate, ctTime,
    13     ctComboBox);
     14    ctComboBox, ctSpinEdit, ctReference);
    1415
    1516  TFormItem = class
     
    2627  TFormItems = class(TListObject)
    2728    function AddItem(Caption, Name: string; ControlType: TControlType;
    28       Visible: Boolean; Rect: TRect): TFormItem;
     29      Visible: Boolean; Rect: TRect; TextPlacement: TAlign = alLeft): TFormItem;
     30  end;
     31
     32  { TDataViewForm }
     33
     34  TDataViewForm = class(TDataView)
     35    Name: string;
     36    Caption: string;
     37    ImageIndex: Integer;
     38    Items: TFormItems;
     39    constructor Create;
     40    destructor Destroy; override;
    2941  end;
    3042
     
    3547    ButtonOk: TButton;
    3648    ButtonCancel: TButton;
     49    PanelControls: TPanel;
    3750    procedure FormCreate(Sender: TObject);
    3851    procedure FormDestroy(Sender: TObject);
    3952  private
     53    FView: TDataViewForm;
     54    procedure SetView(AValue: TDataViewForm);
    4055    { private declarations }
    4156  public
    4257    RuntimeControls: TListObject;
    43     FormItems: TFormItems;
    4458    Values: TDictionaryStringString;
    45     procedure Update;
     59    ItemId: Integer;
     60    property View: TDataViewForm read FView write SetView;
     61    procedure UpdateData;
    4662  end;
    4763
     
    5167implementation
    5268
     69uses
     70  UCore;
     71
     72{ TDataViewForm }
     73
     74constructor TDataViewForm.Create;
     75begin
     76  Items := TFormItems.Create;
     77end;
     78
     79destructor TDataViewForm.Destroy;
     80begin
     81  Items.Free;
     82  inherited Destroy;
     83end;
     84
    5385{ TFormItems }
    5486
    5587function TFormItems.AddItem(Caption, Name: string; ControlType: TControlType;
    56   Visible: Boolean; Rect: TRect): TFormItem;
     88  Visible: Boolean; Rect: TRect; TextPlacement: TAlign = alLeft): TFormItem;
    5789begin
    5890  Result := TFormItem.Create;
     
    6294  Result.Rect := Rect;
    6395  Result.ControlType := ControlType;
     96  Result.TitlePlacement := TextPlacement;
    6497  Add(Result);
    6598end;
     
    71104procedure TFormEdit.FormCreate(Sender: TObject);
    72105begin
    73   FormItems := TFormItems.Create;
    74106  Values := TDictionaryStringString.Create;
    75107  RuntimeControls := TListObject.Create;
     
    80112  Values.Free;
    81113  RuntimeControls.Free;
    82   FormItems.Free;
    83 end;
    84 
    85 procedure TFormEdit.Update;
     114end;
     115
     116procedure TFormEdit.SetView(AValue: TDataViewForm);
     117begin
     118  if FView=AValue then Exit;
     119  FView := AValue;
     120  if Assigned(AValue) then UpdateData
     121    else begin
     122      Values.Clear;
     123      RuntimeControls.Clear;
     124    end;
     125end;
     126
     127procedure TFormEdit.UpdateData;
    86128var
    87129  NewControl: TControl;
    88130  I: Integer;
    89131  TitleRect: TRect;
    90 const
    91   W = 50;
    92   H = 32;
    93 begin
     132  DbRows: TDbRows;
     133  W: Integer;
     134  H: Integer;
     135begin
     136  W := 130;
     137  H := 24;
     138  try
     139    DbRows := TDbRows.Create;
     140    Core.Database.Query(DbRows, 'SELECT * FROM ' + View.Name + ' WHERE Id=' +
     141      IntToStr(ItemId));
     142    if DbRows.Count > 0 then Values.Assign(DbRows[0])
     143      else begin
     144        Values.Free;
     145        Exit;
     146      end;
     147  finally
     148    DbRows.Free;
     149  end;
     150
    94151  RuntimeControls.Clear;
    95   for I := 0 to FormItems.Count - 1 do
    96   with TFormItem(FormItems[I]) do begin
     152  for I := 0 to View.Items.Count - 1 do
     153  with TFormItem(View.Items[I]) do begin
    97154    if TitlePlacement <> alNone then begin
    98155      NewControl := TLabel.Create(Self);
    99       NewControl.Parent := Self;
     156      NewControl.Parent := PanelControls;
    100157      NewControl.Caption := Caption;
     158      NewControl.AutoSize := False;
    101159      TitleRect := Rect;
    102160      case TitlePlacement of
    103         alLeft: TitleRect.Left := TitleRect.Left - 1;
    104         alRight: TitleRect.Left := TitleRect.Left + 1;
    105         alTop: TitleRect.Top := TitleRect.Top - 1;
    106         alBottom: TitleRect.Top := TitleRect.Top + 1;
     161        alLeft: begin
     162          TitleRect.Left := TitleRect.Left - 1;
     163          TitleRect.Right := TitleRect.Right - 1;
     164          TLabel(NewControl).Alignment := taRightJustify;
     165        end;
     166        alRight: begin
     167          TitleRect.Left := TitleRect.Left + 1;
     168          TitleRect.Right := TitleRect.Right + 1;
     169          TLabel(NewControl).Alignment := taLeftJustify;
     170        end;
     171        alTop: begin
     172          TitleRect.Top := TitleRect.Top - 1;
     173          TitleRect.Bottom := TitleRect.Bottom - 1;
     174          TLabel(NewControl).Alignment := taLeftJustify;
     175        end;
     176        alBottom: begin
     177          TitleRect.Top := TitleRect.Top + 1;
     178          TitleRect.Bottom := TitleRect.Bottom + 1;
     179          TLabel(NewControl).Alignment := taLeftJustify;
     180        end;
    107181      end;
    108       NewControl.SetBounds(TitleRect.Left * W, TitleRect.Top * H,
    109         (TitleRect.Right - TitleRect.Left) * W, (TitleRect.Bottom - TitleRect.Top) * H);
     182      NewControl.SetBounds(TitleRect.Left * W + 4, TitleRect.Top * H + 2,
     183        (TitleRect.Right - TitleRect.Left) * W - 8,
     184        (TitleRect.Bottom - TitleRect.Top) * H - 4);
    110185      NewControl.Show;
    111186    end;
    112187    if ControlType = ctLabel then begin
    113188      NewControl := TLabel.Create(Self);
    114       NewControl.Parent := Self;
     189      NewControl.Parent := PanelControls;
    115190      NewControl.Caption := Values.Values[Name];
    116191      NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
     
    122197      NewControl := TEdit.Create(Self);
    123198      TEdit(NewControl).Text := Values.Values[Name];
    124       NewControl.Parent := Self;
    125       NewControl.SetBounds((Rect.Left + 1) * W, Rect.Top * H,
    126        (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    127       NewControl.Show;
    128     end else
    129     if ControlType = ctEdit then begin
     199      NewControl.Parent := PanelControls;
     200      NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
     201       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
     202      NewControl.Show;
     203    end else
     204    if ControlType = ctMemo then begin
    130205      NewControl := TMemo.Create(Self);
    131       NewControl.Parent := Self;
     206      NewControl.Parent := PanelControls;
    132207      TMemo(NewControl).Lines.Text := Values.Values[Name];
    133       NewControl.SetBounds((Rect.Left + 1) * W, Rect.Top * H,
     208      NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
    134209       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    135210      NewControl.Show;
     
    137212    if ControlType = ctCheckBox then begin
    138213      NewControl := TCheckBox.Create(Self);
    139       NewControl.Parent := Self;
     214      NewControl.Parent := PanelControls;
    140215      TCheckBox(NewControl).Enabled :=  Values.Values[Name] = '1';
    141       NewControl.SetBounds((Rect.Left + 1) * W, Rect.Top * H,
     216      NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
    142217       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    143218      NewControl.Show;
     
    145220    if ControlType = ctComboBox then begin
    146221      NewControl := TComboBox.Create(Self);
    147       NewControl.Parent := Self;
     222      NewControl.Parent := PanelControls;
    148223      TComboBox(NewControl).Text := Values.Values[Name];
    149       NewControl.SetBounds((Rect.Left + 1) * W, Rect.Top * H,
     224      NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
     225       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
     226      NewControl.Show;
     227    end else
     228    if ControlType = ctSpinEdit then begin
     229      NewControl := TSpinEdit.Create(Self);
     230      NewControl.Parent := PanelControls;
     231      TSpinEdit(NewControl).Value := StrToInt(Values.Values[Name]);
     232      NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
    150233       (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
    151234      NewControl.Show;
Note: See TracChangeset for help on using the changeset viewer.