| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QT3DCORE_QNODE_P_H |
| 5 | #define QT3DCORE_QNODE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of other Qt classes. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <Qt3DCore/qnode.h> |
| 19 | |
| 20 | #include <functional> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <Qt3DCore/private/propertychangehandler_p.h> |
| 24 | #include <Qt3DCore/private/qchangearbiter_p.h> |
| 25 | #include <Qt3DCore/private/qscene_p.h> |
| 26 | #include <Qt3DCore/private/qt3dcore_global_p.h> |
| 27 | #include <QtCore/private/qobject_p.h> |
| 28 | #include <QQueue> |
| 29 | |
| 30 | QT_BEGIN_NAMESPACE |
| 31 | |
| 32 | namespace Qt3DCore { |
| 33 | |
| 34 | class QNode; |
| 35 | class QAspectEngine; |
| 36 | |
| 37 | class Q_3DCORE_PRIVATE_EXPORT QNodePrivate : public QObjectPrivate |
| 38 | { |
| 39 | public: |
| 40 | QNodePrivate(); |
| 41 | ~QNodePrivate(); |
| 42 | |
| 43 | void init(QNode *parent); |
| 44 | |
| 45 | virtual void setScene(QScene *scene); |
| 46 | QScene *scene() const; |
| 47 | |
| 48 | void setArbiter(QChangeArbiter *arbiter); |
| 49 | |
| 50 | void notifyPropertyChange(const char *name, const QVariant &value); |
| 51 | void notifyDynamicPropertyChange(const QByteArray &name, const QVariant &value); |
| 52 | |
| 53 | void insertTree(QNode *treeRoot, int depth = 0); |
| 54 | void updatePropertyTrackMode(); |
| 55 | |
| 56 | virtual void update(); |
| 57 | void markDirty(QScene::DirtyNodeSet changes); |
| 58 | |
| 59 | Q_DECLARE_PUBLIC(QNode) |
| 60 | |
| 61 | // For now this just protects access to the m_changeArbiter. |
| 62 | // Later on we may decide to extend support for multiple observers. |
| 63 | QChangeArbiter *m_changeArbiter; |
| 64 | QMetaObject *m_typeInfo; |
| 65 | QScene *m_scene; |
| 66 | mutable QNodeId m_id; |
| 67 | QNodeId m_parentId; // Store this so we have it even in parent's QObject dtor |
| 68 | bool m_blockNotifications; |
| 69 | bool m_hasBackendNode; |
| 70 | bool m_enabled; |
| 71 | bool m_notifiedParent; |
| 72 | |
| 73 | static QNodePrivate *get(QNode *q); |
| 74 | static const QNodePrivate *get(const QNode *q); |
| 75 | static void nodePtrDeleter(QNode *q); |
| 76 | |
| 77 | template<typename Caller, typename NodeType> |
| 78 | using DestructionFunctionPointer = void (Caller::*)(NodeType *); |
| 79 | |
| 80 | template<typename Caller, typename NodeType, typename PropertyType> |
| 81 | void registerDestructionHelper(NodeType *, DestructionFunctionPointer<Caller, NodeType>, PropertyType); |
| 82 | |
| 83 | template<typename Caller, typename NodeType> |
| 84 | void registerDestructionHelper(NodeType *node, DestructionFunctionPointer<Caller, NodeType> func, NodeType *&) |
| 85 | { |
| 86 | // If the node is destoyed, we make sure not to keep a dangling pointer to it |
| 87 | Q_Q(QNode); |
| 88 | auto f = [q, func]() { (static_cast<Caller *>(q)->*func)(nullptr); }; |
| 89 | m_destructionConnections.push_back({node, QObject::connect(node, &QNode::nodeDestroyed, q, f)}); |
| 90 | } |
| 91 | |
| 92 | template<typename Caller, typename NodeType> |
| 93 | void registerDestructionHelper(NodeType *node, DestructionFunctionPointer<Caller, NodeType> func, QList<NodeType*> &) |
| 94 | { |
| 95 | // If the node is destoyed, we make sure not to keep a dangling pointer to it |
| 96 | Q_Q(QNode); |
| 97 | auto f = [q, func, node]() { (static_cast<Caller *>(q)->*func)(node); }; |
| 98 | m_destructionConnections.push_back({node, QObject::connect(node, &QNode::nodeDestroyed, q, f)}); |
| 99 | } |
| 100 | |
| 101 | template<typename Caller, typename NodeType> |
| 102 | void registerDestructionHelper(NodeType *node, DestructionFunctionPointer<Caller, NodeType> func, std::vector<NodeType*> &) |
| 103 | { |
| 104 | // If the node is destoyed, we make sure not to keep a dangling pointer to it |
| 105 | Q_Q(QNode); |
| 106 | auto f = [q, func, node]() { (static_cast<Caller *>(q)->*func)(node); }; |
| 107 | m_destructionConnections.push_back({node, QObject::connect(node, &QNode::nodeDestroyed, q, f)}); |
| 108 | } |
| 109 | |
| 110 | template<typename Caller, typename ValueType> |
| 111 | using DestructionFunctionValue = void (Caller::*)(const ValueType&); |
| 112 | |
| 113 | template<typename Caller, typename NodeType, typename ValueType> |
| 114 | void registerDestructionHelper(NodeType *node, DestructionFunctionValue<Caller, ValueType> func, NodeType *&, |
| 115 | const ValueType &resetValue) |
| 116 | { |
| 117 | // If the node is destoyed, we make sure not to keep a dangling pointer to it |
| 118 | Q_Q(QNode); |
| 119 | auto f = [q, func, resetValue]() { (static_cast<Caller *>(q)->*func)(resetValue); }; |
| 120 | m_destructionConnections.push_back({node, QObject::connect(node, &QNode::nodeDestroyed, q, f)}); |
| 121 | } |
| 122 | |
| 123 | template<typename Caller, typename NodeType> |
| 124 | void registerPrivateDestructionHelper(NodeType *node, DestructionFunctionPointer<Caller, NodeType> func) |
| 125 | { |
| 126 | Q_Q(QNode); |
| 127 | // If the node is destoyed, we make sure not to keep a dangling pointer to it |
| 128 | auto f = [this, func, node]() { (static_cast<Caller *>(this)->*func)(node); }; |
| 129 | m_destructionConnections.push_back({node, QObject::connect(node, &QNode::nodeDestroyed, q, f)}); |
| 130 | } |
| 131 | |
| 132 | void unregisterDestructionHelper(QNode *node) |
| 133 | { |
| 134 | m_destructionConnections.erase(begin: std::remove_if(first: m_destructionConnections.begin(), |
| 135 | last: m_destructionConnections.end(), |
| 136 | pred: [node] (const QPair<QNode *, QMetaObject::Connection> &nodeConnectionPair) { |
| 137 | if (nodeConnectionPair.first == node) { |
| 138 | QObject::disconnect(nodeConnectionPair.second); |
| 139 | return true; |
| 140 | } |
| 141 | return false; |
| 142 | }), |
| 143 | end: m_destructionConnections.end()); |
| 144 | } |
| 145 | |
| 146 | static const QMetaObject *findStaticMetaObject(const QMetaObject *metaObject); |
| 147 | |
| 148 | void _q_postConstructorInit(); |
| 149 | void _q_ensureBackendNodeCreated(); |
| 150 | |
| 151 | private: |
| 152 | void createBackendNode(); |
| 153 | void notifyDestructionChangesAndRemoveFromScene(); |
| 154 | void _q_addChild(QNode *childNode); |
| 155 | void _q_removeChild(QNode *childNode); |
| 156 | void _q_setParentHelper(QNode *parent); |
| 157 | void registerNotifiedProperties(); |
| 158 | void unregisterNotifiedProperties(); |
| 159 | void propertyChanged(int propertyIndex); |
| 160 | |
| 161 | void setSceneHelper(QNode *root); |
| 162 | void unsetSceneHelper(QNode *root); |
| 163 | void addEntityComponentToScene(QNode *root); |
| 164 | |
| 165 | friend class PropertyChangeHandler<QNodePrivate>; |
| 166 | bool m_propertyChangesSetup; |
| 167 | PropertyChangeHandler<QNodePrivate> m_signals; |
| 168 | QList<QPair<QNode *, QMetaObject::Connection>> m_destructionConnections; |
| 169 | }; |
| 170 | |
| 171 | class NodePostConstructorInit : public QObject |
| 172 | { |
| 173 | Q_OBJECT |
| 174 | public: |
| 175 | NodePostConstructorInit(QObject *parent = nullptr); |
| 176 | virtual ~NodePostConstructorInit(); |
| 177 | void removeNode(QNode *node); |
| 178 | void addNode(QNode *node); |
| 179 | |
| 180 | public Q_SLOTS: |
| 181 | void processNodes(); |
| 182 | |
| 183 | private: |
| 184 | QQueue<QNodePrivate *> m_nodesToConstruct; |
| 185 | bool m_requestedProcessing; |
| 186 | }; |
| 187 | |
| 188 | } // namespace Qt3DCore |
| 189 | |
| 190 | QT_END_NAMESPACE |
| 191 | |
| 192 | #endif // QT3DCORE_NODE_P_H |
| 193 | |