-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProgressTracker.pas
2834 lines (2434 loc) · 94.9 KB
/
ProgressTracker.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
Progress tracker
Progress tracker is a library meant for calculation and tracking of
progress of complex multi-level and multi-stage operations.
Two classes are implemented for this purpose - TProgressStageNode and
TProgressTracker. Class TProgressStageNode is used internally in
TProgressTracker, but it can be used separately as a standalone solution.
Individual phases of complex progress are called stages in this library.
They are organized into a tree with arbitrary number of branches and levels.
Each stage can contain multiple substages or none at all (such stage is
called simple stage). If a stage contain substages, then this stage is
called a superstage in relation to those substages. Lets consider following
stage tree:
Stage_A --- Stage_A_1
|- Stage_A_2 --- Stage_A_2_I
| |- Stage_A_2_II
|- Stage_A_3
In this example, stage A is a superstage for stages A_1, A_2 and A_3. These
three stages are a substages of stage A. Stage A_2 is a superstage of stages
A_2_I and A_2_II, and these stages are in turn substages of stage A_2.
Only in simple stages (those with no substage) can the progress or position
be directly altered. Change in any simple stage is propagated up the tree
and all superstages up to the root stage change their progress accordingly.
Each stage can have different length within its superstage. This relative
length is calculated from absolute length each stage is given when added.
For example, if we want to have three stages, first taking 1/2 of the length
and other two each 1/4, we can achieve so when defining absolute length of
the first stage as 2 and as 1 for the other two.
Version 2.0.3 (2024-05-03)
Last change 2024-10-04
©2017-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.ProgressTracker
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
* BinaryStreamingLite - github.com/TheLazyTomcat/Lib.BinaryStreamingLite
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
Library AuxExceptions is required only when rebasing local exception classes
(see symbol ProgressTracker_UseAuxExceptions for details).
BinaryStreamingLite can be replaced by full BinaryStreaming.
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit ProgressTracker;
{
ProgressTracker_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
ProgressTracker_UseAuxExceptions to achieve this.
}
{$IF Defined(ProgressTracker_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$ELSEIF Defined(LINUX) and Defined(FPC)}
{$DEFINE Linux}
{$ELSE}
{$MESSAGE FATAL 'Unsupported operating system.'}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH ClassicProcVars+}
{$MODESWITCH DuplicateLocals+}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
interface
uses
SysUtils, Classes,
AuxTypes, AuxClasses{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exeptions
===============================================================================}
type
EPTException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
EPTIndexOutOfBounds = class(EPTException);
EPTInvalidValue = class(EPTException);
EPTInvalidStageID = class(EPTException);
EPTAssignedStageID = class(EPTException);
EPTUnassignedStageID = class(EPTException);
EPTNotSubStageOf = class(EPTException);
EPTSuperStageDiffer = class(EPTException);
{===============================================================================
--------------------------------------------------------------------------------
TProgressStageNode
--------------------------------------------------------------------------------
===============================================================================}
type
TPTStageID = type Integer;
TPTStageArray = array of TPTStageID;
// TPTStageData is used to obtain otherwise internal stage data
TPTStageData = record
AbsoluteLength: Double;
RelativeLength: Double;
RelativeStart: Double;
RelativeProgress: Double;
end;
// stage event/callback procedural types
TPTStageProgressEvent = procedure(Sender: TObject; Stage: TPTStageID; Progress: Double) of object;
TPTStageProgressCallback = procedure(Sender: TObject; Stage: TPTStageID; Progress: Double);
const
PT_STAGEID_INVALID = -1;
PT_STAGEID_MASTER = Low(TPTStageID);
{===============================================================================
TProgressStageNode - class declaration
===============================================================================}
type
TProgressStageNode = class(TCustomListObject)
protected
// progress
fMaximum: UInt64;
fPosition: UInt64;
fProgress: Double;
fLastReportedProgress: Double;
// stage (public)
fSuperStageNode: TProgressStageNode;
fID: TPTStageID;
fSubStageCount: Integer;
fSubStages: array of TProgressStageNode;
// stage (internal)
fAbsoluteLength: Double;
fRelativeLength: Double;
fRelativeStart: Double;
fRelativeProgress: Double;
// settings
fConsecutiveStages: Boolean;
fStrictlyGrowing: Boolean;
fMinProgressDelta: Double;
fGlobalSettings: Boolean;
// updates
fChanged: Boolean;
fUpdateCounter: Integer;
// events
fOnProgressInternal: TNotifyEvent;
fOnProgressEvent: TFloatEvent;
fOnProgressCallBack: TFloatCallback;
fOnSubStageProgressEvent: TPTStageProgressEvent;
fOnSubStageProgressCallBack: TPTStageProgressCallback;
// getters, setters
procedure SetMaximum(Value: UInt64); virtual;
procedure SetPosition(Value: UInt64); virtual;
procedure SetProgress(Value: Double); virtual;
Function GetSubStage(Index: Integer): TProgressStageNode; virtual;
procedure SetConsecutiveStages(Value: Boolean); virtual;
procedure SetStrictlyGrowing(Value: Boolean); virtual;
procedure SetMinProgressDelta(Value: Double); virtual;
procedure SetGlobalSettings(Value: Boolean); virtual;
// list methods
Function GetCapacity: Integer; override;
procedure SetCapacity(Value: Integer); override;
Function GetCount: Integer; override;
procedure SetCount(Value: Integer); override;
// recalculations
procedure RecalculateRelations; virtual;
procedure RecalculateProgress(ForceChange: Boolean = False); virtual;
procedure ProgressFromPosition; virtual;
procedure SubStageProgressHandler(Sender: TObject); virtual;
// progress
procedure DoProgress; virtual; // also manages internal progress events
// init/final
procedure Initialize; virtual;
procedure Finalize; virtual;
// macro/utils
procedure NewSubStageAt(Index: Integer; AbsoluteLength: Double; ID: TPTStageID); virtual;
// internal properties/events
property AbsoluteLength: Double read fAbsoluteLength write fAbsoluteLength;
property RelativeLength: Double read fRelativeLength write fRelativeLength;
property RelativeStart: Double read fRelativeStart write fRelativeStart;
property RelativeProgress: Double read fRelativeProgress write fRelativeProgress;
property OnProgressInternal: TNotifyEvent read fOnProgressInternal write fOnProgressInternal;
public
constructor Create;
constructor CreateAsStage(SuperStageNode: TProgressStageNode; AbsoluteLength: Double; ID: TPTStageID);
destructor Destroy; override;
// updating
Function BeginUpdate: Integer; virtual;
Function EndUpdate: Integer; virtual;
// list functions
Function LowIndex: Integer; override;
Function HighIndex: Integer; override;
Function First: TProgressStageNode; virtual;
Function Last: TProgressStageNode; virtual;
Function IndexOf(Node: TProgressStageNode): Integer; overload; virtual;
Function IndexOf(SubStage: TPTStageID): Integer; overload; virtual;
Function Find(Node: TProgressStageNode; out Index: Integer): Boolean; overload; virtual;
Function Find(SubStage: TPTStageID; out Index: Integer): Boolean; overload; virtual;
Function Add(AbsoluteLength: Double; ID: TPTStageID = PT_STAGEID_INVALID): Integer; virtual;
procedure Insert(Index: Integer; AbsoluteLength: Double; ID: TPTStageID = PT_STAGEID_INVALID); virtual;
procedure Exchange(Idx1, Idx2: Integer); virtual;
procedure Move(SrcIdx, DstIdx: Integer); virtual;
Function Extract(Node: TProgressStageNode): TProgressStageNode; overload; virtual;
Function Extract(SubStage: TPTStageID): TProgressStageNode; overload; virtual;
Function Remove(Node: TProgressStageNode): Integer; overload; virtual;
Function Remove(SubStage: TPTStageID): Integer; overload; virtual;
procedure Delete(Index: Integer); virtual;
procedure Clear; virtual;
// indirect stages access
Function SetSubStageMaximum(SubStage: TPTStageID; NewValue: UInt64): Boolean; virtual;
Function SetSubStagePosition(SubStage: TPTStageID; NewValue: UInt64): Boolean; virtual;
Function SetSubStageProgress(SubStage: TPTStageID; NewValue: Double): Boolean; virtual;
// utility function
Function IsSimpleStage: Boolean; virtual;
Function StageData: TPTStageData; virtual;
Function SubStageLevel: Integer; virtual;
Function TotalSubStageCount: Integer; virtual;
// properties
property Maximum: UInt64 read fMaximum write SetMaximum;
property Position: UInt64 read fPosition write SetPosition;
property Progress: Double read fProgress write SetProgress;
property SuperStageNode: TProgressStageNode read fSuperStageNode;
property ID: TPTStageID read fID write fID;
property SubStages[Index: Integer]: TProgressStageNode read GetSubStage; default;
property ConsecutiveStages: Boolean read fConsecutiveStages write SetConsecutiveStages;
property StrictlyGrowing: Boolean read fStrictlyGrowing write SetStrictlyGrowing;
property MinProgressDelta: Double read fMinProgressDelta write SetMinProgressDelta;
{
When global settings is true, the newly added substage node inherits
settings from owner node and any change to the settings is immediately
projected to all existing subnodes.
Global settings is automatically set to the same value in all subnodes when
changed. Newly added nodes are inheriting the current value.
False by default.
}
property GlobalSettings: Boolean read fGlobalSettings write SetGlobalSettings;
// events
property OnProgress: TFloatEvent read fOnProgressEvent write fOnProgressEvent;
property OnProgressEvent: TFloatEvent read fOnProgressEvent write fOnProgressEvent;
property OnProgressCallBack: TFloatCallback read fOnProgressCallBack write fOnProgressCallBack;
property OnSubStageProgress: TPTStageProgressEvent read fOnSubStageProgressEvent write fOnSubStageProgressEvent;
property OnSubStageProgressEvent: TPTStageProgressEvent read fOnSubStageProgressEvent write fOnSubStageProgressEvent;
property OnSubStageProgressCallBack: TPTStageProgressCallback read fOnSubStageProgressCallBack write fOnSubStageProgressCallBack;
end;
{===============================================================================
--------------------------------------------------------------------------------
TProgressTracker
--------------------------------------------------------------------------------
===============================================================================}
type
TPTTreeSettingsField = (tsfAbsoluteLen,tsfRelativeLen,tsfProgress,tsfMaximum,tsfPosition);
TPTTreeSettingsFields = set of TPTTreeSettingsField;
TPTTreeSettings = record
FullPaths: Boolean;
IncludeMaster: Boolean;
HexadecimalIDs: Boolean;
PathDelimiter: String;
Indentation: String;
ShowHeader: Boolean;
ShownFields: TPTTreeSettingsFields;
end;
const
PT_TREESETTINGS_DEFAULT: TPTTreeSettings = (
FullPaths: False;
IncludeMaster: False;
HexadecimalIDs: False;
PathDelimiter: '.';
Indentation: ' ';
ShowHeader: False;
ShownFields: []);
{===============================================================================
TProgressTracker - class declaration
===============================================================================}
type
TProgressTracker = class(TCustomListObject)
protected
fMasterNode: TProgressStageNode;
fStages: array of TProgressStageNode;
fStageCount: Integer;
// events
fOnProgressEvent: TFloatEvent;
fOnProgressCallBack: TFloatCallback;
fOnStageProgressEvent: TPTStageProgressEvent;
fOnStageProgressCallBack: TPTStageProgressCallback;
// getters, setters
Function GetProgress: Double; virtual;
Function GetConsecutiveStages: Boolean; virtual;
procedure SetConsecutiveStages(Value: Boolean); virtual;
Function GetStrictlyGrowing: Boolean; virtual;
procedure SetStrictlyGrowing(Value: Boolean); virtual;
Function GetMinProgressDelta: Double; virtual;
procedure SetMinProgressDelta(Value: Double); virtual;
Function GetGlobalSettings: Boolean; virtual;
procedure SetGlobalSettings(Value: Boolean); virtual;
Function GetNode(Index: Integer): TProgressStageNode; virtual;
Function GetStageNode(Stage: TPTStageID): TProgressStageNode; virtual;
Function GetStage(Stage: TPTStageID): Double; virtual;
procedure SetStage(Stage: TPTStageID; Value: Double); virtual;
Function GetSubStageCount(Stage: TPTStageID): Integer; virtual;
Function GetSubStageNode(Stage: TPTStageID; Index: Integer): TProgressStageNode; virtual;
Function GetSubStage(Stage: TPTStageID; Index: Integer): TPTStageID; virtual;
// list methods
Function GetCapacity: Integer; override;
procedure SetCapacity(Value: Integer); override;
Function GetCount: Integer; override;
procedure SetCount(Value: Integer); override;
// events
procedure OnMasterProgressHandler(Sender: TObject; Progress: Double); virtual;
procedure OnStageProgressHandler(Sender: TObject; Progress: Double); virtual;
// init/final
procedure Initialize; virtual;
procedure Finalize; virtual;
// intenal list methods
Function InternalFirstUnassignedStageID: TPTStageID; virtual; // also grows the list if necessary
Function ResolveNewStageID(StageID: TPTStageID): TPTStageID; virtual;
Function InternalAdd(SuperStageNode: TProgressStageNode; AbsoluteLength: Double; ID: TPTStageID): TPTStageID; virtual;
Function InternalInsert(SuperStageNode: TProgressStageNode; Index: Integer; AbsoluteLength: Double; ID: TPTStageID): TPTStageID; virtual;
procedure InternalDelete(SuperStageNode: TProgressStageNode; Index: Integer); virtual;
// utility methods
Function ObtainStageNode(Stage: TPTStageID; AllowMaster: Boolean): TProgressStageNode; virtual;
public
constructor Create;
destructor Destroy; override;
// updates
procedure BeginUpdate; virtual;
procedure EndUpdate; virtual;
// index and ID methods
{
LowIndex/HighIndex returns low/high bound of indices used when accessing
Nodes property.
}
Function LowIndex: Integer; override;
Function HighIndex: Integer; override;
{
LowStageID returns lowest stage ID from allocated range. It is always 0.
HighStageID returns highest stage ID from allocated range, irrespective of
whether the id has a node assigned or not.
When no range is allocated, it will return PT_STAGEID_INVALID.
}
Function LowStageID: TPTStageID; virtual;
Function HighStageID: TPTStageID; virtual;
{
LowSubStageIndex/HighSubStageIndex returns lowest/highest allowed index of
substages list for selected stage.
The function will raise an EPTInvalidStageID exception when selected stage
ID is completely outside of allocated range, or EPTUnassignedStageID
exception when selected stage ID is within range but does not have a node
assigned.
}
Function LowSubStageIndex(Stage: TPTStageID): Integer; virtual;
Function HighSubStageIndex(Stage: TPTStageID): Integer; virtual;
{
CheckStageID returns true when selected stage ID is within allocated range,
false otherwise. It ignores whether the ID has assigned node or not.
Parameter AllowMaster indicates whether the function should return true
on stage ID equal to PT_STAGEID_MASTER.
}
Function CheckStageID(StageID: TPTStageID; AllowMaster: Boolean = False): Boolean; virtual;
{
StageIDAssigned returns true when selected stage ID is within allocated
range and a node object is assigned to it, false otherwise.
Can accept PR_STAGEID_MASTER, in which case it will always return true.
}
Function StageIDAssigned(StageID: TPTStageID): Boolean; virtual;
{
CheckSubStageIndex returns true when given index is within allowed bounds
for substage indices in selected stage, false otherwise.
Stage parameter can be set to PT_STAGEID_MASTER to check index of root
stages.
When stage node cannot be obtained, it will raise EPTInvalidStageID or
EPTUnassignedStageID exception.
}
Function CheckSubStageIndex(Stage: TPTStageID; Index: Integer): Boolean; virtual;
{
CheckSubStageID returns true when selected stage contains a subnode with
ID given in SubStageID, false otherwise.
Stage parameter can be set to PT_STAGEID_MASTER to check ID of root stages.
When stage node cannot be obtained, it will raise EPTInvalidStageID or
EPTUnassignedStageID exception.
}
Function CheckSubStageID(Stage,SubStageID: TPTStageID): Boolean; virtual;
{
NodeIndexFromStageID returns index of node (position in Nodes property)
assigned to a given stage ID.
If no node is assigned at this ID, a negative value is returned.
}
Function NodeIndexFromStageID(StageID: TPTStageID): Integer; virtual;
{
StageIDFromNodeIndex returns stage ID of a node at given index (see Nodes
property).
If the index does not point to any node, it will return PT_STAGEID_INVALID.
}
Function StageIDFromNodeIndex(Index: Integer): TPTStageID; virtual;
{
FirstUnassignedStageID returns first ID that does not have a node assigned.
When no such ID can be found, it will return PT_STAGEID_INVALID.
}
Function FirstUnassignedStageID: TPTStageID; virtual;
// list manipulation methods
{
IndexOf
Returns index of stage with given ID within its superstage.
First overload also returns the ID of superstage for which the searched
stage is a substage. This can be PT_STAGEID_MASTER, indicating the stage
is a root stage.
If the stage is not found, then a negative value is returned and value of
SuperStage is undefined.
IndexOfIn
Returns index of selected stage within a given superstage.
If the stage is not a substage of selected superstage, it will return
a negative value.
When the SuperStage does not point to a valid stage, the function will
return a negative value.
SuperStage can be set to PT_STAGEID_MASTER to get index of root stage.
}
Function IndexOf(Stage: TPTStageID; out SuperStage: TPTStageID): Integer; overload; virtual;
Function IndexOf(Stage: TPTStageID): Integer; overload; virtual;
Function IndexOfIn(SuperStage,Stage: TPTStageID): Integer; virtual;
{
Find
Tries to find stage with given ID within its respective superstage.
If it is found, then output parameter SuperStage (in the first overload)
will contain ID of superstage for which the searched stage is a substage
(this ID can be set to PT_STAGEID_MASTER, indicating the stage is a root
stage), Index will contain its position within the superstage and result
will be True.
When not found, then result is set to False and values of SuperStage and
Index are undefined.
FindIn
Tries to find stage with given ID within given superstage.
If found, then parameter Index will be set to its index with the given
superstage and result will be True.
If the stage is not a substage of selected superstage or when the
SuperStage does not point to a valid stage, then result is set to False
and value of Index is undefined.
SuperStage can be set to PT_STAGEID_MASTER to get index of root stage.
}
Function Find(Stage: TPTStageID; out SuperStage: TPTStageID; out Index: Integer): Boolean; overload; virtual;
Function Find(Stage: TPTStageID; out Index: Integer): Boolean; overload; virtual;
Function FindIn(SuperStage,Stage: TPTStageID; out Index: Integer): Boolean; virtual;
{
Add
Adds new root stage.
If ID parameter is not specified (left as invalid), then the newly added
stage will be assigned a first free stage ID and this ID will also be
returned.
When an ID is specified, and it cannot be used (ie. it is already
assigned), an EPTAssignedStageID exception will be raised.
If specified ID is beyond allocated space, the space is reallocated so
that the requested ID can be used - be careful when using this feature,
it will allocate massive memory space when requesting high ID!
AddIn
Adds new stage as a substage of a selected superstage.
If SuperStage is not valid, an EPTInvalidStageID or EPTUnassignedStageID
(depending whether the ID is completely out of bounds, or it just points
to an unassigned ID) exception will be raised.
When SuperStage is set to PT_STAGEID_MASTER, the AddIn method is
equivalent to method Add.
Parameter ID and result behaves the same as in method Add.
}
Function Add(AbsoluteLength: Double; ID: TPTStageID = PT_STAGEID_INVALID): TPTStageID; virtual;
Function AddIn(SuperStage: TPTStageID; AbsoluteLength: Double; ID: TPTStageID = PT_STAGEID_INVALID): TPTStageID; virtual;
{
The ID parameter behaves the same as in add methods. Return value of all
insert methods is bound to ID parameter and also behaves the same as in add
methods.
Insert
Inserts new stage at a position occupied by a selected stage. The new
stage is inserted to the same superstage where the selected insert stage
is currently placed.
If the InsertStage does not exists or is invalid, then EPTInvalidStageID
or EPTUnassignedStageID exception will be raised.
InsertIn
Inserts new substage to a selected superstage at position occupied by
a stage selected by InsertStage parameter.
When the SuperStage is not valid, an EPTInvalidStageID or
EPTUnassignedStageID exception will be raised.
If the InsertStage does not exist or is not a substage of selected
superstage, then an EPTNotSubStageOf exception is raised.
SuperStage can be set to PT_STAGEID_MASTER to insert ne stage between
root stages.
InsertInAt
Inserts new substage to a selected superstage at position given by Index
parameter.
When the SuperStage is not valid, an EPTInvalidStageID or
EPTUnassignedStageID exception will be raised.
If the index is not valid or is not equal to substage count, then an
EPTIndexOutOfBounds exception will be raised.
SuperStage can be set to PT_STAGEID_MASTER.
}
Function Insert(InsertStage: TPTStageID; AbsoluteLength: Double; ID: TPTStageID = PT_STAGEID_INVALID): TPTStageID; virtual;
Function InsertIn(SuperStage,InsertStage: TPTStageID; AbsoluteLength: Double; ID: TPTStageID = PT_STAGEID_INVALID): TPTStageID; virtual;
Function InsertInAt(SuperStage: TPTStageID; Index: Integer; AbsoluteLength: Double; ID: TPTStageID = PT_STAGEID_INVALID): TPTStageID; virtual;
{
Exchange
Exchanges two selected stages. The stages must differ, otherwise the
method exits immediately and does not perform any operation.
If any of the stages is not valid, then an EPTInvalidStageID exception
is raised.
Both stages must be a substage of the same superstage, otherwise an
EPTSuperStageDiffer exception will be raised.
ExchangeIn
Exchanges two stages in a selected superstage. The stages must differ,
otherwise the method exits immediately without performing any operation.
When the selected SuperStage is not valid, an EPTInvalidStageID or
EPTUnassignedStageID exception will be raised.
If any of the stages is not valid or is not a substage of selected
superstage, then an EPTInvalidStageID exception is raised.
ExchangeInAt
Exchanges two stages in a selected superstage at positions given by
indices. The indices must differ, otherwise no operation is performed.
When the selected SuperStage is not valid, an EPTInvalidStageID or
EPTUnassignedStageID exception will be raised.
If any of the indices is not within an allowed bounds, then an
EPTIndexOutOfBounds exception is raised.
}
procedure Exchange(Stage1,Stage2: TPTStageID); virtual;
procedure ExchangeIn(SuperStage: TPTStageID; Stage1,Stage2: TPTStageID); virtual;
procedure ExchangeInAt(SuperStage: TPTStageID; Idx1,Idx2: Integer); virtual;
{
Move
Moves source stage to a place occupied by a destination stage. The stages
must differ, otherwise no operation is performed.
If any of the stages is not valid, then an EPTInvalidStageID exception
is raised.
Both stages must be a substage of the same superstage, otherwise an
EPTSuperStageDiffer exception will be raised.
MoveIn
Moves source stage to a place occupied by a destination stage in a
selected superstage. The stages must differ, otherwise the method exits
immediately without performing any operation.
When the selected SuperStage is not valid, an EPTInvalidStageID or
EPTUnassignedStageID exception will be raised.
If any of the stages is not valid or is not a substage of selected
superstage, then an EPTInvalidStageID exception is raised.
MoveInAt
Moves stage at source position (index) to a destination position (index)
in a selected superstage. The indices must differ, otherwise no operation
is performed.
When the selected SuperStage is not valid, an EPTInvalidStageID or
EPTUnassignedStageID exception will be raised.
If any of the indices is not within an allowed bounds, then an
EPTIndexOutOfBounds exception is raised.
}
procedure Move(SrcStage,DstStage: TPTStageID); virtual;
procedure MoveIn(SuperStage: TPTStageID; SrcStage,DstStage: TPTStageID); virtual;
procedure MoveInAt(SuperStage: TPTStageID; SrcIdx,DstIdx: Integer); virtual;
{
Remove
Removes selected stage, if present, and returns its index in appropriate
superstage and, in case of first overload, outputs ID of this superstage
(can be PT_STAGEID_MASTER).
If the stage is not present, the function will return a negative value
and value of SuperStage will be undefined.
RemoveIn
Removes selected stage, if present, from a given superstage.
If the SuperStage is not valid, then a negative value is returned.
If the selected stage is not present in the given superstage, a negative
value will be returned.
}
Function Remove(Stage: TPTStageID; out SuperStage: TPTStageID): Integer; overload; virtual;
Function Remove(Stage: TPTStageID): Integer; overload; virtual;
Function RemoveIn(SuperStage,Stage: TPTStageID): Integer; virtual;
{
Delete
Deletes selected stage.
If the selected stage is not valid, an EPTInvalidStageID exception will
be raised.
DeleteIn
Deletes selected stage from a selected superstage.
If the superstage does not exists, this method will return an
EPTInvalidStageID or EPTUnassignedStageID exception.
If the selected stage is not a substage of selected superstage, an
EPTNotSubStageOf exception will be raised.
DeleteInAt
Deletes stage at a position given by index from a selected superstage.
If the superstage does not exists, this method will return an
EPTInvalidStageID or EPTUnassignedStageID exception.
If the index does not point to a valid substage, it will raise an
EPTIndexOutOfBounds exception.
}
procedure Delete(Stage: TPTStageID); virtual;
procedure DeleteIn(SuperStage: TPTStageID; Stage: TPTStageID); virtual;
procedure DeleteInAt(SuperStage: TPTStageID; Index: Integer); virtual;
{
If the parameter SuperStage points to a valid stage node, then only
substages of this stage will be removed, otherwise all known stages will
be removed.
If the selected stage is valid and does not contain any substage, then
nothing will happen.
Setting Stage parameter to PT_STAGEID_MASTER has the same effect as setting
it to an invalid value.
}
procedure Clear(Stage: TPTStageID = PT_STAGEID_INVALID); virtual;
// stages information
Function IsSimpleStage(Stage: TPTStageID): Boolean; virtual;
Function IsSubStageOf(SubStage,Stage: TPTStageID): Boolean; virtual;
Function IsSuperStageOf(SuperStage,Stage: TPTStageID): Boolean; virtual;
Function SuperStageOf(Stage: TPTStageID): TPTStageID; virtual;
Function StagePath(Stage: TPTStageID; IncludeMaster: Boolean = False): TPTStageArray; virtual;
procedure StageTree(Tree: TStrings; TreeSettings: TPTTreeSettings); overload; virtual;
procedure StageTree(Tree: TStrings); overload; virtual;
// managed stages access
Function GetStageMaximum(Stage: TPTStageID): UInt64; virtual;
Function SetStageMaximum(Stage: TPTStageID; Maximum: UInt64): UInt64; virtual;
Function GetStagePosition(Stage: TPTStageID): UInt64; virtual;
Function SetStagePosition(Stage: TPTStageID; Position: UInt64): UInt64; virtual;
Function GetStageProgress(Stage: TPTStageID): Double; virtual;
Function SetStageProgress(Stage: TPTStageID; Progress: Double): Double; virtual;
Function GetStageReporting(Stage: TPTStageID): Boolean; virtual;
Function SetStageReporting(Stage: TPTStageID; StageReporting: Boolean): Boolean; virtual;
// tree streaming
procedure SaveToIniFile(const FileName: String); virtual;
procedure LoadFromIniFile(const FileName: String); virtual;
procedure SaveToStream(Stream: TStream); virtual;
procedure LoadFromStream(Stream: TStream); virtual;
procedure SaveToFile(const FileName: String); virtual;
procedure LoadFromFile(const FileName: String); virtual;
procedure LoadFromResource(const ResName: String); virtual;
procedure LoadFromResourceID(ResID: Integer); virtual;
// properties
property Progress: Double read GetProgress;
property ConsecutiveStages: Boolean read GetConsecutiveStages write SetConsecutiveStages;
property StrictlyGrowing: Boolean read GetStrictlyGrowing write SetStrictlyGrowing;
property MinProgressDelta: Double read GetMinProgressDelta write SetMinProgressDelta;
property GlobalSettings: Boolean read GetGlobalSettings write SetGlobalSettings;
// nodes/(sub)stages
property Nodes[Index: Integer]: TProgressStageNode read GetNode;
property StageNodes[Stage: TPTStageID]: TProgressStageNode read GetStageNode;
property Stages[Stage: TPTStageID]: Double read GetStage write SetStage; default;
property SubStageCount[Stage: TPTStageID]: Integer read GetSubStageCount;
property SubStageNodes[Stage: TPTStageID; Index: Integer]: TProgressStageNode read GetSubstageNode;
property SubStages[Stage: TPTStageID; Index: Integer]: TPTStageID read GetSubStage;
// events
property OnProgress: TFloatEvent read fOnProgressEvent write fOnProgressEvent;
property OnProgressEvent: TFloatEvent read fOnProgressEvent write fOnProgressEvent;
property OnProgressCallBack: TFloatCallback read fOnProgressCallBack write fOnProgressCallBack;
property OnStageProgress: TPTStageProgressEvent read fOnStageProgressEvent write fOnStageProgressEvent;
property OnStageProgressEvent: TPTStageProgressEvent read fOnStageProgressEvent write fOnStageProgressEvent;
property OnStageProgressCallBack: TPTStageProgressCallback read fOnStageProgressCallBack write fOnStageProgressCallBack;
end;
implementation
uses
{$IFDEF Windows}Windows,{$ENDIF} IniFiles,
StrRect, BinaryStreamingLite, UInt64Utils;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$PUSH}{$WARN 2005 OFF} // Comment level $1 found
{$IF Defined(FPC) and (FPC_FULLVERSION >= 30000)}
{$DEFINE W5058:=}
{$DEFINE W5092:={$WARN 5092 OFF}} // Variable "$1" of a managed type does not seem to be initialized
{$ELSE}
{$DEFINE W5058:={$WARN 5058 OFF}} // Variable "$1" does not seem to be initialized
{$DEFINE W5092:=}
{$IFEND}
{$POP}
{$ENDIF}
{===============================================================================
Auxiliary functions
===============================================================================}
Function LimitValue(Value: Double): Double;
begin
If Value < 0.0 then
Result := 0.0
else If Value > 1.0 then
Result := 1.0
else
Result := Value;
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5058 W5092{$ENDIF}
procedure InitFormatSettings(out FormatSettings: TFormatSettings);
begin
{$WARN SYMBOL_PLATFORM OFF}
{$IF not Defined(FPC) and (CompilerVersion >= 18)}
// Delphi 2006+
FormatSettings := TFormatSettings.Create(LOCALE_USER_DEFAULT);
{$ELSE}
// older delphi and FPC
{$IFDEF Windows}
// windows
GetLocaleFormatSettings(LOCALE_USER_DEFAULT,FormatSettings);
{$ELSE}
// non-windows
FormatSettings := DefaultFormatSettings;
{$ENDIF}
{$IFEND}
{$WARN SYMBOL_PLATFORM ON}
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TProgressStageNode
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TProgressStageNode - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TProgressStageNode - protected methods
-------------------------------------------------------------------------------}
procedure TProgressStageNode.SetMaximum(Value: UInt64);
begin
If (Value <> fMaximum) and IsSimpleStage then
begin
If (Value < fMaximum) or not fStrictlyGrowing or (fMaximum <= 0) then
begin
If Value < fPosition then
fPosition := Value;
fMaximum := Value;
ProgressFromPosition;
DoProgress;
end;
end;
end;
//------------------------------------------------------------------------------
procedure TProgressStageNode.SetPosition(Value: UInt64);
begin
If (Value <> fPosition) and IsSimpleStage then
begin
If (Value > fPosition) or not fStrictlyGrowing then
begin
If Value > fMaximum then
fMaximum := Value;
fPosition := Value;
ProgressFromPosition;
DoProgress;
end;
end;
end;
//------------------------------------------------------------------------------
procedure TProgressStageNode.SetProgress(Value: Double);
begin
If (Value <> fProgress) and IsSimpleStage then
begin
If (Value > fProgress) or not fStrictlyGrowing then
begin
fProgress := LimitValue(Value);
DoProgress;
end;
end;
end;
//------------------------------------------------------------------------------
Function TProgressStageNode.GetSubStage(Index: Integer): TProgressStageNode;
begin
If CheckIndex(Index) then
Result := fSubStages[Index]
else
raise EPTIndexOutOfBounds.CreateFmt('TProgressStageNode.GetSubStage: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
procedure TProgressStageNode.SetConsecutiveStages(Value: Boolean);
var
i: Integer;
begin
If fGlobalSettings then
For i := LowIndex to HighIndex do
fSubStages[i].ConsecutiveStages := Value;
If Value <> fConsecutiveStages then
begin
fConsecutiveStages := Value;
RecalculateProgress;
DoProgress;
end;
end;
//------------------------------------------------------------------------------
procedure TProgressStageNode.SetStrictlyGrowing(Value: Boolean);
var
i: Integer;
begin
If fGlobalSettings then
For i := LowIndex to HighIndex do
fSubStages[i].StrictlyGrowing := Value;
If Value <> fStrictlyGrowing then
begin
fStrictlyGrowing := Value;
If not IsSimpleStage then
begin
RecalculateProgress;
DoProgress;
end;
end;
end;
//------------------------------------------------------------------------------
procedure TProgressStageNode.SetMinProgressDelta(Value: Double);
var
i: Integer;
begin
If fGlobalSettings then
For i := LowIndex to HighIndex do
fSubStages[i].MinProgressDelta := Value;
If Value <> fMinProgressDelta then
fMinProgressDelta := LimitValue(Value);
end;
//------------------------------------------------------------------------------
procedure TProgressStageNode.SetGlobalSettings(Value: Boolean);
var
i: Integer;
begin
For i := LowIndex to HighIndex do
fSubStages[i].GlobalSettings := Value;
If Value <> fGlobalSettings then
begin
fGlobalSettings := Value;
If fGlobalSettings then
For i := LowIndex to HighIndex do
begin
fSubStages[i].ConsecutiveStages := fConsecutiveStages;
fSubStages[i].StrictlyGrowing := fStrictlyGrowing;
fSubStages[i].MinProgressDelta := fMinProgressDelta;
end;
end;
end;
//------------------------------------------------------------------------------
Function TProgressStageNode.GetCapacity: Integer;
begin
Result := Length(fSubStages);
end;
//------------------------------------------------------------------------------
procedure TProgressStageNode.SetCapacity(Value: Integer);
var
i: Integer;
begin
If Value >= 0 then
begin
If Value <> Length(fSubStages) then
begin
If Value < Count then
begin
For i := Value to HighIndex do
FreeAndNil(fSubStages[i]);
fSubStageCount := Value;
end;
SetLength(fSubStages,Value);
end;
end
else raise EPTInvalidValue.CreateFmt('TProgressStageNode.SetCapacity: Invalid capacity (%d).',[Value]);
end;
//------------------------------------------------------------------------------
Function TProgressStageNode.GetCount: Integer;
begin
Result := fSubStageCount;
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TProgressStageNode.SetCount(Value: Integer);
begin
// do nothing
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
procedure TProgressStageNode.RecalculateRelations;
var
AbsLen: Double;
i: Integer;
RelStart: Double;
begin
// get total absolute length
AbsLen := 0.0;
For i := LowIndex to HighIndex do
AbsLen := AbsLen + fSubStages[i].AbsoluteLength;
// recalculate relative length, start and progress
RelStart := 0.0;
For i := LowIndex to HighIndex do
begin
fSubStages[i].RelativeStart := RelStart;
If AbsLen <> 0.0 then