source: branches/test1/Client/Forms/UFormItemView.pas

Last change on this file was 46, checked in by chronos, 13 years ago
File size: 5.8 KB
Line 
1unit UFormItemView;
2
3{$mode objfpc}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 ExtCtrls, ComCtrls, USqlDatabase, USystem, SpecializedList;
10
11type
12
13 { TItemViewForm }
14
15 TItemViewForm = class(TForm)
16 ButtonClose: TButton;
17 ButtonEdit: TButton;
18 ListView1: TListView;
19 Panel1: TPanel;
20 Panel2: TPanel;
21 PanelControls: TPanel;
22 Splitter1: TSplitter;
23 TabControl1: TTabControl;
24 procedure ButtonCancelClick(Sender: TObject);
25 procedure ButtonCloseClick(Sender: TObject);
26 procedure ButtonEditClick(Sender: TObject);
27 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
28 procedure FormCreate(Sender: TObject);
29 procedure FormDestroy(Sender: TObject);
30 procedure FormResize(Sender: TObject);
31 procedure FormShow(Sender: TObject);
32 procedure ListView1Data(Sender: TObject; Item: TListItem);
33 procedure ListView1Resize(Sender: TObject);
34 procedure TabControl1Change(Sender: TObject);
35 private
36 SubListObject: TChronisObject;
37 TabObjectList: TListInteger;
38 TabDataTypeList: TListObject;
39 procedure LoadSubList;
40 { private declarations }
41 public
42 SelectedObject: TChronisObject;
43 SelectedItemId: Integer;
44 Report: TReport;
45 procedure BuildControls;
46 end;
47
48var
49 ItemViewForm: TItemViewForm;
50
51implementation
52
53uses
54 UFormMain, UFormItemEdit, UDataTypes, UCore;
55
56{$R *.lfm}
57
58resourcestring
59 SExpectedOneRow = 'Expected one row';
60
61{ TItemViewForm }
62
63procedure TItemViewForm.FormClose(Sender: TObject; var CloseAction: TCloseAction
64 );
65begin
66 MainForm.PersistentForm.Save(Self);
67end;
68
69procedure TItemViewForm.FormCreate(Sender: TObject);
70begin
71 SubListObject := TChronisObject.Create;
72 SubListObject.Base := Core.System;
73 Report := TReport.Create;
74 Report.Base := Core.System;
75 TabObjectList := TListInteger.Create;
76 TabDataTypeList := TListObject.Create;
77 TabDataTypeList.OwnsObjects := False;
78 TabControl1.Tabs.Clear;
79 SelectedObject := TChronisObject.Create;
80 SelectedObject.Base := Core.System;
81end;
82
83procedure TItemViewForm.FormDestroy(Sender: TObject);
84begin
85 SubListObject.Free;
86 Report.Free;
87 TabObjectList.Free;
88 TabDataTypeList.Free;
89 SelectedObject.Free;
90end;
91
92procedure TItemViewForm.FormResize(Sender: TObject);
93begin
94 ListView1Resize(Self);
95end;
96
97procedure TItemViewForm.ButtonCancelClick(Sender: TObject);
98begin
99 Close;
100end;
101
102procedure TItemViewForm.ButtonCloseClick(Sender: TObject);
103begin
104 Close;
105end;
106
107procedure TItemViewForm.ButtonEditClick(Sender: TObject);
108var
109 NewItemEditForm: TItemEditForm;
110begin
111 try
112 NewItemEditForm := TItemEditForm.Create(nil);
113 NewItemEditForm.SelectedObject.Assign(SelectedObject);
114 NewItemEditForm.SelectedItemId := SelectedItemId;
115 NewItemEditForm.Caption := NewItemEditForm.Caption + ' ' + SelectedObject.Name;
116 NewItemEditForm.ShowModal;
117 finally
118 NewItemEditForm.Free;
119 end;
120end;
121
122procedure TItemViewForm.FormShow(Sender: TObject);
123begin
124 MainForm.PersistentForm.Load(Self);
125 BuildControls;
126 LoadSubList;
127end;
128
129procedure TItemViewForm.ListView1Data(Sender: TObject; Item: TListItem);
130var
131 I: Integer;
132begin
133 if Item.Index < Report.Count then
134 with TReportLine(Report[Item.Index]) do begin
135 Item.Caption := Items[0];
136 for I := 1 to Items.Count - 1 do
137 if not TReportColumn(Report.Columns[I]).VirtualItem then
138 Item.SubItems.Add(Items[I]);
139 end;
140end;
141
142procedure TItemViewForm.BuildControls;
143var
144 NewControl: TControl;
145 LastTop: Integer;
146 I: Integer;
147 Column: Integer;
148 DataType: TDataType;
149const
150 ColumnCount = 2;
151begin
152 LastTop := 8;
153 Column := 0;
154 Report.Load(SelectedObject, SelectedObject.PrimaryKey + ' = ' +
155 IntToStr(SelectedItemId));
156 for I := PanelControls.ControlCount - 1 downto 0 do
157 PanelControls.Controls[I].Free;
158
159 TabControl1.Tabs.Clear;
160 TabObjectList.Clear;
161 TabDataTypeList.Clear;
162 // Load column names
163
164 if Report.Count > 1 then raise Exception.Create(SExpectedOneRow);
165 if Report.Count > 0 then
166 for I := 0 to Report.Columns.Count - 1 do begin
167 DataType := TReportColumn(Report.Columns[I]).CustomType;
168 DataType.LoadDef(TReportColumn(Report.Columns[I]).CustomType.CustomType);
169 if DataType is TDataTypeRelationMany then begin
170 TabControl1.Tabs.Add(TReportColumn(Report.Columns[I]).Caption);
171 TabDataTypeList.Add(DataType);
172 TabObjectList.Add(SelectedItemId);
173 end else begin
174 NewControl := TLabel.Create(PanelControls);
175 NewControl.Parent := PanelControls;
176 NewControl.Top := LastTop;
177 NewControl.Left := Column * Width div ColumnCount + 10;
178 TLabel(NewControl).Caption := TReportColumn(Report.Columns[I]).Caption + ':';
179
180 NewControl := TLabel.Create(PanelControls);
181 NewControl.Parent := PanelControls;
182 NewControl.Top := LastTop;
183 NewControl.Left := Column * Width div ColumnCount + (Width div ColumnCount) div 2;
184 TLabel(NewControl).Caption := TReportLine(Report[0]).Items[I];
185
186 Column := (Column + 1) mod 2;
187 if Column = 0 then LastTop := LastTop + 24;
188 end;
189 end;
190 Panel2.Visible := TabControl1.Tabs.Count > 0;
191end;
192
193procedure TItemViewForm.LoadSubList;
194begin
195 //SelectedObjectId := 0;
196 if (TabControl1.TabIndex >= 0) and (TabControl1.TabIndex < TabControl1.Tabs.Count) then
197 with ListView1 do
198 with TDataTypeRelationMany(TabDataTypeList[TabControl1.TabIndex]) do begin
199 Visible := True;
200 SubListObject.Load(ObjectId);
201 Report.Load(SubListObject, '`' + PropertyName + '`=' + IntToStr(TabObjectList[TabControl1.TabIndex]));
202
203 Report.FillListColumns(Columns);
204 Items.Count := Report.Count;
205 Refresh;
206 end;
207 ListView1Resize(Self);
208end;
209
210procedure TItemViewForm.ListView1Resize(Sender: TObject);
211var
212 I: Integer;
213begin
214 for I := 0 to ListView1.Columns.Count - 1 do
215 ListView1.Columns[I].Width := ListView1.Width div ListView1.Columns.Count;
216end;
217
218procedure TItemViewForm.TabControl1Change(Sender: TObject);
219begin
220 LoadSubList;
221end;
222
223
224end.
225
Note: See TracBrowser for help on using the repository browser.