-
Notifications
You must be signed in to change notification settings - Fork 6
/
treemodel.h
79 lines (66 loc) · 2.93 KB
/
treemodel.h
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
//
// 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
//
#ifndef hifi_TreeModel_h
#define hifi_TreeModel_h
#include <QAbstractItemModel>
#include <QJsonObject>
#include <QModelIndex>
#include <QObject>
#include <QString>
#include <QVariant>
class TreeItem;
class TreeModel : public QAbstractItemModel {
Q_OBJECT
public:
enum TreeModelRoles
{
TreeModelRoleName = Qt::UserRole + 1,
TreeModelRoleType,
TreeModelRoleData
};
explicit TreeModel(QObject* parent = nullptr);
~TreeModel() override;
// QAbstractItemModel interface
QHash<int, QByteArray> roleNames() const override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
// read methods
QVariant data(const QModelIndex& index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = TreeModelRoleName) const override;
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex& index) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
// write methods
bool setData(const QModelIndex& index, const QVariant& value, int role = TreeModelRoleName) override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role = TreeModelRoleName) override;
bool insertColumns(int position, int columns, const QModelIndex& parent = QModelIndex()) override;
bool removeColumns(int position, int columns, const QModelIndex& parent = QModelIndex()) override;
bool insertRows(int position, int rows, const QModelIndex& parent = QModelIndex()) override;
bool removeRows(int position, int rows, const QModelIndex& parent = QModelIndex()) override;
// invokable from qml
Q_INVOKABLE void loadFromFile(const QString& filename);
Q_INVOKABLE void saveToFile(const QString& filename);
Q_INVOKABLE void newNode(const QModelIndex& parent);
Q_INVOKABLE void deleteNode(const QModelIndex& index);
Q_INVOKABLE void insertNodeAbove(const QModelIndex& index);
Q_INVOKABLE QVariantList getChildrenModelIndices(const QModelIndex& index);
Q_INVOKABLE void copyNode(const QModelIndex& index);
Q_INVOKABLE void copyNodeAndChildren(const QModelIndex& index);
Q_INVOKABLE void pasteOver(const QModelIndex& index);
Q_INVOKABLE void pasteAsChild(const QModelIndex& index);
private:
TreeItem* loadNode(const QJsonObject& jsonObj);
TreeItem* getItem(const QModelIndex& index) const;
QJsonObject jsonFromItem(TreeItem* treeItem);
TreeItem* _rootItem;
QHash<int, QByteArray> _roleNameMapping;
TreeItem* _clipboard;
};
#endif