diff options
author | Kevin Ottens <kevin.ottens@kdab.com> | 2015-12-01 13:11:04 +0100 |
---|---|---|
committer | Kevin Ottens <kevin.ottens@kdab.com> | 2015-12-01 14:24:05 +0000 |
commit | 25737a39a63e7295650975c1359b9868461018ad (patch) | |
tree | 7b6c5aa524f51f91be3cbdd3e970fa5ad0244cb6 | |
parent | bb2e5c6ec62f9bc23cea2c67f129fe1f7873b1be (diff) |
QAspectFactory can now also give registered name of an aspect
Change-Id: I8f89eefb06e20943f7abb82d461b5e90fd9799f9
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
-rw-r--r-- | src/core/aspects/qabstractaspect.h | 4 | ||||
-rw-r--r-- | src/core/aspects/qaspectfactory.cpp | 18 | ||||
-rw-r--r-- | src/core/aspects/qaspectfactory_p.h | 10 | ||||
-rw-r--r-- | tests/auto/core/qaspectfactory/tst_qaspectfactory.cpp | 14 |
4 files changed, 41 insertions, 5 deletions
diff --git a/src/core/aspects/qabstractaspect.h b/src/core/aspects/qabstractaspect.h index 55ded6e5b..d3ec2d739 100644 --- a/src/core/aspects/qabstractaspect.h +++ b/src/core/aspects/qabstractaspect.h @@ -113,7 +113,7 @@ QT_END_NAMESPACE QT_BEGIN_NAMESPACE \ namespace Qt3DCore { \ typedef QAbstractAspect *(*AspectCreateFunction)(QObject *); \ - QT3DCORESHARED_EXPORT void qt3d_QAspectFactory_addDefaultFactory(const QString &, AspectCreateFunction); \ + QT3DCORESHARED_EXPORT void qt3d_QAspectFactory_addDefaultFactory(const QString &, const QMetaObject *, AspectCreateFunction); \ } \ QT_END_NAMESPACE \ namespace { \ @@ -126,7 +126,7 @@ QT_END_NAMESPACE void qt3d_ ## AspectType ## _registerFunction() \ { \ using namespace AspectNamespace; \ - qt3d_QAspectFactory_addDefaultFactory(QStringLiteral(name), qt3d_ ## AspectType ## _createFunction); \ + qt3d_QAspectFactory_addDefaultFactory(QStringLiteral(name), &AspectType::staticMetaObject, qt3d_ ## AspectType ## _createFunction); \ } \ \ Q_CONSTRUCTOR_FUNCTION(qt3d_ ## AspectType ## _registerFunction) \ diff --git a/src/core/aspects/qaspectfactory.cpp b/src/core/aspects/qaspectfactory.cpp index faa946ec9..eeebba39c 100644 --- a/src/core/aspects/qaspectfactory.cpp +++ b/src/core/aspects/qaspectfactory.cpp @@ -40,26 +40,34 @@ #include <QDebug> +#include <Qt3DCore/QAbstractAspect> + QT_BEGIN_NAMESPACE namespace Qt3DCore { typedef QHash<QString, QAspectFactory::CreateFunction> defaultFactories_t; Q_GLOBAL_STATIC(defaultFactories_t, defaultFactories) +typedef QHash<const QMetaObject*, QString> defaultAspectNames_t; +Q_GLOBAL_STATIC(defaultAspectNames_t, defaultAspectNames) QT3DCORESHARED_EXPORT void qt3d_QAspectFactory_addDefaultFactory(const QString &name, + const QMetaObject *metaObject, QAspectFactory::CreateFunction factory) { defaultFactories->insert(name, factory); + defaultAspectNames->insert(metaObject, name); } QAspectFactory::QAspectFactory() - : m_factories(*defaultFactories) + : m_factories(*defaultFactories), + m_aspectNames(*defaultAspectNames) { } QAspectFactory::QAspectFactory(const QAspectFactory &other) - : m_factories(other.m_factories) + : m_factories(other.m_factories), + m_aspectNames(other.m_aspectNames) { } @@ -70,6 +78,7 @@ QAspectFactory::~QAspectFactory() QAspectFactory &QAspectFactory::operator=(const QAspectFactory &other) { m_factories = other.m_factories; + m_aspectNames = other.m_aspectNames; return *this; } @@ -88,6 +97,11 @@ QAbstractAspect *QAspectFactory::createAspect(const QString &aspect, QObject *pa } } +QString QAspectFactory::aspectName(QAbstractAspect *aspect) const +{ + return m_aspectNames.value(aspect->metaObject()); +} + } // namespace Qt3DCore QT_END_NAMESPACE diff --git a/src/core/aspects/qaspectfactory_p.h b/src/core/aspects/qaspectfactory_p.h index f42010d1a..590860db1 100644 --- a/src/core/aspects/qaspectfactory_p.h +++ b/src/core/aspects/qaspectfactory_p.h @@ -55,6 +55,7 @@ QT_BEGIN_NAMESPACE +class QMetaObject; class QObject; namespace Qt3DCore { @@ -74,17 +75,24 @@ public: QAspectFactory &operator=(QAspectFactory &&other) Q_DECL_NOTHROW { m_factories.swap(other.m_factories); + m_aspectNames.swap(other.m_aspectNames); return *this; } #endif - inline void swap(QAspectFactory &other) Q_DECL_NOTHROW { m_factories.swap(other.m_factories); } + inline void swap(QAspectFactory &other) Q_DECL_NOTHROW + { + m_factories.swap(other.m_factories); + m_aspectNames.swap(other.m_aspectNames); + } QStringList availableFactories() const; QAbstractAspect *createAspect(const QString &aspect, QObject *parent = 0) const; + QString aspectName(QAbstractAspect *aspect) const; private: QHash<QString, QAspectFactory::CreateFunction> m_factories; + QHash<const QMetaObject*, QString> m_aspectNames; }; } // namespace Qt3DCore diff --git a/tests/auto/core/qaspectfactory/tst_qaspectfactory.cpp b/tests/auto/core/qaspectfactory/tst_qaspectfactory.cpp index 11e5deaa0..bee09be77 100644 --- a/tests/auto/core/qaspectfactory/tst_qaspectfactory.cpp +++ b/tests/auto/core/qaspectfactory/tst_qaspectfactory.cpp @@ -89,6 +89,20 @@ private Q_SLOTS: QVERIFY(aspect->parent() == Q_NULLPTR); } + void shouldKnowAspectNames() + { + // GIVEN + QAspectFactory factory; + + // WHEN + DefaultFakeAspect fake; + AnotherFakeAspect missing; + + // THEN + QCOMPARE(factory.aspectName(&fake), QString("default")); + QCOMPARE(factory.aspectName(&missing), QString()); + } + void shouldGracefulyHandleMissingFactories() { // GIVEN |