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 "qcomponent.h"
41#include "qcomponent_p.h"
42
43#include <Qt3DCore/qentity.h>
44
45#include <Qt3DCore/private/qentity_p.h>
46#include <Qt3DCore/private/qscene_p.h>
47
48QT_BEGIN_NAMESPACE
49
50namespace Qt3DCore {
51
52QComponentPrivate::QComponentPrivate()
53 : QNodePrivate()
54 , m_shareable(true)
55{
56}
57
58QComponentPrivate::~QComponentPrivate()
59{
60}
61
62void QComponentPrivate::addEntity(QEntity *entity)
63{
64 Q_Q(QComponent);
65 m_entities.append(t: entity);
66
67 if (m_scene != nullptr && !m_scene->hasEntityForComponent(componentUuid: m_id, entityUuid: entity->id())) {
68 if (!m_shareable && !m_scene->entitiesForComponent(id: m_id).isEmpty())
69 qWarning() << "Trying to assign a non shareable component to more than one Entity";
70 m_scene->addEntityForComponent(componentUuid: m_id, entityUuid: entity->id());
71 }
72
73 Q_EMIT q->addedToEntity(entity);
74}
75
76void QComponentPrivate::removeEntity(QEntity *entity)
77{
78 Q_Q(QComponent);
79 if (m_scene != nullptr)
80 m_scene->removeEntityForComponent(componentUuid: m_id, entityUuid: entity->id());
81
82 m_entities.removeAll(t: entity);
83
84 Q_EMIT q->removedFromEntity(entity);
85}
86
87/*!
88 \class Qt3DCore::QComponent
89 \inmodule Qt3DCore
90 \inherits Qt3DCore::QNode
91 \since 5.5
92
93 \brief The base class of scene nodes that can be aggregated by Qt3DCore::QEntity
94 instances as a component.
95
96 A Qt3DCore::QComponent provides a vertical slice of behavior that can be assigned to and
97 sometimes shared across Qt3DCore::QEntity instances.
98
99 Qt3DCore::QComponent subclasses are often aggregated in groups that impart useful
100 behavior to the aggregating entity. For example, to have an Entity that gets
101 drawn by the Qt3D renderer aspect, an entity would most likely aggregate
102 Qt3DCore::QTransform, Qt3DRender::QMesh, and Qt3DRender::QMaterial components.
103
104 \sa Qt3DCore::QEntity
105*/
106
107/*!
108 \fn Qt3DCore::QComponent::addedToEntity(Qt3DCore::QEntity *entity)
109
110 Indicates that a reference has been added to \a entity.
111*/
112/*!
113 \fn Qt3DCore::QComponent::removedFromEntity(Qt3DCore::QEntity *entity)
114
115 Indicates that a reference has been removed from \a entity.
116
117*/
118/*!
119 Constructs a new QComponent instance with \a parent as the parent.
120 \note a QComponent should never be instanced directly,
121 instance one of the subclasses instead.
122*/
123QComponent::QComponent(QNode *parent)
124 : QComponent(*new QComponentPrivate, parent) {}
125
126QComponent::~QComponent()
127{
128 Q_D(QComponent);
129
130 // iterate on copy since removeEntity removes from the list, invalidating the iterator
131 const auto entities = std::move(d->m_entities);
132 for (QEntity *entity : entities) {
133 QEntityPrivate *entityPimpl = static_cast<QEntityPrivate *>(QEntityPrivate::get(q: entity));
134 if (entityPimpl)
135 entityPimpl->m_components.removeAll(t: this);
136 d->removeEntity(entity);
137 }
138}
139
140/*!
141 \property Qt3DCore::QComponent::isShareable
142 Holds the shareable flag of the QComponent. The QComponent can be shared across several
143 entities if \c{true}.
144*/
145bool QComponent::isShareable() const
146{
147 Q_D(const QComponent);
148 return d->m_shareable;
149}
150
151void QComponent::setShareable(bool shareable)
152{
153 Q_D(QComponent);
154 if (d->m_shareable != shareable) {
155 d->m_shareable = shareable;
156 emit shareableChanged(isShareable: shareable);
157 }
158}
159
160/*!
161 Returns a QVector containing all the entities that reference this component.
162*/
163QVector<QEntity *> QComponent::entities() const
164{
165 Q_D(const QComponent);
166 return d->m_entities;
167}
168
169/*! \internal */
170QComponent::QComponent(QComponentPrivate &dd, QNode *parent)
171 : QNode(dd, parent)
172{
173}
174
175} // namespace Qt3DCore
176
177/*!
178 \qmltype Component3D
179 \instantiates Qt3DCore::QComponent
180 \inqmlmodule Qt3D.Core
181 \inherits Node
182 \since 5.5
183 \brief Provides the base type for creating Qt 3D components.
184
185 \TODO
186*/
187
188/*!
189 \qmlproperty bool Component3D::isShareable
190*/
191
192QT_END_NAMESPACE
193

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