Merge branch 'feature/refactorModelMetaDataInCentralLocation' into develop
This commit is contained in:
@ -29,6 +29,7 @@ add_library(${TARGET_APP} STATIC
|
|||||||
model/commands/removerowscommand.h model/commands/removerowscommand.cpp
|
model/commands/removerowscommand.h model/commands/removerowscommand.cpp
|
||||||
model/commands/edititemcommand.h model/commands/edititemcommand.cpp
|
model/commands/edititemcommand.h model/commands/edititemcommand.cpp
|
||||||
data/filehandler.h data/filehandler.cpp
|
data/filehandler.h data/filehandler.cpp
|
||||||
|
model/metadata.h
|
||||||
)
|
)
|
||||||
|
|
||||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
|
||||||
#include "../model/tablemodel.h"
|
#include "../model/metadata.h"
|
||||||
|
|
||||||
QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonData,
|
QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonData,
|
||||||
const QString& objectName) {
|
const QString& objectName) {
|
||||||
@ -23,22 +23,37 @@ QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonD
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
QByteArray JsonParser::itemValuesListToJson(const QList<QHash<int, QVariant>>& itemValuesList,
|
||||||
QHash<int, QVariant> values;
|
const QString& objectName) {
|
||||||
|
QJsonDocument jsonDoc;
|
||||||
|
QJsonObject rootObject;
|
||||||
|
QJsonArray itemArray;
|
||||||
|
for (const QHash<int, QVariant>& itemValues : itemValuesList) {
|
||||||
|
QJsonObject itemObject;
|
||||||
|
|
||||||
// TODO make this more generic (by reading from model meta data)
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
values[TableModel::NameRole] =
|
while (i.hasNext()) {
|
||||||
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::NameRole)].toString();
|
const UserRoles role = i.next();
|
||||||
values[TableModel::DescriptionRole] =
|
const QString roleName = ROLE_NAMES.value(role);
|
||||||
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::DescriptionRole)].toString();
|
const QVariant value = itemValues.value(role);
|
||||||
values[TableModel::InfoRole] =
|
if (STRING_ROLES.contains(role)) {
|
||||||
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::InfoRole)].toString();
|
itemObject.insert(roleName, value.toString());
|
||||||
values[TableModel::AmountRole] =
|
} else if (INT_ROLES.contains(role)) {
|
||||||
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::AmountRole)].toInt();
|
itemObject.insert(roleName, value.toInt());
|
||||||
values[TableModel::FactorRole] =
|
} else if (DOUBLE_ROLES.contains(role)) {
|
||||||
itemJsonObject[TableModel::ROLE_NAMES.value(TableModel::FactorRole)].toDouble();
|
itemObject.insert(roleName, value.toDouble());
|
||||||
|
} else {
|
||||||
|
qCritical() << QString("Can't find data type for role %1!!!").arg(role);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return values;
|
itemArray.append(itemObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
rootObject.insert(objectName, itemArray);
|
||||||
|
jsonDoc.setObject(rootObject);
|
||||||
|
|
||||||
|
return jsonDoc.toJson(QJsonDocument::Compact);
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonParser::JsonParser() {}
|
JsonParser::JsonParser() {}
|
||||||
@ -56,3 +71,30 @@ QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QStrin
|
|||||||
|
|
||||||
return itemArray;
|
return itemArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
||||||
|
QHash<int, QVariant> values;
|
||||||
|
|
||||||
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
|
while (i.hasNext()) {
|
||||||
|
const UserRoles role = i.next();
|
||||||
|
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, role);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@ -8,16 +8,22 @@ 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,
|
||||||
const QString& objectName = "");
|
const QString& objectName = "");
|
||||||
|
static QByteArray itemValuesListToJson(const QList<QHash<int, QVariant>>& itemValuesList,
|
||||||
|
const QString& objectName = "");
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit JsonParser();
|
explicit 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
|
||||||
|
|||||||
@ -12,6 +12,7 @@
|
|||||||
#include "CoreConfig.h"
|
#include "CoreConfig.h"
|
||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
#include "data/filehandler.h"
|
#include "data/filehandler.h"
|
||||||
|
#include "model/metadata.h"
|
||||||
#include "model/tablemodel.h"
|
#include "model/tablemodel.h"
|
||||||
|
|
||||||
#include <QtGui/QUndoStack>
|
#include <QtGui/QUndoStack>
|
||||||
@ -98,7 +99,7 @@ void GenericCore::saveItems() {
|
|||||||
qDebug() << "saving items...";
|
qDebug() << "saving items...";
|
||||||
|
|
||||||
const QJsonDocument doc = m_mainModel->getAllItemsAsJsonDoc();
|
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) {
|
if (successfulSave) {
|
||||||
// QStringList completedTaskStrings = m_model->completedTasks();
|
// QStringList completedTaskStrings = m_model->completedTasks();
|
||||||
// appendCompletedTasksToFile(completedTaskStrings, "completed.txt");
|
// appendCompletedTasksToFile(completedTaskStrings, "completed.txt");
|
||||||
@ -122,7 +123,7 @@ void GenericCore::setupModels() {
|
|||||||
*/
|
*/
|
||||||
void GenericCore::initModelData() {
|
void GenericCore::initModelData() {
|
||||||
qInfo() << "Trying to read model data from file...";
|
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;
|
// qDebug() << "jsonDoc:" << jsonDoc;
|
||||||
// TODO decide on lack of file(s) (config, data) if example items should be generated
|
// TODO decide on lack of file(s) (config, data) if example items should be generated
|
||||||
// (see welcome wizard)
|
// (see welcome wizard)
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
#include "../metadata.h"
|
||||||
#include "../tablemodel.h"
|
#include "../tablemodel.h"
|
||||||
|
|
||||||
EditItemCommand::EditItemCommand(TableModel* model,
|
EditItemCommand::EditItemCommand(TableModel* model,
|
||||||
@ -19,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 TableModel::NameRole:
|
if (USER_FACING_ROLES.contains(role)) {
|
||||||
case TableModel::DescriptionRole:
|
|
||||||
case TableModel::InfoRole:
|
|
||||||
case TableModel::AmountRole:
|
|
||||||
case TableModel::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(TableModel::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(TableModel::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(TableModel::NameRole).toString());
|
commandText = QString("Edit item '%1'").arg(index.data(DEFAULT_ROLE).toString());
|
||||||
}
|
}
|
||||||
setText(commandText);
|
setText(commandText);
|
||||||
|
|
||||||
|
|||||||
@ -8,6 +8,9 @@ class TableModel;
|
|||||||
|
|
||||||
class EditItemCommand : public QUndoCommand {
|
class EditItemCommand : public QUndoCommand {
|
||||||
public:
|
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,
|
EditItemCommand(TableModel* model,
|
||||||
const QModelIndex& index,
|
const QModelIndex& index,
|
||||||
QMap<int, QVariant>& changedValues,
|
QMap<int, QVariant>& changedValues,
|
||||||
|
|||||||
@ -20,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[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);
|
|
||||||
|
|
||||||
m_valueList.append(values);
|
m_valueList.append(values);
|
||||||
}
|
}
|
||||||
|
|||||||
54
model/metadata.h
Normal file
54
model/metadata.h
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#ifndef METADATA_H
|
||||||
|
#define METADATA_H
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QHash>
|
||||||
|
#include <QList>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
/// model data
|
||||||
|
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"},
|
||||||
|
{DescriptionRole, "Description"},
|
||||||
|
{InfoRole, "Info"},
|
||||||
|
{AmountRole, "Amount"},
|
||||||
|
{FactorRole, "Factor"}};
|
||||||
|
static QList<UserRoles> STRING_ROLES = {NameRole, DescriptionRole, InfoRole};
|
||||||
|
static QList<UserRoles> INT_ROLES = {AmountRole};
|
||||||
|
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
|
||||||
|
|
||||||
|
/// JSON keys
|
||||||
|
static const QString ITEM_KEY_STRING = "items";
|
||||||
|
|
||||||
|
/// file naming
|
||||||
|
static const QString ITEM_FILE_NAME = ITEM_KEY_STRING + ".json";
|
||||||
|
|
||||||
|
/// functions
|
||||||
|
static int GET_ROLE_FOR_COLUMN(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 "modelitem.h"
|
||||||
|
|
||||||
#include "tablemodel.h"
|
#include "metadata.h"
|
||||||
|
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
@ -46,24 +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(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());
|
|
||||||
|
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,19 +4,13 @@
|
|||||||
#include "commands/edititemcommand.h"
|
#include "commands/edititemcommand.h"
|
||||||
#include "commands/insertrowscommand.h"
|
#include "commands/insertrowscommand.h"
|
||||||
#include "commands/removerowscommand.h"
|
#include "commands/removerowscommand.h"
|
||||||
|
#include "metadata.h"
|
||||||
#include "modelitem.h"
|
#include "modelitem.h"
|
||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#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() {
|
QByteArray TableModel::generateExampleItems() {
|
||||||
QJsonDocument doc = QJsonDocument();
|
QJsonDocument doc = QJsonDocument();
|
||||||
QJsonObject rootObject;
|
QJsonObject rootObject;
|
||||||
@ -26,18 +20,15 @@ QByteArray TableModel::generateExampleItems() {
|
|||||||
QJsonObject itemObject;
|
QJsonObject itemObject;
|
||||||
// itemObject.insert("uuid", m_uuid.toString());
|
// itemObject.insert("uuid", m_uuid.toString());
|
||||||
// itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate));
|
// itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate));
|
||||||
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::NameRole),
|
itemObject.insert(ROLE_NAMES.value(NameRole), QString("Item %1").arg(row));
|
||||||
QString("Item %1").arg(row));
|
itemObject.insert(ROLE_NAMES.value(DescriptionRole), QString("This is item %1").arg(row));
|
||||||
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::DescriptionRole),
|
itemObject.insert(ROLE_NAMES.value(InfoRole), QString("Info of item %1").arg(row));
|
||||||
QString("This is item %1").arg(row));
|
itemObject.insert(ROLE_NAMES.value(AmountRole), row);
|
||||||
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::InfoRole),
|
itemObject.insert(ROLE_NAMES.value(FactorRole), row * 1.1);
|
||||||
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);
|
|
||||||
|
|
||||||
array.append(itemObject);
|
array.append(itemObject);
|
||||||
}
|
}
|
||||||
rootObject.insert("items", array);
|
rootObject.insert(ITEM_KEY_STRING, array);
|
||||||
|
|
||||||
doc.setObject(rootObject);
|
doc.setObject(rootObject);
|
||||||
return doc.toJson();
|
return doc.toJson();
|
||||||
@ -74,7 +65,7 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
|
|||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
int roleForColumn = getRoleForColumn(column);
|
int roleForColumn = GET_ROLE_FOR_COLUMN(column);
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
@ -93,7 +84,7 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
|
|||||||
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
||||||
if (role == Qt::DisplayRole) {
|
if (role == Qt::DisplayRole) {
|
||||||
if (orientation == Qt::Horizontal) {
|
if (orientation == Qt::Horizontal) {
|
||||||
const int columnRole = getRoleForColumn(section);
|
const int columnRole = GET_ROLE_FOR_COLUMN(section);
|
||||||
const QString headerName = ROLE_NAMES.value(columnRole);
|
const QString headerName = ROLE_NAMES.value(columnRole);
|
||||||
return QString("%1").arg(headerName);
|
return QString("%1").arg(headerName);
|
||||||
} else {
|
} else {
|
||||||
@ -106,7 +97,7 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro
|
|||||||
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
||||||
if (role == Qt::EditRole && checkIndex(index)) {
|
if (role == Qt::EditRole && checkIndex(index)) {
|
||||||
const int column = index.column();
|
const int column = index.column();
|
||||||
const int roleForColumn = getRoleForColumn(column);
|
const int roleForColumn = GET_ROLE_FOR_COLUMN(column);
|
||||||
return setItemData(index, {{roleForColumn, value}});
|
return setItemData(index, {{roleForColumn, value}});
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -129,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;
|
||||||
@ -138,7 +140,7 @@ QJsonDocument TableModel::getAllItemsAsJsonDoc() const {
|
|||||||
QJsonObject itemObject = item->toJsonObject();
|
QJsonObject itemObject = item->toJsonObject();
|
||||||
array.append(itemObject);
|
array.append(itemObject);
|
||||||
}
|
}
|
||||||
rootObject.insert("items", array);
|
rootObject.insert(ITEM_KEY_STRING, array);
|
||||||
|
|
||||||
doc.setObject(rootObject);
|
doc.setObject(rootObject);
|
||||||
return doc;
|
return doc;
|
||||||
@ -176,7 +178,7 @@ void TableModel::insertItems(int startPosition,
|
|||||||
startPosition = m_items.size();
|
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);
|
InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, valueList);
|
||||||
m_undoStack->push(insertCommand);
|
m_undoStack->push(insertCommand);
|
||||||
@ -219,29 +221,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,
|
QMap<int, QVariant> TableModel::onlyChangedValues(const QModelIndex& index,
|
||||||
const QMap<int, QVariant>& roleValueMap) const {
|
const QMap<int, QVariant>& roleValueMap) const {
|
||||||
QMap<int, QVariant> result;
|
QMap<int, QVariant> result;
|
||||||
@ -268,9 +247,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 (intColumns.contains(roleName)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
} else if (DOUBLE_ROLES.contains(role)) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -16,9 +16,6 @@ class TableModel : public QAbstractTableModel {
|
|||||||
friend class EditItemCommand;
|
friend class EditItemCommand;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
|
||||||
static QHash<int, QByteArray> ROLE_NAMES;
|
|
||||||
static QList<QString> intColumns;
|
|
||||||
static QByteArray generateExampleItems();
|
static QByteArray generateExampleItems();
|
||||||
|
|
||||||
explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr);
|
explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr);
|
||||||
@ -35,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:
|
||||||
@ -59,7 +57,6 @@ class TableModel : public QAbstractTableModel {
|
|||||||
void execEditItemData(const int row, const QMap<int, QVariant>& changedValues);
|
void execEditItemData(const int row, const QMap<int, QVariant>& changedValues);
|
||||||
|
|
||||||
/// misc functions
|
/// misc functions
|
||||||
int getRoleForColumn(const int column) const;
|
|
||||||
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
|
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
|
||||||
const QMap<int, QVariant>& roleValueMap) const;
|
const QMap<int, QVariant>& roleValueMap) const;
|
||||||
bool isEmptyValueEqualToZero(const int role) const;
|
bool isEmptyValueEqualToZero(const int role) const;
|
||||||
|
|||||||
Reference in New Issue
Block a user