diff --git a/CMakeLists.txt b/CMakeLists.txt index ec6dc0a..6f599c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ add_library(${TARGET_APP} STATIC model/commands/removerowscommand.h model/commands/removerowscommand.cpp model/commands/edititemcommand.h model/commands/edititemcommand.cpp data/filehandler.h data/filehandler.cpp + model/metadata.h ) include_directories(${CMAKE_CURRENT_BINARY_DIR}) diff --git a/formats/jsonparser.cpp b/formats/jsonparser.cpp index 10ba77a..01bab27 100644 --- a/formats/jsonparser.cpp +++ b/formats/jsonparser.cpp @@ -3,7 +3,7 @@ #include #include -#include "../model/tablemodel.h" +#include "../model/metadata.h" QList> JsonParser::toItemValuesList(const QByteArray& jsonData, const QString& objectName) { @@ -27,16 +27,11 @@ QHash JsonParser::jsonObjectToItemValues(const QJsonObject& itemJ 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(); + values[NameRole] = itemJsonObject[ROLE_NAMES.value(NameRole)].toString(); + values[DescriptionRole] = itemJsonObject[ROLE_NAMES.value(DescriptionRole)].toString(); + values[InfoRole] = itemJsonObject[ROLE_NAMES.value(InfoRole)].toString(); + values[AmountRole] = itemJsonObject[ROLE_NAMES.value(AmountRole)].toInt(); + values[FactorRole] = itemJsonObject[ROLE_NAMES.value(FactorRole)].toDouble(); return values; } diff --git a/model/commands/edititemcommand.cpp b/model/commands/edititemcommand.cpp index 4c57bc1..1e86550 100644 --- a/model/commands/edititemcommand.cpp +++ b/model/commands/edititemcommand.cpp @@ -2,6 +2,7 @@ #include +#include "../metadata.h" #include "../tablemodel.h" EditItemCommand::EditItemCommand(TableModel* model, @@ -20,23 +21,23 @@ EditItemCommand::EditItemCommand(TableModel* model, const QVariant value = changedValues.first(); QString roleName = model->roleNames().value(role); switch (role) { - case TableModel::NameRole: - case TableModel::DescriptionRole: - case TableModel::InfoRole: - case TableModel::AmountRole: - case TableModel::FactorRole: + case NameRole: + case DescriptionRole: + case InfoRole: + case AmountRole: + case FactorRole: commandText = QString("Setting '%1' of item '%2' to '%3'") .arg(roleName) - .arg(index.data(TableModel::NameRole).toString()) + .arg(index.data(NameRole).toString()) .arg(value.toString()); break; default: - commandText = QString("Edit item '%1'").arg(index.data(TableModel::NameRole).toString()); + commandText = QString("Edit item '%1'").arg(index.data(NameRole).toString()); break; } } else { 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); diff --git a/model/commands/edititemcommand.h b/model/commands/edititemcommand.h index a8a2c23..f0ea909 100644 --- a/model/commands/edititemcommand.h +++ b/model/commands/edititemcommand.h @@ -8,6 +8,9 @@ class TableModel; class EditItemCommand : public QUndoCommand { 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, const QModelIndex& index, QMap& changedValues, diff --git a/model/commands/removerowscommand.cpp b/model/commands/removerowscommand.cpp index 306c9aa..41351af 100644 --- a/model/commands/removerowscommand.cpp +++ b/model/commands/removerowscommand.cpp @@ -2,6 +2,7 @@ #include +#include "../metadata.h" #include "../tablemodel.h" RemoveRowsCommand::RemoveRowsCommand(TableModel* model, @@ -22,11 +23,11 @@ RemoveRowsCommand::RemoveRowsCommand(TableModel* model, // TODO use a (static) function "getRoleValueHash" or something QHash values; - values[TableModel::NameRole] = m_tableModel->data(index, TableModel::NameRole); - values[TableModel::DescriptionRole] = m_tableModel->data(index, TableModel::DescriptionRole); - values[TableModel::InfoRole] = m_tableModel->data(index, TableModel::InfoRole); - values[TableModel::AmountRole] = m_tableModel->data(index, TableModel::AmountRole); - values[TableModel::FactorRole] = m_tableModel->data(index, TableModel::FactorRole); + 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); m_valueList.append(values); } diff --git a/model/metadata.h b/model/metadata.h new file mode 100644 index 0000000..76966e9 --- /dev/null +++ b/model/metadata.h @@ -0,0 +1,41 @@ +#ifndef METADATA_H +#define METADATA_H + +#include +#include +#include +#include + +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 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 diff --git a/model/modelitem.cpp b/model/modelitem.cpp index 5df55b6..94fccbd 100644 --- a/model/modelitem.cpp +++ b/model/modelitem.cpp @@ -1,6 +1,6 @@ #include "modelitem.h" -#include "tablemodel.h" +#include "metadata.h" #include #include @@ -48,16 +48,11 @@ QJsonObject ModelItem::toJsonObject() const { QJsonObject itemObject; // itemObject.insert("uuid", m_uuid.toString()); // itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate)); - itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::NameRole), - data(TableModel::NameRole).toString()); - itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::DescriptionRole), - data(TableModel::DescriptionRole).toString()); - itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::InfoRole), - 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()); + 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()); // if (m_modifiedDateUTC.isValid()) { // itemObject.insert("modifiedDateUTC", m_modifiedDateUTC.toString(Qt::ISODate)); diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index ce706fa..057f622 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -4,19 +4,13 @@ #include "commands/edititemcommand.h" #include "commands/insertrowscommand.h" #include "commands/removerowscommand.h" +#include "metadata.h" #include "modelitem.h" #include #include #include -QHash TableModel::ROLE_NAMES = {{NameRole, "Name"}, - {DescriptionRole, "Description"}, - {InfoRole, "Info"}, - {AmountRole, "Amount"}, - {FactorRole, "Factor"}}; -QList TableModel::intColumns = {"Amount", "Factor"}; - QByteArray TableModel::generateExampleItems() { QJsonDocument doc = QJsonDocument(); QJsonObject rootObject; @@ -26,14 +20,11 @@ QByteArray TableModel::generateExampleItems() { QJsonObject itemObject; // itemObject.insert("uuid", m_uuid.toString()); // itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate)); - itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::NameRole), - QString("Item %1").arg(row)); - itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::DescriptionRole), - QString("This is item %1").arg(row)); - itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::InfoRole), - 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); + itemObject.insert(ROLE_NAMES.value(NameRole), QString("Item %1").arg(row)); + itemObject.insert(ROLE_NAMES.value(DescriptionRole), QString("This is item %1").arg(row)); + itemObject.insert(ROLE_NAMES.value(InfoRole), QString("Info of item %1").arg(row)); + itemObject.insert(ROLE_NAMES.value(AmountRole), row); + itemObject.insert(ROLE_NAMES.value(FactorRole), row * 1.1); array.append(itemObject); } @@ -219,29 +210,6 @@ void TableModel::execEditItemData(const int row, const QMap& 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 TableModel::onlyChangedValues(const QModelIndex& index, const QMap& roleValueMap) const { QMap result; diff --git a/model/tablemodel.h b/model/tablemodel.h index 2bb758a..ea21a21 100644 --- a/model/tablemodel.h +++ b/model/tablemodel.h @@ -16,9 +16,6 @@ class TableModel : public QAbstractTableModel { friend class EditItemCommand; public: - enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole }; - static QHash ROLE_NAMES; - static QList intColumns; static QByteArray generateExampleItems(); explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr); @@ -59,7 +56,6 @@ class TableModel : public QAbstractTableModel { void execEditItemData(const int row, const QMap& changedValues); /// misc functions - int getRoleForColumn(const int column) const; QMap onlyChangedValues(const QModelIndex& index, const QMap& roleValueMap) const; bool isEmptyValueEqualToZero(const int role) const;