Changeset 16 for trunk/Forms/UFormEdit.pas
- Timestamp:
- Nov 15, 2012, 2:01:43 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormEdit.pas
r15 r16 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 9 Buttons, ExtCtrls, Spin, ComCtrls, EditBtn, SpecializedList, 10 SpecializedDictionary, UDataView, USqlDatabase ;10 SpecializedDictionary, UDataView, USqlDatabase, UTimeEdit; 11 11 12 12 type … … 19 19 Visible: Boolean; 20 20 Rect: TRect; 21 Control: TControl; // Used by TFormEdit 21 22 ControlType: TControlType; 22 23 TitlePlacement: TAlign; … … 48 49 ButtonOk: TButton; 49 50 ButtonCancel: TButton; 50 CalcEdit1: TCalcEdit;51 51 PanelControls: TPanel; 52 52 procedure FormCreate(Sender: TObject); 53 53 procedure FormDestroy(Sender: TObject); 54 procedure SpeedButton1Click(Sender: TObject);55 54 private 56 55 FView: TDataViewForm; 57 56 procedure SetView(AValue: TDataViewForm); 58 { private declarations }57 procedure RebuildControls; 59 58 public 60 RuntimeControls: TListObject;61 59 Values: TDictionaryStringString; 62 60 ItemId: Integer; 63 61 property View: TDataViewForm read FView write SetView; 64 procedure UpdateData; 62 procedure ClearData; 63 procedure LoadFromDatabase; 64 procedure SaveToDatabase; 65 procedure LoadFromControls; 65 66 end; 66 67 … … 72 73 uses 73 74 UCore, UFormMain; 75 76 resourcestring 77 SItemNotFound = 'Item not found'; 74 78 75 79 { TDataViewForm } … … 108 112 begin 109 113 Values := TDictionaryStringString.Create; 110 RuntimeControls := TListObject.Create;111 114 end; 112 115 113 116 procedure TFormEdit.FormDestroy(Sender: TObject); 114 117 begin 115 Values.Free; 116 RuntimeControls.Free; 117 end; 118 119 procedure TFormEdit.SpeedButton1Click(Sender: TObject); 120 begin 121 118 FreeAndNil(Values); 122 119 end; 123 120 … … 126 123 if FView=AValue then Exit; 127 124 FView := AValue; 128 if Assigned(AValue) then UpdateData125 if Assigned(AValue) then ClearData 129 126 else begin 130 127 Values.Clear; 131 RuntimeControls.Clear; 132 end; 133 end; 134 135 procedure TFormEdit.UpdateData; 128 end; 129 end; 130 131 procedure TFormEdit.RebuildControls; 136 132 var 137 133 NewControl: TControl; 138 134 I: Integer; 139 135 TitleRect: TRect; 140 DbRows: TDbRows;141 136 W: Integer; 142 137 H: Integer; … … 144 139 W := 130; 145 140 H := 24; 146 try147 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 begin152 Values.Free;153 Exit;154 end;155 finally156 DbRows.Free;157 end;158 159 RuntimeControls.Clear;160 141 for I := 0 to View.Items.Count - 1 do 161 142 with TFormItem(View.Items[I]) do begin … … 200 181 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H); 201 182 NewControl.Show; 183 Control := NewControl; 202 184 end else 203 185 if ControlType = ctEdit then begin … … 209 191 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H); 210 192 NewControl.Show; 193 Control := NewControl; 211 194 end else 212 195 if ControlType = ctMemo then begin … … 217 200 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H); 218 201 NewControl.Show; 202 Control := NewControl; 219 203 end else 220 204 if ControlType = ctCheckBox then begin 221 205 NewControl := TCheckBox.Create(Self); 222 206 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; 227 212 end else 228 213 if ControlType = ctComboBox then begin … … 233 218 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H); 234 219 NewControl.Show; 220 Control := NewControl; 235 221 end else 236 222 if ControlType = ctSpinEdit then begin … … 241 227 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H); 242 228 NewControl.Show; 229 Control := NewControl; 243 230 end else 244 231 if ControlType = ctDate then begin … … 249 236 (Rect.Right - Rect.Left) * W - TEditButton(NewControl).Button.Width, (Rect.Bottom - Rect.Top) * H); 250 237 NewControl.Show; 238 Control := NewControl; 251 239 end else 252 240 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; 259 248 end else 260 249 if ControlType = ctReference then begin … … 267 256 (Rect.Bottom - Rect.Top) * H); 268 257 NewControl.Show; 258 Control := NewControl; 259 end; 260 end; 261 end; 262 263 procedure TFormEdit.ClearData; 264 var 265 I: Integer; 266 begin 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; 276 end; 277 278 procedure TFormEdit.LoadFromDatabase; 279 var 280 DbRows: TDbRows; 281 Columns: string; 282 I: Integer; 283 begin 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; 300 end; 301 302 procedure TFormEdit.SaveToDatabase; 303 var 304 DbRows: TDbRows; 305 begin 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; 314 end; 315 316 procedure TFormEdit.LoadFromControls; 317 var 318 I: Integer; 319 begin 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; 269 348 end; 270 349 end;
Note:
See TracChangeset
for help on using the changeset viewer.