From 144460b5aa8f13a3da6b573707055036c864489e Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Sat, 6 Dec 2025 14:34:17 +0100 Subject: [PATCH 1/5] Start with the current data, when editing a table cell. --- genericcore.cpp | 1 - model/tablemodel.cpp | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/genericcore.cpp b/genericcore.cpp index b589405..b70515a 100644 --- a/genericcore.cpp +++ b/genericcore.cpp @@ -82,7 +82,6 @@ QString GenericCore::getMaintenanceToolFilePath() const { /// setting the applicationDirPath hard coded to test update feature from IDE #ifdef QT_DEBUG - // REFACTOR retrieve application name automatically instead of using hard coded name applicationDirPath = QString("/opt/%1").arg(APPLICATION_NAME); #endif diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index a0e3e1a..8f2c461 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -11,7 +11,7 @@ static const QHash ROLE_NAMES = {{NameRole, "Name"}, TableModel::TableModel(QObject* parent) : QAbstractTableModel{parent} { - for (int row = 0; row < 23; ++row) { + for (int row = 0; row < 5; ++row) { QHash values; values[NameRole] = QString("Item %1").arg(row); values[DescriptionRole] = QString("This is item %1").arg(row); @@ -46,6 +46,7 @@ QVariant TableModel::data(const QModelIndex& index, int role) const { int roleForColumn = getRoleForColumn(column); switch (role) { case Qt::DisplayRole: + case Qt::EditRole: return m_items.at(row)->data(roleForColumn); } From 4c906099eb8caa6b1a7efeb69c9ea62e154741db Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Mon, 8 Dec 2025 13:25:02 +0100 Subject: [PATCH 2/5] Items can be added to model dynamically. --- CMakeLists.txt | 1 + formats/jsonparser.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++ formats/jsonparser.h | 23 +++++++++++++++++ genericcore.cpp | 2 +- genericcore.h | 2 +- model/tablemodel.cpp | 39 +++++++++++++++++++++++----- model/tablemodel.h | 12 ++++++++- 7 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 formats/jsonparser.cpp create mode 100644 formats/jsonparser.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 40ab47c..182468c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ add_library(${TARGET_APP} STATIC data/settingshandler.h data/settingshandler.cpp model/tablemodel.h model/tablemodel.cpp model/modelitem.h model/modelitem.cpp + formats/jsonparser.h formats/jsonparser.cpp ) include_directories(${CMAKE_CURRENT_BINARY_DIR}) diff --git a/formats/jsonparser.cpp b/formats/jsonparser.cpp new file mode 100644 index 0000000..5ef95cb --- /dev/null +++ b/formats/jsonparser.cpp @@ -0,0 +1,58 @@ +#include "jsonparser.h" + +#include +#include + +#include "../model/tablemodel.h" + +QList> JsonParser::toItemValuesList(const QByteArray& jsonData, + const QString& objectName) { + QList> result; + + if (jsonData.isEmpty()) { + return result; + } + + QJsonArray itemArray = extractItemArray(jsonData, objectName); + + foreach (QJsonValue value, itemArray) { + QJsonObject itemJsonObject = value.toObject(); + QHash values = jsonObjectToItemValues(itemJsonObject); + result.append(values); + } + return result; +} + +QHash JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) { + QHash values; + + // TODO make this more generic (by reading from model meta data) + values[TableModel::NameRole] = + itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::NameRole)].toString(); + values[TableModel::DescriptionRole] = + itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::DescriptionRole)].toString(); + values[TableModel::InfoRole] = + itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::InfoRole)].toString(); + values[TableModel::AmountRole] = + itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::AmountRole)].toInt(); + values[TableModel::FactorRole] = + itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::FactorRole)].toDouble(); + + return values; +} + +JsonParser::JsonParser() {} + +QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QString& objectName) { + QJsonDocument doc = QJsonDocument::fromJson(jsonData); + QJsonArray itemArray; + if (objectName.isEmpty()) { + itemArray = doc.array(); + + } else { + QJsonObject rootObject = doc.object(); + itemArray = rootObject.value(QString("items")).toArray(); + } + + return itemArray; +} diff --git a/formats/jsonparser.h b/formats/jsonparser.h new file mode 100644 index 0000000..36f557b --- /dev/null +++ b/formats/jsonparser.h @@ -0,0 +1,23 @@ +#ifndef JSONPARSER_H +#define JSONPARSER_H + +#include + +class QJsonObject; +class QString; +class QByteArray; +class QJsonArray; + +class JsonParser { + public: + static QList> toItemValuesList(const QByteArray& jsonData, + const QString& objectName = ""); + + private: + explicit JsonParser(); + + static QJsonArray extractItemArray(const QByteArray& jsonData, const QString& objectName); + static QHash jsonObjectToItemValues(const QJsonObject& itemJsonObject); +}; + +#endif // JSONPARSER_H diff --git a/genericcore.cpp b/genericcore.cpp index b70515a..3cd0747 100644 --- a/genericcore.cpp +++ b/genericcore.cpp @@ -70,7 +70,7 @@ void GenericCore::triggerApplicationUpdate() { QProcess::startDetached(toolFilePath, args); } -std::shared_ptr GenericCore::getModel() const { return m_mainModel; } +std::shared_ptr GenericCore::getModel() const { return m_mainModel; } void GenericCore::setupModels() { m_mainModel = make_shared(this); diff --git a/genericcore.h b/genericcore.h index e017fb3..27fc94c 100644 --- a/genericcore.h +++ b/genericcore.h @@ -21,7 +21,7 @@ class GenericCore : public QObject { bool isApplicationUpdateAvailable(); void triggerApplicationUpdate(); - std::shared_ptr getModel() const; + std::shared_ptr getModel() const; signals: void displayStatusMessage(QString message); diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index 8f2c461..36c57b8 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -1,13 +1,13 @@ #include "tablemodel.h" +#include "../formats/jsonparser.h" #include "modelitem.h" -enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole }; -static const QHash ROLE_NAMES = {{NameRole, "Name"}, - {DescriptionRole, "Description"}, - {InfoRole, "Info"}, - {AmountRole, "Amount"}, - {FactorRole, "Factor"}}; +QHash TableModel::ROLE_NAMES = {{NameRole, "Name"}, + {DescriptionRole, "Description"}, + {InfoRole, "Info"}, + {AmountRole, "Amount"}, + {FactorRole, "Factor"}}; TableModel::TableModel(QObject* parent) : QAbstractTableModel{parent} { @@ -28,6 +28,8 @@ Qt::ItemFlags TableModel::flags(const QModelIndex& index) const { return Qt::ItemIsEditable | QAbstractTableModel::flags(index); } +QHash TableModel::roleNames() const { return ROLE_NAMES; } + int TableModel::rowCount(const QModelIndex& parent) const { return m_items.size(); } int TableModel::columnCount(const QModelIndex& parent) const { return ROLE_NAMES.size(); } @@ -80,6 +82,31 @@ bool TableModel::setData(const QModelIndex& index, const QVariant& value, int ro // bool TableModel::setItemData(const QModelIndex& index, const QMap& roles) {} +void TableModel::appendItems(const QByteArray& jsonDoc) { insertItems(-1, jsonDoc, QModelIndex()); } + +void TableModel::insertItems(int startPosition, + const QByteArray& jsonDoc, + const QModelIndex& parentIndex) { + qInfo() << "Inserting item(s) into model..."; + if (parentIndex != QModelIndex()) { + qWarning() << "Using invalid parent index (no child support for now)"; + } + if (startPosition == -1 || startPosition > m_items.size()) { + /// Appending item(s) + startPosition = m_items.size(); + } + + QList> valueList = JsonParser::toItemValuesList(jsonDoc); + const int nRows = valueList.size(); + beginInsertRows(QModelIndex(), startPosition, startPosition + nRows - 1); + for (int row = 0; row < nRows; ++row) { + const int rowPosition = startPosition + row; + shared_ptr item = make_unique(valueList.at(row)); + m_items.insert(rowPosition, std::move(item)); + } + endInsertRows(); +} + int TableModel::getRoleForColumn(const int column) const { switch (column) { case 0: diff --git a/model/tablemodel.h b/model/tablemodel.h index f6b4180..c44b641 100644 --- a/model/tablemodel.h +++ b/model/tablemodel.h @@ -8,13 +8,19 @@ class ModelItem; using namespace std; class TableModel : public QAbstractTableModel { + Q_OBJECT + public: + enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole }; + static QHash ROLE_NAMES; + explicit TableModel(QObject* parent = nullptr); /// QAbstractItemModel interface Qt::ItemFlags flags(const QModelIndex& index) const override; + QHash roleNames() const override; - int rowCount(const QModelIndex& parent) const override; + int rowCount(const QModelIndex& parent = QModelIndex()) const override; int columnCount(const QModelIndex& parent) const override; QVariant data(const QModelIndex& index, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override; @@ -22,6 +28,10 @@ class TableModel : public QAbstractTableModel { bool setData(const QModelIndex& index, const QVariant& value, int role) override; // bool setItemData(const QModelIndex& index, const QMap& roles) override; + public slots: + void appendItems(const QByteArray& jsonDoc); + void insertItems(int startPosition, const QByteArray& jsonDoc, const QModelIndex& parentIndex); + private: /// members QList> m_items; From d45b1098f907fd8044aa84f3b206fddc59c650b0 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Mon, 8 Dec 2025 13:33:05 +0100 Subject: [PATCH 3/5] Added default invalid parentIndex to TableModel::columnCount and added Q_UNUSED macro to suppress warnings. --- model/tablemodel.cpp | 10 ++++++++-- model/tablemodel.h | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index 36c57b8..dc5fab7 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -30,9 +30,15 @@ Qt::ItemFlags TableModel::flags(const QModelIndex& index) const { QHash TableModel::roleNames() const { return ROLE_NAMES; } -int TableModel::rowCount(const QModelIndex& parent) const { return m_items.size(); } +int TableModel::rowCount(const QModelIndex& parent) const { + Q_UNUSED(parent); + return m_items.size(); +} -int TableModel::columnCount(const QModelIndex& parent) const { return ROLE_NAMES.size(); } +int TableModel::columnCount(const QModelIndex& parent) const { + Q_UNUSED(parent); + return ROLE_NAMES.size(); +} QVariant TableModel::data(const QModelIndex& index, int role) const { const int row = index.row(); diff --git a/model/tablemodel.h b/model/tablemodel.h index c44b641..63ca073 100644 --- a/model/tablemodel.h +++ b/model/tablemodel.h @@ -21,7 +21,7 @@ class TableModel : public QAbstractTableModel { QHash roleNames() const override; int rowCount(const QModelIndex& parent = QModelIndex()) const override; - int columnCount(const QModelIndex& parent) const override; + int columnCount(const QModelIndex& parent = QModelIndex()) const override; QVariant data(const QModelIndex& index, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override; From 169d8f9f1e27cba9ded7195811708d2b4c307bc5 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Mon, 8 Dec 2025 14:50:13 +0100 Subject: [PATCH 4/5] Items can be deleted from the model. --- model/tablemodel.cpp | 19 +++++++++++++++++++ model/tablemodel.h | 3 +++ 2 files changed, 22 insertions(+) diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index dc5fab7..551e125 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -88,6 +88,25 @@ bool TableModel::setData(const QModelIndex& index, const QVariant& value, int ro // bool TableModel::setItemData(const QModelIndex& index, const QMap& roles) {} +bool TableModel::removeRows(int position, int rows, const QModelIndex& parentIndex) { + if (parentIndex != QModelIndex()) { + qWarning() << "Removing of child rows is not supported yet!"; + return false; + } + + const int endPosition = position + rows; + if (position < 0 || endPosition >= m_items.size()) { + qWarning() << "Trying to remove rows is out of bounds!"; + return false; + } + + beginRemoveRows(QModelIndex(), position, position + rows - 1); + m_items.remove(position, rows); + endRemoveRows(); + + return true; +} + void TableModel::appendItems(const QByteArray& jsonDoc) { insertItems(-1, jsonDoc, QModelIndex()); } void TableModel::insertItems(int startPosition, diff --git a/model/tablemodel.h b/model/tablemodel.h index 63ca073..b105975 100644 --- a/model/tablemodel.h +++ b/model/tablemodel.h @@ -29,6 +29,9 @@ class TableModel : public QAbstractTableModel { // bool setItemData(const QModelIndex& index, const QMap& roles) override; public slots: + // bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex()) + // override; + bool removeRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex()) override; void appendItems(const QByteArray& jsonDoc); void insertItems(int startPosition, const QByteArray& jsonDoc, const QModelIndex& parentIndex); From e21c899aac15dd0bd18158ab18f3d53f1a478cb4 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Tue, 9 Dec 2025 09:33:46 +0100 Subject: [PATCH 5/5] Bugfix: Last row could not be removed due to miscalculations of the bounds. --- model/tablemodel.cpp | 10 +++++----- model/tablemodel.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index 551e125..7a05900 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -88,20 +88,20 @@ bool TableModel::setData(const QModelIndex& index, const QVariant& value, int ro // bool TableModel::setItemData(const QModelIndex& index, const QMap& roles) {} -bool TableModel::removeRows(int position, int rows, const QModelIndex& parentIndex) { +bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) { if (parentIndex != QModelIndex()) { qWarning() << "Removing of child rows is not supported yet!"; return false; } - const int endPosition = position + rows; - if (position < 0 || endPosition >= m_items.size()) { + const int lastRow = firstRow + nRows - 1; + if (firstRow < 0 || lastRow >= m_items.size()) { qWarning() << "Trying to remove rows is out of bounds!"; return false; } - beginRemoveRows(QModelIndex(), position, position + rows - 1); - m_items.remove(position, rows); + beginRemoveRows(QModelIndex(), firstRow, lastRow); + m_items.remove(firstRow, nRows); endRemoveRows(); return true; diff --git a/model/tablemodel.h b/model/tablemodel.h index b105975..5778f52 100644 --- a/model/tablemodel.h +++ b/model/tablemodel.h @@ -31,7 +31,7 @@ class TableModel : public QAbstractTableModel { public slots: // bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex()) // override; - bool removeRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex()) override; + bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override; void appendItems(const QByteArray& jsonDoc); void insertItems(int startPosition, const QByteArray& jsonDoc, const QModelIndex& parentIndex);