1 | unit UFormList;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
---|
9 | ActnList, Menus, StdCtrls, ExtCtrls, SpecializedList, USqlDatabase,
|
---|
10 | UListViewSort, UDataView, URegistry;
|
---|
11 |
|
---|
12 | type
|
---|
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 |
|
---|
92 | var
|
---|
93 | FormList: TFormList;
|
---|
94 |
|
---|
95 | implementation
|
---|
96 |
|
---|
97 | {$R *.lfm}
|
---|
98 |
|
---|
99 | uses
|
---|
100 | UCore, UFormEdit, UFormMain;
|
---|
101 |
|
---|
102 | resourcestring
|
---|
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 |
|
---|
111 | constructor TDataViewList.Create;
|
---|
112 | begin
|
---|
113 | Columns := TListDbColumn.Create;
|
---|
114 | end;
|
---|
115 |
|
---|
116 | destructor TDataViewList.Destroy;
|
---|
117 | begin
|
---|
118 | Columns.Free;
|
---|
119 | inherited Destroy;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | procedure TDataViewList.LoadFromRegistry(Context: TRegistryContext);
|
---|
123 | begin
|
---|
124 |
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TDataViewList.SaveToRegistry(Context: TRegistryContext);
|
---|
128 | begin
|
---|
129 |
|
---|
130 | end;
|
---|
131 |
|
---|
132 | { TListColumns }
|
---|
133 |
|
---|
134 | function TListDbColumn.AddItem(Caption, Name: string; Visible: Boolean;
|
---|
135 | Width: Integer): TDbColumn;
|
---|
136 | begin
|
---|
137 | Result := TDbColumn.Create;
|
---|
138 | Result.Caption := Caption;
|
---|
139 | Result.Name := Name;
|
---|
140 | Result.Visible := Visible;
|
---|
141 | Result.Width := Width;
|
---|
142 | Add(Result);
|
---|
143 | end;
|
---|
144 |
|
---|
145 | { TFormList }
|
---|
146 |
|
---|
147 | procedure TFormList.FormCreate(Sender: TObject);
|
---|
148 | begin
|
---|
149 | DbRows := TDbRows.Create;
|
---|
150 | end;
|
---|
151 |
|
---|
152 | procedure TFormList.AModifyExecute(Sender: TObject);
|
---|
153 | var
|
---|
154 | Index: Integer;
|
---|
155 | begin
|
---|
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;
|
---|
171 | end;
|
---|
172 |
|
---|
173 | procedure TFormList.AReloadExecute(Sender: TObject);
|
---|
174 | begin
|
---|
175 | UpdateData;
|
---|
176 | end;
|
---|
177 |
|
---|
178 | procedure TFormList.AAddExecute(Sender: TObject);
|
---|
179 | var
|
---|
180 | Index: Integer;
|
---|
181 | begin
|
---|
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;
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TFormList.ADeleteExecute(Sender: TObject);
|
---|
200 | begin
|
---|
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;
|
---|
206 | end;
|
---|
207 |
|
---|
208 | procedure TFormList.ADuplicateExecute(Sender: TObject);
|
---|
209 | begin
|
---|
210 | if FormEdit.ShowModal = mrOk then begin
|
---|
211 | end;
|
---|
212 | end;
|
---|
213 |
|
---|
214 | procedure TFormList.FormDestroy(Sender: TObject);
|
---|
215 | begin
|
---|
216 | ListView1.Items.Count := 0;
|
---|
217 | FreeAndNil(DbRows);
|
---|
218 | end;
|
---|
219 |
|
---|
220 | procedure TFormList.FormShow(Sender: TObject);
|
---|
221 | begin
|
---|
222 | UpdateInterface;
|
---|
223 | end;
|
---|
224 |
|
---|
225 | procedure TFormList.ListView1Data(Sender: TObject; Item: TListItem);
|
---|
226 | var
|
---|
227 | I: Integer;
|
---|
228 | begin
|
---|
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;
|
---|
236 | end;
|
---|
237 |
|
---|
238 | procedure TFormList.ListView1SelectItem(Sender: TObject; Item: TListItem;
|
---|
239 | Selected: Boolean);
|
---|
240 | begin
|
---|
241 | UpdateInterface;
|
---|
242 | end;
|
---|
243 |
|
---|
244 | procedure TFormList.SetView(AValue: TDataViewList);
|
---|
245 | begin
|
---|
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;
|
---|
253 | end;
|
---|
254 |
|
---|
255 | procedure TFormList.UpdateInterface;
|
---|
256 | begin
|
---|
257 | AAdd.Enabled := True;
|
---|
258 | AModify.Enabled := Assigned(ListView1.Selected);
|
---|
259 | ADelete.Enabled := Assigned(ListView1.Selected);
|
---|
260 | ADuplicate.Enabled := Assigned(ListView1.Selected);
|
---|
261 | end;
|
---|
262 |
|
---|
263 | procedure TFormList.UpdateData;
|
---|
264 | var
|
---|
265 | NewColumn: TListColumn;
|
---|
266 | I: Integer;
|
---|
267 | Filter: string;
|
---|
268 | begin
|
---|
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)]);
|
---|
286 | end;
|
---|
287 |
|
---|
288 | end.
|
---|
289 |
|
---|