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

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