Skip to content

Commit

Permalink
StatusQ: ModelCount simplified and property added
Browse files Browse the repository at this point in the history
Closes: #15740
  • Loading branch information
micieslak committed Oct 17, 2024
1 parent c22a15e commit 9332d1a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
5 changes: 4 additions & 1 deletion ui/StatusQ/include/StatusQ/modelcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ class QAbstractItemModel;
class ModelCount : public QObject {
Q_OBJECT
Q_PROPERTY(int count READ count NOTIFY countChanged)
Q_PROPERTY(bool empty READ empty NOTIFY emptyChanged)

public:
explicit ModelCount(QObject* parent = nullptr);

static ModelCount* qmlAttachedProperties(QObject* object);

int count() const;
bool empty() const;

signals:
void countChanged();
void emptyChanged();

private:
int m_intermediateCount = 0;
int m_count = 0;
};

QML_DECLARE_TYPEINFO(ModelCount, QML_HAS_ATTACHED_PROPERTIES)
39 changes: 21 additions & 18 deletions ui/StatusQ/src/modelcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ ModelCount::ModelCount(QObject* parent) : QObject(parent)
if (model == nullptr)
return;

connect(model, &QAbstractItemModel::rowsInserted,
this, &ModelCount::countChanged);
connect(model, &QAbstractItemModel::rowsRemoved,
this, &ModelCount::countChanged);
m_count = model->rowCount();

auto storeIntermediateCount = [this, model] {
this->m_intermediateCount = model->rowCount();
};
auto update = [this, model] {
auto wasEmpty = m_count == 0;
auto count = model->rowCount();

auto emitIfChanged = [this, model] {
if (this->m_intermediateCount != model->rowCount())
emit this->countChanged();
};
if (m_count == count)
return;

m_count = count;
emit this->countChanged();

connect(model, &QAbstractItemModel::modelAboutToBeReset, this, storeIntermediateCount);
connect(model, &QAbstractItemModel::layoutAboutToBeChanged, storeIntermediateCount);
if (wasEmpty != (count == 0))
this->emptyChanged();
};

connect(model, &QAbstractItemModel::modelReset, this, emitIfChanged);
connect(model, &QAbstractItemModel::layoutChanged, this, emitIfChanged);
connect(model, &QAbstractItemModel::rowsInserted, this, update);
connect(model, &QAbstractItemModel::rowsRemoved, this, update);
connect(model, &QAbstractItemModel::modelReset, this, update);
connect(model, &QAbstractItemModel::layoutChanged, this, update);
}

ModelCount* ModelCount::qmlAttachedProperties(QObject* object)
Expand All @@ -37,8 +38,10 @@ ModelCount* ModelCount::qmlAttachedProperties(QObject* object)

int ModelCount::count() const
{
if (auto model = qobject_cast<QAbstractItemModel*>(parent()))
return model->rowCount();
return m_count;
}

return 0;
bool ModelCount::empty() const
{
return m_count == 0;
}
32 changes: 24 additions & 8 deletions ui/StatusQ/tests/tst_ModelCount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,59 @@ private slots:
ModelCount modelCount(&model);

QCOMPARE(modelCount.count(), 4);
QCOMPARE(modelCount.empty(), false);

QSignalSpy spy(&modelCount, &ModelCount::countChanged);
QSignalSpy countSpy(&modelCount, &ModelCount::countChanged);
QSignalSpy emptySpy(&modelCount, &ModelCount::emptyChanged);

model.insert(1, { "e" });

QCOMPARE(spy.count(), 1);
QCOMPARE(countSpy.count(), 1);
QCOMPARE(emptySpy.count(), 0);
QCOMPARE(modelCount.count(), 5);
QCOMPARE(modelCount.empty(), false);

model.remove(0);

QCOMPARE(spy.count(), 2);
QCOMPARE(countSpy.count(), 2);
QCOMPARE(emptySpy.count(), 0);
QCOMPARE(modelCount.count(), 4);
QCOMPARE(modelCount.empty(), false);

model.update(0, 0, "aa");

QCOMPARE(spy.count(), 2);
QCOMPARE(countSpy.count(), 2);
QCOMPARE(emptySpy.count(), 0);
QCOMPARE(modelCount.count(), 4);
QCOMPARE(modelCount.empty(), false);

model.invert();

QCOMPARE(spy.count(), 2);
QCOMPARE(countSpy.count(), 2);
QCOMPARE(emptySpy.count(), 0);
QCOMPARE(modelCount.count(), 4);
QCOMPARE(modelCount.empty(), false);

model.removeEverySecond();

QCOMPARE(spy.count(), 3);
QCOMPARE(countSpy.count(), 3);
QCOMPARE(emptySpy.count(), 0);
QCOMPARE(modelCount.count(), 2);
QCOMPARE(modelCount.empty(), false);

model.reset();

QCOMPARE(spy.count(), 3);
QCOMPARE(countSpy.count(), 3);
QCOMPARE(emptySpy.count(), 0);
QCOMPARE(modelCount.count(), 2);
QCOMPARE(modelCount.empty(), false);

model.resetAndClear();

QCOMPARE(spy.count(), 4);
QCOMPARE(countSpy.count(), 4);
QCOMPARE(emptySpy.count(), 1);
QCOMPARE(modelCount.count(), 0);
QCOMPARE(modelCount.empty(), true);
}
};

Expand Down
2 changes: 1 addition & 1 deletion ui/imports/shared/views/AssetsViewAdaptor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ QObject {
if (!model.visible)
return false

if (filteredBalances.ModelCount.count === 0)
if (filteredBalances.ModelCount.empty)
return false

if (hasCommunityId)
Expand Down

0 comments on commit 9332d1a

Please sign in to comment.