1 | unit UFormRecord;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
|
---|
7 | ComCtrls, ActnList, StdCtrls, EditBtn, UDatabase, Spin, Generics.Collections;
|
---|
8 |
|
---|
9 | type
|
---|
10 |
|
---|
11 | { TFormRecord }
|
---|
12 |
|
---|
13 | TFormRecord = class(TForm)
|
---|
14 | ASave: TAction;
|
---|
15 | ACancel: TAction;
|
---|
16 | ActionList1: TActionList;
|
---|
17 | ButtonOk: TButton;
|
---|
18 | ButtonCancel: TButton;
|
---|
19 | Panel1: TPanel;
|
---|
20 | procedure ACancelExecute(Sender: TObject);
|
---|
21 | procedure ASaveExecute(Sender: TObject);
|
---|
22 | procedure FormCreate(Sender: TObject);
|
---|
23 | procedure FormDestroy(Sender: TObject);
|
---|
24 | procedure FormShow(Sender: TObject);
|
---|
25 | public
|
---|
26 | Table: TTable;
|
---|
27 | Row: TRecord;
|
---|
28 | Controls: TObjectList<TControl>;
|
---|
29 | Labels: TObjectList<TControl>;
|
---|
30 | procedure ReloadControls;
|
---|
31 | procedure Load(DataRecord: TRecord);
|
---|
32 | procedure Save(DataRecord: TRecord);
|
---|
33 | end;
|
---|
34 |
|
---|
35 |
|
---|
36 | implementation
|
---|
37 |
|
---|
38 | uses
|
---|
39 | UDataTypes, UCore;
|
---|
40 |
|
---|
41 | {$R *.lfm}
|
---|
42 |
|
---|
43 | resourcestring
|
---|
44 | STableRecordEdit = 'Table record edit';
|
---|
45 |
|
---|
46 | { TFormRecord }
|
---|
47 |
|
---|
48 | procedure TFormRecord.FormShow(Sender: TObject);
|
---|
49 | begin
|
---|
50 | Caption := STableRecordEdit + ' - ' + Table.Caption;
|
---|
51 | ReloadControls;
|
---|
52 | end;
|
---|
53 |
|
---|
54 | procedure TFormRecord.ACancelExecute(Sender: TObject);
|
---|
55 | begin
|
---|
56 | ModalResult := mrCancel;
|
---|
57 | end;
|
---|
58 |
|
---|
59 | procedure TFormRecord.ASaveExecute(Sender: TObject);
|
---|
60 | begin
|
---|
61 | ModalResult := mrOk;
|
---|
62 | end;
|
---|
63 |
|
---|
64 | procedure TFormRecord.FormCreate(Sender: TObject);
|
---|
65 | begin
|
---|
66 | Controls := TObjectList<TControl>.Create;
|
---|
67 | Labels := TObjectList<TControl>.Create;
|
---|
68 | end;
|
---|
69 |
|
---|
70 | procedure TFormRecord.FormDestroy(Sender: TObject);
|
---|
71 | begin
|
---|
72 | FreeAndNil(Labels);
|
---|
73 | FreeAndNil(Controls);
|
---|
74 | end;
|
---|
75 |
|
---|
76 | procedure TFormRecord.ReloadControls;
|
---|
77 | begin
|
---|
78 | end;
|
---|
79 |
|
---|
80 | procedure TFormRecord.Load(DataRecord: TRecord);
|
---|
81 | var
|
---|
82 | I: Integer;
|
---|
83 | NewLabel: TLabel;
|
---|
84 | NewControl: TControl;
|
---|
85 | CellRect: TRect;
|
---|
86 | const
|
---|
87 | LabelWidth = 300;
|
---|
88 | var
|
---|
89 | LineHeight: Integer;
|
---|
90 | begin
|
---|
91 | LineHeight := Core.ScaleDPI1.ScaleY(35, Core.ScaleDPI1.DesignDPI.Y);
|
---|
92 |
|
---|
93 | Row := DataRecord;
|
---|
94 | Controls.Clear;
|
---|
95 | Labels.Clear;
|
---|
96 | for I := 0 to Table.Fields.Count - 1 do begin
|
---|
97 | CellRect := Rect(10, 10 + I * LineHeight, Panel1.Width - 20, (I + 1) * LineHeight);
|
---|
98 | NewLabel := TLabel.Create(Panel1);
|
---|
99 | NewLabel.Caption := Table.Fields[I].TextBefore;
|
---|
100 | NewLabel.Parent := Panel1;
|
---|
101 | NewLabel.Left := CellRect.Left;
|
---|
102 | NewLabel.Top := CellRect.Top;
|
---|
103 | NewLabel.Visible := True;
|
---|
104 | Labels.Add(NewLabel);
|
---|
105 | case Table.Fields[I].DataType.FieldType of
|
---|
106 | ftString: begin
|
---|
107 | NewControl := TEdit.Create(Panel1);
|
---|
108 | NewControl.Anchors := [akLeft, akTop, akRight];
|
---|
109 | NewControl.Parent := Panel1;
|
---|
110 | NewControl.Left := CellRect.Left + LabelWidth;
|
---|
111 | NewControl.Top := CellRect.Top;
|
---|
112 | NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
|
---|
113 | NewControl.Visible := True;
|
---|
114 | TEdit(NewControl).Text := TValueString(Row.Values[I]).Value;
|
---|
115 | Controls.Add(NewControl);
|
---|
116 | end;
|
---|
117 | ftInteger: begin
|
---|
118 | NewControl := TSpinEdit.Create(Panel1);
|
---|
119 | NewControl.Parent := Panel1;
|
---|
120 | NewControl.Left := CellRect.Left + LabelWidth;
|
---|
121 | NewControl.Top := CellRect.Top;
|
---|
122 | NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
|
---|
123 | NewControl.Visible := True;
|
---|
124 | TSpinEdit(NewControl).Value := TValueInteger(Row.Values[I]).Value;
|
---|
125 | Controls.Add(NewControl);
|
---|
126 | end;
|
---|
127 | ftDateTime: begin
|
---|
128 | NewControl := TDateEdit.Create(Panel1);
|
---|
129 | NewControl.Parent := Panel1;
|
---|
130 | NewControl.Left := CellRect.Left + LabelWidth;
|
---|
131 | NewControl.Top := CellRect.Top;
|
---|
132 | NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
|
---|
133 | NewControl.Visible := True;
|
---|
134 | TDateEdit(NewControl).Date := TValueDateTime(Row.Values[I]).Value;
|
---|
135 | Controls.Add(NewControl);
|
---|
136 | end;
|
---|
137 | ftBoolean: begin
|
---|
138 | NewControl := TCheckBox.Create(Panel1);
|
---|
139 | NewControl.Parent := Panel1;
|
---|
140 | NewControl.Left := CellRect.Left + LabelWidth;
|
---|
141 | NewControl.Top := CellRect.Top;
|
---|
142 | NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
|
---|
143 | NewControl.Visible := True;
|
---|
144 | TCheckBox(NewControl).Checked := TValueBoolean(Row.Values[I]).Value;
|
---|
145 | Controls.Add(NewControl);
|
---|
146 | end;
|
---|
147 | ftFloat: begin
|
---|
148 | NewControl := TFloatSpinEdit.Create(Panel1);
|
---|
149 | NewControl.Parent := Panel1;
|
---|
150 | NewControl.Left := CellRect.Left + LabelWidth;
|
---|
151 | NewControl.Top := CellRect.Top;
|
---|
152 | NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
|
---|
153 | NewControl.Visible := True;
|
---|
154 | TFloatSpinEdit(NewControl).Value := TValueFloat(Row.Values[I]).Value;
|
---|
155 | Controls.Add(NewControl);
|
---|
156 | end;
|
---|
157 | ftImage: begin
|
---|
158 | NewControl := TImage.Create(Panel1);
|
---|
159 | NewControl.Parent := Panel1;
|
---|
160 | NewControl.Left := CellRect.Left + LabelWidth;
|
---|
161 | NewControl.Top := CellRect.Top;
|
---|
162 | NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
|
---|
163 | NewControl.Height := NewLabel.Height;
|
---|
164 | NewControl.Visible := True;
|
---|
165 | //TImage(NewControl).Value := TValueFloat(Row.Values[I]).Value;
|
---|
166 | Controls.Add(NewControl);
|
---|
167 | end;
|
---|
168 | ftReference: begin
|
---|
169 | NewControl := TComboBox.Create(Panel1);
|
---|
170 | NewControl.Parent := Panel1;
|
---|
171 | NewControl.Left := CellRect.Left + LabelWidth;
|
---|
172 | NewControl.Top := CellRect.Top;
|
---|
173 | NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
|
---|
174 | NewControl.Height := NewLabel.Height;
|
---|
175 | NewControl.Visible := True;
|
---|
176 |
|
---|
177 | TComboBox(NewControl).Items.Add(IntToStr(TValueReference(Row.Values[I]).Value));
|
---|
178 | Controls.Add(NewControl);
|
---|
179 | end;
|
---|
180 | end;
|
---|
181 | end;
|
---|
182 | end;
|
---|
183 |
|
---|
184 | procedure TFormRecord.Save(DataRecord: TRecord);
|
---|
185 | var
|
---|
186 | I: Integer;
|
---|
187 | begin
|
---|
188 | for I := 0 to Table.Fields.Count - 1 do begin
|
---|
189 | case Table.Fields[I].DataType.FieldType of
|
---|
190 | ftString: TValueString(Row.Values[I]).Value := TEdit(Controls[I]).Text;
|
---|
191 | ftInteger: TValueInteger(Row.Values[I]).Value := TSpinEdit(Controls[I]).Value;
|
---|
192 | ftDateTime: TValueDateTime(Row.Values[I]).Value := TDateEdit(Controls[I]).Date;
|
---|
193 | ftBoolean: TValueBoolean(Row.Values[I]).Value := TCheckBox(Controls[I]).Checked;
|
---|
194 | ftFloat: TValueFloat(Row.Values[I]).Value := TFloatSpinEdit(Controls[I]).Value;
|
---|
195 | //ftImage: TValueFloat(Row.Values[I]).Value := TFloatSpinEdit(Controls[I]).Value;
|
---|
196 | end;
|
---|
197 | end;
|
---|
198 | end;
|
---|
199 |
|
---|
200 | end.
|
---|
201 |
|
---|