Refactored JsonParser to not use roles (or their names) directly.
This commit is contained in:
@ -23,20 +23,6 @@ QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonD
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
|
||||||
QHash<int, QVariant> values;
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
values[AmountRole] = itemJsonObject[ROLE_NAMES.value(AmountRole)].toInt();
|
|
||||||
values[FactorRole] = itemJsonObject[ROLE_NAMES.value(FactorRole)].toDouble();
|
|
||||||
|
|
||||||
return values;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonParser::JsonParser() {}
|
JsonParser::JsonParser() {}
|
||||||
|
|
||||||
QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QString& objectName) {
|
QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QString& objectName) {
|
||||||
@ -52,3 +38,28 @@ QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QStrin
|
|||||||
|
|
||||||
return itemArray;
|
return itemArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
||||||
|
QHash<int, QVariant> values;
|
||||||
|
|
||||||
|
for (auto iter = ROLE_NAMES.cbegin(), end = ROLE_NAMES.cend(); iter != end; ++iter) {
|
||||||
|
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, iter.key());
|
||||||
|
values.insert(keyValuePair.first, keyValuePair.second);
|
||||||
|
}
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
pair<int, QVariant> JsonParser::getKeyValuePair(const QJsonObject& itemJsonObject, const int role) {
|
||||||
|
QVariant result;
|
||||||
|
const QJsonValue jsonValue = itemJsonObject[ROLE_NAMES.value(role)];
|
||||||
|
if (STRING_ROLES.contains(role)) {
|
||||||
|
result = jsonValue.toString();
|
||||||
|
} else if (INT_ROLES.contains(role)) {
|
||||||
|
result = jsonValue.toInt();
|
||||||
|
} else if (DOUBLE_ROLES.contains(role)) {
|
||||||
|
result = jsonValue.toDouble();
|
||||||
|
} else {
|
||||||
|
qCritical() << QString("Cant find data type of role %1!!!").arg(role);
|
||||||
|
}
|
||||||
|
return pair<int, QVariant>(role, result.toString());
|
||||||
|
}
|
||||||
|
|||||||
@ -8,6 +8,8 @@ class QString;
|
|||||||
class QByteArray;
|
class QByteArray;
|
||||||
class QJsonArray;
|
class QJsonArray;
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
class JsonParser {
|
class JsonParser {
|
||||||
public:
|
public:
|
||||||
static QList<QHash<int, QVariant>> toItemValuesList(const QByteArray& jsonData,
|
static QList<QHash<int, QVariant>> toItemValuesList(const QByteArray& jsonData,
|
||||||
@ -18,6 +20,8 @@ class JsonParser {
|
|||||||
|
|
||||||
static QJsonArray extractItemArray(const QByteArray& jsonData, const QString& objectName);
|
static QJsonArray extractItemArray(const QByteArray& jsonData, const QString& objectName);
|
||||||
static QHash<int, QVariant> jsonObjectToItemValues(const QJsonObject& itemJsonObject);
|
static QHash<int, QVariant> jsonObjectToItemValues(const QJsonObject& itemJsonObject);
|
||||||
|
|
||||||
|
static pair<int, QVariant> getKeyValuePair(const QJsonObject& itemJsonObject, const int role);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // JSONPARSER_H
|
#endif // JSONPARSER_H
|
||||||
|
|||||||
@ -13,10 +13,9 @@ static QHash<int, QByteArray> ROLE_NAMES = {{NameRole, "Name"},
|
|||||||
{InfoRole, "Info"},
|
{InfoRole, "Info"},
|
||||||
{AmountRole, "Amount"},
|
{AmountRole, "Amount"},
|
||||||
{FactorRole, "Factor"}};
|
{FactorRole, "Factor"}};
|
||||||
// TODO ? use a data structure containing the type information of each column
|
static QList<UserRoles> STRING_ROLES = {NameRole, DescriptionRole, InfoRole};
|
||||||
// ([QString, QString, QString, int, double], {{NameRole, QString},...})
|
static QList<UserRoles> INT_ROLES = {AmountRole};
|
||||||
// instead of INT_COLUMNS,...
|
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
|
||||||
static QList<QString> INT_COLUMNS = {"Amount", "Factor"};
|
|
||||||
|
|
||||||
/// JSON keys
|
/// JSON keys
|
||||||
static const QString ITEM_KEY_STRING = "items";
|
static const QString ITEM_KEY_STRING = "items";
|
||||||
@ -24,6 +23,7 @@ static const QString ITEM_KEY_STRING = "items";
|
|||||||
/// file naming
|
/// file naming
|
||||||
static const QString ITEM_FILE_NAME = ITEM_KEY_STRING + ".json";
|
static const QString ITEM_FILE_NAME = ITEM_KEY_STRING + ".json";
|
||||||
|
|
||||||
|
/// functions
|
||||||
static int GET_ROLE_FOR_COLUMN(const int column) {
|
static int GET_ROLE_FOR_COLUMN(const int column) {
|
||||||
switch (column) {
|
switch (column) {
|
||||||
case 0:
|
case 0:
|
||||||
|
|||||||
@ -236,9 +236,11 @@ QMap<int, QVariant> TableModel::onlyChangedValues(const QModelIndex& index,
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool TableModel::isEmptyValueEqualToZero(const int role) const {
|
bool TableModel::isEmptyValueEqualToZero(const int role) const {
|
||||||
const QString roleName = ROLE_NAMES.value(role);
|
if (INT_ROLES.contains(role)) {
|
||||||
if (INT_COLUMNS.contains(roleName)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if (DOUBLE_ROLES.contains(role)) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user