Using "model/metadata.h" for file and JSON object naming as well. Using all upper case for naming static meta data constants and functions.

This commit is contained in:
2025-12-29 10:35:00 +01:00
parent 0c3d711513
commit 3610aa3841
4 changed files with 24 additions and 12 deletions

View File

@ -26,7 +26,8 @@ QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonD
QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
QHash<int, QVariant> 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();

View File

@ -12,6 +12,7 @@
#include "CoreConfig.h"
#include "constants.h"
#include "data/filehandler.h"
#include "model/metadata.h"
#include "model/tablemodel.h"
#include <QtGui/QUndoStack>
@ -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)

View File

@ -6,15 +6,25 @@
#include <QList>
#include <QString>
/// model data
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"};
// 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<QString> 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;

View File

@ -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<QHash<int, QVariant>> valueList = JsonParser::toItemValuesList(jsonDoc, "items");
QList<QHash<int, QVariant>> valueList = JsonParser::toItemValuesList(jsonDoc, ITEM_KEY_STRING);
InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, valueList);
m_undoStack->push(insertCommand);
@ -237,7 +237,7 @@ QMap<int, QVariant> 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;