-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathufFileVirtualTree.pas
517 lines (459 loc) · 12.6 KB
/
ufFileVirtualTree.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
unit ufFileVirtualTree;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, ComCtrls,
StdCtrls, ufFileTree, IniFiles, VirtualTrees;
type
{ TTreeData }
TTreeData
=
class
//Text
public
Text : String;
//Key
public
Key : String;
//Value
private
FValue: String;
FdValue: TDateTime;
procedure SetValue ( _Value: String );
procedure SetdValue( _dValue: TDateTime);
public
property Value : String read FValue write SetValue ;
property dValue: TDateTime read FdValue write SetdValue;
//IsLeaf
public
IsLeaf: Boolean;
end;
{ TfFileVirtualTree }
TfFileVirtualTree
=
class(TForm)
bGetChecked: TButton;
bGetSelection: TButton;
bfFileTree: TButton;
bLoad_from_File: TButton;
bOD: TButton;
Edit1: TEdit;
eFileName: TEdit;
Label1: TLabel;
Label2: TLabel;
lCompute_Aggregates: TLabel;
m: TMemo;
od: TOpenDialog;
Panel1: TPanel;
Panel2: TPanel;
pb: TProgressBar;
Splitter1: TSplitter;
vst: TVirtualStringTree;
procedure bfFileTreeClick(Sender: TObject);
procedure bGetCheckedClick(Sender: TObject);
procedure bGetSelectionClick(Sender: TObject);
procedure bLoad_from_FileClick(Sender: TObject);
procedure bODClick(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure mChange(Sender: TObject);
procedure vstChecked(Sender: TBaseVirtualTree; Node: PVirtualNode);
procedure vstGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
procedure vstInitNode(Sender: TBaseVirtualTree; ParentNode,
Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
private
slFiles: TStringList;
slNodes: TStringList;
slTreeData: TStringList;
procedure vst_addnode_from_key_value( _Key, _Value: String);
function NewTreeData(_Text, _Key, _Value: String; _IsLeaf: Boolean): TTreeData;
function NewNode_from_TreeData(_Parent: PVirtualNode; _td: TTreeData
): PVirtualNode;
function Add_Leaf(_Parent: PVirtualNode; _Text, _Key, _Value: String): PVirtualNode;
function Add_Node(_Parent: PVirtualNode; _Text: String): PVirtualNode;
function TreeData_from_Node( _Node: PVirtualNode): TTreeData;
procedure Compute_Aggregates;
public
function Get_Selected: String;
function Get_Checked: String;
procedure Load_from_File( _FileName: String);
end;
var
fFileVirtualTree: TfFileVirtualTree;
implementation
{$R *.lfm}
//duplicated for convenience from uuStrings.pas
function StrToK( Key: String; var S: String): String;
var
I: Integer;
begin
I:= Pos( Key, S);
if I = 0
then
begin
Result:= S;
S:= '';
end
else
begin
Result:= Copy( S, 1, I-1);
Delete( S, 1, (I-1)+Length( Key));
end;
end;
//duplicated for convenience from uuStrings.pas
{ Formate_Liste
Concatène les éléments de S en les séparant par la chaine Separateur
et retourne le résultat.
}
procedure Formate_Liste( var S: String; Separateur, Element: String); overload;
const sys_Vide=''; //duplicated for convenience from u_sys_.pas
begin
if Element = sys_Vide then exit;
if S <> sys_Vide
then
S:= S + Separateur;
S:= S + Element;
end;
{ TTreeData }
function Fix_Time( FixTime_Value: String): String;
function Colon_count( _Value: String): Integer;
begin
Result:= 0;
while Pos(':',_Value) > 0
do
begin
StrToK( ':',_Value);
Inc(Result);
end;
end;
begin
case Colon_count( FixTime_Value)
of
0: Fix_Time:= '0:0:'+FixTime_Value;
1: Fix_Time:= '0:' +FixTime_Value;
2: Fix_Time:= FixTime_Value;
end;
end;
procedure TTreeData.SetValue( _Value: String);
procedure FdValue_from_FValue;
Begin
if not TryStrToDateTime( Fix_Time(FValue), FdValue)
then
FdValue:= 0;
end;
begin
FValue:= _Value;
FdValue_from_FValue;
end;
procedure TTreeData.SetdValue( _dValue: TDateTime);
procedure Strip_leading_zeroes;
const
s='0:';
begin
while 1=Pos(s, FValue)
do
Delete( FValue, 1, Length(s));
end;
begin
FdValue:= _dValue;
FValue:= FormatDateTime( 'h:n:ss', FdValue);
Strip_leading_zeroes;
end;
{ TfFileVirtualTree }
procedure TfFileVirtualTree.FormCreate(Sender: TObject);
begin
end;
procedure TfFileVirtualTree.FormDestroy(Sender: TObject);
begin
FreeAndNil( slFiles);
FreeAndNil( slNodes);
FreeAndNil( slTreeData);//todo: TTreeData not freed( may be freed by vst ?)
end;
procedure TfFileVirtualTree.mChange(Sender: TObject);
begin
end;
function TfFileVirtualTree.NewTreeData( _Text, _Key, _Value: String; _IsLeaf: Boolean): TTreeData;
begin
Result:= TTreeData.Create;
Result.Text := _Text;
Result.Key := _Key;
Result.Value:= _Value;
Result.IsLeaf:= _IsLeaf;
slTreeData.AddObject( _Key, Result);
end;
procedure TfFileVirtualTree.bLoad_from_FileClick(Sender: TObject);
begin
Load_from_File( eFileName.Text);
end;
procedure TfFileVirtualTree.bODClick(Sender: TObject);
begin
od.FileName:= eFileName.Text;
if od.Execute
then
eFileName.Text:= od.FileName;
end;
procedure TfFileVirtualTree.Edit1Change(Sender: TObject);
begin
end;
procedure TfFileVirtualTree.Load_from_File( _FileName: String);
procedure slFiles_from_ini_file;
var
ini: TINIFile;
begin
ini:= TINIFile.Create( _FileName);
try
ini.ReadSectionRaw( 'Files', slFiles);
finally
slFiles.Sort;
FreeAndNil( ini);
end;
end;
procedure tv_from_slFiles;
var
i: Integer;
Key, Value: String;
begin
pb.Min:= -1;
pb.Max:= slFiles.Count-1;
for i:= 0 to slFiles.Count-1
do
begin
Key := slFiles.Names [ i];
Value:= slFiles.ValueFromIndex[ i];
vst_addnode_from_key_value( Key, Value);
pb.Position:= i;
end;
lCompute_Aggregates.Show;
Application.ProcessMessages;
Compute_Aggregates;
lCompute_Aggregates.Hide;
end;
begin
vst.Clear;
slFiles:= TStringList.Create;
slFiles.Clear;
slFiles_from_ini_file;
slNodes:= TStringList.Create;
slNodes.Clear;
slTreeData:= TStringList.Create;
slTreeData.Clear;
tv_from_slFiles;
end;
function TfFileVirtualTree.NewNode_from_TreeData( _Parent: PVirtualNode; _td: TTreeData): PVirtualNode;
begin
Result:= vst.AddChild( _Parent, Pointer(_td));
end;
function TfFileVirtualTree.Add_Leaf( _Parent: PVirtualNode; _Text, _Key, _Value: String): PVirtualNode;
var
td: TTreeData;
begin
td:= NewTreeData( _Text, _Key, _Value, True);
Result:= NewNode_from_TreeData( _Parent, td);
end;
function TfFileVirtualTree.Add_Node(_Parent: PVirtualNode; _Text: String): PVirtualNode;
var
td: TTreeData;
begin
td:= NewTreeData( _Text, '', '', False);
Result:= NewNode_from_TreeData( _Parent, td);
end;
function TfFileVirtualTree.TreeData_from_Node(_Node: PVirtualNode): TTreeData;
var
po: ^TObject;
begin
Result:= nil;
if nil = _Node then exit;
po:= vst.GetNodeData( _Node);
if po = nil then exit;
if not (po^ is TTreeData) then exit;
Result:= TTreeData(po^)
end;
procedure TfFileVirtualTree.vst_addnode_from_key_value(_Key, _Value: String);
const
Separator='\';
var
sTreePath: String;
procedure Recursif( Root: String; Parent: PVirtualNode);
var
s, sCle: String;
i: Integer;
Node: PVirtualNode;
begin
s:= StrTok( Separator, sTreePath);
if sTreePath = ''
then //terminal case for recursion, add Value
s:= s;
sCle:= Root;
Formate_Liste( sCle, Separator, s);
i:= slNodes.IndexOf( sCle);
if i = -1
then
begin
if sTreePath = ''
then
Node:= Add_leaf( Parent, s, _Key, _Value)
else
Node:= Add_Node( Parent, s);
slNodes.AddObject( sCle, TObject(Node));
end
else
Node:= PVirtualNode( slNodes.Objects[i]);
if sTreePath = ''
then //terminal case for recursion
begin
end
else
Recursif( sCle, Node);
end;
begin
sTreePath:= _Key;
Recursif( '', nil);
end;
procedure TfFileVirtualTree.vstGetText( Sender: TBaseVirtualTree;
Node: PVirtualNode;
Column: TColumnIndex;
TextType: TVSTTextType;
var CellText: String);
var
td: TTreeData;
begin
td:= TreeData_from_Node( Node);
case Column
of
0: CellText:= td.Text;
1: CellText:= td.Value;
end;
end;
procedure TfFileVirtualTree.vstInitNode( Sender: TBaseVirtualTree;
ParentNode, Node: PVirtualNode;
var InitialStates: TVirtualNodeInitStates);
begin
Node^.CheckType:=ctCheckBox;
end;
procedure TfFileVirtualTree.vstChecked( Sender: TBaseVirtualTree; Node: PVirtualNode);
var // This ensures that all children follow the status checked/unchecked of the parent
cs: TCheckState;
procedure CheckChilds( _Parent: PVirtualNode);
var
vn: PVirtualNode;
begin
vn:= vst.GetFirstChild(_Parent);
while nil <> vn
do
begin
vn^.CheckState:= cs;
CheckChilds( vn);
vn:= vst.GetNextSibling(vn);
end;
end;
begin
cs:= Node^.CheckState;
CheckChilds( Node);
vst.Refresh;
end;
procedure TfFileVirtualTree.Compute_Aggregates;
function Compute_Childs( _Parent: PVirtualNode): TDateTime;
procedure Compute_node_dValue;
var
td: TTreeData;
Parent_dValue: TDateTime;
vn: PVirtualNode;
vn_dValue: TDateTime;
begin
Parent_dValue:= 0;
vn:= vst.GetFirstChild(_Parent);
while nil <> vn
do
begin
vn_dValue:= Compute_Childs( vn);
Parent_dValue:= Parent_dValue + vn_dValue;
vn:= vst.GetNextSibling(vn);
end;
Result:= Parent_dValue;
td:= TreeData_from_Node( _Parent);
if Assigned( td)
then
td.dValue:= Parent_dValue;
end;
procedure Compute_leaf_dValue;
var
td: TTreeData;
begin
td:= TreeData_from_Node( _Parent);
if nil = td then exit;
Result:= td.dValue;
end;
begin
Result:= 0;
if _Parent^.ChildCount = 0
then
Compute_leaf_dValue
else
Compute_node_dValue;
end;
begin
Compute_Childs( vst.RootNode);
end;
function TfFileVirtualTree.Get_Selected: String;
var
vn: PVirtualNode;
td: TTreeData;
begin
Result:= '';
vn:= vst.GetFirstSelected;
while nil <> vn
do
begin
td:= TreeData_from_Node( vn);
if td.IsLeaf
then
Formate_Liste( Result, #13#10, td.Key+' '+td.Value);
vn:= vst.GetNextSelected( vn);
end;
end;
function TfFileVirtualTree.Get_Checked: String;
var
vn: PVirtualNode;
td: TTreeData;
Total_Run_Time : Double;
Total_Time : Double;
Load_Time : Double;
begin
if not TryStrToDateTime( Fix_Time(Edit1.Text), Load_Time)
then
Load_Time:= 0;
Total_Run_Time:=0;
Total_Time:=0;
Result:= '';
vn:= vst.GetFirstChecked;
while nil <> vn
do
begin
td:= TreeData_from_Node( vn);
if td.IsLeaf
then
Begin
Formate_Liste( Result, #13#10, td.Key+' '+td.Value);
Total_Run_Time:=Total_Run_Time+td.dValue;
Total_Time:=Total_Time+td.dValue+Load_Time;
end;
vn:= vst.GetNextChecked( vn);
end;
Formate_Liste( Result, #13#10#13#10, 'Total Run Time: '+FormatDateTime( 'h:n:ss', Total_Run_Time));
Result:='Total Time Including Loading: '+FormatDateTime( 'h:n:ss', Total_Time)+#13#10#13#10+Result;
end;
procedure TfFileVirtualTree.bGetSelectionClick(Sender: TObject);
begin
m.Lines.Text:= Get_Selected;
end;
procedure TfFileVirtualTree.bGetCheckedClick(Sender: TObject);
begin
m.Lines.Text:= Get_Checked;
end;
procedure TfFileVirtualTree.bfFileTreeClick(Sender: TObject);
begin
fFileTree.Show;
end;
end.