diff options
author | Juan Jose Casafranca <juan.casafranca@kdab.com> | 2018-02-04 20:34:06 +0100 |
---|---|---|
committer | Juan José Casafranca <juan.casafranca@kdab.com> | 2018-09-19 13:32:08 +0000 |
commit | b3d15538a995843a000be92b03fc7b1ac8b87826 (patch) | |
tree | 4162b563087ccb9028ab720727e087b4701688a1 | |
parent | 4c7f3546b42c320c600bdba92e4e9126cbeedae3 (diff) |
Add an easy way to get a particular component from an entity
Change-Id: I188fab5019d3e05354a4c1582fb12097a569fbb8
Reviewed-by: Mike Krus <mike.krus@kdab.com>
-rw-r--r-- | src/core/nodes/qentity.cpp | 7 | ||||
-rw-r--r-- | src/core/nodes/qentity.h | 13 | ||||
-rw-r--r-- | tests/auto/core/qentity/tst_qentity.cpp | 33 |
3 files changed, 53 insertions, 0 deletions
diff --git a/src/core/nodes/qentity.cpp b/src/core/nodes/qentity.cpp index 64ea65087..3c8805a67 100644 --- a/src/core/nodes/qentity.cpp +++ b/src/core/nodes/qentity.cpp @@ -77,6 +77,13 @@ namespace Qt3DCore { \sa Qt3DCore::QComponent, Qt3DCore::QTransform */ +/*! + \fn template<typename T> QVector<T *> QEntity::componentsOfType() const + + Returns all the components added to this entity that can be cast to + type T or an empty vector if there are no such components. +*/ + /*! \internal */ QEntityPrivate::QEntityPrivate() : QNodePrivate() diff --git a/src/core/nodes/qentity.h b/src/core/nodes/qentity.h index dc7dc62c1..ef6aedc4d 100644 --- a/src/core/nodes/qentity.h +++ b/src/core/nodes/qentity.h @@ -62,6 +62,19 @@ public: QComponentVector components() const; + template<class T> + QVector<T *> componentsOfType() const + { + QVector<T*> matchComponents; + const QComponentVector components = this->components(); + for (QComponent *component : components) { + T *typedComponent = qobject_cast<T*>(component); + if (typedComponent != nullptr) + matchComponents.append(typedComponent); + } + return matchComponents; + } + void addComponent(QComponent *comp); void removeComponent(QComponent *comp); diff --git a/tests/auto/core/qentity/tst_qentity.cpp b/tests/auto/core/qentity/tst_qentity.cpp index e27cd1fc9..d7847342b 100644 --- a/tests/auto/core/qentity/tst_qentity.cpp +++ b/tests/auto/core/qentity/tst_qentity.cpp @@ -53,6 +53,8 @@ private slots: void addComponentsSeveralParentsSingleAggregations(); void addComponentsSeveralParentsSeveralAggregations(); + void retrieveSingleComponent(); + void removeComponentSingleParentSingleAggregation(); void removeComponentSingleParentSeveralAggregations(); void removeComponentsSeveralParentsSingleAggreation(); @@ -76,6 +78,14 @@ public: {} }; +class MyQ2Component : public Qt3DCore::QComponent +{ + Q_OBJECT +public: + explicit MyQ2Component(Qt3DCore::QNode *parent = 0) + : QComponent(parent) + {} +}; class MyEntity : public Qt3DCore::QEntity { @@ -504,6 +514,29 @@ void tst_Entity::removeComponentsSeveralParentsSeveralAggregations() QCOMPARE(comp3->entities().size(), 0); } +void tst_Entity::retrieveSingleComponent() +{ + // GIVEN + QScopedPointer<Qt3DCore::QEntity> entity1(new QEntity()); + + MyQComponent *comp1 = new MyQComponent(entity1.data()); + MyQComponent *comp2 = new MyQComponent(entity1.data()); + QCoreApplication::processEvents(); + entity1->addComponent(comp1); + entity1->addComponent(comp2); + + // WHEN + QVector<MyQComponent*> myQComponentsInEntity = entity1->componentsOfType<MyQComponent>(); + QVector<MyQ2Component*> myQ2ComponentsInEntity = entity1->componentsOfType<MyQ2Component>(); + + // THEN + QVERIFY(myQComponentsInEntity.size() == 2); + QVERIFY(myQComponentsInEntity[0] == comp1); + QVERIFY(myQComponentsInEntity[1] == comp2); + + QVERIFY(myQ2ComponentsInEntity.size() == 0); +} + void tst_Entity::addSeveralTimesSameComponent() { // GIVEN |