Moved the model meta data for roles and columns into "model/metadata.h" to gather meta data like this in one central place.

This commit is contained in:
2025-12-29 10:30:12 +01:00
parent b2f01a7990
commit 0c3d711513
9 changed files with 78 additions and 77 deletions

View File

@ -29,6 +29,7 @@ add_library(${TARGET_APP} STATIC
model/commands/removerowscommand.h model/commands/removerowscommand.cpp model/commands/removerowscommand.h model/commands/removerowscommand.cpp
model/commands/edititemcommand.h model/commands/edititemcommand.cpp model/commands/edititemcommand.h model/commands/edititemcommand.cpp
data/filehandler.h data/filehandler.cpp data/filehandler.h data/filehandler.cpp
model/metadata.h
) )
include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR})

View File

@ -3,7 +3,7 @@
#include <QJsonArray> #include <QJsonArray>
#include <QJsonObject> #include <QJsonObject>
#include "../model/tablemodel.h" #include "../model/metadata.h"
QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonData, QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonData,
const QString& objectName) { const QString& objectName) {
@ -27,16 +27,11 @@ QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJ
QHash<int, QVariant> values; QHash<int, QVariant> values;
// TODO make this more generic (by reading from model meta data) // TODO make this more generic (by reading from model meta data)
values[TableModel::NameRole] = values[NameRole] = itemJsonObject[ROLE_NAMES.value(NameRole)].toString();
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::NameRole)].toString(); values[DescriptionRole] = itemJsonObject[ROLE_NAMES.value(DescriptionRole)].toString();
values[TableModel::DescriptionRole] = values[InfoRole] = itemJsonObject[ROLE_NAMES.value(InfoRole)].toString();
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::DescriptionRole)].toString(); values[AmountRole] = itemJsonObject[ROLE_NAMES.value(AmountRole)].toInt();
values[TableModel::InfoRole] = values[FactorRole] = itemJsonObject[ROLE_NAMES.value(FactorRole)].toDouble();
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; return values;
} }

View File

