source: trunk/Forms/UFormList.pas

Last change on this file was 24, checked in by chronos, 12 years ago
  • Opraveno: Ladění komunikačního protokolu s terminálem BF-630.
File size: 7.4 KB
Line 
1unit UFormList;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
9 ActnList, Menus, StdCtrls, ExtCtrls, SpecializedList, USqlDatabase,
10 UListViewSort, UDataView, URegistry, UPrintPreview;
11
12type
13 TColumnControlType = (ctText, ctReference, ctBoolean);
14
15 TDbColumn = class
16 Caption: string;
17 Name: string;
18 Visible: Boolean;
19 Width: Integer;
20 ControlType: TColumnControlType;
21 ReferencedTable: string;
22 end;
23
24 { TListColumns }
25
26 TListDbColumn = class(TListObject)
27 function AddItem(Caption, Name: string; Visible: Boolean; Width: Integer): TDbColumn;
28 end;
29
30 { TDataViewList }
31
32 TDataViewList = class(TDataView)
33 Caption: string;
34 Name: string;
35 ImageIndex: Integer;
36 Columns: TListDbColumn;
37 constructor Create;
38 destructor Destroy; override;
39 procedure LoadFromRegistry(Context: TRegistryContext);
40 procedure SaveToRegistry(Context: TRegistryContext);
41 end;
42
43 { TFormList }
44
45 TFormList = class(TForm)
46 AAdd: TAction;
47 APrint: TAction;
48 AExport: TAction;
49 AReload: TAction;
50 AModify: TAction;
51 ADelete: TAction;
52 ADuplicate: TAction;
53 ActionList1: TActionList;
54 LabelItemCount: TLabel;
55 ListView1: TListView;
56 MenuItem1: TMenuItem;
57 MenuItem2: TMenuItem;
58 MenuItem3: TMenuItem;
59 MenuItem4: TMenuItem;
60 MenuItem5: TMenuItem;
61 Panel1: TPanel;
62 PopupMenu1: TPopupMenu;
63 PrintPreview1: TPrintPreview;
64 ToolBar1: TToolBar;
65 ToolButton1: TToolButton;
66 ToolButton2: TToolButton;
67 ToolButton3: TToolButton;
68 ToolButton4: TToolButton;
69 ToolButton5: TToolButton;
70 ToolButton6: TToolButton;
71 ToolButton7: TToolButton;
72 procedure AAddExecute(Sender: TObject);
73 procedure ADeleteExecute(Sender: TObject);
74 procedure ADuplicateExecute(Sender: TObject);
75 procedure AModifyExecute(Sender: TObject);
76 procedure APrintExecute(Sender: TObject);
77 procedure AReloadExecute(Sender: TObject);
78 procedure FormCreate(Sender: TObject);
79 procedure FormDestroy(Sender: TObject);
80 procedure FormShow(Sender: TObject);
81 procedure ListView1Data(Sender: TObject; Item: TListItem);
82 procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
83 Selected: Boolean);
84 procedure PrintPreview1Print(Sender: TObject);
85 private
86 FView: TDataViewList;
87 procedure SetView(AValue: TDataViewList);
88 public
89 DbRows: TDbRows;
90 property View: TDataViewList read FView write SetView;
91 procedure UpdateInterface;
92 procedure UpdateData;
93 end;
94
95var
96 FormList: TFormList;
97
98implementation
99
100{$R *.lfm}
101
102uses
103 UCore, UFormEdit, UFormMain;
104
105resourcestring
106 SItemDeletion = 'Item deletion';
107 SDoYouWantToDeleteItem = 'Do you want to really delete item?';
108 SItemCount = 'Item count: %s';
109 SModifyItem = 'Modify item';
110 SAddItem = 'Add item';
111
112{ TDataViewList }
113
114constructor TDataViewList.Create;
115begin
116 Columns := TListDbColumn.Create;
117end;
118
119destructor TDataViewList.Destroy;
120begin
121 Columns.Free;
122 inherited Destroy;
123end;
124
125procedure TDataViewList.LoadFromRegistry(Context: TRegistryContext);
126begin
127
128end;
129
130procedure TDataViewList.SaveToRegistry(Context: TRegistryContext);
131begin
132
133end;
134
135{ TListColumns }
136
137function TListDbColumn.AddItem(Caption, Name: string; Visible: Boolean;
138 Width: Integer): TDbColumn;
139begin
140 Result := TDbColumn.Create;
141 Result.Caption := Caption;
142 Result.Name := Name;
143 Result.Visible := Visible;
144 Result.Width := Width;
145 Add(Result);
146end;
147
148{ TFormList }
149
150procedure TFormList.FormCreate(Sender: TObject);
151begin
152 DbRows := TDbRows.Create;
153end;
154
155procedure TFormList.AModifyExecute(Sender: TObject);
156var
157 Index: Integer;
158begin
159 try
160 FormEdit := TFormEdit.Create(FormMain);
161 Index := FormMain.DataViewLists.IndexOf(View);
162 FormEdit.ItemId := Integer(ListView1.Selected.Data);
163 FormEdit.View := TDataViewForm(FormMain.DataViewForms[Index]);
164 FormEdit.LoadFromDatabase;
165 FormEdit.Caption := SModifyItem;
166 Core.CoolTranslator1.TranslateComponentRecursive(FormEdit);
167 if FormEdit.ShowModal = mrOk then begin
168 FormEdit.SaveToDatabase;
169 UpdateData;
170 end;
171 finally
172 FormEdit.Free;
173 end;
174end;
175
176procedure TFormList.APrintExecute(Sender: TObject);
177begin
178 PrintPreview1.Preview;
179end;
180
181procedure TFormList.AReloadExecute(Sender: TObject);
182begin
183 UpdateData;
184end;
185
186procedure TFormList.AAddExecute(Sender: TObject);
187var
188 Index: Integer;
189begin
190 try
191 FormEdit := TFormEdit.Create(FormMain);
192 Index := FormMain.DataViewLists.IndexOf(View);
193 FormEdit.ItemId := 0;
194 FormEdit.View := TDataViewForm(FormMain.DataViewForms[Index]);
195 FormEdit.ClearData;
196 FormEdit.Caption := SAddItem;
197 Core.CoolTranslator1.TranslateComponentRecursive(FormEdit);
198 if FormEdit.ShowModal = mrOk then begin
199 FormEdit.SaveToDatabase;
200 UpdateData;
201 end;
202 finally
203 FormEdit.Free;
204 end;
205end;
206
207procedure TFormList.ADeleteExecute(Sender: TObject);
208begin
209 if MessageDlg(SItemDeletion, SDoYouWantToDeleteItem, mtConfirmation,
210 [mbYes, mbNo], 0) = mrYes then begin
211 Core.Database.Delete(View.Name, '`Id` = ' + IntToStr(Integer(ListView1.Selected.Data)));
212 UpdateData;
213 end;
214end;
215
216procedure TFormList.ADuplicateExecute(Sender: TObject);
217begin
218 if FormEdit.ShowModal = mrOk then begin
219 end;
220end;
221
222procedure TFormList.FormDestroy(Sender: TObject);
223begin
224 ListView1.Items.Count := 0;
225 FreeAndNil(DbRows);
226end;
227
228procedure TFormList.FormShow(Sender: TObject);
229begin
230 UpdateInterface;
231end;
232
233procedure TFormList.ListView1Data(Sender: TObject; Item: TListItem);
234var
235 I: Integer;
236begin
237 if (Item.Index >= 0) and (Item.Index < DbRows.Count) then begin
238 for I := 0 to View.Columns.Count - 1 do begin
239 Item.Data := Pointer(StrToInt(DbRows[Item.Index].Values['Id']));
240 if I = 0 then Item.Caption := DbRows[Item.Index].Values[TDbColumn(View.Columns[I]).Name]
241 else Item.SubItems.Add(DbRows[Item.Index].Values[TDbColumn(View.Columns[I]).Name]);
242 end;
243 end;
244end;
245
246procedure TFormList.ListView1SelectItem(Sender: TObject; Item: TListItem;
247 Selected: Boolean);
248begin
249 UpdateInterface;
250end;
251
252procedure TFormList.PrintPreview1Print(Sender: TObject);
253begin
254
255end;
256
257procedure TFormList.SetView(AValue: TDataViewList);
258begin
259 if FView = AValue then Exit;
260 if Assigned(AValue) then begin
261 end else begin
262 DbRows.Clear;
263 ListView1.Items.Clear;
264 end;
265 FView := AValue;
266end;
267
268procedure TFormList.UpdateInterface;
269begin
270 AAdd.Enabled := True;
271 AModify.Enabled := Assigned(ListView1.Selected);
272 ADelete.Enabled := Assigned(ListView1.Selected);
273 ADuplicate.Enabled := Assigned(ListView1.Selected);
274end;
275
276procedure TFormList.UpdateData;
277var
278 NewColumn: TListColumn;
279 I: Integer;
280 Filter: string;
281begin
282 ListView1.Columns.Clear;
283 Filter := 'Id';
284 for I := 0 to View.Columns.Count - 1 do
285 with TDbColumn(View.Columns[I]) do begin
286 NewColumn := ListView1.Columns.Add;
287 NewColumn.Caption := Caption;
288 NewColumn.Width := Width;
289 NewColumn.Visible := Visible;
290 Filter := Filter + ', `' + Name + '`';
291 end;
292 if Core.Database.Connected then
293 Core.Database.Query(DbRows, 'SELECT ' + Filter + ' FROM ' + View.Name);
294
295 ListView1.Items.Count := DbRows.Count;
296 ListView1.Refresh;
297
298 LabelItemCount.Caption := Format(SItemCount, [IntToStr(DbRows.Count)]);
299end;
300
301end.
302
Note: See TracBrowser for help on using the repository browser.