1/****************************************************************************
2**
3** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QT3DCORE_QNODE_H
41#define QT3DCORE_QNODE_H
42
43#include <Qt3DCore/qnodecreatedchange.h>
44#include <Qt3DCore/qnodeid.h>
45#include <Qt3DCore/qnodecommand.h>
46#include <Qt3DCore/qscenechange.h>
47#include <Qt3DCore/qt3dcore_global.h>
48#include <QtCore/QObject>
49
50#define Q_NODE_NULLPTR static_cast<Qt3DCore::QNode *>(nullptr)
51
52QT_BEGIN_NAMESPACE
53
54namespace Qt3DCore {
55
56class QNode;
57class QNodePrivate;
58class QEntity;
59class QAspectEngine;
60
61#if defined(QT_BUILD_INTERNAL)
62class QBackendNodeTester;
63#endif
64
65typedef QVector<QNode *> QNodeVector;
66typedef QSharedPointer<QNode> QNodePtr;
67
68class Q_3DCORESHARED_EXPORT QNode : public QObject
69{
70 Q_OBJECT
71 Q_PROPERTY(Qt3DCore::QNode *parent READ parentNode WRITE setParent NOTIFY parentChanged)
72 Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
73 Q_PROPERTY(PropertyTrackingMode defaultPropertyTrackingMode READ defaultPropertyTrackingMode WRITE setDefaultPropertyTrackingMode NOTIFY defaultPropertyTrackingModeChanged REVISION 9)
74public:
75
76 enum PropertyTrackingMode : quint16 {
77 TrackFinalValues,
78 DontTrackValues,
79 TrackAllValues
80 };
81 Q_ENUM(PropertyTrackingMode)
82
83 explicit QNode(QNode *parent = nullptr);
84 virtual ~QNode();
85
86 QNodeId id() const;
87 QNode *parentNode() const;
88
89 bool notificationsBlocked() const;
90 bool blockNotifications(bool block);
91
92 QNodeVector childNodes() const;
93
94 bool isEnabled() const;
95 PropertyTrackingMode defaultPropertyTrackingMode() const;
96
97 void setPropertyTracking(const QString &propertyName, PropertyTrackingMode trackMode);
98 PropertyTrackingMode propertyTracking(const QString &propertyName) const;
99 void clearPropertyTracking(const QString &propertyName);
100 void clearPropertyTrackings();
101
102 QNodeCommand::CommandId sendCommand(const QString &name, const QVariant &data = QVariant(),
103 QNodeCommand::CommandId replyTo = QNodeCommand::CommandId());
104 void sendReply(const QNodeCommandPtr &command);
105
106public Q_SLOTS:
107 void setParent(QNode *parent);
108 void setEnabled(bool isEnabled);
109 void setDefaultPropertyTrackingMode(PropertyTrackingMode mode);
110
111Q_SIGNALS:
112 void parentChanged(QObject *parent);
113 void enabledChanged(bool enabled);
114 void defaultPropertyTrackingModeChanged(PropertyTrackingMode mode);
115 void nodeDestroyed();
116
117protected:
118 explicit QNode(QNodePrivate &dd, QNode *parent = nullptr);
119 void notifyObservers(const QSceneChangePtr &change);
120 virtual void sceneChangeEvent(const QSceneChangePtr &change);
121
122private:
123 Q_DECLARE_PRIVATE(QNode)
124 virtual QNodeCreatedChangeBasePtr createNodeCreationChange() const;
125
126 // We only want setParent(QNode *) to be callable
127 // when dealing with QNode objects
128 void setParent(QObject *) Q_DECL_EQ_DELETE;
129
130 Q_PRIVATE_SLOT(d_func(), void _q_postConstructorInit())
131 Q_PRIVATE_SLOT(d_func(), void _q_addChild(Qt3DCore::QNode *))
132 Q_PRIVATE_SLOT(d_func(), void _q_removeChild(Qt3DCore::QNode *))
133 Q_PRIVATE_SLOT(d_func(), void _q_setParentHelper(Qt3DCore::QNode *))
134
135 friend class QAspectEngine;
136 friend class QAspectEnginePrivate;
137 friend class QNodeCreatedChangeGenerator;
138 friend class QPostman;
139 friend class QScene;
140
141#if defined(QT_BUILD_INTERNAL)
142 friend class QBackendNodeTester;
143#endif
144};
145
146inline QNodeId qIdForNode(QNode *node){ return node ? node->id() : QNodeId(); }
147
148template<typename T>
149inline QNodeIdVector qIdsForNodes(const T &nodes)
150{
151 QNodeIdVector ids;
152 ids.reserve(nodes.size());
153 for (const auto n : nodes)
154 ids.push_back(n->id());
155 return ids;
156}
157
158struct QNodeIdTypePair
159{
160 QNodeIdTypePair() Q_DECL_NOTHROW
161 : id()
162 , type(nullptr)
163 {}
164
165 explicit QNodeIdTypePair(QNodeId _id, const QMetaObject *_type) Q_DECL_NOTHROW
166 : id(_id)
167 , type(_type)
168 {}
169
170 QNodeId id;
171 const QMetaObject *type;
172};
173QT3D_DECLARE_TYPEINFO(Qt3DCore, QNodeIdTypePair, Q_PRIMITIVE_TYPE)
174
175} // namespace Qt3DCore
176
177QT_END_NAMESPACE
178
179#endif
180