source: trunk/Forms/FormList.pas

Last change on this file was 317, checked in by chronos, 6 months ago
  • Modified: Remove U prefix from unit names.
  • Modified: Use TFormEx for all forms for code simplification.
File size: 7.7 KB
Line 
1unit FormList;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
7 ActnList, Menus, ItemList, FormEx;
8
9type
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
60implementation
61
62{$R *.lfm}
63
64uses
65 FormItem;
66
67resourcestring
68 SRemoveItems = 'Remove items';
69 SRemoveItemsQuery = 'Do you want to remove selected items?';
70 SNew = 'New';
71
72{ TFormList }
73
74procedure TFormList.ARemoveExecute(Sender: TObject);
75var
76 I: Integer;
77begin
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;
91end;
92
93procedure TFormList.AModifyExecute(Sender: TObject);
94var
95 TempEntry: TItem;
96 FormItem: TFormItem;
97begin
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;
115end;
116
117procedure TFormList.AAddExecute(Sender: TObject);
118var
119 TempEntry: TItem;
120 FormItem: TFormItem;
121begin
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;
139end;
140
141procedure TFormList.ACloneExecute(Sender: TObject);
142var
143 TempEntry: TItem;
144 FormItem: TFormItem;
145begin
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;
164end;
165
166procedure TFormList.ASelectAllExecute(Sender: TObject);
167var
168 I: Integer;
169begin
170 for I := 0 to ListView1.Items.Count - 1 do
171 ListView1.Items[I].Selected := True;
172end;
173
174procedure TFormList.FormCreate(Sender: TObject);
175var
176 I: Integer;
177begin
178 for I := 0 to ToolBar1.ButtonCount - 1 do
179 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
180 MinItemCount := 0;
181 MaxItemCount := -1;
182end;
183
184procedure TFormList.FormShow(Sender: TObject);
185begin
186 UpdateListViewColumns;
187 UpdateList;
188 UpdateInterface;
189end;
190
191procedure TFormList.ListView1CustomDrawSubItem(Sender: TCustomListView;
192 Item: TListItem; SubItem: Integer; State: TCustomDrawState;
193 var DefaultDraw: Boolean);
194var
195 ItemFields: TItemFields;
196 ItemField: TItemField;
197begin
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;
210end;
211
212procedure TFormList.ListView1Data(Sender: TObject; Item: TListItem);
213var
214 I: Integer;
215 Fields: TItemFields;
216begin
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;
231end;
232
233procedure TFormList.ListView1KeyPress(Sender: TObject; var Key: char);
234begin
235 if Key = #13 then AModify.Execute;
236 if Key = #27 then Close;
237end;
238
239procedure TFormList.ListView1KeyUp(Sender: TObject; var Key: Word;
240 Shift: TShiftState);
241begin
242 if Key = 45 then AAdd.Execute;
243 if Key = 46 then ARemove.Execute;
244end;
245
246procedure TFormList.ListView1SelectItem(Sender: TObject;
247 Item: TListItem; Selected: Boolean);
248begin
249 UpdateInterface;
250end;
251
252procedure TFormList.SetList(AValue: TItemList);
253begin
254 if FList = AValue then Exit;
255 FList := AValue;
256end;
257
258procedure TFormList.UpdateListViewColumns;
259var
260 I: Integer;
261 Fields: TItemFields;
262begin
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;
278end;
279
280procedure TFormList.UpdateInterface;
281begin
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;
287end;
288
289procedure TFormList.UpdateList;
290begin
291 if Assigned(List) then ListView1.Items.Count := List.Count
292 else ListView1.Items.Count := 0;
293 ListView1.Refresh;
294end;
295
296end.
297
Note: See TracBrowser for help on using the repository browser.