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#include "qbackendnode.h"
41#include "qbackendnode_p.h"
42
43#include <Qt3DCore/qaspectengine.h>
44#include <Qt3DCore/qnode.h>
45#include <Qt3DCore/qnodecommand.h>
46#include <Qt3DCore/qpropertyupdatedchange.h>
47
48#include <Qt3DCore/private/corelogging_p.h>
49
50QT_BEGIN_NAMESPACE
51
52namespace Qt3DCore {
53
54/*! \internal */
55QBackendNodeMapper::~QBackendNodeMapper()
56{
57}
58
59QBackendNodePrivate::QBackendNodePrivate(QBackendNode::Mode mode)
60 : q_ptr(nullptr)
61 , m_mode(mode)
62 , m_arbiter(nullptr)
63 , m_enabled(false)
64{
65}
66
67// Called by backend thread (renderer or other) while we are locked to sync changes
68void QBackendNodePrivate::setArbiter(QLockableObserverInterface *arbiter)
69{
70 Q_ASSERT(m_mode == QBackendNode::ReadWrite);
71 m_arbiter = arbiter;
72}
73
74// Called by backend thread/worker threads. We don't need locking
75// as setting/unsetting the arbiter cannot happen at that time
76void QBackendNodePrivate::notifyObservers(const QSceneChangePtr &e)
77{
78 Q_ASSERT(m_mode == QBackendNode::ReadWrite);
79 if (m_arbiter != nullptr)
80 m_arbiter->sceneChangeEvent(e);
81}
82
83void QBackendNodePrivate::sceneChangeEvent(const QSceneChangePtr &e)
84{
85 q_func()->sceneChangeEvent(e);
86}
87
88void QBackendNodePrivate::setEnabled(bool enabled)
89{
90 m_enabled = enabled;
91}
92
93QBackendNodePrivate *QBackendNodePrivate::get(QBackendNode *n)
94{
95 return n->d_func();
96}
97
98void QBackendNodePrivate::addedToEntity(QNode *frontend)
99{
100 Q_UNUSED(frontend)
101}
102
103void QBackendNodePrivate::removedFromEntity(QNode *frontend)
104{
105 Q_UNUSED(frontend)
106}
107
108void QBackendNodePrivate::componentAdded(QNode *frontend)
109{
110 Q_UNUSED(frontend)
111}
112
113void QBackendNodePrivate::componentRemoved(QNode *frontend)
114{
115 Q_UNUSED(frontend)
116}
117
118/*!
119 * \class Qt3DCore::QBackendNodeMapper
120 * \inheaderfile Qt3DCore/QBackendNodeMapper
121 * \inmodule Qt3DCore
122 *
123 * \brief Creates and maps backend nodes to their respective frontend nodes.
124 */
125
126/*!
127 * \fn Qt3DCore::QBackendNode *Qt3DCore::QBackendNodeMapper::create(const Qt3DCore::QNodeCreatedChangeBasePtr &change) const
128 *
129 * \TODO
130 *
131 * \a change
132 *
133 * \return created node.
134 */
135
136/*!
137 * \fn Qt3DCore::QBackendNode * Qt3DCore::QBackendNodeMapper::get(Qt3DCore::QNodeId id) const
138 *
139 * \return backend node for the given node \a id.
140 */
141
142/*!
143 * \fn void Qt3DCore::QBackendNodeMapper::destroy(Qt3DCore::QNodeId id) const
144 *
145 * Destroys the backend node for the given node \a id.
146 */
147
148/*!
149 * \class Qt3DCore::QBackendNode
150 * \inheaderfile Qt3DCore/QBackendNode
151 * \inmodule Qt3DCore
152 *
153 * \brief The base class for all Qt3D backend nodes.
154 */
155
156/*!
157 * \enum Qt3DCore::QBackendNode::Mode
158 *
159 * The mode for the backend node.
160 *
161 * \value ReadOnly
162 * \value ReadWrite
163 */
164
165QBackendNode::QBackendNode(QBackendNode::Mode mode)
166 : d_ptr(new QBackendNodePrivate(mode))
167{
168 d_ptr->q_ptr = this;
169}
170
171QBackendNode::~QBackendNode()
172{
173 delete d_ptr;
174}
175
176/*!
177 * Sets the peer \a id.
178 */
179void QBackendNode::setPeerId(QNodeId id) Q_DECL_NOTHROW
180{
181 Q_D(QBackendNode);
182 d->m_peerId = id;
183}
184
185/*!
186 * \return the peer id of the backend node.
187 */
188QNodeId QBackendNode::peerId() const Q_DECL_NOTHROW
189{
190 Q_D(const QBackendNode);
191 return d->m_peerId;
192}
193
194/*!
195 * \return \c true if the backend node is enabled.
196 */
197bool QBackendNode::isEnabled() const Q_DECL_NOTHROW
198{
199 Q_D(const QBackendNode);
200 return d->m_enabled;
201}
202
203/*!
204 * \return the mode of the backend mode.
205 */
206QBackendNode::Mode QBackendNode::mode() const Q_DECL_NOTHROW
207{
208 Q_D(const QBackendNode);
209 return d->m_mode;
210}
211
212/*!
213 * \internal
214 */
215QBackendNode::QBackendNode(QBackendNodePrivate &dd)
216 : d_ptr(&dd)
217{
218 d_ptr->q_ptr = this;
219}
220
221/*!
222 * Notifies observers of scene change \a e.
223 * \obsolete
224 */
225void QBackendNode::notifyObservers(const QSceneChangePtr &e)
226{
227 Q_D(QBackendNode);
228 d->notifyObservers(e);
229}
230
231/*!
232 \obsolete
233
234 Send the command named \a name with contents \a data,
235 and specify \a replyTo as the command id to which the
236 reply needs to be sent.
237*/
238QNodeCommand::CommandId QBackendNode::sendCommand(const QString &name,
239 const QVariant &data,
240 QNodeCommand::CommandId replyTo)
241{
242 auto e = QNodeCommandPtr::create(arguments: peerId());
243 e->setName(name);
244 e->setData(data);
245 e->setReplyToCommandId(replyTo);
246 e->setDeliveryFlags(QSceneChange::Nodes);
247 notifyObservers(e);
248 return e->commandId();
249}
250
251/*!
252 Send the reply to \a command.
253 \obsolete
254*/
255void QBackendNode::sendReply(const QNodeCommandPtr &command)
256{
257 command->setDeliveryFlags(QSceneChange::Nodes);
258 notifyObservers(e: command);
259}
260
261/*!
262 * \obsolete
263 */
264void QBackendNode::initializeFromPeer(const QNodeCreatedChangeBasePtr &change)
265{
266 Q_UNUSED(change)
267 qCDebug(Nodes) << Q_FUNC_INFO << change->metaObject()->className() << "does not override";
268}
269
270/*!
271 * Enables or disables the backend node by \a enabled.
272 */
273void QBackendNode::setEnabled(bool enabled) Q_DECL_NOTHROW
274{
275 Q_D(QBackendNode);
276 d->m_enabled = enabled;
277}
278
279/*!
280 * \obsolete
281 */
282void QBackendNode::sceneChangeEvent(const QSceneChangePtr &e)
283{
284 Q_D(QBackendNode);
285
286 switch (e->type()) {
287 case PropertyUpdated: {
288 auto propertyChange = qSharedPointerCast<QPropertyUpdatedChange>(src: e);
289 if (propertyChange->propertyName() == QByteArrayLiteral("enabled"))
290 d->m_enabled = propertyChange->value().toBool();
291 break;
292 }
293 default:
294 break;
295 }
296}
297
298} // Qt3D
299
300QT_END_NAMESPACE
301

source code of qt3d/src/core/nodes/qbackendnode.cpp