-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeViewModel.pas
153 lines (136 loc) · 3.49 KB
/
TreeViewModel.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
{ **
DelphiPI (Delphi Package Installer)
Author : ibrahim dursun (ibrahimdursun gmail)
License : GNU General Public License 2.0
** }
unit TreeViewModel;
interface
uses Classes, Generics.Collections, TreeModel;
type
TTreeViewModel<T : INode> = class(TTreeModelBase<T>)
private
fNodes : TList<T>;
function SplitString(const str:string):TDynStrArray;
public
constructor Create(const nodes: TList<T>);
function GetChild(const parent: T; index: Integer): T; override;
function GetChildCount(const parent: T): integer; override;
end;
implementation
uses JclStrings, StrUtils;
{ TTreeViewModel }
constructor TTreeViewModel<T>.Create(const nodes: TList<T>);
begin
fNodes := nodes;
end;
function TTreeViewModel<T>.GetChild(const parent: T; index: Integer): T;
var
prefix, nodePath: string;
node: T;
words: TDynStrArray;
I, currentLevel, nextLevel : integer;
immediateChild: boolean;
list : TStringList;
nodes: TList<T>;
begin
Result := default(T);
prefix := '';
currentLevel := 0;
nextLevel := 0;
if parent <> nil then
begin
prefix := parent.GetNodePath;
currentLevel := StrCharCount(prefix, '\');
nextLevel := currentLevel + 1;
end;
i := 0;
list := TStringList.Create;
list.Sorted := true;
list.Duplicates := dupIgnore;
try
for node in fNodes do
begin
nodePath := node.GetNodePath;
if not StartsStr(prefix, nodePath) then
Continue;
words := SplitString(nodePath);
if Length(words) = 0 then
Continue;
if Length(words) <= nextLevel then
Continue;
list.Add(words[nextLevel]);
if (List.Count = index+1) then
begin
immediateChild := Length(words) = nextLevel + 1;
if immediateChild then
Result := node
else begin
if prefix <> '' then
Result := DoCreateLogicalNode(words[nextLevel], prefix + '\' + words[nextLevel])
else
Result := DoCreateLogicalNode(words[nextLevel], words[nextLevel]);
end;
Break;
end;
end;
finally
list.Free;
end;
end;
function TTreeViewModel<T>.GetChildCount(const parent: T): integer;
var
prefix: string;
node: T;
nodePath: string;
words: TDynStrArray;
I: Integer;
list: TStringList;
currentLevel, nextLevel : integer;
begin
Result := 0;
prefix := '';
currentLevel := 0;
nextLevel := 0;
if parent <> nil then
begin
prefix := parent.GetNodePath;
currentLevel := StrCharCount(prefix, '\');
nextLevel := currentLevel + 1;
end;
list := TStringList.Create;
list.Sorted := true;
list.Duplicates := TDuplicates.dupIgnore;
try
for node in fNodes do
begin
nodePath := node.GetNodePath;
if not StartsStr(prefix, nodePath) then
Continue;
words := SplitString(nodePath);
if Length(words) = 0 then
Continue;
if Length(words) <= nextLevel then
Continue;
list.Add(words[nextLevel]);
end;
finally
Result := list.Count;
list.Free;
end;
end;
function TTreeViewModel<T>.SplitString(const str:string):TDynStrArray;
var
I, lastIndex: Integer;
words : TStringList;
begin
words := TStringList.Create;
try
ExtractStrings(['\'], [], PWideChar(str), words);
SetLength(Result, words.Count);
for I := 0 to words.Count - 1 do
Result[i] := words[i];
finally
words.Free;
end;
end;
end.