summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Ottens <kevin.ottens@kdab.com>2016-05-19 11:29:50 +0200
committerSean Harmer <sean.harmer@kdab.com>2016-05-23 11:57:42 +0000
commit6682a119b4fad6edc0f1b4b0e1613227ff916bae (patch)
tree67b7ab21f08328e74aa84c0cb567ab1224ce6f62
parent4e047448f42fe89c44bebe29c58532c48b5ab18a (diff)
Use QVector instead of QVariantList properties
Change-Id: If7b2688c6032132cfabe5ff02ecbbcf3efbb29d0 Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
-rw-r--r--src/quick3d/imports/render/plugins.qmltypes30
-rw-r--r--src/render/framegraph/qsortpolicy.cpp55
-rw-r--r--src/render/framegraph/qsortpolicy.h12
-rw-r--r--src/render/framegraph/qsortpolicy_p.h19
-rw-r--r--src/render/framegraph/sortpolicy.cpp14
-rw-r--r--tests/auto/render/qsortpolicy/tst_qsortpolicy.cpp46
6 files changed, 83 insertions, 93 deletions
diff --git a/src/quick3d/imports/render/plugins.qmltypes b/src/quick3d/imports/render/plugins.qmltypes
index 19f03de65..af491af6c 100644
--- a/src/quick3d/imports/render/plugins.qmltypes
+++ b/src/quick3d/imports/render/plugins.qmltypes
@@ -1368,10 +1368,32 @@ Module {
}
Component {
name: "Qt3DRender::QSortPolicy"
- defaultProperty: "data"
- prototype: "Qt3DRender::QSortPolicy"
- Property { name: "data"; type: "QObject"; isList: true; isReadonly: true }
- Property { name: "childNodes"; type: "Qt3DCore::QNode"; isList: true; isReadonly: true }
+ prototype: "Qt3DRender::QFrameGraphNode"
+ Enum {
+ name: "SortType"
+ values: {
+ "StateChangeCost": 1,
+ "BackToFront": 2,
+ "Material": 4
+ }
+ }
+ Property { name: "sortTypes"; type: "QVector<int>" }
+ Signal {
+ name: "sortTypesChanged"
+ Parameter { name: "sortTypes"; type: "QVector<SortType>" }
+ }
+ Signal {
+ name: "sortTypesChanged"
+ Parameter { name: "sortTypes"; type: "QVector<int>" }
+ }
+ Method {
+ name: "setSortTypes"
+ Parameter { name: "sortTypes"; type: "QVector<SortType>" }
+ }
+ Method {
+ name: "setSortTypes"
+ Parameter { name: "sortTypesInt"; type: "QVector<int>" }
+ }
}
Component {
name: "Qt3DRender::QSpotLight"
diff --git a/src/render/framegraph/qsortpolicy.cpp b/src/render/framegraph/qsortpolicy.cpp
index ec4f58850..fbbff6541 100644
--- a/src/render/framegraph/qsortpolicy.cpp
+++ b/src/render/framegraph/qsortpolicy.cpp
@@ -77,62 +77,35 @@ QNodeCreatedChangeBasePtr QSortPolicy::createNodeCreationChange() const
return creationChange;
}
-void QSortPolicy::addSortType(Qt3DRender::QSortPolicy::SortType sortType)
-{
- Q_D(QSortPolicy);
- if (!d->m_sortTypes.contains(sortType)) {
- d->m_sortTypes.append(sortType);
-
- if (d->m_changeArbiter != nullptr) {
- QPropertyValueAddedChangePtr propertyChange(new QPropertyValueAddedChange(id()));
- propertyChange->setPropertyName("sortType");
- propertyChange->setAddedValue(QVariant::fromValue(sortType));
- d->notifyObservers(propertyChange);
- }
- }
-}
-
-void QSortPolicy::removeSortType(SortType sortType)
-{
- Q_D(QSortPolicy);
- if (d->m_changeArbiter != nullptr) {
- QPropertyValueRemovedChangePtr propertyChange(new QPropertyValueRemovedChange(id()));
- propertyChange->setPropertyName("sortType");
- propertyChange->setRemovedValue(QVariant::fromValue(sortType));
- d->notifyObservers(propertyChange);
- }
- d->m_sortTypes.removeOne(sortType);
-}
-
QVector<QSortPolicy::SortType> QSortPolicy::sortTypes() const
{
Q_D(const QSortPolicy);
return d->m_sortTypes;
}
-QVariantList QSortPolicy::sortTypeList() const
+QVector<int> QSortPolicy::sortTypesInt() const
{
Q_D(const QSortPolicy);
- QVariantList ret;
- ret.reserve(d->m_sortTypes.size());
- for (const auto type : d->m_sortTypes)
- ret.append(QVariant(type));
-
- return ret;
+ QVector<int> sortTypesInt;
+ transformVector(d->m_sortTypes, sortTypesInt);
+ return sortTypesInt;
}
-void QSortPolicy::setSortTypes(QVector<QSortPolicy::SortType> sortTypes)
+void QSortPolicy::setSortTypes(const QVector<SortType> &sortTypes)
{
Q_D(QSortPolicy);
- d->m_sortTypes = std::move(sortTypes);
+ if (sortTypes != d->m_sortTypes) {
+ d->m_sortTypes = sortTypes;
+ emit sortTypesChanged(sortTypes);
+ emit sortTypesChanged(sortTypesInt());
+ }
}
-void QSortPolicy::setSortTypes(const QVariantList &sortTypes)
+void QSortPolicy::setSortTypes(const QVector<int> &sortTypesInt)
{
- Q_D(QSortPolicy);
- d->m_sortTypes.clear();
- for (const auto &typeVariant : sortTypes)
- d->m_sortTypes.append(static_cast<QSortPolicy::SortType>(typeVariant.toInt()));
+ QVector<SortType> sortTypes;
+ transformVector(sortTypesInt, sortTypes);
+ setSortTypes(sortTypes);
}
} // namespace Qt3DRender
diff --git a/src/render/framegraph/qsortpolicy.h b/src/render/framegraph/qsortpolicy.h
index ba92b542c..010fa461b 100644
--- a/src/render/framegraph/qsortpolicy.h
+++ b/src/render/framegraph/qsortpolicy.h
@@ -51,7 +51,7 @@ class QSortPolicyPrivate;
class QT3DRENDERSHARED_EXPORT QSortPolicy : public QFrameGraphNode
{
Q_OBJECT
- Q_PROPERTY(QVariantList sortTypes READ sortTypeList WRITE setSortTypes NOTIFY sortTypesChanged)
+ Q_PROPERTY(QVector<int> sortTypes READ sortTypesInt WRITE setSortTypes NOTIFY sortTypesChanged)
public:
explicit QSortPolicy(Qt3DCore::QNode *parent = nullptr);
~QSortPolicy();
@@ -63,18 +63,16 @@ public:
};
Q_ENUM(SortType)
- void addSortType(SortType sortType);
- void removeSortType(SortType sortType);
QVector<SortType> sortTypes() const;
- QVariantList sortTypeList() const;
+ QVector<int> sortTypesInt() const;
public Q_SLOTS:
- void setSortTypes(QVector<QSortPolicy::SortType> sortTypes);
- void setSortTypes(const QVariantList &sortTypes);
+ void setSortTypes(const QVector<SortType> &sortTypes);
+ void setSortTypes(const QVector<int> &sortTypesInt);
Q_SIGNALS:
void sortTypesChanged(const QVector<SortType> &sortTypes);
- void sortTypesChanged(const QVariantList &sortTypes);
+ void sortTypesChanged(const QVector<int> &sortTypes);
protected:
explicit QSortPolicy(QSortPolicyPrivate &dd, Qt3DCore::QNode *parent = nullptr);
diff --git a/src/render/framegraph/qsortpolicy_p.h b/src/render/framegraph/qsortpolicy_p.h
index e9e27408d..484f7a206 100644
--- a/src/render/framegraph/qsortpolicy_p.h
+++ b/src/render/framegraph/qsortpolicy_p.h
@@ -75,6 +75,25 @@ struct QSortPolicyData
} // namespace Qt3DRender
+namespace {
+ template<class From, class To>
+ To typeCastHelper(From type)
+ {
+ return static_cast<To>(type);
+ }
+
+ template<class From, class To>
+ void transformVector(const QVector<From> &input, QVector<To> &output)
+ {
+ Q_ASSERT(output.isEmpty());
+
+ output.reserve(input.size());
+ std::transform(input.constBegin(), input.constEnd(),
+ std::back_inserter(output),
+ typeCastHelper<From, To>);
+ }
+}
+
QT_END_NAMESPACE
#endif // QT3DRENDER_QSORTPOLICY_P_H
diff --git a/src/render/framegraph/sortpolicy.cpp b/src/render/framegraph/sortpolicy.cpp
index e5e0c40d2..628fa75e4 100644
--- a/src/render/framegraph/sortpolicy.cpp
+++ b/src/render/framegraph/sortpolicy.cpp
@@ -55,14 +55,12 @@ SortPolicy::SortPolicy()
void SortPolicy::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e)
{
- QPropertyUpdatedChangePtr propertyChange = qSharedPointerCast<QPropertyUpdatedChange>(e);
- if (propertyChange->propertyName() == QByteArrayLiteral("sortType")) {
- const QSortPolicy::SortType cId = propertyChange->value().value<QSortPolicy::SortType>();
- if (cId == QSortPolicy::StateChangeCost || cId == QSortPolicy::BackToFront || cId == QSortPolicy::Material) {
- if (e->type() == PropertyValueAdded)
- m_sortTypes.append(cId);
- else if (e->type() == PropertyValueRemoved)
- m_sortTypes.removeAll(cId);
+ if (e->type() == Qt3DCore::PropertyUpdated) {
+ Qt3DCore::QPropertyUpdatedChangePtr propertyChange = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(e);
+ if (propertyChange->propertyName() == QByteArrayLiteral("sortTypes")) {
+ auto sortTypesInt = propertyChange->value().value<QVector<int>>();
+ m_sortTypes.clear();
+ transformVector(sortTypesInt, m_sortTypes);
}
}
markDirty(AbstractRenderer::AllDirty);
diff --git a/tests/auto/render/qsortpolicy/tst_qsortpolicy.cpp b/tests/auto/render/qsortpolicy/tst_qsortpolicy.cpp
index 020929e09..b4328815f 100644
--- a/tests/auto/render/qsortpolicy/tst_qsortpolicy.cpp
+++ b/tests/auto/render/qsortpolicy/tst_qsortpolicy.cpp
@@ -63,11 +63,9 @@ private Q_SLOTS:
QTest::newRow("defaultConstructed") << defaultConstructed << QVector<Qt3DRender::QSortPolicy::SortType>();
Qt3DRender::QSortPolicy *sortPolicyWithSortTypes = new Qt3DRender::QSortPolicy();
- Qt3DRender::QSortPolicy::SortType sortType1 = Qt3DRender::QSortPolicy::BackToFront;
- Qt3DRender::QSortPolicy::SortType sortType2 = Qt3DRender::QSortPolicy::Material;
- QVector<Qt3DRender::QSortPolicy::SortType> sortTypes; sortTypes << sortType1 << sortType2;
- sortPolicyWithSortTypes->addSortType(sortType1);
- sortPolicyWithSortTypes->addSortType(sortType2);
+ auto sortTypes = QVector<Qt3DRender::QSortPolicy::SortType>() << Qt3DRender::QSortPolicy::BackToFront
+ << Qt3DRender::QSortPolicy::Material;
+ sortPolicyWithSortTypes->setSortTypes(sortTypes);
QTest::newRow("sortPolicyWithSortTypes") << sortPolicyWithSortTypes << sortTypes ;
}
@@ -108,38 +106,20 @@ private Q_SLOTS:
arbiter.setArbiterOnNode(sortPolicy.data());
// WHEN
- Qt3DRender::QSortPolicy::SortType sortType1 = Qt3DRender::QSortPolicy::BackToFront;
- sortPolicy->addSortType(sortType1);
+ auto sortTypes = QVector<Qt3DRender::QSortPolicy::SortType>() << Qt3DRender::QSortPolicy::BackToFront
+ << Qt3DRender::QSortPolicy::Material
+ << Qt3DRender::QSortPolicy::Material;
+ auto sortTypesInt = QVector<int>();
+ transformVector(sortTypes, sortTypesInt);
+ sortPolicy->setSortTypes(sortTypes);
QCoreApplication::processEvents();
// THEN
QCOMPARE(arbiter.events.size(), 1);
- Qt3DCore::QPropertyValueAddedChangePtr addChange = arbiter.events.first().staticCast<Qt3DCore::QPropertyValueAddedChange>();
- QCOMPARE(addChange->propertyName(), "sortType");
- QCOMPARE(addChange->subjectId(),sortPolicy->id());
- QCOMPARE(addChange->addedValue().value<Qt3DRender::QSortPolicy::SortType>(), sortType1);
- QCOMPARE(addChange->type(), Qt3DCore::PropertyValueAdded);
-
- arbiter.events.clear();
-
- // WHEN
- sortPolicy->addSortType(sortType1);
- QCoreApplication::processEvents();
-
- // THEN
- QCOMPARE(arbiter.events.size(), 0);
-
- // WHEN
- sortPolicy->removeSortType(sortType1);
- QCoreApplication::processEvents();
-
- // THEN
- QCOMPARE(arbiter.events.size(), 1);
- Qt3DCore::QPropertyValueRemovedChangePtr removeChange = arbiter.events.first().staticCast<Qt3DCore::QPropertyValueRemovedChange>();
- QCOMPARE(removeChange->propertyName(), "sortType");
- QCOMPARE(removeChange->subjectId(), sortPolicy->id());
- QCOMPARE(removeChange->removedValue().value<Qt3DRender::QSortPolicy::SortType>(), sortType1);
- QCOMPARE(removeChange->type(), Qt3DCore::PropertyValueRemoved);
+ Qt3DCore::QPropertyUpdatedChangePtr change = arbiter.events.first().staticCast<Qt3DCore::QPropertyUpdatedChange>();
+ QCOMPARE(change->propertyName(), "sortTypes");
+ QCOMPARE(change->value().value<QVector<int>>(), sortTypesInt);
+ QCOMPARE(change->type(), Qt3DCore::PropertyUpdated);
arbiter.events.clear();
}