FlightGear next
FGQmlPropertyNode.hxx
Go to the documentation of this file.
1// FGQmlPropertyNode.hxx - expose SGPropertyNode to QML
2//
3// Copyright (C) 2019 James Turner <james@flightgear.org>
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License as
7// published by the Free Software Foundation; either version 2 of the
8// License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful, but
11// WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19#ifndef FGQMLPROPERTYNODE_HXX
20#define FGQMLPROPERTYNODE_HXX
21
22#include <QObject>
23#include <QQmlListProperty>
24#include <QVariant>
25
26#include <simgear/props/props.hxx>
27
28#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
29#define QML_LIST_INDEX_TYPE qsizetype
30#else
31#define QML_LIST_INDEX_TYPE int
32#endif
33
34class FGQmlPropertyNode : public QObject,
35 public SGPropertyChangeListener
36{
37 Q_OBJECT
38public:
39 explicit FGQmlPropertyNode(QObject *parent = nullptr);
40
41 Q_PROPERTY(QVariant value READ value NOTIFY valueChangedNotify)
42 Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
44
45 Q_PROPERTY(QQmlListProperty<FGQmlPropertyNode> childProps READ childProps NOTIFY childPropsChanged)
46
47
48 Q_INVOKABLE bool set(QVariant newValue);
49
50 // children accessor
51 QVariant value() const;
52
53 QString path() const;
54
56
57 // C++ API, not accessible from QML
58 void setNode(SGPropertyNode_ptr node);
59
60 SGPropertyNode_ptr node() const;
61
62 QQmlListProperty<FGQmlPropertyNode> childProps()
63 {
64 return QQmlListProperty<FGQmlPropertyNode>(this, nullptr,
65 children_count,
66 child_at);
67 }
68
69
70 int childCount() const;
71 FGQmlPropertyNode* childAt(int index) const;
72
73 static QVariant propertyValueAsVariant(SGPropertyNode* p);
74
75protected:
76 // SGPropertyChangeListener API
77
78 void valueChanged(SGPropertyNode * node) override;
79 void childAdded(SGPropertyNode* pr, SGPropertyNode* child) override;
80 void childRemoved(SGPropertyNode* pr, SGPropertyNode* child) override;
81
82 signals:
83
84 void valueChangedNotify(QVariant value);
85
86 void pathChanged(QString path);
87
90
91 public slots:
92
93 void setPath(QString path);
94
95 private:
96 static QML_LIST_INDEX_TYPE children_count(QQmlListProperty<FGQmlPropertyNode>* prop)
97 {
98 return static_cast<FGQmlPropertyNode*>(prop->object)->childCount();
99 }
100
101 static FGQmlPropertyNode* child_at(QQmlListProperty<FGQmlPropertyNode>* prop,
103 {
104 return static_cast<FGQmlPropertyNode*>(prop->object)->childAt(index);
105 }
106
107 SGPropertyNode_ptr _prop;
108};
109
110#endif // FGQMLPROPERTYNODE_HXX
#define p(x)
#define QML_LIST_INDEX_TYPE
Q_INVOKABLE bool set(QVariant newValue)
FGQmlPropertyNode * childAt(int index) const
void valueChangedNotify(QVariant value)
void parentPropChanged(FGQmlPropertyNode *parentProp)
SGPropertyNode_ptr node() const
static QVariant propertyValueAsVariant(SGPropertyNode *p)
QQmlListProperty< FGQmlPropertyNode > childProps
void setNode(SGPropertyNode_ptr node)
void childPropsChanged()
void pathChanged(QString path)
FGQmlPropertyNode * parentProp
void setPath(QString path)
void childRemoved(SGPropertyNode *pr, SGPropertyNode *child) override
void childAdded(SGPropertyNode *pr, SGPropertyNode *child) override
FGQmlPropertyNode(QObject *parent=nullptr)
void valueChanged(SGPropertyNode *node) override