1 | unit FormList;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
---|
7 | ActnList, Menus, ItemList, FormEx;
|
---|
8 |
|
---|
9 | type
|
---|
10 | { TFormList }
|
---|
11 |
|
---|
12 | TFormList = class(TFormEx)
|
---|
13 | AAdd: TAction;
|
---|
14 | ASelectAll: TAction;
|
---|
15 | AModify: TAction;
|
---|
16 | ARemove: TAction;
|
---|
17 | AClone: TAction;
|
---|
18 | ActionList1: TActionList;
|
---|
19 | ListView1: TListView;
|
---|
20 | MenuItem1: TMenuItem;
|
---|
21 | MenuItem2: TMenuItem;
|
---|
22 | MenuItem3: TMenuItem;
|
---|
23 | MenuItem4: TMenuItem;
|
---|
24 | MenuItem5: TMenuItem;
|
---|
25 | PopupMenu1: TPopupMenu;
|
---|
26 | ToolBar1: TToolBar;
|
---|
27 | ToolButton1: TToolButton;
|
---|
28 | ToolButton2: TToolButton;
|
---|
29 | ToolButton3: TToolButton;
|
---|
30 | ToolButton4: TToolButton;
|
---|
31 | procedure AAddExecute(Sender: TObject);
|
---|
32 | procedure ACloneExecute(Sender: TObject);
|
---|
33 | procedure AModifyExecute(Sender: TObject);
|
---|
34 | procedure ARemoveExecute(Sender: TObject);
|
---|
35 | procedure ASelectAllExecute(Sender: TObject);
|
---|
36 | procedure FormCreate(Sender: TObject);
|
---|
37 | procedure FormShow(Sender: TObject);
|
---|
38 | procedure ListView1CustomDrawSubItem(Sender: TCustomListView;
|
---|
39 | Item: TListItem; SubItem: Integer; State: TCustomDrawState;
|
---|
40 | var DefaultDraw: Boolean);
|
---|
41 | procedure ListView1Data(Sender: TObject; Item: TListItem);
|
---|
42 | procedure ListView1KeyPress(Sender: TObject; var Key: char);
|
---|
43 | procedure ListView1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState
|
---|
44 | );
|
---|
45 | procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
|
---|
46 | Selected: Boolean);
|
---|
47 | private
|
---|
48 | FList: TItemList;
|
---|
49 | procedure SetList(AValue: TItemList);
|
---|
50 | procedure UpdateListViewColumns;
|
---|
51 | public
|
---|
52 | MinItemCount: Integer;
|
---|
53 | MaxItemCount: Integer;
|
---|
54 | procedure UpdateInterface;
|
---|
55 | procedure UpdateList;
|
---|
56 | property List: TItemList read FList write SetList;
|
---|
57 | end;
|
---|
58 |
|
---|
59 |
|
---|
60 | implementation
|
---|
61 |
|
---|
62 | {$R *.lfm}
|
---|
63 |
|
---|
64 | uses
|
---|
65 | FormItem;
|
---|
66 |
|
---|
67 | resourcestring
|
---|
68 | SRemoveItems = 'Remove items';
|
---|
69 | SRemoveItemsQuery = 'Do you want to remove selected items?';
|
---|
70 | SNew = 'New';
|
---|
71 |
|
---|
72 | { TFormList }
|
---|
73 |
|
---|
74 | procedure TFormList.ARemoveExecute(Sender: TObject);
|
---|
75 | var
|
---|
76 | I: Integer;
|
---|
77 | begin
|
---|
78 | if Assigned(ListView1.Selected) then begin
|
---|
79 | if MessageDlg(SRemoveItems, SRemoveItemsQuery,
|
---|
80 | TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
|
---|
81 | for I := ListView1.Items.Count - 1 downto 0 do
|
---|
82 | if ListView1.Items[I].Selected then begin
|
---|
83 | if List.Count <= MinItemCount then Break;
|
---|
84 | ListView1.Items[I].Selected := False;
|
---|
85 | List.Remove(TItem(ListView1.Items[I].Data));
|
---|
86 | end;
|
---|
87 | UpdateList;
|
---|
88 | UpdateInterface;
|
---|
89 | end;
|
---|
90 | end;
|
---|
91 | end;
|
---|
92 |
|
---|
93 | procedure TFormList.AModifyExecute(Sender: TObject);
|
---|
94 | var
|
---|
95 | TempEntry: TItem;
|
---|
96 | FormItem: TFormItem;
|
---|
97 | begin
|
---|
98 | if Assigned(ListView1.Selected) then
|
---|
99 | with TItem(ListView1.Selected.Data) do begin
|
---|
100 | TempEntry := List.CreateItem;
|
---|
101 | TempEntry.Assign(TItem(ListView1.Selected.Data));
|
---|
102 | FormItem := TFormItem.Create(Self);
|
---|
103 | try
|
---|
104 | FormItem.Item := TempEntry;
|
---|
105 | if FormItem.ShowModal = mrOk then begin
|
---|
106 | TItem(ListView1.Selected.Data).Assign(TempEntry);
|
---|
107 | UpdateList;
|
---|
108 | UpdateInterface;
|
---|
109 | end;
|
---|
110 | finally
|
---|
111 | FreeAndNil(FormItem);
|
---|
112 | FreeAndNil(TempEntry);
|
---|
113 | end;
|
---|
114 | end;
|
---|
115 | end;
|
---|
116 |
|
---|
117 | procedure TFormList.AAddExecute(Sender: TObject);
|
---|
118 | var
|
---|
119 | TempEntry: TItem;
|
---|
120 | FormItem: TFormItem;
|
---|
121 | begin
|
---|
122 | if (MaxItemCount <> -1) and (List.Count >= MaxItemCount) then Exit;
|
---|
123 |
|
---|
124 | TempEntry := List.CreateItem;
|
---|
125 | FormItem := TFormItem.Create(Self);
|
---|
126 | try
|
---|
127 | TempEntry.Name := List.GetNextAvailableName(SNew + ' ' + LowerCase(List.GetItemClass.GetClassName));
|
---|
128 | FormItem.Item := TempEntry;
|
---|
129 | if FormItem.ShowModal = mrOk then begin
|
---|
130 | List.Add(TempEntry);
|
---|
131 | TempEntry := nil;
|
---|
132 | UpdateList;
|
---|
133 | UpdateInterface;
|
---|
134 | end;
|
---|
135 | finally
|
---|
136 | FreeAndNil(FormItem);
|
---|
137 | end;
|
---|
138 | TempEntry.Free;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TFormList.ACloneExecute(Sender: TObject);
|
---|
142 | var
|
---|
143 | TempEntry: TItem;
|
---|
144 | FormItem: TFormItem;
|
---|
145 | begin
|
---|
146 | if (MaxItemCount <> -1) and (List.Count >= MaxItemCount) then Exit;
|
---|
147 |
|
---|
148 | TempEntry := List.CreateItem;
|
---|
149 | TempEntry.Assign(TItem(ListView1.Selected.Data));
|
---|
150 | FormItem := TFormItem.Create(Self);
|
---|
151 | try
|
---|
152 | TempEntry.Name := List.GetNextAvailableName(TempEntry.Name);
|
---|
153 | FormItem.Item := TempEntry;
|
---|
154 | if FormItem.ShowModal = mrOk then begin
|
---|
155 | List.Add(TempEntry);
|
---|
156 | TempEntry := nil;
|
---|
157 | UpdateList;
|
---|
158 | UpdateInterface;
|
---|
159 | end;
|
---|
160 | finally
|
---|
161 | FreeAndNil(FormItem);
|
---|
162 | end;
|
---|
163 | TempEntry.Free;
|
---|
164 | end;
|
---|
165 |
|
---|
166 | procedure TFormList.ASelectAllExecute(Sender: TObject);
|
---|
167 | var
|
---|
168 | I: Integer;
|
---|
169 | begin
|
---|
170 | for I := 0 to ListView1.Items.Count - 1 do
|
---|
171 | ListView1.Items[I].Selected := True;
|
---|
172 | end;
|
---|
173 |
|
---|
174 | procedure TFormList.FormCreate(Sender: TObject);
|
---|
175 | var
|
---|
176 | I: Integer;
|
---|
177 | begin
|
---|
178 | for I := 0 to ToolBar1.ButtonCount - 1 do
|
---|
179 | ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
|
---|
180 | MinItemCount := 0;
|
---|
181 | MaxItemCount := -1;
|
---|
182 | end;
|
---|
183 |
|
---|
184 | procedure TFormList.FormShow(Sender: TObject);
|
---|
185 | begin
|
---|
186 | UpdateListViewColumns;
|
---|
187 | UpdateList;
|
---|
188 | UpdateInterface;
|
---|
189 | end;
|
---|
190 |
|
---|
191 | procedure TFormList.ListView1CustomDrawSubItem(Sender: TCustomListView;
|
---|
192 | Item: TListItem; SubItem: Integer; State: TCustomDrawState;
|
---|
193 | var DefaultDraw: Boolean);
|
---|
194 | var
|
---|
195 | ItemFields: TItemFields;
|
---|
196 | ItemField: TItemField;
|
---|
197 | begin
|
---|
198 | ItemFields := TItem(Item.Data).GetFields;
|
---|
199 | try
|
---|
200 | ItemField := ItemFields[SubItem];
|
---|
201 | if ItemField.DataType = dtColor then
|
---|
202 | with ListView1.Canvas do begin
|
---|
203 | Brush.Color := TItem(Item.Data).GetValueColor(ItemField.Index);
|
---|
204 | Brush.Style := bsSolid;
|
---|
205 | FillRect(Item.DisplayRectSubItem(SubItem, drBounds));
|
---|
206 | end;
|
---|
207 | finally
|
---|
208 | ItemFields.Free;
|
---|
209 | end;
|
---|
210 | end;
|
---|
211 |
|
---|
212 | procedure TFormList.ListView1Data(Sender: TObject; Item: TListItem);
|
---|
213 | var
|
---|
214 | I: Integer;
|
---|
215 | Fields: TItemFields;
|
---|
216 | begin
|
---|
217 | if Item.Index < ListView1.Items.Count then
|
---|
218 | with TItem(List[Item.Index]) do begin
|
---|
219 | //Item.Caption := GetName;
|
---|
220 | Item.Data := List[Item.Index];
|
---|
221 | Fields := List.GetItemClass.GetFields;
|
---|
222 | try
|
---|
223 | for I := 0 to Fields.Count - 1 do begin
|
---|
224 | if I = 0 then Item.Caption := GetValueAsText(Fields[I].Index)
|
---|
225 | else Item.SubItems.Add(GetValueAsText(Fields[I].Index));
|
---|
226 | end;
|
---|
227 | finally
|
---|
228 | Fields.Free;
|
---|
229 | end;
|
---|
230 | end;
|
---|
231 | end;
|
---|
232 |
|
---|
233 | procedure TFormList.ListView1KeyPress(Sender: TObject; var Key: char);
|
---|
234 | begin
|
---|
235 | if Key = #13 then AModify.Execute;
|
---|
236 | if Key = #27 then Close;
|
---|
237 | end;
|
---|
238 |
|
---|
239 | procedure TFormList.ListView1KeyUp(Sender: TObject; var Key: Word;
|
---|
240 | Shift: TShiftState);
|
---|
241 | begin
|
---|
242 | if Key = 45 then AAdd.Execute;
|
---|
243 | if Key = 46 then ARemove.Execute;
|
---|
244 | end;
|
---|
245 |
|
---|
246 | procedure TFormList.ListView1SelectItem(Sender: TObject;
|
---|
247 | Item: TListItem; Selected: Boolean);
|
---|
248 | begin
|
---|
249 | UpdateInterface;
|
---|
250 | end;
|
---|
251 |
|
---|
252 | procedure TFormList.SetList(AValue: TItemList);
|
---|
253 | begin
|
---|
254 | if FList = AValue then Exit;
|
---|
255 | FList := AValue;
|
---|
256 | end;
|
---|
257 |
|
---|
258 | procedure TFormList.UpdateListViewColumns;
|
---|
259 | var
|
---|
260 | I: Integer;
|
---|
261 | Fields: TItemFields;
|
---|
262 | begin
|
---|
263 | Fields := List.GetItemClass.GetFields;
|
---|
264 | ListView1.Columns.BeginUpdate;
|
---|
265 | try
|
---|
266 | while ListView1.Columns.Count < Fields.Count do
|
---|
267 | ListView1.Columns.Add;
|
---|
268 | while ListView1.Columns.Count > Fields.Count do
|
---|
269 | ListView1.Columns.Delete(ListView1.Columns.Count - 1);
|
---|
270 | for I := 0 to ListView1.Columns.Count - 1 do begin
|
---|
271 | ListView1.Columns[I].Caption := Fields[I].Name;
|
---|
272 | ListView1.Columns[I].Width := Scale96ToScreen(100);
|
---|
273 | end;
|
---|
274 | finally
|
---|
275 | ListView1.Columns.EndUpdate;
|
---|
276 | end;
|
---|
277 | Fields.Free;
|
---|
278 | end;
|
---|
279 |
|
---|
280 | procedure TFormList.UpdateInterface;
|
---|
281 | begin
|
---|
282 | ARemove.Enabled := Assigned(FList) and Assigned(ListView1.Selected) and (List.Count > MinItemCount);
|
---|
283 | AModify.Enabled := Assigned(FList) and Assigned(ListView1.Selected);
|
---|
284 | AAdd.Enabled := Assigned(FList) and ((MaxItemCount = -1) or ((MaxItemCount <> -1) and (List.Count < MaxItemCount)));
|
---|
285 | AClone.Enabled := Assigned(FList) and ((MaxItemCount = -1) or ((MaxItemCount <> -1) and (List.Count < MaxItemCount)));
|
---|
286 | ASelectAll.Enabled := ListView1.Items.Count > 0;
|
---|
287 | end;
|
---|
288 |
|
---|
289 | procedure TFormList.UpdateList;
|
---|
290 | begin
|
---|
291 | if Assigned(List) then ListView1.Items.Count := List.Count
|
---|
292 | else ListView1.Items.Count := 0;
|
---|
293 | ListView1.Refresh;
|
---|
294 | end;
|
---|
295 |
|
---|
296 | end.
|
---|
297 |
|
---|