source: trunk/Forms/FormProperties.pas

Last change on this file was 162, checked in by chronos, 17 months ago
  • Modified: Updated Common package.
File size: 11.6 KB
Line 
1unit FormProperties;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
7 Menus, ActnList, VCard, DataFile, ListViewSort, LazUTF8, FormEx,
8 Generics.Collections;
9
10type
11
12 { TFormProperties }
13
14 TFormProperties = class(TFormEx)
15 AAdd: TAction;
16 AClone: TAction;
17 ASaveValueToFile: TAction;
18 ALoadValueFromFile: TAction;
19 ASelectAll: TAction;
20 ARemove: TAction;
21 AModify: TAction;
22 ActionList1: TActionList;
23 ListView1: TListView;
24 ListViewFilter1: TListViewFilter;
25 ListViewSort1: TListViewSort;
26 MenuItem1: TMenuItem;
27 MenuItem2: TMenuItem;
28 MenuItem3: TMenuItem;
29 MenuItem4: TMenuItem;
30 MenuItem5: TMenuItem;
31 MenuItem6: TMenuItem;
32 MenuItem7: TMenuItem;
33 MenuItem8: TMenuItem;
34 OpenDialog1: TOpenDialog;
35 PopupMenuField: TPopupMenu;
36 SaveDialog1: TSaveDialog;
37 StatusBar1: TStatusBar;
38 ToolBar1: TToolBar;
39 ToolButton1: TToolButton;
40 ToolButton2: TToolButton;
41 ToolButton3: TToolButton;
42 ToolButton4: TToolButton;
43 ToolButton5: TToolButton;
44 ToolButton6: TToolButton;
45 ToolButton7: TToolButton;
46 procedure AAddExecute(Sender: TObject);
47 procedure ACloneExecute(Sender: TObject);
48 procedure ALoadValueFromFileExecute(Sender: TObject);
49 procedure AModifyExecute(Sender: TObject);
50 procedure ARemoveExecute(Sender: TObject);
51 procedure ASaveValueToFileExecute(Sender: TObject);
52 procedure ASelectAllExecute(Sender: TObject);
53 procedure FormCreate(Sender: TObject);
54 procedure FormShow(Sender: TObject);
55 procedure ListView1Data(Sender: TObject; Item: TListItem);
56 procedure ListView1DblClick(Sender: TObject);
57 procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
58 Selected: Boolean);
59 procedure ListViewFilter1Change(Sender: TObject);
60 procedure ListViewSort1ColumnWidthChanged(Sender: TObject);
61 function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
62 procedure ListViewSort1Filter(ListViewSort: TListViewSort);
63 private
64 FProperties: TContactProperties;
65 FUpdateCount: Integer;
66 procedure FilterList(List: TObjectList<TObject>);
67 procedure SetProperties(AValue: TContactProperties);
68 procedure DoUpdateInterface;
69 public
70 property Properties: TContactProperties read FProperties write SetProperties;
71 procedure ReloadList;
72 procedure BeginUpdate;
73 procedure EndUpdate;
74 procedure UpdateInterface;
75 end;
76
77
78implementation
79
80{$R *.lfm}
81
82uses
83 FormProperty, Core, Common;
84
85resourcestring
86 SRemovePropertites = 'Remove fields';
87 SRemovePropertiesQuery = 'Do you want to remove selected fields?';
88 STotal = 'Total';
89 SFiltered = 'Filtered';
90 SSelected = 'Selected';
91 STextFiles = 'Text files';
92 SValue = 'Value';
93 SEndUpdateTooLow = 'Update counter error';
94
95const
96 TextFileExt = '.txt';
97
98{ TFormProperties }
99
100procedure TFormProperties.ListView1Data(Sender: TObject; Item: TListItem);
101
102 procedure AddItem(Text: string; IsCaption: Boolean = False);
103 begin
104 if IsCaption then begin
105 if Text <> '' then Item.Caption := Text
106 else Item.Caption := ' ';
107 end else begin
108 if Text <> '' then Item.SubItems.Add(Text)
109 else Item.SubItems.Add(' ');
110 end;
111 end;
112
113begin
114 if Item.Index < ListViewSort1.List.Count then
115 with TContactProperty(ListViewSort1.List[Item.Index]) do begin
116 AddItem(Name, True);
117 AddItem(Attributes.DelimitedText);
118 AddItem(Value);
119 Item.Data := ListViewSort1.List[Item.Index];
120 end;
121end;
122
123procedure TFormProperties.ListView1DblClick(Sender: TObject);
124begin
125 AModify.Execute;
126end;
127
128procedure TFormProperties.ListView1SelectItem(Sender: TObject; Item: TListItem;
129 Selected: Boolean);
130begin
131 UpdateInterface;
132end;
133
134procedure TFormProperties.ListViewFilter1Change(Sender: TObject);
135begin
136 ReloadList;
137 UpdateInterface;
138end;
139
140procedure TFormProperties.ListViewSort1ColumnWidthChanged(Sender: TObject);
141begin
142 ListViewFilter1.UpdateFromListView(ListView1);
143end;
144
145function TFormProperties.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
146begin
147 Result := 0;
148 if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
149 with ListViewSort1 do
150 case Column of
151 0: Result := CompareString(TContactProperty(Item1).Name, TContactProperty(Item2).Name);
152 1: Result := CompareString(TContactProperty(Item1).Attributes.DelimitedText, TContactProperty(Item2).Attributes.DelimitedText);
153 2: Result := CompareString(TContactProperty(Item1).Value, TContactProperty(Item2).Value);
154 end;
155 if ListViewSort1.Order = soDown then Result := -Result;
156 end else Result := 0;
157end;
158
159procedure TFormProperties.ListViewSort1Filter(ListViewSort: TListViewSort);
160begin
161 if Assigned(Properties) then Properties.AssignToList(ListViewSort1.List)
162 else ListViewSort1.List.Clear;
163 FilterList(ListViewSort1.List);
164end;
165
166procedure TFormProperties.FilterList(List: TObjectList<TObject>);
167var
168 I: Integer;
169 FoundCount: Integer;
170 EnteredCount: Integer;
171begin
172 EnteredCount := ListViewFilter1.TextEnteredCount;
173 for I := List.Count - 1 downto 0 do begin
174 if List.Items[I] is TContactProperty then begin
175 with TContactProperty(List.Items[I]) do begin
176 with ListViewFilter1 do
177 if Visible and (EnteredCount > 0) then begin
178 FoundCount := 0;
179 if Pos(UTF8LowerCase(StringGrid.Cells[0, 0]),
180 UTF8LowerCase(TContactProperty(List.Items[I]).Name)) > 0 then Inc(FoundCount);
181 if Pos(UTF8LowerCase(StringGrid.Cells[1, 0]),
182 UTF8LowerCase(TContactProperty(List.Items[I]).Attributes.DelimitedText)) > 0 then Inc(FoundCount);
183 if Pos(UTF8LowerCase(StringGrid.Cells[2, 0]),
184 UTF8LowerCase(TContactProperty(List.Items[I]).Value)) > 0 then Inc(FoundCount);
185 if FoundCount <> EnteredCount then List.Delete(I);
186 end;
187 end;
188 end else
189 if TContactProperty(List.Items[I]) is TContactProperty then begin
190 List.Delete(I);
191 end;
192 end;
193end;
194
195procedure TFormProperties.SetProperties(AValue: TContactProperties);
196begin
197 if FProperties = AValue then Exit;
198 FProperties := AValue;
199 ReloadList;
200 UpdateInterface;
201end;
202
203procedure TFormProperties.FormShow(Sender: TObject);
204begin
205 ReloadList;
206 UpdateInterface;
207 ListViewFilter1.UpdateFromListView(ListView1);
208end;
209
210procedure TFormProperties.AAddExecute(Sender: TObject);
211var
212 FormProperty: TFormProperty;
213 ContactProperty: TContactProperty;
214begin
215 FormProperty := TFormProperty.Create(nil);
216 try
217 ContactProperty := TContactProperty.Create;
218 FormProperty.ContactProperty := ContactProperty;
219 try
220 if FormProperty.ShowModal = mrOK then begin
221 Properties.Add(ContactProperty);
222 ContactProperty := nil;
223 ReloadList;
224 UpdateInterface;
225 end;
226 finally
227 if Assigned(ContactProperty) then
228 ContactProperty.Free;
229 end;
230 finally
231 FormProperty.Free;
232 end;
233end;
234
235procedure TFormProperties.ACloneExecute(Sender: TObject);
236var
237 FormProperty: TFormProperty;
238 ContactProperty: TContactProperty;
239begin
240 FormProperty := TFormProperty.Create(nil);
241 try
242 ContactProperty := TContactProperty.Create;
243 ContactProperty.Assign(TContactProperty(ListView1.Selected.Data));
244 FormProperty.ContactProperty := ContactProperty;
245 try
246 if FormProperty.ShowModal = mrOK then begin
247 Properties.Add(ContactProperty);
248 ContactProperty := nil;
249 ReloadList;
250 UpdateInterface;
251 end;
252 finally
253 if Assigned(ContactProperty) then
254 ContactProperty.Free;
255 end;
256 finally
257 FormProperty.Free;
258 end;
259end;
260
261procedure TFormProperties.ALoadValueFromFileExecute(Sender: TObject);
262begin
263 if Assigned(ListView1.Selected) then begin
264 OpenDialog1.Filter := STextFiles + '|*' + TextFileExt + '|' + SAllFiles + '|*.*';
265 OpenDialog1.DefaultExt := TextFileExt;
266 OpenDialog1.InitialDir := ExtractFileDir(Core.Core.LastPropertyValueFileName);
267 OpenDialog1.FileName := ExtractFileName(Core.Core.LastPropertyValueFileName);
268 if OpenDialog1.Execute then begin
269 TContactProperty(ListView1.Selected.Data).Value := LoadFileToStr(OpenDialog1.FileName);
270 Core.Core.LastPropertyValueFileName := OpenDialog1.FileName;
271 ReloadList;
272 end;
273 end;
274end;
275
276procedure TFormProperties.AModifyExecute(Sender: TObject);
277var
278 FormProperty: TFormProperty;
279 ContactProperty: TContactProperty;
280begin
281 FormProperty := TFormProperty.Create(nil);
282 try
283 ContactProperty := TContactProperty.Create;
284 try
285 ContactProperty.Assign(TContactProperty(ListView1.Selected.Data));
286 FormProperty.ContactProperty := ContactProperty;
287 if FormProperty.ShowModal = mrOK then begin
288 TContactProperty(ListView1.Selected.Data).Assign(ContactProperty);
289 ReloadList;
290 UpdateInterface;
291 end;
292 finally
293 ContactProperty.Free;
294 end;
295 finally
296 FormProperty.Free;
297 end;
298end;
299
300procedure TFormProperties.ARemoveExecute(Sender: TObject);
301var
302 I: Integer;
303begin
304 if Assigned(ListView1.Selected) then
305 if MessageDlg(SRemovePropertites, SRemovePropertiesQuery,
306 TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
307 for I := ListView1.Items.Count - 1 downto 0 do
308 if ListView1.Items[I].Selected then begin
309 Properties.Delete(Properties.IndexOf(ListView1.Items[I].Data));
310 end;
311 ReloadList;
312 UpdateInterface;
313 end;
314end;
315
316procedure TFormProperties.ASaveValueToFileExecute(Sender: TObject);
317begin
318 if Assigned(ListView1.Selected) then begin
319 SaveDialog1.Filter := STextFiles + '|*' + TextFileExt + '|' + SAllFiles + '|*.*';
320 SaveDialog1.DefaultExt := TextFileExt;
321 SaveDialog1.InitialDir := ExtractFileDir(Core.Core.LastPropertyValueFileName);
322 SaveDialog1.FileName := SValue + TextFileExt;
323 if SaveDialog1.Execute then begin
324 SaveStringToFile(TContactProperty(ListView1.Selected.Data).Value, SaveDialog1.FileName);
325 Core.Core.LastPropertyValueFileName := SaveDialog1.FileName;
326 end;
327 end;
328end;
329
330procedure TFormProperties.ASelectAllExecute(Sender: TObject);
331begin
332 ListView1.SelectAll;
333 UpdateInterface;
334end;
335
336procedure TFormProperties.FormCreate(Sender: TObject);
337var
338 I: Integer;
339begin
340 FProperties := nil;
341 for I := 0 to ToolBar1.ButtonCount - 1 do begin
342 ToolBar1.Buttons[I].ShowHint := True;
343 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
344 end;
345end;
346
347procedure TFormProperties.ReloadList;
348begin
349 ListViewSort1.Refresh;
350end;
351
352procedure TFormProperties.BeginUpdate;
353begin
354 Inc(FUpdateCount);
355end;
356
357procedure TFormProperties.EndUpdate;
358begin
359 if FUpdateCount <= 0 then raise Exception(SEndUpdateTooLow);
360 Dec(FUpdateCount);
361 if FUpdateCount = 0 then DoUpdateInterface;
362end;
363
364procedure TFormProperties.DoUpdateInterface;
365var
366 Text: string;
367 SelectedCount: Integer;
368 Selected: Boolean;
369begin
370 if not ListView1.HandleAllocated then Exit;
371
372 Selected := Assigned(ListView1.Selected);
373 AAdd.Enabled := Assigned(Properties);
374 AModify.Enabled := Assigned(Properties) and Selected;
375 AClone.Enabled := Assigned(Properties) and Selected;
376 ARemove.Enabled := Assigned(Properties) and Selected;
377 ALoadValueFromFile.Enabled := Assigned(Properties) and Selected;
378 ASaveValueToFile.Enabled := Assigned(Properties) and Selected;
379 ASelectAll.Enabled := ListView1.Items.Count > 0;
380
381 Text := '';
382 if Assigned(Properties) then begin
383 Text := STotal + ': ' + IntToStr(Properties.Count);
384 if ListView1.Items.Count < Properties.Count then
385 Text := Text + ', ' + SFiltered + ': ' + IntToStr(ListView1.Items.Count);
386 SelectedCount := ListView1.SelCount;
387 if SelectedCount > 0 then
388 Text := Text + ', ' + SSelected + ': ' + IntToStr(SelectedCount);
389 end;
390 StatusBar1.Panels[0].Text := Text;
391end;
392
393procedure TFormProperties.UpdateInterface;
394begin
395 if FUpdateCount = 0 then DoUpdateInterface;
396end;
397
398end.
399
Note: See TracBrowser for help on using the repository browser.