@ -2,6 +2,7 @@
#include <QDebug> #include <QDebug>
#include "../metadata.h"
#include "../tablemodel.h" #include "../tablemodel.h"
EditItemCommand::EditItemCommand(TableModel* model, EditItemCommand::EditItemCommand(TableModel* model,
@ -20,23 +21,23 @@ EditItemCommand::EditItemCommand(TableModel* model,
const QVariant value = changedValues.first(); const QVariant value = changedValues.first();
QString roleName = model->roleNames().value(role); QString roleName = model->roleNames().value(role);
switch (role) { switch (role) {
case TableModel::NameRole: case NameRole:
case TableModel::DescriptionRole: case DescriptionRole:
case TableModel::InfoRole: case InfoRole:
case TableModel::AmountRole: case AmountRole:
case TableModel::FactorRole: case FactorRole:
commandText = QString("Setting '%1' of item '%2' to '%3'") commandText = QString("Setting '%1' of item '%2' to '%3'")
.arg(roleName) .arg(roleName)
.arg(index.data(TableModel::NameRole).toString()) .arg(index.data(NameRole).toString())
.arg(value.toString()); .arg(value.toString());
break; break;
default: default:
commandText = QString("Edit item '%1'").arg(index.data(TableModel::NameRole).toString()); commandText = QString("Edit item '%1'").arg(index.data(NameRole).toString());
break; break;
} }
} else { } else {
qDebug() << "More than one value to change. Using a generic command text..."; qDebug() << "More than one value to change. Using a generic command text...";
commandText = QString("Edit item '%1'").arg(index.data(TableModel::NameRole).toString()); commandText = QString("Edit item '%1'").arg(index.data(NameRole).toString());
} }
setText(commandText); setText(commandText);

View File

@ -8,6 +8,9 @@ class TableModel;
class EditItemCommand : public QUndoCommand { class EditItemCommand : public QUndoCommand {
public: public:
// TODO don't use simple pointer to model
/// Using simple pointer to model because there was a crash when closing the application with an
/// unclean undo stack
EditItemCommand(TableModel* model, EditItemCommand(TableModel* model,
const QModelIndex& index, const QModelIndex& index,
QMap<int, QVariant>& changedValues, QMap<int, QVariant>& changedValues,

View File

@ -2,6 +2,7 @@
#include <QDebug> #include <QDebug>
#include "../metadata.h"
#include "../tablemodel.h" #include "../tablemodel.h"
RemoveRowsCommand::RemoveRowsCommand(TableModel* model, RemoveRowsCommand::RemoveRowsCommand(TableModel* model,
@ -22,11 +23,11 @@ RemoveRowsCommand::RemoveRowsCommand(TableModel* model,
// TODO use a (static) function "getRoleValueHash" or something // TODO use a (static) function "getRoleValueHash" or something
QHash<int, QVariant> values; QHash<int, QVariant> values;
values[TableModel::NameRole] = m_tableModel->data(index, TableModel::NameRole); values[NameRole] = m_tableModel->data(index, NameRole);
values[TableModel::DescriptionRole] = m_tableModel->data(index, TableModel::DescriptionRole); values[DescriptionRole] = m_tableModel->data(index, DescriptionRole);
values[TableModel::InfoRole] = m_tableModel->data(index, TableModel::InfoRole); values[InfoRole] = m_tableModel->data(index, InfoRole);
values[TableModel::AmountRole] = m_tableModel->data(index, TableModel::AmountRole); values[AmountRole] = m_tableModel->data(index, AmountRole);
values[TableModel::FactorRole] = m_tableModel->data(index, TableModel::FactorRole); values[FactorRole] = m_tableModel->data(index, FactorRole);
m_valueList.append(values); m_valueList.append(values);
} }

41
model/metadata.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef METADATA_H
#define METADATA_H
#include <QDebug>
#include <QHash>
#include <QList>
#include <QString>
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
static QHash<int, QByteArray> ROLE_NAMES = {{NameRole, "Name"},
{DescriptionRole, "Description"},
{InfoRole, "Info"},
{AmountRole, "Amount"},
{FactorRole, "Factor"}};
static QList<QString> intColumns = {"Amount", "Factor"};
static int getRoleForColumn(const int column) {
switch (column) {
case 0:
return NameRole;
break;
case 1:
return DescriptionRole;
break;
case 2:
return InfoRole;
break;
case 3:
return AmountRole;
break;
case 4:
return FactorRole;
break;
default:
qWarning() << QString("No role found for column %1! Returning 'NameRole'...").arg(column);
return NameRole;
break;
}
}
#endif // METADATA_H

View File

@ -1,6 +1,6 @@
#include "modelitem.h" #include "modelitem.h"
#include "tablemodel.h" #include "metadata.h"
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
@ -48,16 +48,11 @@ QJsonObject ModelItem::toJsonObject() const {
QJsonObject itemObject; QJsonObject itemObject;
// itemObject.insert("uuid", m_uuid.toString()); // itemObject.insert("uuid", m_uuid.toString());
// itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate)); // itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate));
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::NameRole), itemObject.insert(ROLE_NAMES.value(NameRole), data(NameRole).toString());
data(TableModel::NameRole).toString()); itemObject.insert(ROLE_NAMES.value(DescriptionRole), data(DescriptionRole).toString());
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::DescriptionRole), itemObject.insert(ROLE_NAMES.value(InfoRole), data(InfoRole).toString());
data(TableModel::DescriptionRole).toString()); itemObject.insert(ROLE_NAMES.value(AmountRole), data(AmountRole).toInt());
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::InfoRole), itemObject.insert(ROLE_NAMES.value(FactorRole), data(FactorRole).toReal());
data(TableModel::InfoRole).toString());
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::AmountRole),
data(TableModel::AmountRole).toInt());
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::FactorRole),
data(TableModel::FactorRole).toReal());
// if (m_modifiedDateUTC.isValid()) { // if (m_modifiedDateUTC.isValid()) {
// itemObject.insert("modifiedDateUTC", m_modifiedDateUTC.toString(Qt::ISODate)); // itemObject.insert("modifiedDateUTC", m_modifiedDateUTC.toString(Qt::ISODate));

