Added a ModelSummary class to make the overview over the model content accessible via QProperty system & a SummaryWidget to show this information. Only property rowCount as a proof of concept. Other properties will follow.

This commit is contained in:
2026-02-15 16:36:13 +01:00
parent b28a35280c
commit dac9ac46f2
12 changed files with 157 additions and 10 deletions

View File

@ -32,6 +32,7 @@ add_library(${TARGET_APP} STATIC
model/tablemodel.h model/tablemodel.cpp
model/modelitem.h model/modelitem.cpp
model/generalsortfiltermodel.h model/generalsortfiltermodel.cpp
model/modelsummary.h model/modelsummary.cpp
model/commands/insertrowscommand.h model/commands/insertrowscommand.cpp
model/commands/removerowscommand.h model/commands/removerowscommand.cpp
model/commands/edititemcommand.h model/commands/edititemcommand.cpp

View File

@ -17,11 +17,10 @@
#include "data/settingshandler.h"
#include "model/generalsortfiltermodel.h"
#include "model/metadata.h"
#include "model/modelsummary.h"
#include "model/tablemodel.h"
#include "network/servercommunicator.h"
using namespace std;
GenericCore::GenericCore() {
qDebug() << "Creating core...";
@ -97,6 +96,8 @@ std::shared_ptr<GeneralSortFilterModel> GenericCore::getSortFilterModel() const
return m_sortFilterModel;
}
shared_ptr<ModelSummary> GenericCore::getModelSummary() const { return m_modelSummary; }
/**
* Save items to default file (in standard location).
* @brief GenericCore::saveItems Saves item fo file.
@ -163,6 +164,8 @@ void GenericCore::setupModels() {
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
m_sortFilterModel = make_shared<GeneralSortFilterModel>(m_mainModel);
m_modelSummary = make_shared<ModelSummary>(m_mainModel, this);
/// QAbstractItemModelTester
#ifdef QT_DEBUG
m_mainModelTester = make_unique<QAbstractItemModelTester>(

View File

@ -14,8 +14,11 @@ class QString;
class TableModel;
class GeneralSortFilterModel;
class ModelSummary;
class ServerCommunicator;
using namespace std;
class GenericCore : public QObject {
Q_OBJECT
@ -30,8 +33,10 @@ class GenericCore : public QObject {
void triggerApplicationUpdate(const bool saveChanges);
QUndoStack* getModelUndoStack() const;
std::shared_ptr<TableModel> getModel() const;
std::shared_ptr<GeneralSortFilterModel> getSortFilterModel() const;
shared_ptr<TableModel> getModel() const;
shared_ptr<GeneralSortFilterModel> getSortFilterModel() const;
shared_ptr<ModelSummary> getModelSummary() const;
void saveItems();
void importCSVFile(const QString& filePath);
@ -60,10 +65,11 @@ class GenericCore : public QObject {
private:
QUndoStack* m_modelUndoStack;
std::shared_ptr<TableModel> m_mainModel;
std::shared_ptr<GeneralSortFilterModel> m_sortFilterModel;
std::unique_ptr<QAbstractItemModelTester> m_mainModelTester;
std::unique_ptr<QAbstractItemModelTester> m_proxyModelTester;
shared_ptr<TableModel> m_mainModel;
shared_ptr<GeneralSortFilterModel> m_sortFilterModel;
shared_ptr<ModelSummary> m_modelSummary;
unique_ptr<QAbstractItemModelTester> m_mainModelTester;
unique_ptr<QAbstractItemModelTester> m_proxyModelTester;
void setupModels();
void initModelData();
@ -71,7 +77,7 @@ class GenericCore : public QObject {
QString getMaintenanceToolFilePath() const;
/// Network communication
std::unique_ptr<ServerCommunicator> m_serverCommunicator;
unique_ptr<ServerCommunicator> m_serverCommunicator;
void setupServerConfiguration();
void applyServerConfiguration();
};

View File

@ -0,0 +1,22 @@
#include "modelsummary.h"
#include "tablemodel.h"
ModelSummary::ModelSummary(std::shared_ptr<TableModel> model, QObject* parent)
: QObject(parent)
, m_model(model) {
Q_ASSERT(model);
connect(m_model.get(), &TableModel::rowCountChanged, this, &ModelSummary::rowCountChanged);
}
ModelSummary::~ModelSummary() {}
int ModelSummary::rowCount() const {
const int nRows = m_model->rowCount();
return nRows;
}
QBindable<int> ModelSummary::bindableRowCount() {
m_rowCount = m_model->rowCount();
return &m_rowCount;
}

View File

@ -0,0 +1,32 @@
#ifndef MODELSUMMARY_H
#define MODELSUMMARY_H
#include <QObject>
#include <QProperty>
class TableModel;
using namespace std;
class ModelSummary : public QObject {
Q_OBJECT
Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged BINDABLE bindableRowCount)
public:
ModelSummary(shared_ptr<TableModel> model, QObject* parent = nullptr);
~ModelSummary();
int rowCount() const;
QBindable<int> bindableRowCount();
signals:
void rowCountChanged();
private:
shared_ptr<TableModel> m_model;
Q_OBJECT_BINDABLE_PROPERTY(ModelSummary, int, m_rowCount, &ModelSummary::rowCountChanged);
};
#endif // MODELSUMMARY_H

View File

@ -46,7 +46,10 @@ QByteArray TableModel::generateExampleItems() {
TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
: QAbstractTableModel{parent}
, m_undoStack(undoStack) {}
, m_undoStack(undoStack) {
connect(this, &TableModel::rowsInserted, this, &TableModel::onRowCountChanged);
connect(this, &TableModel::rowsRemoved, this, &TableModel::onRowCountChanged);
}
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
if (!index.isValid()) {
@ -279,6 +282,16 @@ void TableModel::insertItems(int startPosition,
m_undoStack->push(insertCommand);
}
void TableModel::onRowCountChanged(const QModelIndex& parent, int first, int last) {
Q_UNUSED(first);
Q_UNUSED(last);
if (parent != QModelIndex()) {
return;
}
emit rowCountChanged();
}
void TableModel::execInsertItems(const int firstRow, const QList<ModelItemValues> valueList) {
const int nRows = valueList.size();
qDebug() << "Inserting" << nRows << "items...";

View File

@ -2,6 +2,7 @@
#define TABLEMODEL_H
#include <QAbstractTableModel>
#include "metadata.h"
class QUndoStack;
class ModelItem;
@ -55,6 +56,12 @@ class TableModel : public QAbstractTableModel {
const QList<ModelItemValues>& itemValuesList,
const QModelIndex& parentIndex = QModelIndex());
signals:
void rowCountChanged();
private slots:
void onRowCountChanged(const QModelIndex& parent, int first, int last);
private:
/// *** members ***
// TODO shared_ptr -> unique_ptr