From d19e22fd541dec42cd24c28dbc981ed81e8d04ff Mon Sep 17 00:00:00 2001 From: Grecko Date: Thu, 21 Sep 2017 23:30:50 +0200 Subject: [PATCH] feat: add a defaultValue property to SwitchRole --- proxyrole.cpp | 21 +++++++++++++++++++-- proxyrole.h | 8 +++++++- tests/tst_switchrole.qml | 11 +++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/proxyrole.cpp b/proxyrole.cpp index 21d0cc3..1001c80 100644 --- a/proxyrole.cpp +++ b/proxyrole.cpp @@ -117,7 +117,7 @@ QString SwitchRole::defaultRoleName() const return m_defaultRoleName; } -void SwitchRole::setDefaultRoleName(QString defaultRoleName) +void SwitchRole::setDefaultRoleName(const QString& defaultRoleName) { if (m_defaultRoleName == defaultRoleName) return; @@ -127,6 +127,21 @@ void SwitchRole::setDefaultRoleName(QString defaultRoleName) invalidate(); } +QVariant SwitchRole::defaultValue() const +{ + return m_defaultValue; +} + +void SwitchRole::setDefaultValue(const QVariant& defaultValue) +{ + if (m_defaultValue == defaultValue) + return; + + m_defaultValue = defaultValue; + Q_EMIT defaultValueChanged(); + invalidate(); +} + QQmlListProperty SwitchRole::filters() { return QQmlListProperty(this, &m_filters, @@ -166,7 +181,9 @@ QVariant SwitchRole::data(const QModelIndex &sourceIndex, const QQmlSortFilterPr return value; } } - return proxyModel.sourceData(sourceIndex, m_defaultRoleName); + if (!m_defaultRoleName.isEmpty()) + return proxyModel.sourceData(sourceIndex, m_defaultRoleName); + return m_defaultValue; } void SwitchRole::append_filter(QQmlListProperty* list, Filter* filter) diff --git a/proxyrole.h b/proxyrole.h index 7ce84f4..485e5a4 100644 --- a/proxyrole.h +++ b/proxyrole.h @@ -84,13 +84,17 @@ class SwitchRole : public ProxyRole { Q_OBJECT Q_PROPERTY(QString defaultRoleName READ defaultRoleName WRITE setDefaultRoleName NOTIFY defaultRoleNameChanged) + Q_PROPERTY(QVariant defaultValue READ defaultValue WRITE setDefaultValue NOTIFY defaultValueChanged) Q_PROPERTY(QQmlListProperty filters READ filters) public: using ProxyRole::ProxyRole; QString defaultRoleName() const; - void setDefaultRoleName(QString defaultRoleName); + void setDefaultRoleName(const QString& defaultRoleName); + + QVariant defaultValue() const; + void setDefaultValue(const QVariant& defaultValue); QQmlListProperty filters(); void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) override; @@ -99,6 +103,7 @@ public: Q_SIGNALS: void defaultRoleNameChanged(); + void defaultValueChanged(); private: QVariant data(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) override; @@ -109,6 +114,7 @@ private: static void clear_filters(QQmlListProperty* list); QString m_defaultRoleName; + QVariant m_defaultValue; QList m_filters; }; diff --git a/tests/tst_switchrole.qml b/tests/tst_switchrole.qml index 15cd06e..f5c0fe5 100644 --- a/tests/tst_switchrole.qml +++ b/tests/tst_switchrole.qml @@ -18,6 +18,7 @@ Item { sourceModel: listModel proxyRoles: SwitchRole { + id: switchRole name: "switchRole" filters: [ ValueFilter { @@ -40,6 +41,7 @@ Item { } ] defaultRoleName: "name" + defaultValue: "foo" } } @@ -86,5 +88,14 @@ Item { listModel.setProperty(1, "name", "2"); listModel.setProperty(0, "favorite", true); } + + function test_defaultValue() { + switchRole.defaultRoleName = ""; + compare(instantiator.objectAt(1).switchRole, "foo"); + switchRole.defaultValue = "bar"; + compare(instantiator.objectAt(1).switchRole, "bar"); + switchRole.defaultRoleName = "name"; + switchRole.defaultValue = "foo"; + } } }