View File

@ -4,19 +4,13 @@
#include "commands/edititemcommand.h" #include "commands/edititemcommand.h"
#include "commands/insertrowscommand.h" #include "commands/insertrowscommand.h"
#include "commands/removerowscommand.h" #include "commands/removerowscommand.h"
#include "metadata.h"
#include "modelitem.h" #include "modelitem.h"
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
{DescriptionRole, "Description"},
{InfoRole, "Info"},
{AmountRole, "Amount"},
{FactorRole, "Factor"}};
QList<QString> TableModel::intColumns = {"Amount", "Factor"};
QByteArray TableModel::generateExampleItems() { QByteArray TableModel::generateExampleItems() {
QJsonDocument doc = QJsonDocument(); QJsonDocument doc = QJsonDocument();
QJsonObject rootObject; QJsonObject rootObject;
@ -26,14 +20,11 @@ QByteArray TableModel::generateExampleItems() {
QJsonObject itemObject; QJsonObject itemObject;
// itemObject.insert("uuid", m_uuid.toString()); // itemObject.insert("uuid", m_uuid.toString());
// itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate)); // itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate));
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::NameRole), itemObject.insert(ROLE_NAMES.value(NameRole), QString("Item %1").arg(row));
QString("Item %1").arg(row)); itemObject.insert(ROLE_NAMES.value(DescriptionRole), QString("This is item %1").arg(row));
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::DescriptionRole), itemObject.insert(ROLE_NAMES.value(InfoRole), QString("Info of item %1").arg(row));
QString("This is item %1").arg(row)); itemObject.insert(ROLE_NAMES.value(AmountRole), row);
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::InfoRole), itemObject.insert(ROLE_NAMES.value(FactorRole), row * 1.1);
QString("Info of item %1").arg(row));
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::AmountRole), row);
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::FactorRole), row * 1.1);
array.append(itemObject); array.append(itemObject);
} }
@ -219,29 +210,6 @@ void TableModel::execEditItemData(const int row, const QMap<int, QVariant>& chan
} }
} }
int TableModel::getRoleForColumn(const int column) const {
switch (column) {
case 0:
return NameRole;
break;
case 1:
return DescriptionRole;
break;
case 2:
return InfoRole;
break;
case 3:
return AmountRole;
break;
case 4:
return FactorRole;
break;
default:
return NameRole;
break;
}
}
QMap<int, QVariant> TableModel::onlyChangedValues(const QModelIndex& index, QMap<int, QVariant> TableModel::onlyChangedValues(const QModelIndex& index,
const QMap<int, QVariant>& roleValueMap) const { const QMap<int, QVariant>& roleValueMap) const {
QMap<int, QVariant> result; QMap<int, QVariant> result;

View File

@ -16,9 +16,6 @@ class TableModel : public QAbstractTableModel {
friend class EditItemCommand; friend class EditItemCommand;
public: public:
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
static QHash<int, QByteArray> ROLE_NAMES;
static QList<QString> intColumns;
static QByteArray generateExampleItems(); static QByteArray generateExampleItems();
explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr); explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr);
@ -59,7 +56,6 @@ class TableModel : public QAbstractTableModel {
void execEditItemData(const int row, const QMap<int, QVariant>& changedValues); void execEditItemData(const int row, const QMap<int, QVariant>& changedValues);
/// misc functions /// misc functions
int getRoleForColumn(const int column) const;
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index, QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
const QMap<int, QVariant>& roleValueMap) const; const QMap<int, QVariant>& roleValueMap) const;
bool isEmptyValueEqualToZero(const int role) const; bool isEmptyValueEqualToZero(const int role) const;