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 "quick3dnode_p.h"
41
42#include <QtCore/QDebug>
43
44QT_BEGIN_NAMESPACE
45
46namespace Qt3DCore {
47namespace Quick {
48
49/*!
50 \qmltype Node
51 \inqmlmodule Qt3D.Core
52 \since 5.5
53
54 \brief A base QML type that other types inherit. It cannot be directly
55 created.
56*/
57
58Quick3DNode::Quick3DNode(QObject *parent)
59 : QObject(parent)
60{
61}
62
63/*!
64 \qmlproperty list<QtQml::QtObject> Qt3DCore::Node::data
65 \default
66*/
67
68QQmlListProperty<QObject> Quick3DNode::data()
69{
70 return QQmlListProperty<QObject>(this, 0,
71 Quick3DNode::appendData,
72 Quick3DNode::dataCount,
73 Quick3DNode::dataAt,
74 Quick3DNode::clearData);
75}
76
77/*!
78 \qmlproperty list<Node> Qt3DCore::Node::childNodes
79 \readonly
80*/
81
82QQmlListProperty<QNode> Quick3DNode::childNodes()
83{
84 return QQmlListProperty<QNode>(this, 0,
85 Quick3DNode::appendChild,
86 Quick3DNode::childCount,
87 Quick3DNode::childAt,
88 Quick3DNode::clearChildren);
89}
90
91void Quick3DNode::appendData(QQmlListProperty<QObject> *list, QObject *obj)
92{
93 if (!obj)
94 return;
95
96 Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
97 self->childAppended(idx: 0, child: obj);
98}
99
100QObject *Quick3DNode::dataAt(QQmlListProperty<QObject> *list, int index)
101{
102 Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
103 return self->parentNode()->children().at(i: index);
104}
105
106int Quick3DNode::dataCount(QQmlListProperty<QObject> *list)
107{
108 Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
109 return self->parentNode()->children().count();
110}
111
112void Quick3DNode::clearData(QQmlListProperty<QObject> *list)
113{
114 Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
115 for (QObject *const child : self->parentNode()->children())
116 self->childRemoved(idx: 0, child);
117}
118
119void Quick3DNode::appendChild(QQmlListProperty<Qt3DCore::QNode> *list, Qt3DCore::QNode *obj)
120{
121 if (!obj)
122 return;
123
124 Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
125 Q_ASSERT(!self->parentNode()->children().contains(obj));
126
127 self->childAppended(idx: 0, child: obj);
128}
129
130Qt3DCore::QNode *Quick3DNode::childAt(QQmlListProperty<Qt3DCore::QNode> *list, int index)
131{
132 Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
133 return qobject_cast<QNode *>(object: self->parentNode()->children().at(i: index));
134}
135
136int Quick3DNode::childCount(QQmlListProperty<Qt3DCore::QNode> *list)
137{
138 Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
139 return self->parentNode()->children().count();
140}
141
142void Quick3DNode::clearChildren(QQmlListProperty<Qt3DCore::QNode> *list)
143{
144 Quick3DNode *self = static_cast<Quick3DNode *>(list->object);
145 for (QObject *const child : self->parentNode()->children())
146 self->childRemoved(idx: 0, child);
147}
148
149void Quick3DNode::childAppended(int, QObject *obj)
150{
151 QNode *parentNode = this->parentNode();
152 if (obj->parent() == parentNode)
153 obj->setParent(0);
154 // Set after otherwise addChild might not work
155 if (QNode *n = qobject_cast<QNode *>(object: obj))
156 n->setParent(parentNode);
157 else
158 obj->setParent(parentNode);
159}
160
161void Quick3DNode::childRemoved(int, QObject *obj)
162{
163 if (QNode *n = qobject_cast<QNode *>(object: obj))
164 n->setParent(Q_NODE_NULLPTR);
165 else
166 obj->setParent(nullptr);
167}
168
169} // namespace Quick
170} // namespace Qt3DCore
171
172QT_END_NAMESPACE
173

source code of qt3d/src/quick3d/quick3d/items/quick3dnode.cpp