feat(StatusQ): Generic attachd type for providing model count
Closes: #15374
This commit is contained in:
parent
05b47fb831
commit
d3d0080b2d
|
@ -102,6 +102,7 @@ add_library(StatusQ SHARED
|
||||||
include/StatusQ/functionaggregator.h
|
include/StatusQ/functionaggregator.h
|
||||||
include/StatusQ/groupingmodel.h
|
include/StatusQ/groupingmodel.h
|
||||||
include/StatusQ/leftjoinmodel.h
|
include/StatusQ/leftjoinmodel.h
|
||||||
|
include/StatusQ/modelcount.h
|
||||||
include/StatusQ/modelentry.h
|
include/StatusQ/modelentry.h
|
||||||
include/StatusQ/modelsyncedcontainer.h
|
include/StatusQ/modelsyncedcontainer.h
|
||||||
include/StatusQ/modelutilsinternal.h
|
include/StatusQ/modelutilsinternal.h
|
||||||
|
@ -130,6 +131,7 @@ add_library(StatusQ SHARED
|
||||||
src/functionaggregator.cpp
|
src/functionaggregator.cpp
|
||||||
src/groupingmodel.cpp
|
src/groupingmodel.cpp
|
||||||
src/leftjoinmodel.cpp
|
src/leftjoinmodel.cpp
|
||||||
|
src/modelcount.cpp
|
||||||
src/modelentry.cpp
|
src/modelentry.cpp
|
||||||
src/modelutilsinternal.cpp
|
src/modelutilsinternal.cpp
|
||||||
src/movablemodel.cpp
|
src/movablemodel.cpp
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QQmlEngine>
|
||||||
|
|
||||||
|
class QAbstractItemModel;
|
||||||
|
|
||||||
|
class ModelCount : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PROPERTY(int count READ count NOTIFY countChanged)
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ModelCount(QObject* parent = nullptr);
|
||||||
|
|
||||||
|
static ModelCount* qmlAttachedProperties(QObject* object);
|
||||||
|
|
||||||
|
int count() const;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void countChanged();
|
||||||
|
|
||||||
|
private:
|
||||||
|
int m_intermediateCount = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
QML_DECLARE_TYPEINFO(ModelCount, QML_HAS_ATTACHED_PROPERTIES)
|
|
@ -0,0 +1,44 @@
|
||||||
|
#include "StatusQ/modelcount.h"
|
||||||
|
|
||||||
|
#include <QAbstractItemModel>
|
||||||
|
|
||||||
|
ModelCount::ModelCount(QObject* parent) : QObject(parent)
|
||||||
|
{
|
||||||
|
auto model = qobject_cast<QAbstractItemModel*>(parent);
|
||||||
|
|
||||||
|
if (model == nullptr)
|
||||||
|
return;
|
||||||
|
|
||||||
|
connect(model, &QAbstractItemModel::rowsInserted,
|
||||||
|
this, &ModelCount::countChanged);
|
||||||
|
connect(model, &QAbstractItemModel::rowsRemoved,
|
||||||
|
this, &ModelCount::countChanged);
|
||||||
|
|
||||||
|
auto storeIntermediateCount = [this, model] {
|
||||||
|
this->m_intermediateCount = model->rowCount();
|
||||||
|
};
|
||||||
|
|
||||||
|
auto emitIfChanged = [this, model] {
|
||||||
|
if (this->m_intermediateCount != model->rowCount())
|
||||||
|
emit this->countChanged();
|
||||||
|
};
|
||||||
|
|
||||||
|
connect(model, &QAbstractItemModel::modelAboutToBeReset, this, storeIntermediateCount);
|
||||||
|
connect(model, &QAbstractItemModel::layoutAboutToBeChanged, storeIntermediateCount);
|
||||||
|
|
||||||
|
connect(model, &QAbstractItemModel::modelReset, this, emitIfChanged);
|
||||||
|
connect(model, &QAbstractItemModel::layoutChanged, this, emitIfChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelCount* ModelCount::qmlAttachedProperties(QObject* object)
|
||||||
|
{
|
||||||
|
return new ModelCount(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ModelCount::count() const
|
||||||
|
{
|
||||||
|
if (auto model = qobject_cast<QAbstractItemModel*>(parent()))
|
||||||
|
return model->rowCount();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -12,6 +12,7 @@
|
||||||
#include "StatusQ/functionaggregator.h"
|
#include "StatusQ/functionaggregator.h"
|
||||||
#include "StatusQ/groupingmodel.h"
|
#include "StatusQ/groupingmodel.h"
|
||||||
#include "StatusQ/leftjoinmodel.h"
|
#include "StatusQ/leftjoinmodel.h"
|
||||||
|
#include "StatusQ/modelcount.h"
|
||||||
#include "StatusQ/modelentry.h"
|
#include "StatusQ/modelentry.h"
|
||||||
#include "StatusQ/modelutilsinternal.h"
|
#include "StatusQ/modelutilsinternal.h"
|
||||||
#include "StatusQ/movablemodel.h"
|
#include "StatusQ/movablemodel.h"
|
||||||
|
@ -71,6 +72,9 @@ public:
|
||||||
qmlRegisterType<ModelEntry>("StatusQ", 0, 1, "ModelEntry");
|
qmlRegisterType<ModelEntry>("StatusQ", 0, 1, "ModelEntry");
|
||||||
qmlRegisterType<SnapshotObject>("StatusQ", 0, 1, "SnapshotObject");
|
qmlRegisterType<SnapshotObject>("StatusQ", 0, 1, "SnapshotObject");
|
||||||
|
|
||||||
|
qmlRegisterUncreatableType<ModelCount>("StatusQ", 0, 1,
|
||||||
|
"ModelCount", "This is attached type, cannot be created directly.");
|
||||||
|
|
||||||
qmlRegisterSingletonType<ModelUtilsInternal>(
|
qmlRegisterSingletonType<ModelUtilsInternal>(
|
||||||
"StatusQ.Internal", 0, 1, "ModelUtils", &ModelUtilsInternal::qmlInstance);
|
"StatusQ.Internal", 0, 1, "ModelUtils", &ModelUtilsInternal::qmlInstance);
|
||||||
|
|
||||||
|
|
|
@ -115,3 +115,7 @@ add_test(NAME SnapshotObjectTest COMMAND SnapshotObjectTest)
|
||||||
add_executable(GroupingModelTest tst_GroupingModel.cpp)
|
add_executable(GroupingModelTest tst_GroupingModel.cpp)
|
||||||
target_link_libraries(GroupingModelTest PRIVATE Qt5::Qml StatusQ StatusQTestLib)
|
target_link_libraries(GroupingModelTest PRIVATE Qt5::Qml StatusQ StatusQTestLib)
|
||||||
add_test(NAME GroupingModelTest COMMAND GroupingModelTest)
|
add_test(NAME GroupingModelTest COMMAND GroupingModelTest)
|
||||||
|
|
||||||
|
add_executable(ModelCountTest tst_ModelCount.cpp)
|
||||||
|
target_link_libraries(ModelCountTest PRIVATE StatusQ StatusQTestLib)
|
||||||
|
add_test(NAME ModelCountTest COMMAND ModelCountTest)
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
#include <QtTest>
|
||||||
|
|
||||||
|
#include <StatusQ/modelcount.h>
|
||||||
|
|
||||||
|
#include <TestHelpers/testmodel.h>
|
||||||
|
|
||||||
|
|
||||||
|
class TestModelCount : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void modelCountTest()
|
||||||
|
{
|
||||||
|
TestModel model({
|
||||||
|
{ "name", { "a", "b", "c", "d" }}
|
||||||
|
});
|
||||||
|
|
||||||
|
ModelCount modelCount(&model);
|
||||||
|
|
||||||
|
QCOMPARE(modelCount.count(), 4);
|
||||||
|
|
||||||
|
QSignalSpy spy(&modelCount, &ModelCount::countChanged);
|
||||||
|
|
||||||
|
model.insert(1, { "e" });
|
||||||
|
|
||||||
|
QCOMPARE(spy.count(), 1);
|
||||||
|
QCOMPARE(modelCount.count(), 5);
|
||||||
|
|
||||||
|
model.remove(0);
|
||||||
|
|
||||||
|
QCOMPARE(spy.count(), 2);
|
||||||
|
QCOMPARE(modelCount.count(), 4);
|
||||||
|
|
||||||
|
model.update(0, 0, "aa");
|
||||||
|
|
||||||
|
QCOMPARE(spy.count(), 2);
|
||||||
|
QCOMPARE(modelCount.count(), 4);
|
||||||
|
|
||||||
|
model.invert();
|
||||||
|
|
||||||
|
QCOMPARE(spy.count(), 2);
|
||||||
|
QCOMPARE(modelCount.count(), 4);
|
||||||
|
|
||||||
|
model.removeEverySecond();
|
||||||
|
|
||||||
|
QCOMPARE(spy.count(), 3);
|
||||||
|
QCOMPARE(modelCount.count(), 2);
|
||||||
|
|
||||||
|
model.reset();
|
||||||
|
|
||||||
|
QCOMPARE(spy.count(), 3);
|
||||||
|
QCOMPARE(modelCount.count(), 2);
|
||||||
|
|
||||||
|
model.resetAndClear();
|
||||||
|
|
||||||
|
QCOMPARE(spy.count(), 4);
|
||||||
|
QCOMPARE(modelCount.count(), 0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
QTEST_MAIN(TestModelCount)
|
||||||
|
#include "tst_ModelCount.moc"
|
Loading…
Reference in New Issue