-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpletreemodel.cpp
More file actions
290 lines (248 loc) · 8.6 KB
/
Copy pathsimpletreemodel.cpp
File metadata and controls
290 lines (248 loc) · 8.6 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
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026, Meld Studio, Inc.
#include "simpletreemodel.h"
#include <QtCore/QVariantMap>
using namespace Qt::Literals::StringLiterals;
// =============================================================================
// Construction / Destruction
// =============================================================================
SimpleTreeModel::SimpleTreeModel(QObject *parent)
: QAbstractItemModel(parent)
, m_root(new Node)
{
}
SimpleTreeModel::~SimpleTreeModel()
{
delete m_root;
}
// =============================================================================
// QAbstractItemModel interface
// =============================================================================
QModelIndex SimpleTreeModel::index(int row, int column,
const QModelIndex &parent) const
{
Node *parentNode = nodeForIndex(parent);
if (row < 0 || row >= parentNode->children.size())
return {};
if (column < 0 || column >= 1)
return {};
return createIndex(row, column, parentNode->children.at(row));
}
QModelIndex SimpleTreeModel::parent(const QModelIndex &child) const
{
if (!child.isValid())
return {};
Node *node = nodeForIndex(child);
if (!node || !node->parent || node->parent == m_root)
return {};
return indexForNode(node->parent);
}
int SimpleTreeModel::rowCount(const QModelIndex &parent) const
{
return nodeForIndex(parent)->children.size();
}
int SimpleTreeModel::columnCount(const QModelIndex & /*parent*/) const
{
return 1;
}
QVariant SimpleTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return {};
if (role != Qt::DisplayRole && role != Qt::EditRole)
return {};
return nodeForIndex(index)->name;
}
bool SimpleTreeModel::setData(const QModelIndex &index, const QVariant &value,
int role)
{
if (!index.isValid())
return false;
if (role != Qt::DisplayRole && role != Qt::EditRole)
return false;
Node *node = nodeForIndex(index);
const QString newName = value.toString();
if (node->name == newName)
return false;
node->name = newName;
emit dataChanged(index, index, {role});
return true;
}
Qt::ItemFlags SimpleTreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsDropEnabled; // Allow drops on the invisible root
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable
| Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}
QHash<int, QByteArray> SimpleTreeModel::roleNames() const
{
return {
{Qt::DisplayRole, "display"},
{Qt::EditRole, "edit"},
};
}
// =============================================================================
// Row manipulation
// =============================================================================
bool SimpleTreeModel::insertRows(int row, int count,
const QModelIndex &parent)
{
Node *parentNode = nodeForIndex(parent);
if (row < 0 || row > parentNode->children.size() || count <= 0)
return false;
beginInsertRows(parent, row, row + count - 1);
for (int i = 0; i < count; ++i) {
auto *node = new Node;
node->parent = parentNode;
parentNode->children.insert(row + i, node);
}
endInsertRows();
return true;
}
bool SimpleTreeModel::removeRows(int row, int count,
const QModelIndex &parent)
{
Node *parentNode = nodeForIndex(parent);
if (row < 0 || count <= 0 || row + count > parentNode->children.size())
return false;
beginRemoveRows(parent, row, row + count - 1);
for (int i = 0; i < count; ++i) {
Node *child = parentNode->children.takeAt(row);
delete child;
}
endRemoveRows();
return true;
}
bool SimpleTreeModel::moveRows(const QModelIndex &sourceParent,
int sourceRow, int count,
const QModelIndex &destinationParent,
int destinationChild)
{
Node *srcParent = nodeForIndex(sourceParent);
Node *dstParent = nodeForIndex(destinationParent);
if (sourceRow < 0 || count <= 0
|| sourceRow + count > srcParent->children.size()) {
return false;
}
if (destinationChild < 0 || destinationChild > dstParent->children.size())
return false;
// Prevent moving into own subtree: walk dstParent up to see if it's a
// descendant of any of the items being moved.
for (int i = 0; i < count; ++i) {
Node *movingNode = srcParent->children.at(sourceRow + i);
Node *check = dstParent;
while (check) {
if (check == movingNode)
return false;
check = check->parent;
}
}
if (!beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1,
destinationParent, destinationChild)) {
return false;
}
QList<Node *> moved;
moved.reserve(count);
for (int i = 0; i < count; ++i)
moved.append(srcParent->children.takeAt(sourceRow));
// When moving within the same parent, Qt's beginMoveRows already accounts
// for the shifted indices, but our actual data structure needs the adjusted
// insert position since we already removed the items above.
int insertAt = destinationChild;
if (srcParent == dstParent && sourceRow < destinationChild)
insertAt -= count;
for (int i = 0; i < count; ++i) {
moved[i]->parent = dstParent;
dstParent->children.insert(insertAt + i, moved[i]);
}
endMoveRows();
return true;
}
// =============================================================================
// QML convenience methods
// =============================================================================
void SimpleTreeModel::appendRow(const QVariant &data)
{
const QString name = extractName(data);
addChild(name, m_root);
}
void SimpleTreeModel::appendRow(const QModelIndex &parent, const QVariant &data)
{
const QString name = extractName(data);
Node *parentNode = nodeForIndex(parent);
addChild(name, parentNode);
}
void SimpleTreeModel::removeItem(const QModelIndex &index)
{
if (!index.isValid())
return;
removeRows(index.row(), 1, index.parent());
}
bool SimpleTreeModel::setData(const QModelIndex &index, const QVariant &value,
const QString &roleName)
{
const QHash<int, QByteArray> roles = roleNames();
const QByteArray roleNameBytes = roleName.toUtf8();
for (auto it = roles.constBegin(); it != roles.constEnd(); ++it) {
if (it.value() == roleNameBytes)
return setData(index, value, it.key());
}
return false;
}
// =============================================================================
// C++ convenience
// =============================================================================
SimpleTreeModel::Node *SimpleTreeModel::rootNode() const
{
return m_root;
}
SimpleTreeModel::Node *SimpleTreeModel::nodeForIndex(const QModelIndex &index) const
{
if (!index.isValid())
return m_root;
return static_cast<Node *>(index.internalPointer());
}
QModelIndex SimpleTreeModel::indexForNode(Node *node) const
{
if (!node || node == m_root)
return {};
Node *p = node->parent;
if (!p)
return {};
const int row = p->children.indexOf(node);
if (row < 0)
return {};
return createIndex(row, 0, node);
}
SimpleTreeModel::Node *SimpleTreeModel::addChild(const QString &name,
Node *parent)
{
if (!parent)
parent = m_root;
const int row = parent->children.size();
beginInsertRows(indexForNode(parent), row, row);
auto *node = new Node;
node->name = name;
node->parent = parent;
parent->children.append(node);
endInsertRows();
return node;
}
// =============================================================================
// Internal helpers
// =============================================================================
QString SimpleTreeModel::extractName(const QVariant &data)
{
// Use toMap() which handles both QVariantMap and QJSValue (from QML).
const QVariantMap map = data.toMap();
if (!map.isEmpty()) {
// Try "name" first (matches the demo's convention), then "display"
if (map.contains(u"name"_s))
return map.value(u"name"_s).toString();
if (map.contains(u"display"_s))
return map.value(u"display"_s).toString();
}
// Fallback: treat the entire variant as a string
return data.toString();
}