source: tags/1.4.0/Forms/UFormProperties.pas

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