2024-06-28 09:25:03 +00:00
|
|
|
#include "StatusQ/modelcount.h"
|
|
|
|
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
|
|
|
|
ModelCount::ModelCount(QObject* parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
auto model = qobject_cast<QAbstractItemModel*>(parent);
|
|
|
|
|
|
|
|
if (model == nullptr)
|
|
|
|
return;
|
|
|
|
|
2024-10-17 10:40:02 +00:00
|
|
|
m_count = model->rowCount();
|
2024-06-28 09:25:03 +00:00
|
|
|
|
2024-10-17 10:40:02 +00:00
|
|
|
auto update = [this, model] {
|
|
|
|
auto wasEmpty = m_count == 0;
|
|
|
|
auto count = model->rowCount();
|
2024-06-28 09:25:03 +00:00
|
|
|
|
2024-10-17 10:40:02 +00:00
|
|
|
if (m_count == count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_count = count;
|
|
|
|
emit this->countChanged();
|
2024-06-28 09:25:03 +00:00
|
|
|
|
2024-10-17 10:40:02 +00:00
|
|
|
if (wasEmpty != (count == 0))
|
|
|
|
this->emptyChanged();
|
|
|
|
};
|
2024-06-28 09:25:03 +00:00
|
|
|
|
2024-10-17 10:40:02 +00:00
|
|
|
connect(model, &QAbstractItemModel::rowsInserted, this, update);
|
|
|
|
connect(model, &QAbstractItemModel::rowsRemoved, this, update);
|
|
|
|
connect(model, &QAbstractItemModel::modelReset, this, update);
|
|
|
|
connect(model, &QAbstractItemModel::layoutChanged, this, update);
|
2024-06-28 09:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ModelCount* ModelCount::qmlAttachedProperties(QObject* object)
|
|
|
|
{
|
|
|
|
return new ModelCount(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelCount::count() const
|
|
|
|
{
|
2024-10-17 10:40:02 +00:00
|
|
|
return m_count;
|
|
|
|
}
|
2024-06-28 09:25:03 +00:00
|
|
|
|
2024-10-17 10:40:02 +00:00
|
|
|
bool ModelCount::empty() const
|
|
|
|
{
|
|
|
|
return m_count == 0;
|
2024-06-28 09:25:03 +00:00
|
|
|
}
|