-
Notifications
You must be signed in to change notification settings - Fork 6
/
treemodel.cpp
418 lines (336 loc) · 12 KB
/
treemodel.cpp
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
//
// TreeModel
//
// Created by Anthony Thibault on 6/5/2019
// Copyright 2019 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "treeitem.h"
#include <QDebug>
#include <QFile>
#include <QtGlobal>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QStringList>
#include <QJSValue>
#include "treemodel.h"
static TreeItem* newBlankTreeItem() {
QList<QVariant> columnData;
columnData << "newNode";
columnData << "clip";
columnData << QJsonObject(); // blank
return new TreeItem(columnData);
}
TreeModel::TreeModel(QObject* parent) : QAbstractItemModel(parent) {
_roleNameMapping[TreeModelRoleName] = "name";
_roleNameMapping[TreeModelRoleType] = "type";
_roleNameMapping[TreeModelRoleData] = "data";
QList<QVariant> rootData;
rootData << "Name" << "Type" << "Data";
_rootItem = new TreeItem(rootData);
_clipboard = nullptr;
}
TreeModel::~TreeModel() {
delete _rootItem;
}
QHash<int, QByteArray> TreeModel::roleNames() const {
return _roleNameMapping;
}
Qt::ItemFlags TreeModel::flags(const QModelIndex& index) const {
if (!index.isValid()) {
return Qt::NoItemFlags;
}
return QAbstractItemModel::flags(index);
}
QVariant TreeModel::data(const QModelIndex& index, int role) const {
TreeItem* item = getItem(index);
return item->data(role - Qt::UserRole - 1);
}
QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
return _rootItem->data(section);
}
return QVariant();
}
QModelIndex TreeModel::index(int row, int column, const QModelIndex& parent) const {
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
TreeItem *parentItem;
if (!parent.isValid()) {
parentItem = _rootItem;
} else {
parentItem = static_cast<TreeItem*>(parent.internalPointer());
}
TreeItem *childItem = parentItem->child(row);
if (childItem) {
return createIndex(row, column, childItem);
} else {
return QModelIndex();
}
}
QModelIndex TreeModel::parent(const QModelIndex& index) const {
if (!index.isValid()) {
return QModelIndex();
}
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem *parentItem = childItem->parentItem();
if (parentItem == _rootItem) {
return QModelIndex();
}
return createIndex(parentItem->row(), 0, parentItem);
}
int TreeModel::rowCount(const QModelIndex& parent) const {
TreeItem* parentItem = getItem(parent);
return parentItem->childCount();
}
int TreeModel::columnCount(const QModelIndex& parent) const {
return _rootItem->columnCount();
}
bool TreeModel::setData(const QModelIndex& index, const QVariant& value, int role) {
TreeItem* item = getItem(index);
bool returnValue = item->setData(role - Qt::UserRole - 1, value);
emit dataChanged(index, index);
return returnValue;
}
bool TreeModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role) {
return false;
}
bool TreeModel::insertColumns(int position, int columns, const QModelIndex& parent) {
return false;
}
bool TreeModel::removeColumns(int position, int columns, const QModelIndex& parent) {
return false;
}
bool TreeModel::insertRows(int position, int rows, const QModelIndex& parent) {
return false;
}
bool TreeModel::removeRows(int position, int rows, const QModelIndex& parent) {
return false;
}
void TreeModel::loadFromFile(const QString& filename) {
beginResetModel();
QFile file(filename);
if (!file.exists()) {
qCritical() << "TreeModel::loadFromFile, failed to open file" << filename;
} else if (!file.open(QIODevice::ReadOnly)) {
qCritical() << "TreeModel::loadFromFile, failed to open file" << filename;
} else {
qDebug() << "TreeModel::loadFromFile, success opening file" << filename;
QByteArray contents = file.readAll();
QJsonParseError error;
auto doc = QJsonDocument::fromJson(contents, &error);
if (error.error != QJsonParseError::NoError) {
qCritical() << "TreeModel::loadFromFile, failed to parse json, error" << error.errorString();
} else {
QJsonObject obj = doc.object();
// version
QJsonValue versionVal = obj.value("version");
if (!versionVal.isString()) {
qCritical() << "TreeModel::loadFromFile, bad string \"version\"";
return;
}
QString version = versionVal.toString();
// check version
if (version != "1.0" && version != "1.1") {
qCritical() << "TreeModel::loadFromFile, bad version number" << version << "expected \"1.0\" or \"1.1\"";
return;
}
// root
QJsonValue rootVal = obj.value("root");
if (!rootVal.isObject()) {
qCritical() << "TreeModel::loadFromFile, bad object \"root\"";
return;
}
QList<QVariant> columnData;
columnData << QString("root");
columnData << QString("root");
columnData << QString("root");
// create root item
_rootItem = new TreeItem(columnData);
_rootItem->appendChild(loadNode(rootVal.toObject()));
}
}
endResetModel();
}
void TreeModel::saveToFile(const QString& filename) {
QJsonObject obj;
obj.insert("version", "1.1");
const int FIRST_CHILD = 0;
obj.insert("root", jsonFromItem(_rootItem->child(FIRST_CHILD)));
QJsonDocument doc(obj);
QByteArray byteArray = doc.toJson(QJsonDocument::Indented);
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
qCritical() << "TreeModel::safeToFile, failed to open file" << filename;
} else {
file.write(byteArray);
}
}
void TreeModel::newNode(const QModelIndex& parent) {
TreeItem* parentItem = _rootItem;
if (parent.isValid()) {
parentItem = static_cast<TreeItem*>(parent.internalPointer());
}
beginInsertRows(parent, parentItem->childCount(), parentItem->childCount());
TreeItem* childItem = newBlankTreeItem();
parentItem->appendChild(childItem);
endInsertRows();
}
void TreeModel::deleteNode(const QModelIndex& index) {
TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
TreeItem* parentItem = item->parentItem();
int childNum = parentItem->findChild(item);
if (childNum >= 0) {
beginRemoveRows(createIndex(0, 0, reinterpret_cast<quintptr>(parentItem)), childNum, childNum);
parentItem->removeChild(childNum);
endRemoveRows();
}
}
void TreeModel::insertNodeAbove(const QModelIndex& index) {
TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
TreeItem* parentItem = item->parentItem();
int childNum = parentItem->findChild(item);
if (childNum >= 0) {
TreeItem* newItem = newBlankTreeItem();
QModelIndex parentIndex = createIndex(0, 0, reinterpret_cast<quintptr>(parentItem));
// remove item
beginRemoveRows(parentIndex, childNum, childNum);
parentItem->removeChild(childNum);
endRemoveRows();
// append item to newItem
newItem->appendChild(item);
// then insert newItem
beginInsertRows(parentIndex, childNum, childNum);
parentItem->insertChild(childNum, newItem);
endInsertRows();
}
}
QVariantList TreeModel::getChildrenModelIndices(const QModelIndex& index) {
QVariantList indices;
TreeItem* parent = static_cast<TreeItem*>(index.internalPointer());
for (int i = 0; i < parent->childCount(); ++i) {
TreeItem* child = parent->child(i);
indices.push_back(createIndex(i, 0, reinterpret_cast<quintptr>(child)));
}
return indices;
}
void TreeModel::copyNode(const QModelIndex& index) {
TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
// TODO: delete previous clipboard
_clipboard = item->cloneNode();
}
void TreeModel::copyNodeAndChildren(const QModelIndex& index) {
TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
// TODO: delete previous clipboard
_clipboard = item->cloneNodeAndChildren();
}
void TreeModel::pasteOver(const QModelIndex& index) {
if (_clipboard) {
TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
TreeItem* parentItem = item->parentItem();
int childNum = parentItem->findChild(item);
if (childNum >= 0) {
QModelIndex parentIndex = createIndex(0, 0, reinterpret_cast<quintptr>(parentItem));
// remove item
beginRemoveRows(parentIndex, childNum, childNum);
parentItem->removeChild(childNum);
endRemoveRows();
// then insert clone of _clipboard
beginInsertRows(parentIndex, childNum, childNum);
parentItem->insertChild(childNum, _clipboard->cloneNodeAndChildren());
endInsertRows();
}
}
}
void TreeModel::pasteAsChild(const QModelIndex& index) {
if (_clipboard) {
TreeItem* parentItem = _rootItem;
if (index.isValid()) {
parentItem = static_cast<TreeItem*>(index.internalPointer());
}
beginInsertRows(index, parentItem->childCount(), parentItem->childCount());
parentItem->appendChild(_clipboard->cloneNodeAndChildren());
endInsertRows();
}
}
TreeItem* TreeModel::loadNode(const QJsonObject& jsonObj) {
// id
auto idVal = jsonObj.value("id");
if (!idVal.isString()) {
qCritical() << "loadNode, bad string \"id\"";
return nullptr;
}
QString id = idVal.toString();
// type
auto typeVal = jsonObj.value("type");
if (!typeVal.isString()) {
qCritical() << "loadNode, bad object \"type\", id =" << id;
return nullptr;
}
QString typeStr = typeVal.toString();
// data
auto dataValue = jsonObj.value("data");
if (!dataValue.isObject()) {
qCritical() << "AnimNodeLoader, bad string \"data\", id =" << id;
return nullptr;
}
QList<QVariant> columnData;
columnData << id;
columnData << typeStr;
columnData << dataValue.toVariant();
// create node
TreeItem* node = new TreeItem(columnData);
// children
auto childrenValue = jsonObj.value("children");
if (!childrenValue.isArray()) {
qCritical() << "AnimNodeLoader, bad array \"children\", id =" << id;
return nullptr;
}
auto childrenArray = childrenValue.toArray();
for (const auto& childValue : childrenArray) {
if (!childValue.isObject()) {
qCritical() << "AnimNodeLoader, bad object in \"children\", id =" << id;
return nullptr;
}
TreeItem* child = loadNode(childValue.toObject());
if (child) {
node->appendChild(child);
} else {
return nullptr;
}
}
return node;
}
TreeItem* TreeModel::getItem(const QModelIndex& index) const {
if (index.isValid()) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item) {
return item;
}
}
return _rootItem;
}
QJsonObject TreeModel::jsonFromItem(TreeItem* treeItem) {
QJsonObject obj;
const int ID_COLUMN = 0;
obj.insert("id", treeItem->data(ID_COLUMN).toJsonValue());
const int TYPE_COLUMN = 1;
obj.insert("type", treeItem->data(TYPE_COLUMN).toJsonValue());
const int DATA_COLUMN = 2;
QVariant data = treeItem->data(DATA_COLUMN);
if (data.canConvert<QJSValue>()) {
obj.insert("data", data.value<QJSValue>().toVariant().toJsonValue());
} else {
obj.insert("data", data.toJsonValue());
}
QJsonArray children;
for (int i = 0; i < treeItem->childCount(); i++) {
children.push_back(jsonFromItem(treeItem->child(i)));
}
obj.insert("children", children);
return obj;
}