diff options
author | Sean Harmer <sean.harmer@kdab.com> | 2017-09-26 15:21:50 +0100 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2017-09-27 12:51:00 +0000 |
commit | 78175fa89bc95b88466ecf1ac8b42244762e62e2 (patch) | |
tree | b5fe152b2e3288878a185cb95501da0fa1f4ecf8 | |
parent | a286529cbe2ae51e6bf19aaea4203afd6b4884de (diff) |
Add convenience function to ChannelMapper to return list of Mappings
Caches the resolution of node ids to ChannelMappings. Updated test to
check this new feature.
Change-Id: I0c2f539c2bd91e7918b98c5b60c9ca486b63ec08
Reviewed-by: Kevin Ottens <kevin.ottens@kdab.com>
-rw-r--r-- | src/animation/backend/channelmapper.cpp | 20 | ||||
-rw-r--r-- | src/animation/backend/channelmapper_p.h | 12 | ||||
-rw-r--r-- | tests/auto/animation/channelmapper/tst_channelmapper.cpp | 36 |
3 files changed, 66 insertions, 2 deletions
diff --git a/src/animation/backend/channelmapper.cpp b/src/animation/backend/channelmapper.cpp index 0e5555096..48a1335fa 100644 --- a/src/animation/backend/channelmapper.cpp +++ b/src/animation/backend/channelmapper.cpp @@ -38,6 +38,7 @@ #include <Qt3DAnimation/qchannelmapper.h> #include <Qt3DAnimation/private/qchannelmapper_p.h> #include <Qt3DAnimation/private/animationlogging_p.h> +#include <Qt3DAnimation/private/managers_p.h> #include <Qt3DCore/qpropertyupdatedchange.h> #include <Qt3DCore/qpropertynodeaddedchange.h> #include <Qt3DCore/qpropertynoderemovedchange.h> @@ -50,6 +51,7 @@ namespace Animation { ChannelMapper::ChannelMapper() : BackendNode(ReadOnly) , m_mappingIds() + , m_isDirty(true) { } @@ -58,12 +60,15 @@ void ChannelMapper::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<QChannelMapperData>>(change); const auto &data = typedChange->data; m_mappingIds = data.mappingIds; + m_isDirty = true; } void ChannelMapper::cleanup() { setEnabled(false); m_mappingIds.clear(); + m_mappings.clear(); + m_isDirty = true; } void ChannelMapper::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) @@ -74,6 +79,7 @@ void ChannelMapper::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) if (change->propertyName() == QByteArrayLiteral("mappings")) { m_mappingIds.push_back(change->addedNodeId()); setDirty(Handler::ChannelMappingsDirty); + m_isDirty = true; } break; } @@ -83,6 +89,7 @@ void ChannelMapper::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) if (change->propertyName() == QByteArrayLiteral("mappings")) { m_mappingIds.removeOne(change->removedNodeId()); setDirty(Handler::ChannelMappingsDirty); + m_isDirty = true; } break; } @@ -93,6 +100,19 @@ void ChannelMapper::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) QBackendNode::sceneChangeEvent(e); } +void ChannelMapper::updateMappings() const +{ + m_mappings.clear(); + m_mappings.reserve(m_mappingIds.size()); + const auto mappingManager = m_handler->channelMappingManager(); + for (const auto &mappingId : m_mappingIds) { + const auto mapping = mappingManager->lookupResource(mappingId); + Q_ASSERT(mapping); + m_mappings.push_back(mapping); + } + m_isDirty = false; +} + } // namespace Animation } // namespace Qt3DAnimation diff --git a/src/animation/backend/channelmapper_p.h b/src/animation/backend/channelmapper_p.h index 710de01ab..8d8f03511 100644 --- a/src/animation/backend/channelmapper_p.h +++ b/src/animation/backend/channelmapper_p.h @@ -74,10 +74,22 @@ public: void setMappingIds(const QVector<Qt3DCore::QNodeId> &mappingIds) { m_mappingIds = mappingIds; } QVector<Qt3DCore::QNodeId> mappingIds() const { return m_mappingIds; } + QVector<ChannelMapping*> mappings() const + { + if (m_isDirty) + updateMappings(); + return m_mappings; + } + private: void initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change) Q_DECL_FINAL; + void updateMappings() const; QVector<Qt3DCore::QNodeId> m_mappingIds; + + // Cached data + mutable QVector<ChannelMapping*> m_mappings; + mutable bool m_isDirty; }; } // namespace Animation diff --git a/tests/auto/animation/channelmapper/tst_channelmapper.cpp b/tests/auto/animation/channelmapper/tst_channelmapper.cpp index 4dcb52aef..446e1a0c5 100644 --- a/tests/auto/animation/channelmapper/tst_channelmapper.cpp +++ b/tests/auto/animation/channelmapper/tst_channelmapper.cpp @@ -30,6 +30,8 @@ #include <qbackendnodetester.h> #include <Qt3DAnimation/private/handler_p.h> #include <Qt3DAnimation/private/channelmapper_p.h> +#include <Qt3DAnimation/private/channelmapping_p.h> +#include <Qt3DAnimation/private/managers_p.h> #include <Qt3DAnimation/qchannelmapper.h> #include <Qt3DAnimation/qchannelmapping.h> #include <Qt3DAnimation/private/qchannelmapper_p.h> @@ -116,13 +118,40 @@ private Q_SLOTS: // WHEN Qt3DAnimation::QChannelMapping mapping; const Qt3DCore::QNodeId mappingId = mapping.id(); - const auto nodeAddedChange = Qt3DCore::QPropertyNodeAddedChangePtr::create(Qt3DCore::QNodeId(), &mapping); + Qt3DAnimation::Animation::ChannelMapping *backendMapping + = handler.channelMappingManager()->getOrCreateResource(mappingId); + backendMapping->setHandler(&handler); + simulateInitialization(&mapping, backendMapping); + + auto nodeAddedChange = Qt3DCore::QPropertyNodeAddedChangePtr::create(Qt3DCore::QNodeId(), &mapping); nodeAddedChange->setPropertyName("mappings"); backendMapper.sceneChangeEvent(nodeAddedChange); // THEN QCOMPARE(backendMapper.mappingIds().size(), 1); QCOMPARE(backendMapper.mappingIds().first(), mappingId); + QCOMPARE(backendMapper.mappings().size(), 1); + QCOMPARE(backendMapper.mappings().first(), backendMapping); + + // WHEN + Qt3DAnimation::QChannelMapping mapping2; + const Qt3DCore::QNodeId mappingId2 = mapping2.id(); + Qt3DAnimation::Animation::ChannelMapping *backendMapping2 + = handler.channelMappingManager()->getOrCreateResource(mappingId2); + backendMapping2->setHandler(&handler); + simulateInitialization(&mapping2, backendMapping2); + + nodeAddedChange = Qt3DCore::QPropertyNodeAddedChangePtr::create(Qt3DCore::QNodeId(), &mapping2); + nodeAddedChange->setPropertyName("mappings"); + backendMapper.sceneChangeEvent(nodeAddedChange); + + // THEN + QCOMPARE(backendMapper.mappingIds().size(), 2); + QCOMPARE(backendMapper.mappingIds().first(), mappingId); + QCOMPARE(backendMapper.mappingIds().last(), mappingId2); + QCOMPARE(backendMapper.mappings().size(), 2); + QCOMPARE(backendMapper.mappings().first(), backendMapping); + QCOMPARE(backendMapper.mappings().last(), backendMapping2); // WHEN const auto nodeRemovedChange = Qt3DCore::QPropertyNodeRemovedChangePtr::create(Qt3DCore::QNodeId(), &mapping); @@ -130,7 +159,10 @@ private Q_SLOTS: backendMapper.sceneChangeEvent(nodeRemovedChange); // THEN - QCOMPARE(backendMapper.mappingIds().size(), 0); + QCOMPARE(backendMapper.mappingIds().size(), 1); + QCOMPARE(backendMapper.mappingIds().first(), mappingId2); + QCOMPARE(backendMapper.mappings().size(), 1); + QCOMPARE(backendMapper.mappings().first(), backendMapping2); } }; |