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 <QtCore/QLoggingCategory>
15#include <QtQmlIntegration/QtQmlIntegration>
16
17Q_DECLARE_LOGGING_CATEGORY(QmlObjectTreeModelLog)
18
19
26{
27 Q_OBJECT
28 QML_ELEMENT
29 QML_UNCREATABLE("")
30
31public:
32 explicit QmlObjectTreeModel(QObject* parent = nullptr);
33 ~QmlObjectTreeModel() override;
34
35 // -- ObjectItemModelBase overrides --
36 int count() const override;
37 void setDirty(bool dirty) override;
38 void clear() override;
39
40 // -- QAbstractItemModel overrides --
41 QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
42 QModelIndex parent(const QModelIndex& child) const override;
43 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
44 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
45 bool hasChildren(const QModelIndex& parent = QModelIndex()) const override;
46 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
47 bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
48 bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
49 bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
50 QHash<int, QByteArray> roleNames() const override;
51
52 // -- QML-accessible tree operations --
53
55 Q_INVOKABLE QObject* getObject(const QModelIndex& index) const;
56
58 Q_INVOKABLE QModelIndex appendItem(QObject* object, const QModelIndex& parentIndex = QModelIndex());
59
61 Q_INVOKABLE QModelIndex appendItem(QObject* object, const QModelIndex& parentIndex, const QString& nodeType);
62
64 Q_INVOKABLE QModelIndex insertItem(int row, QObject* object, const QModelIndex& parentIndex = QModelIndex());
65
67 Q_INVOKABLE QModelIndex insertItem(int row, QObject* object, const QModelIndex& parentIndex, const QString& nodeType);
68
71 Q_INVOKABLE QObject* removeItem(const QModelIndex& index);
72
74 Q_INVOKABLE int childCount(const QModelIndex& parentIndex = QModelIndex()) const { return rowCount(parentIndex); }
75
77 Q_INVOKABLE QModelIndex childIndex(int row, const QModelIndex& parentIndex = QModelIndex()) const { return index(row, 0, parentIndex); }
78
80 Q_INVOKABLE QModelIndex indexForObject(QObject* object) const;
81
83 Q_INVOKABLE QModelIndex parentIndex(const QModelIndex& index) const { return parent(index); }
84
86 Q_INVOKABLE int depth(const QModelIndex& index) const;
87
88 // -- C++ convenience API --
89 void appendRootItem(QObject* object);
90 void appendChild(const QModelIndex& parentIndex, QObject* object);
91 QObject* removeAt(const QModelIndex& parentIndex, int row);
92 void removeChildren(const QModelIndex& parentIndex);
93 void clearAndDeleteContents();
94 bool contains(QObject* object) const;
95
96 static constexpr int NodeTypeRole = Qt::UserRole + 2;
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
118 TreeNode _rootNode;
119 int _totalCount = 0;
120};
Q_DECLARE_LOGGING_CATEGORY(AndroidSerialLog)
QModelIndex parentIndex(const QModelIndex &index) const
Convenience wrapper around parent()
QModelIndex childIndex(int row, const QModelIndex &parentIndex=QModelIndex()) const
Returns the QModelIndex for the child at row under parentIndex.
int childCount(const QModelIndex &parentIndex=QModelIndex()) const
Number of direct children under parentIndex.