From b8fe4e65024baef929c54f5385caed6548f90dd4 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Wed, 31 Dec 2025 10:39:32 +0100 Subject: [PATCH] Iterating over USER_FACING_ROLES to get all the item values when parsing and keeping them in edit and remove commands. Instead of using each role explicitly. --- formats/jsonparser.cpp | 39 ++++++++++++++++++++++++++-- formats/jsonparser.h | 2 ++ model/commands/edititemcommand.cpp | 25 +++++++----------- model/commands/removerowscommand.cpp | 9 +------ model/metadata.h | 19 ++++++++------ model/modelitem.cpp | 29 +++++++++++---------- model/tablemodel.cpp | 11 ++++++++ model/tablemodel.h | 1 + 8 files changed, 89 insertions(+), 46 deletions(-) diff --git a/formats/jsonparser.cpp b/formats/jsonparser.cpp index 1121949..e63005b 100644 --- a/formats/jsonparser.cpp +++ b/formats/jsonparser.cpp @@ -23,6 +23,39 @@ QList> JsonParser::toItemValuesList(const QByteArray& jsonD return result; } +QByteArray JsonParser::itemValuesListToJson(const QList>& itemValuesList, + const QString& objectName) { + QJsonDocument jsonDoc; + QJsonObject rootObject; + QJsonArray itemArray; + for (const QHash& itemValues : itemValuesList) { + QJsonObject itemObject; + + QListIterator i(USER_FACING_ROLES); + while (i.hasNext()) { + const UserRoles role = i.next(); + const QString roleName = ROLE_NAMES.value(role); + const QVariant value = itemValues.value(role); + if (STRING_ROLES.contains(role)) { + itemObject.insert(roleName, value.toString()); + } else if (INT_ROLES.contains(role)) { + itemObject.insert(roleName, value.toInt()); + } else if (DOUBLE_ROLES.contains(role)) { + itemObject.insert(roleName, value.toDouble()); + } else { + qCritical() << QString("Can't find data type for role %1!!!").arg(role); + } + } + + itemArray.append(itemObject); + } + + rootObject.insert(objectName, itemArray); + jsonDoc.setObject(rootObject); + + return jsonDoc.toJson(QJsonDocument::Compact); +} + JsonParser::JsonParser() {} QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QString& objectName) { @@ -42,8 +75,10 @@ QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QStrin QHash JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) { QHash values; - for (auto iter = ROLE_NAMES.cbegin(), end = ROLE_NAMES.cend(); iter != end; ++iter) { - std::pair keyValuePair = getKeyValuePair(itemJsonObject, iter.key()); + QListIterator i(USER_FACING_ROLES); + while (i.hasNext()) { + const UserRoles role = i.next(); + std::pair keyValuePair = getKeyValuePair(itemJsonObject, role); values.insert(keyValuePair.first, keyValuePair.second); } return values; diff --git a/formats/jsonparser.h b/formats/jsonparser.h index 8feb08d..702414d 100644 --- a/formats/jsonparser.h +++ b/formats/jsonparser.h @@ -14,6 +14,8 @@ class JsonParser { public: static QList> toItemValuesList(const QByteArray& jsonData, const QString& objectName = ""); + static QByteArray itemValuesListToJson(const QList>& itemValuesList, + const QString& objectName = ""); private: explicit JsonParser(); diff --git a/model/commands/edititemcommand.cpp b/model/commands/edititemcommand.cpp index 1e86550..449272b 100644 --- a/model/commands/edititemcommand.cpp +++ b/model/commands/edititemcommand.cpp @@ -20,24 +20,19 @@ EditItemCommand::EditItemCommand(TableModel* model, const int role = changedValues.firstKey(); const QVariant value = changedValues.first(); QString roleName = model->roleNames().value(role); - switch (role) { - case NameRole: - case DescriptionRole: - case InfoRole: - case AmountRole: - case FactorRole: - commandText = QString("Setting '%1' of item '%2' to '%3'") - .arg(roleName) - .arg(index.data(NameRole).toString()) - .arg(value.toString()); - break; - default: - commandText = QString("Edit item '%1'").arg(index.data(NameRole).toString()); - break; + + if (USER_FACING_ROLES.contains(role)) { + commandText = QString("Setting '%1' of item '%2' to '%3'") + .arg(roleName) + .arg(index.data(DEFAULT_ROLE).toString()) + .arg(value.toString()); + } else { + qWarning() << "Role didn't match! Using a generic command text..."; + commandText = QString("Edit item '%1'").arg(index.data(DEFAULT_ROLE).toString()); } } else { qDebug() << "More than one value to change. Using a generic command text..."; - commandText = QString("Edit item '%1'").arg(index.data(NameRole).toString()); + commandText = QString("Edit item '%1'").arg(index.data(DEFAULT_ROLE).toString()); } setText(commandText); diff --git a/model/commands/removerowscommand.cpp b/model/commands/removerowscommand.cpp index 41351af..487a916 100644 --- a/model/commands/removerowscommand.cpp +++ b/model/commands/removerowscommand.cpp @@ -2,7 +2,6 @@ #include -#include "../metadata.h" #include "../tablemodel.h" RemoveRowsCommand::RemoveRowsCommand(TableModel* model, @@ -21,13 +20,7 @@ RemoveRowsCommand::RemoveRowsCommand(TableModel* model, const int rowPosition = startRow + row; QModelIndex index = m_tableModel->index(rowPosition, 0); - // TODO use a (static) function "getRoleValueHash" or something - QHash values; - values[NameRole] = m_tableModel->data(index, NameRole); - values[DescriptionRole] = m_tableModel->data(index, DescriptionRole); - values[InfoRole] = m_tableModel->data(index, InfoRole); - values[AmountRole] = m_tableModel->data(index, AmountRole); - values[FactorRole] = m_tableModel->data(index, FactorRole); + QHash values = m_tableModel->getItemValues(index); m_valueList.append(values); } diff --git a/model/metadata.h b/model/metadata.h index aac7c71..580d931 100644 --- a/model/metadata.h +++ b/model/metadata.h @@ -8,14 +8,17 @@ /// model data enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole }; -static QHash ROLE_NAMES = {{NameRole, "Name"}, - {DescriptionRole, "Description"}, - {InfoRole, "Info"}, - {AmountRole, "Amount"}, - {FactorRole, "Factor"}}; -static QList STRING_ROLES = {NameRole, DescriptionRole, InfoRole}; -static QList INT_ROLES = {AmountRole}; -static QList DOUBLE_ROLES = {FactorRole}; +static UserRoles DEFAULT_ROLE = NameRole; +static QList USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole, + FactorRole}; +static QHash ROLE_NAMES = {{NameRole, "Name"}, + {DescriptionRole, "Description"}, + {InfoRole, "Info"}, + {AmountRole, "Amount"}, + {FactorRole, "Factor"}}; +static QList STRING_ROLES = {NameRole, DescriptionRole, InfoRole}; +static QList INT_ROLES = {AmountRole}; +static QList DOUBLE_ROLES = {FactorRole}; /// JSON keys static const QString ITEM_KEY_STRING = "items"; diff --git a/model/modelitem.cpp b/model/modelitem.cpp index 94fccbd..7abf6bf 100644 --- a/model/modelitem.cpp +++ b/model/modelitem.cpp @@ -46,19 +46,22 @@ bool ModelItem::setItemData(const QMap& changedValues) { QJsonObject ModelItem::toJsonObject() const { QJsonObject itemObject; - // itemObject.insert("uuid", m_uuid.toString()); - // itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate)); - itemObject.insert(ROLE_NAMES.value(NameRole), data(NameRole).toString()); - itemObject.insert(ROLE_NAMES.value(DescriptionRole), data(DescriptionRole).toString()); - itemObject.insert(ROLE_NAMES.value(InfoRole), data(InfoRole).toString()); - itemObject.insert(ROLE_NAMES.value(AmountRole), data(AmountRole).toInt()); - itemObject.insert(ROLE_NAMES.value(FactorRole), data(FactorRole).toReal()); + // TODO add UUID and dates (entry, modification, end) - // if (m_modifiedDateUTC.isValid()) { - // itemObject.insert("modifiedDateUTC", m_modifiedDateUTC.toString(Qt::ISODate)); - // } - // if (m_endDateUTC.isValid()) { - // itemObject.insert("endDateUTC", m_endDateUTC.toString(Qt::ISODate)); - // } + QListIterator i(USER_FACING_ROLES); + while (i.hasNext()) { + const UserRoles role = i.next(); + const QString roleName = ROLE_NAMES.value(role); + const QVariant value = data(role); + if (STRING_ROLES.contains(role)) { + itemObject.insert(roleName, value.toString()); + } else if (INT_ROLES.contains(role)) { + itemObject.insert(roleName, value.toInt()); + } else if (DOUBLE_ROLES.contains(role)) { + itemObject.insert(roleName, value.toDouble()); + } else { + qCritical() << QString("Cant find data type of role %1!!!").arg(role); + } + } return itemObject; } diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index 0071eb5..2ae6cdf 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -120,6 +120,17 @@ bool TableModel::setItemData(const QModelIndex& index, const QMap return false; } +QHash TableModel::getItemValues(const QModelIndex& index) const { + QHash values; + + QListIterator i(USER_FACING_ROLES); + while (i.hasNext()) { + const UserRoles role = i.next(); + values.insert(role, data(index, role)); + } + return values; +} + QJsonDocument TableModel::getAllItemsAsJsonDoc() const { QJsonDocument doc = QJsonDocument(); QJsonObject rootObject; diff --git a/model/tablemodel.h b/model/tablemodel.h index ea21a21..c7650f2 100644 --- a/model/tablemodel.h +++ b/model/tablemodel.h @@ -32,6 +32,7 @@ class TableModel : public QAbstractTableModel { bool setData(const QModelIndex& index, const QVariant& value, int role) override; bool setItemData(const QModelIndex& index, const QMap& roles) override; + QHash getItemValues(const QModelIndex& index) const; QJsonDocument getAllItemsAsJsonDoc() const; public slots: