source: tags/1.2.0/Forms/UFormProperties.pas

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