Repository navigation
Expand file tree
/
Copy pathTreeData.cs
More file actions
237 lines (209 loc) · 7.83 KB
/
Copy pathTreeData.cs
File metadata and controls
237 lines (209 loc) · 7.83 KB
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
// Copyright (C) 2026 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
using Qt.Bridge.Models;
using Qt.DotNet;
namespace ModelsAndViews
{
public class TreeData : TreeModel
{
public List<string> Columns { get; } = ["First Name", "LastName"];
public class TreeNode
{
private static Dictionary<nint, TreeNode> Nodes { get; } = new();
private static nint NextId { get; set; } = 1;
public static TreeNode Find(nint id)
{
if (!Nodes.TryGetValue(id, out var node))
return null;
return node;
}
public nint Id { get; private set; }
public List<string> Values { get; set; } = new();
public TreeNode(params string[] values)
{
Nodes[Id = NextId++] = this;
Values = values.ToList();
}
public TreeNode Parent { get; set; }
public List<TreeNode> Children { get; } = new();
public TreeNode Insert(int index, TreeNode child)
{
if (child.Parent != null)
return this;
try {
Children.Insert(index, child);
} catch (ArgumentOutOfRangeException) {
return this;
}
child.Parent = this;
return this;
}
public TreeNode Add(TreeNode child)
{
return Insert(Children.Count, child);
}
public TreeNode Add(params string[] values)
{
return Add(new TreeNode(values));
}
public int Index
{
get
{
if (Parent == null)
return -1;
return Parent.Children.IndexOf(this);
}
}
}
public static TreeNode Root { get; } = new TreeNode()
.Add(new TreeNode("Beatles")
.Add("John", "Lennon")
.Add("Paul", "McCartney")
.Add("George", "Harrison")
.Add("Ringo", "Starr"))
.Add(new TreeNode("Rolling", "Stones")
.Add("Mick", "Jagger")
.Add("Keith", "Richards")
.Add("Charlie", "Watts")
.Add("Bill", "Wyman")
.Add("Brian", "Jones"));
public override ModelIndex Parent(ModelIndex index)
{
if (index is not { IsValid: true, Id: > 0 })
return null;
if (TreeNode.Find(index.Id) is not { } node)
return ModelIndex.Empty;
if (node.Parent == null || node.Parent == Root)
return ModelIndex.Empty;
return new ModelIndex(node.Parent.Index, 0, node.Parent.Id);
}
public override bool HasChildren(ModelIndex index)
{
if (index is not { IsValid: true, Id: > 0 })
return false;
if (TreeNode.Find(index.Id) is not { } node)
return false;
return node.Children.Any();
}
public override ModelIndex Index(int row, int column, ModelIndex index)
{
var node = index switch
{
{ IsValid: true, Id: > 0 } => TreeNode.Find(index.Id),
_ => Root
};
if (node == null)
return ModelIndex.Empty;
if (row < 0)
return ModelIndex.Empty;
if (row >= (node.Children.Any() ? node.Children.Count : 1))
return ModelIndex.Empty;
if (column < 0)
return ModelIndex.Empty;
if (column >= 2)
return ModelIndex.Empty;
if (node.Children.Any())
node = node.Children[row];
return new ModelIndex(row, column, node.Id);
}
public override int RowCount(ModelIndex parent)
{
var node = parent switch
{
{ IsValid: true, Id: > 0 } => TreeNode.Find(parent.Id),
_ => Root
};
if (node == null)
return 0;
return node.Children.Any() ? node.Children.Count : 1;
}
public override int ColumnCount(ModelIndex parent) => Columns.Count;
private static Dictionary<int, string> RoleNamesById { get; } = new()
{
{ Roles.DisplayRole, "display" },
{ Roles.EditRole, "edit" }
};
public override Dictionary<int, string> RoleNames() => RoleNamesById;
public override int Flags(ModelIndex index) => base.Flags(index) | ItemFlags.ItemIsEditable;
public override object Data(ModelIndex index, int role)
{
if (index is not { IsValid: true, Id: > 0 })
return null;
if (TreeNode.Find(index.Id) is not { } node)
return null;
if (index.Column < 0 || index.Column >= Columns.Count)
return null;
return role switch
{
Roles.DisplayRole or Roles.EditRole
when index.Column < node.Values.Count => node.Values[index.Column],
Roles.DisplayRole or Roles.EditRole => "",
_ => null
};
}
public override bool SetData(ModelIndex index, object value, int role)
{
if (index is not { IsValid: true, Id: > 0 })
return false;
if (TreeNode.Find(index.Id) is not { } node || node.Values is not { } row)
return false;
if (index.Column < 0 || index.Column >= row.Count)
return false;
if (role != Roles.EditRole)
return false;
row[index.Column] = value.ToString();
DataChanged(index, index, [Roles.DisplayRole, Roles.EditRole]);
return true;
}
public override object HeaderData(int section, int orientation, int role)
{
return (orientation, role) switch
{
(HeaderOrientation.Horizontal, Roles.DisplayRole)
when 0 <= section && section < Columns.Count => Columns[section],
_ => null
};
}
public override bool InsertRows(int row, int count, ModelIndex parent = null)
{
if (parent is not { IsValid: true, Id: > 0 })
return false;
if (TreeNode.Find(parent.Id) is not { } node)
return false;
if (row < 0 || row > node.Children.Count)
return false;
if (count < 1)
return false;
BeginInsertRows(parent, row, row + count - 1);
try {
var newNodes = Enumerable.Range(0, count)
.Select(_ => new TreeNode("(empty)", "(empty)"))
.ToList();
foreach (var newNode in newNodes)
node.Insert(row, newNode);
} finally {
EndInsertRows();
}
return true;
}
public override bool RemoveRows(int row, int count, ModelIndex parent = null)
{
if (parent is not { IsValid: true, Id: > 0 })
return false;
if (TreeNode.Find(parent.Id) is not { } node)
return false;
if (row < 0 || row >= node.Children.Count)
return false;
if (count < 1 || row + count > node.Children.Count)
return false;
BeginRemoveRows(parent, row, row + count - 1);
try {
node.Children.RemoveRange(row, count);
} finally {
EndRemoveRows();
}
return true;
}
}
}