1 | unit UFormItemView;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
---|
9 | ExtCtrls, ComCtrls, USqlDatabase, USystem, SpecializedList;
|
---|
10 |
|
---|
11 | type
|
---|
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 |
|
---|
48 | var
|
---|
49 | ItemViewForm: TItemViewForm;
|
---|
50 |
|
---|
51 | implementation
|
---|
52 |
|
---|
53 | uses
|
---|
54 | UFormMain, UFormItemEdit, UDataTypes, UCore;
|
---|
55 |
|
---|
56 | {$R *.lfm}
|
---|
57 |
|
---|
58 | resourcestring
|
---|
59 | SExpectedOneRow = 'Expected one row';
|
---|
60 |
|
---|
61 | { TItemViewForm }
|
---|
62 |
|
---|
63 | procedure TItemViewForm.FormClose(Sender: TObject; var CloseAction: TCloseAction
|
---|
64 | );
|
---|
65 | begin
|
---|
66 | MainForm.PersistentForm.Save(Self);
|
---|
67 | end;
|
---|
68 |
|
---|
69 | procedure TItemViewForm.FormCreate(Sender: TObject);
|
---|
70 | begin
|
---|
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;
|
---|
81 | end;
|
---|
82 |
|
---|
83 | procedure TItemViewForm.FormDestroy(Sender: TObject);
|
---|
84 | begin
|
---|
85 | SubListObject.Free;
|
---|
86 | Report.Free;
|
---|
87 | TabObjectList.Free;
|
---|
88 | TabDataTypeList.Free;
|
---|
89 | SelectedObject.Free;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | procedure TItemViewForm.FormResize(Sender: TObject);
|
---|
93 | begin
|
---|
94 | ListView1Resize(Self);
|
---|
95 | end;
|
---|
96 |
|
---|
97 | procedure TItemViewForm.ButtonCancelClick(Sender: TObject);
|
---|
98 | begin
|
---|
99 | Close;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TItemViewForm.ButtonCloseClick(Sender: TObject);
|
---|
103 | begin
|
---|
104 | Close;
|
---|
105 | end;
|
---|
106 |
|
---|
107 | procedure TItemViewForm.ButtonEditClick(Sender: TObject);
|
---|
108 | var
|
---|
109 | NewItemEditForm: TItemEditForm;
|
---|
110 | begin
|
---|
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;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | procedure TItemViewForm.FormShow(Sender: TObject);
|
---|
123 | begin
|
---|
124 | MainForm.PersistentForm.Load(Self);
|
---|
125 | BuildControls;
|
---|
126 | LoadSubList;
|
---|
127 | end;
|
---|
128 |
|
---|
129 | procedure TItemViewForm.ListView1Data(Sender: TObject; Item: TListItem);
|
---|
130 | var
|
---|
131 | I: Integer;
|
---|
132 | begin
|
---|
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;
|
---|
140 | end;
|
---|
141 |
|
---|
142 | procedure TItemViewForm.BuildControls;
|
---|
143 | var
|
---|
144 | NewControl: TControl;
|
---|
145 | LastTop: Integer;
|
---|
146 | I: Integer;
|
---|
147 | Column: Integer;
|
---|
148 | DataType: TDataType;
|
---|
149 | const
|
---|
150 | ColumnCount = 2;
|
---|
151 | begin
|
---|
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;
|
---|
191 | end;
|
---|
192 |
|
---|
193 | procedure TItemViewForm.LoadSubList;
|
---|
194 | begin
|
---|
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);
|
---|
208 | end;
|
---|
209 |
|
---|
210 | procedure TItemViewForm.ListView1Resize(Sender: TObject);
|
---|
211 | var
|
---|
212 | I: Integer;
|
---|
213 | begin
|
---|
214 | for I := 0 to ListView1.Columns.Count - 1 do
|
---|
215 | ListView1.Columns[I].Width := ListView1.Width div ListView1.Columns.Count;
|
---|
216 | end;
|
---|
217 |
|
---|
218 | procedure TItemViewForm.TabControl1Change(Sender: TObject);
|
---|
219 | begin
|
---|
220 | LoadSubList;
|
---|
221 | end;
|
---|
222 |
|
---|
223 |
|
---|
224 | end.
|
---|
225 |
|
---|