1 | unit UFormProperties;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
---|
9 | ComCtrls, Menus, ActnList, UContact, UListViewSort, fgl, LazUTF8;
|
---|
10 |
|
---|
11 | type
|
---|
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 |
|
---|
75 | var
|
---|
76 | FormProperties: TFormProperties;
|
---|
77 |
|
---|
78 |
|
---|
79 | implementation
|
---|
80 |
|
---|
81 | {$R *.lfm}
|
---|
82 |
|
---|
83 | uses
|
---|
84 | UFormProperty, UCore, UCommon;
|
---|
85 |
|
---|
86 | resourcestring
|
---|
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 |
|
---|
96 | const
|
---|
97 | TextFileExt = '.txt';
|
---|
98 |
|
---|
99 | { TFormProperties }
|
---|
100 |
|
---|
101 | procedure 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 |
|
---|
114 | begin
|
---|
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;
|
---|
122 | end;
|
---|
123 |
|
---|
124 | procedure TFormProperties.ListView1DblClick(Sender: TObject);
|
---|
125 | begin
|
---|
126 | AModify.Execute;
|
---|
127 | end;
|
---|
128 |
|
---|
129 | procedure TFormProperties.ListView1SelectItem(Sender: TObject; Item: TListItem;
|
---|
130 | Selected: Boolean);
|
---|
131 | begin
|
---|
132 | UpdateInterface;
|
---|
133 | end;
|
---|
134 |
|
---|
135 | procedure TFormProperties.ListViewFilter1Change(Sender: TObject);
|
---|
136 | begin
|
---|
137 | ReloadList;
|
---|
138 | UpdateInterface;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TFormProperties.ListViewSort1ColumnWidthChanged(Sender: TObject);
|
---|
142 | begin
|
---|
143 | ListViewFilter1.UpdateFromListView(ListView1);
|
---|
144 | end;
|
---|
145 |
|
---|
146 | function TFormProperties.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
---|
147 | begin
|
---|
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;
|
---|
158 | end;
|
---|
159 |
|
---|
160 | procedure TFormProperties.ListViewSort1Filter(ListViewSort: TListViewSort);
|
---|
161 | begin
|
---|
162 | if Assigned(Properties) then Properties.AssignToList(ListViewSort1.List)
|
---|
163 | else ListViewSort1.List.Clear;
|
---|
164 | FilterList(ListViewSort1.List);
|
---|
165 | end;
|
---|
166 |
|
---|
167 | procedure TFormProperties.FilterList(List: TFPGObjectList<TObject>);
|
---|
168 | var
|
---|
169 | I: Integer;
|
---|
170 | FoundCount: Integer;
|
---|
171 | EnteredCount: Integer;
|
---|
172 | begin
|
---|
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;
|
---|
194 | end;
|
---|
195 |
|
---|
196 | procedure TFormProperties.SetProperties(AValue: TContactProperties);
|
---|
197 | begin
|
---|
198 | if FProperties = AValue then Exit;
|
---|
199 | FProperties := AValue;
|
---|
200 | ReloadList;
|
---|
201 | UpdateInterface;
|
---|
202 | end;
|
---|
203 |
|
---|
204 | procedure TFormProperties.FormShow(Sender: TObject);
|
---|
205 | begin
|
---|
206 | Core.PersistentForm1.Load(Self);
|
---|
207 | Core.ThemeManager1.UseTheme(Self);
|
---|
208 | Core.Translator.TranslateComponentRecursive(Self);
|
---|
209 | ReloadList;
|
---|
210 | UpdateInterface;
|
---|
211 | ListViewFilter1.UpdateFromListView(ListView1);
|
---|
212 | end;
|
---|
213 |
|
---|
214 | procedure TFormProperties.AAddExecute(Sender: TObject);
|
---|
215 | var
|
---|
216 | FormProperty: TFormProperty;
|
---|
217 | ContactProperty: TContactProperty;
|
---|
218 | begin
|
---|
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;
|
---|
238 | end;
|
---|
239 |
|
---|
240 | procedure TFormProperties.ACloneExecute(Sender: TObject);
|
---|
241 | var
|
---|
242 | FormProperty: TFormProperty;
|
---|
243 | ContactProperty: TContactProperty;
|
---|
244 | begin
|
---|
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;
|
---|
265 | end;
|
---|
266 |
|
---|
267 | procedure TFormProperties.ALoadValueFromFileExecute(Sender: TObject);
|
---|
268 | begin
|
---|
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;
|
---|
280 | end;
|
---|
281 |
|
---|
282 | procedure TFormProperties.AModifyExecute(Sender: TObject);
|
---|
283 | var
|
---|
284 | FormProperty: TFormProperty;
|
---|
285 | ContactProperty: TContactProperty;
|
---|
286 | begin
|
---|
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;
|
---|
305 | end;
|
---|
306 |
|
---|
307 | procedure TFormProperties.ARemoveExecute(Sender: TObject);
|
---|
308 | var
|
---|
309 | I: Integer;
|
---|
310 | begin
|
---|
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;
|
---|
322 | end;
|
---|
323 |
|
---|
324 | procedure TFormProperties.ASaveValueToFileExecute(Sender: TObject);
|
---|
325 | begin
|
---|
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;
|
---|
336 | end;
|
---|
337 |
|
---|
338 | procedure TFormProperties.ASelectAllExecute(Sender: TObject);
|
---|
339 | begin
|
---|
340 | ListView1.SelectAll;
|
---|
341 | UpdateInterface;
|
---|
342 | end;
|
---|
343 |
|
---|
344 | procedure TFormProperties.FormClose(Sender: TObject; var CloseAction: TCloseAction
|
---|
345 | );
|
---|
346 | begin
|
---|
347 | Core.PersistentForm1.Save(Self);
|
---|
348 | end;
|
---|
349 |
|
---|
350 | procedure TFormProperties.FormCreate(Sender: TObject);
|
---|
351 | var
|
---|
352 | I: Integer;
|
---|
353 | begin
|
---|
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;
|
---|
359 | end;
|
---|
360 |
|
---|
361 | procedure TFormProperties.ReloadList;
|
---|
362 | begin
|
---|
363 | ListViewSort1.Refresh;
|
---|
364 | end;
|
---|
365 |
|
---|
366 | procedure TFormProperties.UpdateInterface;
|
---|
367 | var
|
---|
368 | Text: string;
|
---|
369 | SelectedCount: Integer;
|
---|
370 | begin
|
---|
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;
|
---|
389 | end;
|
---|
390 |
|
---|
391 | end.
|
---|
392 |
|
---|