diff --git a/formats/jsonparser.cpp b/formats/jsonparser.cpp index 01bab27..9de0ea1 100644 --- a/formats/jsonparser.cpp +++ b/formats/jsonparser.cpp @@ -26,7 +26,8 @@ QList> JsonParser::toItemValuesList(const QByteArray& jsonD QHash JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) { QHash values; - // TODO make this more generic (by reading from model meta data) + // TODO iterate over "public & editable" roles (the ones that should occur in JSON file) + // & use a (static) function to retrieve it (i. e. getRoleValue(int role)) values[NameRole] = itemJsonObject[ROLE_NAMES.value(NameRole)].toString(); values[DescriptionRole] = itemJsonObject[ROLE_NAMES.value(DescriptionRole)].toString(); values[InfoRole] = itemJsonObject[ROLE_NAMES.value(InfoRole)].toString(); diff --git a/genericcore.cpp b/genericcore.cpp index 3893b9c..86459f5 100644 --- a/genericcore.cpp +++ b/genericcore.cpp @@ -12,6 +12,7 @@ #include "CoreConfig.h" #include "constants.h" #include "data/filehandler.h" +#include "model/metadata.h" #include "model/tablemodel.h" #include @@ -98,7 +99,7 @@ void GenericCore::saveItems() { qDebug() << "saving items..."; const QJsonDocument doc = m_mainModel->getAllItemsAsJsonDoc(); - const bool successfulSave = FileHandler::saveToFile(doc, "items.json"); + const bool successfulSave = FileHandler::saveToFile(doc, ITEM_FILE_NAME); if (successfulSave) { // QStringList completedTaskStrings = m_model->completedTasks(); // appendCompletedTasksToFile(completedTaskStrings, "completed.txt"); @@ -122,7 +123,7 @@ void GenericCore::setupModels() { */ void GenericCore::initModelData() { qInfo() << "Trying to read model data from file..."; - const QByteArray jsonDoc = FileHandler::loadJSONDataFromFile("items.json"); + const QByteArray jsonDoc = FileHandler::loadJSONDataFromFile(ITEM_FILE_NAME); // qDebug() << "jsonDoc:" << jsonDoc; // TODO decide on lack of file(s) (config, data) if example items should be generated // (see welcome wizard) diff --git a/model/metadata.h b/model/metadata.h index 76966e9..1c1342a 100644 --- a/model/metadata.h +++ b/model/metadata.h @@ -6,15 +6,25 @@ #include #include +/// 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 intColumns = {"Amount", "Factor"}; +// TODO ? use a data structure containing the type information of each column +// ([QString, QString, QString, int, double], {{NameRole, QString},...}) +// instead of INT_COLUMNS,... +static QList INT_COLUMNS = {"Amount", "Factor"}; -static int getRoleForColumn(const int column) { +/// JSON keys +static const QString ITEM_KEY_STRING = "items"; + +/// file naming +static const QString ITEM_FILE_NAME = ITEM_KEY_STRING + ".json"; + +static int GET_ROLE_FOR_COLUMN(const int column) { switch (column) { case 0: return NameRole; diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index 057f622..9885a45 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -28,7 +28,7 @@ QByteArray TableModel::generateExampleItems() { array.append(itemObject); } - rootObject.insert("items", array); + rootObject.insert(ITEM_KEY_STRING, array); doc.setObject(rootObject); return doc.toJson(); @@ -65,7 +65,7 @@ QVariant TableModel::data(const QModelIndex& index, int role) const { return QVariant(); } - int roleForColumn = getRoleForColumn(column); + int roleForColumn = GET_ROLE_FOR_COLUMN(column); switch (role) { case Qt::DisplayRole: case Qt::EditRole: @@ -84,7 +84,7 @@ QVariant TableModel::data(const QModelIndex& index, int role) const { QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role == Qt::DisplayRole) { if (orientation == Qt::Horizontal) { - const int columnRole = getRoleForColumn(section); + const int columnRole = GET_ROLE_FOR_COLUMN(section); const QString headerName = ROLE_NAMES.value(columnRole); return QString("%1").arg(headerName); } else { @@ -97,7 +97,7 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) { if (role == Qt::EditRole && checkIndex(index)) { const int column = index.column(); - const int roleForColumn = getRoleForColumn(column); + const int roleForColumn = GET_ROLE_FOR_COLUMN(column); return setItemData(index, {{roleForColumn, value}}); } return false; @@ -129,7 +129,7 @@ QJsonDocument TableModel::getAllItemsAsJsonDoc() const { QJsonObject itemObject = item->toJsonObject(); array.append(itemObject); } - rootObject.insert("items", array); + rootObject.insert(ITEM_KEY_STRING, array); doc.setObject(rootObject); return doc; @@ -167,7 +167,7 @@ void TableModel::insertItems(int startPosition, startPosition = m_items.size(); } - QList> valueList = JsonParser::toItemValuesList(jsonDoc, "items"); + QList> valueList = JsonParser::toItemValuesList(jsonDoc, ITEM_KEY_STRING); InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, valueList); m_undoStack->push(insertCommand); @@ -237,7 +237,7 @@ QMap TableModel::onlyChangedValues(const QModelIndex& index, bool TableModel::isEmptyValueEqualToZero(const int role) const { const QString roleName = ROLE_NAMES.value(role); - if (intColumns.contains(roleName)) { + if (INT_COLUMNS.contains(roleName)) { return true; } return false;