Changeset 9 for trunk/Forms/UFormEdit.pas
- Timestamp:
- Nov 14, 2012, 4:08:53 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormEdit.pas
r8 r9 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 Buttons, ExtCtrls, SpecializedList, SpecializedDictionary; 9 Buttons, ExtCtrls, Spin, SpecializedList, SpecializedDictionary, UDataView, 10 USqlDatabase; 10 11 11 12 type 12 13 TControlType = (ctLabel, ctCheckBox, ctEdit, ctMemo, ctDate, ctTime, 13 ctComboBox );14 ctComboBox, ctSpinEdit, ctReference); 14 15 15 16 TFormItem = class … … 26 27 TFormItems = class(TListObject) 27 28 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; 29 41 end; 30 42 … … 35 47 ButtonOk: TButton; 36 48 ButtonCancel: TButton; 49 PanelControls: TPanel; 37 50 procedure FormCreate(Sender: TObject); 38 51 procedure FormDestroy(Sender: TObject); 39 52 private 53 FView: TDataViewForm; 54 procedure SetView(AValue: TDataViewForm); 40 55 { private declarations } 41 56 public 42 57 RuntimeControls: TListObject; 43 FormItems: TFormItems;44 58 Values: TDictionaryStringString; 45 procedure Update; 59 ItemId: Integer; 60 property View: TDataViewForm read FView write SetView; 61 procedure UpdateData; 46 62 end; 47 63 … … 51 67 implementation 52 68 69 uses 70 UCore; 71 72 { TDataViewForm } 73 74 constructor TDataViewForm.Create; 75 begin 76 Items := TFormItems.Create; 77 end; 78 79 destructor TDataViewForm.Destroy; 80 begin 81 Items.Free; 82 inherited Destroy; 83 end; 84 53 85 { TFormItems } 54 86 55 87 function TFormItems.AddItem(Caption, Name: string; ControlType: TControlType; 56 Visible: Boolean; Rect: TRect ): TFormItem;88 Visible: Boolean; Rect: TRect; TextPlacement: TAlign = alLeft): TFormItem; 57 89 begin 58 90 Result := TFormItem.Create; … … 62 94 Result.Rect := Rect; 63 95 Result.ControlType := ControlType; 96 Result.TitlePlacement := TextPlacement; 64 97 Add(Result); 65 98 end; … … 71 104 procedure TFormEdit.FormCreate(Sender: TObject); 72 105 begin 73 FormItems := TFormItems.Create;74 106 Values := TDictionaryStringString.Create; 75 107 RuntimeControls := TListObject.Create; … … 80 112 Values.Free; 81 113 RuntimeControls.Free; 82 FormItems.Free; 83 end; 84 85 procedure TFormEdit.Update; 114 end; 115 116 procedure TFormEdit.SetView(AValue: TDataViewForm); 117 begin 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; 125 end; 126 127 procedure TFormEdit.UpdateData; 86 128 var 87 129 NewControl: TControl; 88 130 I: Integer; 89 131 TitleRect: TRect; 90 const 91 W = 50; 92 H = 32; 93 begin 132 DbRows: TDbRows; 133 W: Integer; 134 H: Integer; 135 begin 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 94 151 RuntimeControls.Clear; 95 for I := 0 to FormItems.Count - 1 do96 with TFormItem( FormItems[I]) do begin152 for I := 0 to View.Items.Count - 1 do 153 with TFormItem(View.Items[I]) do begin 97 154 if TitlePlacement <> alNone then begin 98 155 NewControl := TLabel.Create(Self); 99 NewControl.Parent := Self;156 NewControl.Parent := PanelControls; 100 157 NewControl.Caption := Caption; 158 NewControl.AutoSize := False; 101 159 TitleRect := Rect; 102 160 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; 107 181 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); 110 185 NewControl.Show; 111 186 end; 112 187 if ControlType = ctLabel then begin 113 188 NewControl := TLabel.Create(Self); 114 NewControl.Parent := Self;189 NewControl.Parent := PanelControls; 115 190 NewControl.Caption := Values.Values[Name]; 116 191 NewControl.SetBounds(Rect.Left * W, Rect.Top * H, … … 122 197 NewControl := TEdit.Create(Self); 123 198 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 = ct Editthen begin199 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 130 205 NewControl := TMemo.Create(Self); 131 NewControl.Parent := Self;206 NewControl.Parent := PanelControls; 132 207 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, 134 209 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H); 135 210 NewControl.Show; … … 137 212 if ControlType = ctCheckBox then begin 138 213 NewControl := TCheckBox.Create(Self); 139 NewControl.Parent := Self;214 NewControl.Parent := PanelControls; 140 215 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, 142 217 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H); 143 218 NewControl.Show; … … 145 220 if ControlType = ctComboBox then begin 146 221 NewControl := TComboBox.Create(Self); 147 NewControl.Parent := Self;222 NewControl.Parent := PanelControls; 148 223 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, 150 233 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H); 151 234 NewControl.Show;
Note:
See TracChangeset
for help on using the changeset viewer.