source: trunk/Packages/ChronisPlatform/Forms/UFormEdit.pas

Last change on this file was 5, checked in by chronos, 12 years ago
  • Added: Required packages.
File size: 10.5 KB
Line 
1unit UFormEdit;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 Buttons, ExtCtrls, Spin, ComCtrls, EditBtn, SpecializedList,
10 SpecializedDictionary, UDataView, USqlDatabase, UTimeEdit;
11
12type
13 TControlType = (ctLabel, ctCheckBox, ctEdit, ctMemo, ctDate, ctTime, ctDateTime,
14 ctComboBox, ctSpinEdit, ctReference);
15
16 TFormItem = class
17 Caption: string;
18 Name: string;
19 Visible: Boolean;
20 Rect: TRect;
21 Control: TControl; // Used by TFormEdit
22 ControlType: TControlType;
23 TitlePlacement: TAlign;
24 ReferencedTable: string;
25 end;
26
27 { TFormItems }
28
29 TFormItems = class(TListObject)
30 function AddItem(Caption, Name: string; ControlType: TControlType;
31 Visible: Boolean; Rect: TRect; TextPlacement: TAlign = alLeft): TFormItem;
32 end;
33
34 { TDataViewForm }
35
36 TDataViewForm = class(TDataView)
37 Name: string;
38 Caption: string;
39 ImageIndex: Integer;
40 Items: TFormItems;
41 constructor Create;
42 destructor Destroy; override;
43 end;
44
45 { TFormEdit }
46
47 TFormEdit = class(TForm)
48 Bevel1: TBevel;
49 ButtonOk: TButton;
50 ButtonCancel: TButton;
51 PanelControls: TPanel;
52 procedure FormCreate(Sender: TObject);
53 procedure FormDestroy(Sender: TObject);
54 private
55 FView: TDataViewForm;
56 procedure SetView(AValue: TDataViewForm);
57 procedure RebuildControls;
58 public
59 Values: TDictionaryStringString;
60 ItemId: Integer;
61 property View: TDataViewForm read FView write SetView;
62 procedure ClearData;
63 procedure LoadFromDatabase;
64 procedure SaveToDatabase;
65 procedure LoadFromControls;
66 end;
67
68var
69 FormEdit: TFormEdit;
70
71implementation
72
73uses
74 UCore, UFormMain;
75
76resourcestring
77 SItemNotFound = 'Item not found';
78
79{ TDataViewForm }
80
81constructor TDataViewForm.Create;
82begin
83 Items := TFormItems.Create;
84end;
85
86destructor TDataViewForm.Destroy;
87begin
88 Items.Free;
89 inherited Destroy;
90end;
91
92{ TFormItems }
93
94function TFormItems.AddItem(Caption, Name: string; ControlType: TControlType;
95 Visible: Boolean; Rect: TRect; TextPlacement: TAlign = alLeft): TFormItem;
96begin
97 Result := TFormItem.Create;
98 Result.Caption := Caption;
99 Result.Name := Name;
100 Result.Visible := Visible;
101 Result.Rect := Rect;
102 Result.ControlType := ControlType;
103 Result.TitlePlacement := TextPlacement;
104 Add(Result);
105end;
106
107{$R *.lfm}
108
109{ TFormEdit }
110
111procedure TFormEdit.FormCreate(Sender: TObject);
112begin
113 Values := TDictionaryStringString.Create;
114end;
115
116procedure TFormEdit.FormDestroy(Sender: TObject);
117begin
118 FreeAndNil(Values);
119end;
120
121procedure TFormEdit.SetView(AValue: TDataViewForm);
122begin
123 if FView=AValue then Exit;
124 FView := AValue;
125 if Assigned(AValue) then ClearData
126 else begin
127 Values.Clear;
128 end;
129end;
130
131procedure TFormEdit.RebuildControls;
132var
133 NewControl: TControl;
134 I: Integer;
135 TitleRect: TRect;
136 W: Integer;
137 H: Integer;
138begin
139 W := 130;
140 H := 24;
141 for I := PanelControls.ControlCount - 1 downto 0 do
142 PanelControls.Controls[I].Free;
143 for I := 0 to View.Items.Count - 1 do
144 with TFormItem(View.Items[I]) do begin
145 if TitlePlacement <> alNone then begin
146 NewControl := TLabel.Create(Self);
147 NewControl.Parent := PanelControls;
148 NewControl.Caption := Caption;
149 NewControl.AutoSize := False;
150 TitleRect := Rect;
151 case TitlePlacement of
152 alLeft: begin
153 TitleRect.Left := TitleRect.Left - 1;
154 TitleRect.Right := TitleRect.Right - 1;
155 TLabel(NewControl).Alignment := taRightJustify;
156 end;
157 alRight: begin
158 TitleRect.Left := TitleRect.Left + 1;
159 TitleRect.Right := TitleRect.Right + 1;
160 TLabel(NewControl).Alignment := taLeftJustify;
161 end;
162 alTop: begin
163 TitleRect.Top := TitleRect.Top - 1;
164 TitleRect.Bottom := TitleRect.Bottom - 1;
165 TLabel(NewControl).Alignment := taLeftJustify;
166 end;
167 alBottom: begin
168 TitleRect.Top := TitleRect.Top + 1;
169 TitleRect.Bottom := TitleRect.Bottom + 1;
170 TLabel(NewControl).Alignment := taLeftJustify;
171 end;
172 end;
173 NewControl.SetBounds(TitleRect.Left * W + 4, TitleRect.Top * H + 2,
174 (TitleRect.Right - TitleRect.Left) * W - 8,
175 (TitleRect.Bottom - TitleRect.Top) * H - 4);
176 NewControl.Show;
177 end;
178 if ControlType = ctLabel then begin
179 NewControl := TLabel.Create(Self);
180 NewControl.Parent := PanelControls;
181 NewControl.Caption := Values.Values[Name];
182 NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
183 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
184 NewControl.Show;
185 Control := NewControl;
186 end else
187 if ControlType = ctEdit then begin
188 NewControl.Show;
189 NewControl := TEdit.Create(Self);
190 TEdit(NewControl).Text := Values.Values[Name];
191 NewControl.Parent := PanelControls;
192 NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
193 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
194 NewControl.Show;
195 Control := NewControl;
196 end else
197 if ControlType = ctMemo then begin
198 NewControl := TMemo.Create(Self);
199 NewControl.Parent := PanelControls;
200 TMemo(NewControl).Lines.Text := Values.Values[Name];
201 NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
202 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
203 NewControl.Show;
204 Control := NewControl;
205 end else
206 if ControlType = ctCheckBox then begin
207 NewControl := TCheckBox.Create(Self);
208 NewControl.Parent := PanelControls;
209 TCheckBox(NewControl).Checked := Values.Values[Name] = '1';
210 NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
211 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
212 NewControl.Show;
213 Control := NewControl;
214 end else
215 if ControlType = ctComboBox then begin
216 NewControl := TComboBox.Create(Self);
217 NewControl.Parent := PanelControls;
218 TComboBox(NewControl).Text := Values.Values[Name];
219 NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
220 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
221 NewControl.Show;
222 Control := NewControl;
223 end else
224 if ControlType = ctSpinEdit then begin
225 NewControl := TSpinEdit.Create(Self);
226 NewControl.Parent := PanelControls;
227 TSpinEdit(NewControl).Value := StrToInt(Values.Values[Name]);
228 NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
229 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
230 NewControl.Show;
231 Control := NewControl;
232 end else
233 if ControlType = ctDate then begin
234 NewControl := TDateEdit.Create(Self);
235 NewControl.Parent := PanelControls;
236 TDateEdit(NewControl).Date := SQLToDate(Values.Values[Name]);
237 NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
238 (Rect.Right - Rect.Left) * W - TEditButton(NewControl).Button.Width, (Rect.Bottom - Rect.Top) * H);
239 NewControl.Show;
240 Control := NewControl;
241 end else
242 if ControlType = ctTime then begin
243 NewControl := TTimeEdit.Create(Self);
244 NewControl.Parent := PanelControls;
245 TTimeEdit(NewControl).Time := SQLToTime(Values.Values[Name]);
246 NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
247 (Rect.Right - Rect.Left) * W, (Rect.Bottom - Rect.Top) * H);
248 NewControl.Show;
249 Control := NewControl;
250 end else
251 if ControlType = ctReference then begin
252 NewControl := TEditButton.Create(Self);
253 NewControl.Parent := PanelControls;
254 TEditButton(NewControl).Text := Values.Values[Name];
255 FormMain.ImageList1.GetBitmap(4, TEditButton(NewControl).Glyph);
256 NewControl.SetBounds(Rect.Left * W, Rect.Top * H,
257 (Rect.Right - Rect.Left) * W - TEditButton(NewControl).Button.Width,
258 (Rect.Bottom - Rect.Top) * H);
259 NewControl.Show;
260 Control := NewControl;
261 end;
262 end;
263end;
264
265procedure TFormEdit.ClearData;
266var
267 I: Integer;
268begin
269 Values.Clear;
270 for I := 0 to View.Items.Count - 1 do
271 with TFormItem(View.Items[I]) do begin
272 case ControlType of
273 ctCheckBox, ctSpinEdit, ctReference: Values.Add(Name, '0');
274 ctDateTime: Values.Add(Name, '0000-00-00 00:00:00');
275 ctDate: Values.Add(Name, '0000-00-00');
276 ctTime: Values.Add(Name, '00:00:00');
277 else Values.Add(Name, '');
278 end;
279 end;
280 RebuildControls;
281end;
282
283procedure TFormEdit.LoadFromDatabase;
284var
285 DbRows: TDbRows;
286 Columns: string;
287 I: Integer;
288begin
289 Columns := '';
290 for I := 0 to View.Items.Count - 1 do
291 with TFormItem(View.Items[I]) do
292 if Visible then
293 Columns := Columns + ', ' + Name;
294 Delete(Columns, 1, 2);
295 try
296 DbRows := TDbRows.Create;
297 Core.Database.Query(DbRows, 'SELECT ' + Columns + ' FROM `' + View.Name + '` WHERE `Id`=' +
298 IntToStr(ItemId));
299 if DbRows.Count > 0 then Values.Assign(DbRows[0])
300 else raise Exception.Create(SItemNotFound);
301 finally
302 DbRows.Free;
303 end;
304 RebuildControls;
305end;
306
307procedure TFormEdit.SaveToDatabase;
308var
309 DbRows: TDbRows;
310begin
311 LoadFromControls;
312 try
313 DbRows := TDbRows.Create;
314 if ItemId < 1 then Core.Database.Insert(View.Name, Values)
315 else Core.Database.Update(View.Name, Values, '`Id`=' + IntToStr(ItemId));
316 finally
317 DbRows.Free;
318 end;
319end;
320
321procedure TFormEdit.LoadFromControls;
322var
323 I: Integer;
324begin
325 for I := 0 to View.Items.Count - 1 do
326 with TFormItem(View.Items[I]) do begin
327 if ControlType = ctLabel then begin
328 Values.Values[Name] := TLabel(Control).Caption;
329 end else
330 if ControlType = ctEdit then begin
331 Values.Values[Name] := TEdit(Control).Text;
332 end else
333 if ControlType = ctMemo then begin
334 Values.Values[Name] := TMemo(Control).Lines.Text;
335 end else
336 if ControlType = ctCheckBox then begin
337 Values.Values[Name] := BoolToStr(TCheckBox(Control).Checked);
338 end else
339 if ControlType = ctComboBox then begin
340 Values.Values[Name] := TComboBox(Control).Text;
341 end else
342 if ControlType = ctSpinEdit then begin
343 Values.Values[Name] := IntToStr(TSpinEdit(Control).Value);
344 end else
345 if ControlType = ctDate then begin
346 Values.Values[Name] := DateToSQL(TDateEdit(Control).Date);
347 end else
348 if ControlType = ctTime then begin
349 Values.Values[Name] := TimeToSQL(TTimeEdit(Control).Time);
350 end else
351 if ControlType = ctReference then begin
352 Values.Values[Name] := TEditButton(Control).Text;
353 end;
354 end;
355end;
356
357end.
358
Note: See TracBrowser for help on using the repository browser.