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:
@ -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})
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "../model/tablemodel.h"
|
||||
#include "../model/metadata.h"
|
||||
|
||||
QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonData,
|
||||
const QString& objectName) {
|
||||
@ -27,16 +27,11 @@ QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJ
|
||||
QHash<int, QVariant> 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;
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#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);
|
||||
|
||||
|
||||
@ -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<int, QVariant>& changedValues,
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#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<int, QVariant> 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);
|
||||
}
|
||||
|
||||
41
model/metadata.h
Normal file
41
model/metadata.h
Normal 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
|
||||
@ -1,6 +1,6 @@
|
||||
#include "modelitem.h"
|
||||
|
||||
#include "tablemodel.h"
|
||||
#include "metadata.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
@ -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));
|
||||
|
||||
@ -4,19 +4,13 @@
|
||||
#include "commands/edititemcommand.h"
|
||||
#include "commands/insertrowscommand.h"
|
||||
#include "commands/removerowscommand.h"
|
||||
#include "metadata.h"
|
||||
#include "modelitem.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#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() {
|
||||
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<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,
|
||||
const QMap<int, QVariant>& roleValueMap) const {
|
||||
QMap<int, QVariant> result;
|
||||
|
||||
@ -16,9 +16,6 @@ class TableModel : public QAbstractTableModel {
|
||||
friend class EditItemCommand;
|
||||
|
||||
public:
|
||||
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
||||
static QHash<int, QByteArray> ROLE_NAMES;
|
||||
static QList<QString> 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<int, QVariant>& changedValues);
|
||||
|
||||
/// misc functions
|
||||
int getRoleForColumn(const int column) const;
|
||||
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
|
||||
const QMap<int, QVariant>& roleValueMap) const;
|
||||
bool isEmptyValueEqualToZero(const int role) const;
|
||||
|
||||
Reference in New Issue
Block a user