source: trunk/Forms/UFormList.pas

Last change on this file was 60, checked in by chronos, 12 years ago
File size: 7.1 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;
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 ToolBar1: TToolBar;
64 ToolButton1: TToolButton;
65 ToolButton2: TToolButton;
66 ToolButton3: TToolButton;
67 ToolButton4: TToolButton;
68 ToolButton5: TToolButton;
69 ToolButton6: TToolButton;
70 ToolButton7: TToolButton;
71 procedure AAddExecute(Sender: TObject);
72 procedure ADeleteExecute(Sender: TObject);
73 procedure ADuplicateExecute(Sender: TObject);
74 procedure AModifyExecute(Sender: TObject);
75 procedure AReloadExecute(Sender: TObject);
76 procedure FormCreate(Sender: TObject);
77 procedure FormDestroy(Sender: TObject);
78 procedure FormShow(Sender: TObject);
79 procedure ListView1Data(Sender: TObject; Item: TListItem);
80 procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
81 Selected: Boolean);
82 private
83 FView: TDataViewList;
84 procedure SetView(AValue: TDataViewList);
85 public
86 DbRows: TDbRows;
87 property View: TDataViewList read FView write SetView;
88 procedure UpdateInterface;
89 procedure UpdateData;
90 end;
91
92var
93 FormList: TFormList;
94
95implementation
96
97{$R *.lfm}
98
99uses
100 UCore, UFormEdit, UFormMain;
101
102resourcestring
103 SItemDeletion = 'Item deletion';
104 SDoYouWantToDeleteItem = 'Do you want to really delete item?';
105 SItemCount = 'Item count: %s';
106 SModifyItem = 'Modify item';
107 SAddItem = 'Add item';
108
109{ TDataViewList }
110
111constructor TDataViewList.Create;
112begin
113 Columns := TListDbColumn.Create;
114end;
115
116destructor TDataViewList.Destroy;
117begin
118 Columns.Free;
119 inherited Destroy;
120end;
121
122procedure TDataViewList.LoadFromRegistry(Context: TRegistryContext);
123begin
124
125end;
126
127procedure TDataViewList.SaveToRegistry(Context: TRegistryContext);
128begin
129
130end;
131
132{ TListColumns }
133
134function TListDbColumn.AddItem(Caption, Name: string; Visible: Boolean;
135 Width: Integer): TDbColumn;
136begin
137 Result := TDbColumn.Create;
138 Result.Caption := Caption;
139 Result.Name := Name;
140 Result.Visible := Visible;
141 Result.Width := Width;
142 Add(Result);
143end;
144
145{ TFormList }
146
147procedure TFormList.FormCreate(Sender: TObject);
148begin
149 DbRows := TDbRows.Create;
150end;
151
152procedure TFormList.AModifyExecute(Sender: TObject);
153var
154 Index: Integer;
155begin
156 try
157 FormEdit := TFormEdit.Create(FormMain);
158 Index := FormMain.DataViewLists.IndexOf(View);
159 FormEdit.ItemId := Integer(ListView1.Selected.Data);
160 FormEdit.View := TDataViewForm(FormMain.DataViewForms[Index]);
161 FormEdit.LoadFromDatabase;
162 FormEdit.Caption := SModifyItem;
163 Core.CoolTranslator1.TranslateComponentRecursive(FormEdit);
164 if FormEdit.ShowModal = mrOk then begin
165 FormEdit.SaveToDatabase;
166 UpdateData;
167 end;
168 finally
169 FormEdit.Free;
170 end;
171end;
172
173procedure TFormList.AReloadExecute(Sender: TObject);
174begin
175 UpdateData;
176end;
177
178procedure TFormList.AAddExecute(Sender: TObject);
179var
180 Index: Integer;
181begin
182 try
183 FormEdit := TFormEdit.Create(FormMain);
184 Index := FormMain.DataViewLists.IndexOf(View);
185 FormEdit.ItemId := 0;
186 FormEdit.View := TDataViewForm(FormMain.DataViewForms[Index]);
187 FormEdit.ClearData;
188 FormEdit.Caption := SAddItem;
189 Core.CoolTranslator1.TranslateComponentRecursive(FormEdit);
190 if FormEdit.ShowModal = mrOk then begin
191 FormEdit.SaveToDatabase;
192 UpdateData;
193 end;
194 finally
195 FormEdit.Free;
196 end;
197end;
198
199procedure TFormList.ADeleteExecute(Sender: TObject);
200begin
201 if MessageDlg(SItemDeletion, SDoYouWantToDeleteItem, mtConfirmation,
202 [mbYes, mbNo], 0) = mrYes then begin
203 Core.Database.Delete(View.Name, '`Id` = ' + IntToStr(Integer(ListView1.Selected.Data)));
204 UpdateData;
205 end;
206end;
207
208procedure TFormList.ADuplicateExecute(Sender: TObject);
209begin
210 if FormEdit.ShowModal = mrOk then begin
211 end;
212end;
213
214procedure TFormList.FormDestroy(Sender: TObject);
215begin
216 ListView1.Items.Count := 0;
217 FreeAndNil(DbRows);
218end;
219
220procedure TFormList.FormShow(Sender: TObject);
221begin
222 UpdateInterface;
223end;
224
225procedure TFormList.ListView1Data(Sender: TObject; Item: TListItem);
226var
227 I: Integer;
228begin
229 if (Item.Index >= 0) and (Item.Index < DbRows.Count) then begin
230 for I := 0 to View.Columns.Count - 1 do begin
231 Item.Data := Pointer(StrToInt(DbRows[Item.Index].Values['Id']));
232 if I = 0 then Item.Caption := DbRows[Item.Index].Values[TDbColumn(View.Columns[I]).Name]
233 else Item.SubItems.Add(DbRows[Item.Index].Values[TDbColumn(View.Columns[I]).Name]);
234 end;
235 end;
236end;
237
238procedure TFormList.ListView1SelectItem(Sender: TObject; Item: TListItem;
239 Selected: Boolean);
240begin
241 UpdateInterface;
242end;
243
244procedure TFormList.SetView(AValue: TDataViewList);
245begin
246 if FView = AValue then Exit;
247 if Assigned(AValue) then begin
248 end else begin
249 DbRows.Clear;
250 ListView1.Items.Clear;
251 end;
252 FView := AValue;
253end;
254
255procedure TFormList.UpdateInterface;
256begin
257 AAdd.Enabled := True;
258 AModify.Enabled := Assigned(ListView1.Selected);
259 ADelete.Enabled := Assigned(ListView1.Selected);
260 ADuplicate.Enabled := Assigned(ListView1.Selected);
261end;
262
263procedure TFormList.UpdateData;
264var
265 NewColumn: TListColumn;
266 I: Integer;
267 Filter: string;
268begin
269 ListView1.Columns.Clear;
270 Filter := 'Id';
271 for I := 0 to View.Columns.Count - 1 do
272 with TDbColumn(View.Columns[I]) do begin
273 NewColumn := ListView1.Columns.Add;
274 NewColumn.Caption := Caption;
275 NewColumn.Width := Width;
276 NewColumn.Visible := Visible;
277 Filter := Filter + ', `' + Name + '`';
278 end;
279 if Core.Database.Connected then
280 Core.Database.Query(DbRows, 'SELECT ' + Filter + ' FROM ' + View.Name);
281
282 ListView1.Items.Count := DbRows.Count;
283 ListView1.Refresh;
284
285 LabelItemCount.Caption := Format(SItemCount, [IntToStr(DbRows.Count)]);
286end;
287
288end.
289
Note: See TracBrowser for help on using the repository browser.