StatusQ: ConstantRole - custom SFPM proxy role added (#16405)

Closes: #16401
This commit is contained in:
Michał 2024-09-25 21:13:53 +02:00 committed by GitHub
parent bb7e5be065
commit d2d3a56810
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 161 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,26 @@
#pragma once
#include <proxyroles/singlerole.h>
#include <QVariant>
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;
};

View File

@ -0,0 +1,52 @@
#include "StatusQ/constantrole.h"
#include <qqmlsortfilterproxymodel.h>
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;
}

View File

@ -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<FastExpressionRole>("StatusQ", 0, 1, "FastExpressionRole");
qmlRegisterType<FastExpressionSorter>("StatusQ", 0, 1, "FastExpressionSorter");
qmlRegisterType<UndefinedFilter>("StatusQ", 0, 1, "UndefinedFilter");
qmlRegisterType<ConstantRole>("StatusQ", 0, 1, "ConstantRole");
qmlRegisterType<ObjectProxyModel>("StatusQ", 0, 1, "ObjectProxyModel");
qmlRegisterType<LeftJoinModel>("StatusQ", 0, 1, "LeftJoinModel");

View File

@ -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)
}
}
}