QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QmlObjectTreeModel.h
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * (c) 2009-2024 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 *
5 * QGroundControl is licensed according to the terms in the file
6 * COPYING.md in the root of the source code directory.
7 *
8 ****************************************************************************/
9
10#pragma once
11
12#include "ObjectItemModelBase.h"
13
14#include <QtQmlIntegration/QtQmlIntegration>
15
25{
29
30public:
31 explicit QmlObjectTreeModel(QObject* parent = nullptr);
32 ~QmlObjectTreeModel() override;
33
34 // -- ObjectItemModelBase overrides --
35 int count() const override;
36 void setDirty(bool dirty) override;
37 void clear() override;
38
39 // -- QAbstractItemModel overrides --
40 QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
41 QModelIndex parent(const QModelIndex& child) const override;
42 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
43 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
44 bool hasChildren(const QModelIndex& parent = QModelIndex()) const override;
45 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
46 bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
47 bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
48 bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
49 QHash<int, QByteArray> roleNames() const override;
50
51 // -- QML-accessible tree operations --
52
54 Q_INVOKABLE QObject* getObject(const QModelIndex& index) const;
55
58
60 Q_INVOKABLE QModelIndex appendItem(QObject* object, const QModelIndex& parentIndex, const QString& nodeType);
61
63 Q_INVOKABLE QModelIndex insertItem(int row, QObject* object, const QModelIndex& parentIndex = QModelIndex());
64
66 Q_INVOKABLE QModelIndex insertItem(int row, QObject* object, const QModelIndex& parentIndex, const QString& nodeType);
67
70 Q_INVOKABLE QObject* removeItem(const QModelIndex& index);
71
74
76 Q_INVOKABLE QModelIndex childIndex(int row, const QModelIndex& parentIndex = QModelIndex()) const { return index(row, 0, parentIndex); }
77
79 Q_INVOKABLE QModelIndex indexForObject(QObject* object) const;
80
83
85 Q_INVOKABLE int depth(const QModelIndex& index) const;
86
87 // -- C++ convenience API --
88 void appendRootItem(QObject* object);
89 void appendChild(const QModelIndex& parentIndex, QObject* object);
90 QObject* removeAt(const QModelIndex& parentIndex, int row);
93 bool contains(QObject* object) const;
94
95 static constexpr int NodeTypeRole = Qt::UserRole + 2;
96 static constexpr int SeparatorRole = Qt::UserRole + 3;
97
98private:
99 struct TreeNode {
100 QObject* object = nullptr;
101 TreeNode* parentNode = nullptr;
102 QList<TreeNode*> children;
103 QString nodeType;
104
106 int row() const;
107 };
108
109 TreeNode* _nodeFromIndex(const QModelIndex& index) const;
110 QModelIndex _indexForNode(const TreeNode* node) const;
111 TreeNode* _findNode(const TreeNode* root, const QObject* object) const;
112 static int _subtreeCount(const TreeNode* node);
113 void _disconnectSubtree(TreeNode* node);
114 void _deleteSubtree(TreeNode* node, bool deleteObjects);
115 void _connectDirtyChanged(QObject* object);
116 void _disconnectDirtyChanged(QObject* object);
117 void _emitSeparatorChanged(const QModelIndex& parentIdx, int fromRow);
118
119 TreeNode _rootNode;
120 int _totalCount = 0;
121};
Common base for QObject*-based item models (flat lists and trees).
A tree model for QObject* items, usable from both C++ and QML.
void appendChild(const QModelIndex &parentIndex, QObject *object)
void setDirty(bool dirty) override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Q_INVOKABLE QModelIndex childIndex(int row, const QModelIndex &parentIndex=QModelIndex()) const
Returns the QModelIndex for the child at row under parentIndex.
Q_INVOKABLE int childCount(const QModelIndex &parentIndex=QModelIndex()) const
Number of direct children under parentIndex.
static constexpr int NodeTypeRole
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int count() const override
void removeChildren(const QModelIndex &parentIndex)
Removes all children of parentIndex without removing the parent itself.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Q_INVOKABLE QModelIndex parentIndex(const QModelIndex &index) const
Convenience wrapper around parent()
Q_INVOKABLE QObject * removeItem(const QModelIndex &index)
QObject * removeAt(const QModelIndex &parentIndex, int row)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void appendRootItem(QObject *object)
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Q_INVOKABLE QModelIndex indexForObject(QObject *object) const
Searches the entire tree for object and returns its QModelIndex (invalid if not found)
void clearAndDeleteContents()
Clears the tree and calls deleteLater on every QObject.
Q_INVOKABLE QModelIndex insertItem(int row, QObject *object, const QModelIndex &parentIndex=QModelIndex())
Inserts object at row under parentIndex. Returns the new item's index.
Q_INVOKABLE int depth(const QModelIndex &index) const
Returns the depth of index (0 = root-level item, -1 = invalid index)
Q_INVOKABLE QModelIndex appendItem(QObject *object, const QModelIndex &parentIndex=QModelIndex())
Appends object as the last child of parentIndex (root if invalid). Returns the new item's index.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
bool contains(QObject *object) const
QHash< int, QByteArray > roleNames() const override
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
Q_INVOKABLE QObject * getObject(const QModelIndex &index) const
Returns the QObject* stored at index, or nullptr if invalid.
static constexpr int SeparatorRole
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
QModelIndex parent(const QModelIndex &child) const override