diff --git a/ui/StatusQ/CMakeLists.txt b/ui/StatusQ/CMakeLists.txt index 2bf3a24f39..bb45cade8c 100644 --- a/ui/StatusQ/CMakeLists.txt +++ b/ui/StatusQ/CMakeLists.txt @@ -95,6 +95,7 @@ add_library(StatusQ SHARED include/StatusQ/aggregator.h include/StatusQ/clipboardutils.h include/StatusQ/concatmodel.h + include/StatusQ/constantrole.h include/StatusQ/fastexpressionfilter.h include/StatusQ/fastexpressionrole.h include/StatusQ/fastexpressionsorter.h @@ -128,6 +129,7 @@ add_library(StatusQ SHARED src/aggregator.cpp src/clipboardutils.cpp src/concatmodel.cpp + src/constantrole.cpp src/fastexpressionfilter.cpp src/fastexpressionrole.cpp src/fastexpressionsorter.cpp diff --git a/ui/StatusQ/include/StatusQ/constantrole.h b/ui/StatusQ/include/StatusQ/constantrole.h new file mode 100644 index 0000000000..f713450564 --- /dev/null +++ b/ui/StatusQ/include/StatusQ/constantrole.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +class ConstantRole : public qqsfpm::SingleRole +{ + Q_OBJECT + Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged) + +public: + using SingleRole::SingleRole; + + const QVariant& value() const; + void setValue(const QVariant& value); + +Q_SIGNALS: + void valueChanged(); + +private: + QVariant data(const QModelIndex& sourceIndex, + const qqsfpm::QQmlSortFilterProxyModel& proxyModel) override; + + QVariant m_value; + +}; diff --git a/ui/StatusQ/src/constantrole.cpp b/ui/StatusQ/src/constantrole.cpp new file mode 100644 index 0000000000..ae0f4db842 --- /dev/null +++ b/ui/StatusQ/src/constantrole.cpp @@ -0,0 +1,52 @@ +#include "StatusQ/constantrole.h" + +#include + +using namespace qqsfpm; + +/*! + \qmltype ConstantRole + \inherits SingleRole + \inqmlmodule StatusQ + \brief A custom role serving fixed value for all rows. + + \code + SortFilterProxyModel { + sourceModel: numberModel + proxyRoles: ConstantRole { + name: "type" + value: "regular" + } + } + \endcode +*/ + +/*! + \qmlproperty variant ConstantRole::value + + Value served for all rows for the specified role. +*/ +const QVariant& ConstantRole::value() const +{ + return m_value; +} + +void ConstantRole::setValue(const QVariant& value) +{ + if (m_value == value) + return; + + m_value = value; + emit valueChanged(); + invalidate(); +} + + +QVariant ConstantRole::data(const QModelIndex& sourceIndex, + const QQmlSortFilterProxyModel& proxyModel) +{ + Q_UNUSED(sourceIndex) + Q_UNUSED(proxyModel) + + return m_value; +} diff --git a/ui/StatusQ/src/plugin.cpp b/ui/StatusQ/src/plugin.cpp index 5a9779a60e..796fb4dc1b 100644 --- a/ui/StatusQ/src/plugin.cpp +++ b/ui/StatusQ/src/plugin.cpp @@ -5,6 +5,7 @@ #include "StatusQ/clipboardutils.h" #include "StatusQ/concatmodel.h" +#include "StatusQ/constantrole.h" #include "StatusQ/fastexpressionfilter.h" #include "StatusQ/fastexpressionrole.h" #include "StatusQ/fastexpressionsorter.h" @@ -33,6 +34,7 @@ #include "StatusQ/urlutils.h" #include "StatusQ/writableproxymodel.h" + #include "wallet/managetokenscontroller.h" #include "wallet/managetokensmodel.h" @@ -67,6 +69,7 @@ public: qmlRegisterType("StatusQ", 0, 1, "FastExpressionRole"); qmlRegisterType("StatusQ", 0, 1, "FastExpressionSorter"); qmlRegisterType("StatusQ", 0, 1, "UndefinedFilter"); + qmlRegisterType("StatusQ", 0, 1, "ConstantRole"); qmlRegisterType("StatusQ", 0, 1, "ObjectProxyModel"); qmlRegisterType("StatusQ", 0, 1, "LeftJoinModel"); diff --git a/ui/StatusQ/tests/TestCore/TestModels/tst_ConstantRole.qml b/ui/StatusQ/tests/TestCore/TestModels/tst_ConstantRole.qml new file mode 100644 index 0000000000..013f853990 --- /dev/null +++ b/ui/StatusQ/tests/TestCore/TestModels/tst_ConstantRole.qml @@ -0,0 +1,78 @@ +import QtQml 2.15 +import QtQuick 2.15 +import QtTest 1.15 + +import SortFilterProxyModel 0.2 + +import StatusQ 0.1 + +Item { + id: root + + Component { + id: testComponent + + QtObject { + property int d: 0 + + readonly property ListModel source: ListModel { + ListElement { a: 1; b: 2; c: 3 } + } + + readonly property ConstantRole constantRole: constantRole + + readonly property SortFilterProxyModel model: SortFilterProxyModel { + id: testModel + + sourceModel: ListModel { + ListElement { a: 1; b: 2; c: 3 } + } + + proxyRoles: ConstantRole { + id: constantRole + + name: "constantRole" + value: 42 + } + } + + readonly property Instantiator instantiator: Instantiator { + model: testModel + + QtObject { + property int constantRole: model.constantRole + } + } + + readonly property SignalSpy dataChangedSignalSpy: SignalSpy { + target: testModel + signalName: "dataChanged" + } + + readonly property SignalSpy modelResetSignalSpy: SignalSpy { + target: testModel + signalName: "modelReset" + } + } + } + + TestCase { + name: "ConstantRole" + + function test_constantRoleValue() { + const obj = createTemporaryObject(testComponent, root) + + const instantiator = obj.instantiator + const listModel = obj.source + + compare(instantiator.object.constantRole, 42) + compare(obj.dataChangedSignalSpy.count, 0) + + obj.constantRole.value = 43 + + compare(instantiator.object.constantRole, 43) + compare(obj.dataChangedSignalSpy.count, 1) + compare(obj.modelResetSignalSpy.count, 0) + } + } +}