Iterating over USER_FACING_ROLES to get all the item values when parsing and keeping them in edit and remove commands. Instead of using each role explicitly.
This commit is contained in:
@ -23,6 +23,39 @@ QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonD
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QByteArray JsonParser::itemValuesListToJson(const QList<QHash<int, QVariant>>& itemValuesList,
|
||||||
|
const QString& objectName) {
|
||||||
|
QJsonDocument jsonDoc;
|
||||||
|
QJsonObject rootObject;
|
||||||
|
QJsonArray itemArray;
|
||||||
|
for (const QHash<int, QVariant>& itemValues : itemValuesList) {
|
||||||
|
QJsonObject itemObject;
|
||||||
|
|
||||||
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
|
while (i.hasNext()) {
|
||||||
|
const UserRoles role = i.next();
|
||||||
|
const QString roleName = ROLE_NAMES.value(role);
|
||||||
|
const QVariant value = itemValues.value(role);
|
||||||
|
if (STRING_ROLES.contains(role)) {
|
||||||
|
itemObject.insert(roleName, value.toString());
|
||||||
|
} else if (INT_ROLES.contains(role)) {
|
||||||
|
itemObject.insert(roleName, value.toInt());
|
||||||
|
} else if (DOUBLE_ROLES.contains(role)) {
|
||||||
|
itemObject.insert(roleName, value.toDouble());
|
||||||
|
} else {
|
||||||
|
qCritical() << QString("Can't find data type for role %1!!!").arg(role);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
itemArray.append(itemObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
rootObject.insert(objectName, itemArray);
|
||||||
|
jsonDoc.setObject(rootObject);
|
||||||
|
|
||||||
|
return jsonDoc.toJson(QJsonDocument::Compact);
|
||||||
|
}
|
||||||
|
|
||||||
JsonParser::JsonParser() {}
|
JsonParser::JsonParser() {}
|
||||||
|
|
||||||
QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QString& objectName) {
|
QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QString& objectName) {
|
||||||
@ -42,8 +75,10 @@ QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QStrin
|
|||||||
QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
||||||
QHash<int, QVariant> values;
|
QHash<int, QVariant> values;
|
||||||
|
|
||||||
for (auto iter = ROLE_NAMES.cbegin(), end = ROLE_NAMES.cend(); iter != end; ++iter) {
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, iter.key());
|
while (i.hasNext()) {
|
||||||
|
const UserRoles role = i.next();
|
||||||
|
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, role);
|
||||||
values.insert(keyValuePair.first, keyValuePair.second);
|
values.insert(keyValuePair.first, keyValuePair.second);
|
||||||
}
|
}
|
||||||
return values;
|
return values;
|
||||||
|
|||||||
@ -14,6 +14,8 @@ class JsonParser {
|
|||||||
public:
|
public:
|
||||||
static QList<QHash<int, QVariant>> toItemValuesList(const QByteArray& jsonData,
|
static QList<QHash<int, QVariant>> toItemValuesList(const QByteArray& jsonData,
|
||||||
const QString& objectName = "");
|
const QString& objectName = "");
|
||||||
|
static QByteArray itemValuesListToJson(const QList<QHash<int, QVariant>>& itemValuesList,
|
||||||
|
const QString& objectName = "");
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit JsonParser();
|
explicit JsonParser();
|
||||||
|
|||||||
@ -20,24 +20,19 @@ EditItemCommand::EditItemCommand(TableModel* model,
|
|||||||
const int role = changedValues.firstKey();
|
const int role = changedValues.firstKey();
|
||||||
const QVariant value = changedValues.first();
|
const QVariant value = changedValues.first();
|
||||||
QString roleName = model->roleNames().value(role);
|
QString roleName = model->roleNames().value(role);
|
||||||
switch (role) {
|
|
||||||
case NameRole:
|
if (USER_FACING_ROLES.contains(role)) {
|
||||||
case DescriptionRole:
|
|
||||||
case InfoRole:
|
|
||||||
case AmountRole:
|
|
||||||
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(NameRole).toString())
|
.arg(index.data(DEFAULT_ROLE).toString())
|
||||||
.arg(value.toString());
|
.arg(value.toString());
|
||||||
break;
|
} else {
|
||||||
default:
|
qWarning() << "Role didn't match! Using a generic command text...";
|
||||||
commandText = QString("Edit item '%1'").arg(index.data(NameRole).toString());
|
commandText = QString("Edit item '%1'").arg(index.data(DEFAULT_ROLE).toString());
|
||||||
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(NameRole).toString());
|
commandText = QString("Edit item '%1'").arg(index.data(DEFAULT_ROLE).toString());
|
||||||
}
|
}
|
||||||
setText(commandText);
|
setText(commandText);
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
#include "../metadata.h"
|
|
||||||
#include "../tablemodel.h"
|
#include "../tablemodel.h"
|
||||||
|
|
||||||
RemoveRowsCommand::RemoveRowsCommand(TableModel* model,
|
RemoveRowsCommand::RemoveRowsCommand(TableModel* model,
|
||||||
@ -21,13 +20,7 @@ RemoveRowsCommand::RemoveRowsCommand(TableModel* model,
|
|||||||
const int rowPosition = startRow + row;
|
const int rowPosition = startRow + row;
|
||||||
QModelIndex index = m_tableModel->index(rowPosition, 0);
|
QModelIndex index = m_tableModel->index(rowPosition, 0);
|
||||||
|
|
||||||
// TODO use a (static) function "getRoleValueHash" or something
|
QHash<int, QVariant> values = m_tableModel->getItemValues(index);
|
||||||
QHash<int, QVariant> values;
|
|
||||||
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);
|
m_valueList.append(values);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,9 @@
|
|||||||
|
|
||||||
/// model data
|
/// model data
|
||||||
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
||||||
|
static UserRoles DEFAULT_ROLE = NameRole;
|
||||||
|
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole,
|
||||||
|
FactorRole};
|
||||||
static QHash<int, QByteArray> ROLE_NAMES = {{NameRole, "Name"},
|
static QHash<int, QByteArray> ROLE_NAMES = {{NameRole, "Name"},
|
||||||
{DescriptionRole, "Description"},
|
{DescriptionRole, "Description"},
|
||||||
{InfoRole, "Info"},
|
{InfoRole, "Info"},
|
||||||
|
|||||||
@ -46,19 +46,22 @@ bool ModelItem::setItemData(const QMap<int, QVariant>& changedValues) {
|
|||||||
|
|
||||||
QJsonObject ModelItem::toJsonObject() const {
|
QJsonObject ModelItem::toJsonObject() const {
|
||||||
QJsonObject itemObject;
|
QJsonObject itemObject;
|
||||||
// itemObject.insert("uuid", m_uuid.toString());
|
// TODO add UUID and dates (entry, modification, end)
|
||||||
// itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate));
|
|
||||||
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()) {
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
// itemObject.insert("modifiedDateUTC", m_modifiedDateUTC.toString(Qt::ISODate));
|
while (i.hasNext()) {
|
||||||
// }
|
const UserRoles role = i.next();
|
||||||
// if (m_endDateUTC.isValid()) {
|
const QString roleName = ROLE_NAMES.value(role);
|
||||||
// itemObject.insert("endDateUTC", m_endDateUTC.toString(Qt::ISODate));
|
const QVariant value = data(role);
|
||||||
// }
|
if (STRING_ROLES.contains(role)) {
|
||||||
|
itemObject.insert(roleName, value.toString());
|
||||||
|
} else if (INT_ROLES.contains(role)) {
|
||||||
|
itemObject.insert(roleName, value.toInt());
|
||||||
|
} else if (DOUBLE_ROLES.contains(role)) {
|
||||||
|
itemObject.insert(roleName, value.toDouble());
|
||||||
|
} else {
|
||||||
|
qCritical() << QString("Cant find data type of role %1!!!").arg(role);
|
||||||
|
}
|
||||||
|
}
|
||||||
return itemObject;
|
return itemObject;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -120,6 +120,17 @@ bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QHash<int, QVariant> TableModel::getItemValues(const QModelIndex& index) const {
|
||||||
|
QHash<int, QVariant> values;
|
||||||
|
|
||||||
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
|
while (i.hasNext()) {
|
||||||
|
const UserRoles role = i.next();
|
||||||
|
values.insert(role, data(index, role));
|
||||||
|
}
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
QJsonDocument TableModel::getAllItemsAsJsonDoc() const {
|
QJsonDocument TableModel::getAllItemsAsJsonDoc() const {
|
||||||
QJsonDocument doc = QJsonDocument();
|
QJsonDocument doc = QJsonDocument();
|
||||||
QJsonObject rootObject;
|
QJsonObject rootObject;
|
||||||
|
|||||||
@ -32,6 +32,7 @@ class TableModel : public QAbstractTableModel {
|
|||||||
bool setData(const QModelIndex& index, const QVariant& value, int role) override;
|
bool setData(const QModelIndex& index, const QVariant& value, int role) override;
|
||||||
bool setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) override;
|
bool setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) override;
|
||||||
|
|
||||||
|
QHash<int, QVariant> getItemValues(const QModelIndex& index) const;
|
||||||
QJsonDocument getAllItemsAsJsonDoc() const;
|
QJsonDocument getAllItemsAsJsonDoc() const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|||||||
Reference in New Issue
Block a user