Using the typedef ModelItemValues for "QHash<int, QVariant>".
This commit is contained in:
@ -40,8 +40,8 @@ QByteArray FileHandler::loadJSONDataFromFile(const QString fileName) {
|
|||||||
return fileContent.second;
|
return fileContent.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QHash<int, QVariant>> FileHandler::getItemValuesFromCSVFile(const QString& filePath) {
|
QList<ModelItemValues> FileHandler::getItemValuesFromCSVFile(const QString& filePath) {
|
||||||
QList<QHash<int, QVariant>> result;
|
QList<ModelItemValues> result;
|
||||||
QFile file;
|
QFile file;
|
||||||
file.setFileName(filePath);
|
file.setFileName(filePath);
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
|
|||||||
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
|
|
||||||
|
typedef QHash<int, QVariant> ModelItemValues;
|
||||||
|
|
||||||
class QJsonDocument;
|
class QJsonDocument;
|
||||||
class QString;
|
class QString;
|
||||||
class QByteArray;
|
class QByteArray;
|
||||||
@ -14,7 +16,7 @@ class FileHandler {
|
|||||||
static QByteArray loadJSONDataFromFile(const QString fileName);
|
static QByteArray loadJSONDataFromFile(const QString fileName);
|
||||||
|
|
||||||
/// CSV
|
/// CSV
|
||||||
static QList<QHash<int, QVariant>> getItemValuesFromCSVFile(const QString& filePath);
|
static QList<ModelItemValues> getItemValuesFromCSVFile(const QString& filePath);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit FileHandler();
|
explicit FileHandler();
|
||||||
|
|||||||
@ -9,15 +9,15 @@
|
|||||||
|
|
||||||
using namespace rapidcsv;
|
using namespace rapidcsv;
|
||||||
|
|
||||||
QList<QHash<int, QVariant>> CsvParser::getItemsFromCSVFile(const QString& fileName) {
|
QList<ModelItemValues> CsvParser::getItemsFromCSVFile(const QString& fileName) {
|
||||||
Document doc(fileName.toStdString());
|
Document doc(fileName.toStdString());
|
||||||
|
|
||||||
const bool isCompatible = isCsvCompatible(doc);
|
const bool isCompatible = isCsvCompatible(doc);
|
||||||
if (isCompatible) {
|
if (isCompatible) {
|
||||||
const QList<QHash<int, QVariant>> result = createListItemsFromCsvEntries(doc);
|
const QList<ModelItemValues> result = createListItemsFromCsvEntries(doc);
|
||||||
return result;
|
return result;
|
||||||
} else {
|
} else {
|
||||||
return QList<QHash<int, QVariant>>();
|
return QList<ModelItemValues>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,9 +47,8 @@ bool CsvParser::isCsvCompatible(const rapidcsv::Document& doc) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QHash<int, QVariant>> CsvParser::createListItemsFromCsvEntries(
|
QList<ModelItemValues> CsvParser::createListItemsFromCsvEntries(const rapidcsv::Document& doc) {
|
||||||
const rapidcsv::Document& doc) {
|
QList<ModelItemValues> result;
|
||||||
QList<QHash<int, QVariant>> result;
|
|
||||||
const int rowCount = doc.GetRowCount();
|
const int rowCount = doc.GetRowCount();
|
||||||
const QList<QString> headerNames = GET_HEADER_NAMES();
|
const QList<QString> headerNames = GET_HEADER_NAMES();
|
||||||
|
|
||||||
@ -58,7 +57,7 @@ QList<QHash<int, QVariant>> CsvParser::createListItemsFromCsvEntries(
|
|||||||
|
|
||||||
/// get item values for each row
|
/// get item values for each row
|
||||||
for (int row = 0; row < rowCount; ++row) {
|
for (int row = 0; row < rowCount; ++row) {
|
||||||
const QHash<int, QVariant> itemValues = getItemValuesForRow(headerNames, columnValueMap, row);
|
const ModelItemValues itemValues = getItemValuesForRow(headerNames, columnValueMap, row);
|
||||||
result.append(itemValues);
|
result.append(itemValues);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@ -85,11 +84,11 @@ QHash<QString, std::vector<std::string>> CsvParser::extractColumnValues(
|
|||||||
return columnValueMap;
|
return columnValueMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<int, QVariant> CsvParser::getItemValuesForRow(
|
ModelItemValues CsvParser::getItemValuesForRow(
|
||||||
const QList<QString>& headerNames,
|
const QList<QString>& headerNames,
|
||||||
const QHash<QString, std::vector<std::string>>& columnValueMap,
|
const QHash<QString, std::vector<std::string>>& columnValueMap,
|
||||||
const int row) {
|
const int row) {
|
||||||
QHash<int, QVariant> result;
|
ModelItemValues result;
|
||||||
for (const QString& columnName : headerNames) {
|
for (const QString& columnName : headerNames) {
|
||||||
if (!columnValueMap.contains(columnName)) {
|
if (!columnValueMap.contains(columnName)) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@ -3,23 +3,25 @@
|
|||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
|
typedef QHash<int, QVariant> ModelItemValues;
|
||||||
|
|
||||||
namespace rapidcsv {
|
namespace rapidcsv {
|
||||||
class Document;
|
class Document;
|
||||||
}
|
}
|
||||||
|
|
||||||
class CsvParser {
|
class CsvParser {
|
||||||
public:
|
public:
|
||||||
static QList<QHash<int, QVariant>> getItemsFromCSVFile(const QString& fileName);
|
static QList<ModelItemValues> getItemsFromCSVFile(const QString& fileName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit CsvParser();
|
explicit CsvParser();
|
||||||
|
|
||||||
static bool isCsvCompatible(const rapidcsv::Document& doc);
|
static bool isCsvCompatible(const rapidcsv::Document& doc);
|
||||||
static QList<QHash<int, QVariant>> createListItemsFromCsvEntries(const rapidcsv::Document& doc);
|
static QList<ModelItemValues> createListItemsFromCsvEntries(const rapidcsv::Document& doc);
|
||||||
static QHash<QString, std::vector<std::string>> extractColumnValues(
|
static QHash<QString, std::vector<std::string>> extractColumnValues(
|
||||||
const QList<QString> headerNames,
|
const QList<QString> headerNames,
|
||||||
const rapidcsv::Document& doc);
|
const rapidcsv::Document& doc);
|
||||||
static QHash<int, QVariant> getItemValuesForRow(
|
static ModelItemValues getItemValuesForRow(
|
||||||
const QList<QString>& headerNames,
|
const QList<QString>& headerNames,
|
||||||
const QHash<QString, std::vector<std::string>>& columnValueMap,
|
const QHash<QString, std::vector<std::string>>& columnValueMap,
|
||||||
const int row);
|
const int row);
|
||||||
|
|||||||
@ -5,9 +5,9 @@
|
|||||||
|
|
||||||
#include "../model/metadata.h"
|
#include "../model/metadata.h"
|
||||||
|
|
||||||
QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonData,
|
QList<ModelItemValues> JsonParser::toItemValuesList(const QByteArray& jsonData,
|
||||||
const QString& objectName) {
|
const QString& objectName) {
|
||||||
QList<QHash<int, QVariant>> result;
|
QList<ModelItemValues> result;
|
||||||
|
|
||||||
if (jsonData.isEmpty()) {
|
if (jsonData.isEmpty()) {
|
||||||
return result;
|
return result;
|
||||||
@ -16,14 +16,14 @@ QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonD
|
|||||||
QJsonArray itemArray = extractItemArray(jsonData, objectName);
|
QJsonArray itemArray = extractItemArray(jsonData, objectName);
|
||||||
|
|
||||||
foreach (QJsonValue value, itemArray) {
|
foreach (QJsonValue value, itemArray) {
|
||||||
QJsonObject itemJsonObject = value.toObject();
|
QJsonObject itemJsonObject = value.toObject();
|
||||||
QHash<int, QVariant> values = jsonObjectToItemValues(itemJsonObject);
|
ModelItemValues values = jsonObjectToItemValues(itemJsonObject);
|
||||||
result.append(values);
|
result.append(values);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray JsonParser::itemValuesListToJson(const QList<QHash<int, QVariant>>& itemValuesList,
|
QByteArray JsonParser::itemValuesListToJson(const QList<ModelItemValues>& itemValuesList,
|
||||||
const QString& objectName) {
|
const QString& objectName) {
|
||||||
QJsonDocument jsonDoc;
|
QJsonDocument jsonDoc;
|
||||||
QJsonObject rootObject;
|
QJsonObject rootObject;
|
||||||
@ -72,8 +72,8 @@ QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QStrin
|
|||||||
return itemArray;
|
return itemArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<int, QVariant> JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
||||||
QHash<int, QVariant> values;
|
ModelItemValues values;
|
||||||
|
|
||||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
|
|||||||
@ -8,20 +8,22 @@ class QString;
|
|||||||
class QByteArray;
|
class QByteArray;
|
||||||
class QJsonArray;
|
class QJsonArray;
|
||||||
|
|
||||||
|
typedef QHash<int, QVariant> ModelItemValues;
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class JsonParser {
|
class JsonParser {
|
||||||
public:
|
public:
|
||||||
static QList<QHash<int, QVariant>> toItemValuesList(const QByteArray& jsonData,
|
static QList<ModelItemValues> toItemValuesList(const QByteArray& jsonData,
|
||||||
const QString& objectName = "");
|
const QString& objectName = "");
|
||||||
static QByteArray itemValuesListToJson(const QList<QHash<int, QVariant>>& itemValuesList,
|
static QByteArray itemValuesListToJson(const QList<ModelItemValues>& itemValuesList,
|
||||||
const QString& objectName = "");
|
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 ModelItemValues jsonObjectToItemValues(const QJsonObject& itemJsonObject);
|
||||||
|
|
||||||
static pair<int, QVariant> getKeyValuePair(const QJsonObject& itemJsonObject, const int role);
|
static pair<int, QVariant> getKeyValuePair(const QJsonObject& itemJsonObject, const int role);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -111,8 +111,7 @@ void GenericCore::saveItems() {
|
|||||||
void GenericCore::importCSVFile(const QString& filePath) {
|
void GenericCore::importCSVFile(const QString& filePath) {
|
||||||
qInfo() << "importing items from CSV...";
|
qInfo() << "importing items from CSV...";
|
||||||
qDebug() << "filePath:" << filePath;
|
qDebug() << "filePath:" << filePath;
|
||||||
const QList<QHash<int, QVariant>> itemValuesList =
|
const QList<ModelItemValues> itemValuesList = FileHandler::getItemValuesFromCSVFile(filePath);
|
||||||
FileHandler::getItemValuesFromCSVFile(filePath);
|
|
||||||
// NEXT inform UI on errors
|
// NEXT inform UI on errors
|
||||||
if (itemValuesList.isEmpty()) {
|
if (itemValuesList.isEmpty()) {
|
||||||
qDebug() << "No items found. Doing nothing...";
|
qDebug() << "No items found. Doing nothing...";
|
||||||
|
|||||||
@ -5,9 +5,9 @@
|
|||||||
#include "../tablemodel.h"
|
#include "../tablemodel.h"
|
||||||
|
|
||||||
InsertRowsCommand::InsertRowsCommand(TableModel* model,
|
InsertRowsCommand::InsertRowsCommand(TableModel* model,
|
||||||
int startRow,
|
int startRow,
|
||||||
QList<QHash<int, QVariant> > valueList,
|
QList<ModelItemValues> valueList,
|
||||||
QUndoCommand* parent)
|
QUndoCommand* parent)
|
||||||
: QUndoCommand(parent)
|
: QUndoCommand(parent)
|
||||||
, m_tableModel(model)
|
, m_tableModel(model)
|
||||||
, m_startRow(startRow)
|
, m_startRow(startRow)
|
||||||
|
|||||||
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <QUndoCommand>
|
#include <QUndoCommand>
|
||||||
|
|
||||||
|
typedef QHash<int, QVariant> ModelItemValues;
|
||||||
|
|
||||||
class TableModel;
|
class TableModel;
|
||||||
|
|
||||||
class InsertRowsCommand : public QUndoCommand {
|
class InsertRowsCommand : public QUndoCommand {
|
||||||
@ -11,9 +13,9 @@ class InsertRowsCommand : public QUndoCommand {
|
|||||||
/// Using simple pointer to model because there was a crash when closing the application with an
|
/// Using simple pointer to model because there was a crash when closing the application with an
|
||||||
/// unclean undo stack
|
/// unclean undo stack
|
||||||
InsertRowsCommand(TableModel* model,
|
InsertRowsCommand(TableModel* model,
|
||||||
int startRow,
|
int startRow,
|
||||||
QList<QHash<int, QVariant> > valueList,
|
QList<ModelItemValues> valueList,
|
||||||
QUndoCommand* parent = nullptr);
|
QUndoCommand* parent = nullptr);
|
||||||
|
|
||||||
/// QUndoCommand interface
|
/// QUndoCommand interface
|
||||||
void undo() override;
|
void undo() override;
|
||||||
@ -22,7 +24,7 @@ class InsertRowsCommand : public QUndoCommand {
|
|||||||
private:
|
private:
|
||||||
TableModel* m_tableModel;
|
TableModel* m_tableModel;
|
||||||
const int m_startRow;
|
const int m_startRow;
|
||||||
const QList<QHash<int, QVariant> > m_valueList;
|
const QList<ModelItemValues> m_valueList;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // INSERTROWSCOMMAND_H
|
#endif // INSERTROWSCOMMAND_H
|
||||||
|
|||||||
@ -20,7 +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);
|
||||||
|
|
||||||
QHash<int, QVariant> values = m_tableModel->getItemValues(index);
|
ModelItemValues values = m_tableModel->getItemValues(index);
|
||||||
|
|
||||||
m_valueList.append(values);
|
m_valueList.append(values);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
class TableModel;
|
class TableModel;
|
||||||
|
|
||||||
|
typedef QHash<int, QVariant> ModelItemValues;
|
||||||
|
|
||||||
class RemoveRowsCommand : public QUndoCommand {
|
class RemoveRowsCommand : public QUndoCommand {
|
||||||
public:
|
public:
|
||||||
// TODO don't use simple pointer to model
|
// TODO don't use simple pointer to model
|
||||||
@ -22,7 +24,7 @@ class RemoveRowsCommand : public QUndoCommand {
|
|||||||
private:
|
private:
|
||||||
TableModel* m_tableModel;
|
TableModel* m_tableModel;
|
||||||
const int m_startRow;
|
const int m_startRow;
|
||||||
QList<QHash<int, QVariant>> m_valueList;
|
QList<ModelItemValues> m_valueList;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // REMOVEROWSCOMMAND_H
|
#endif // REMOVEROWSCOMMAND_H
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
|
|
||||||
ModelItem::ModelItem(const QHash<int, QVariant> values)
|
ModelItem::ModelItem(const ModelItemValues values)
|
||||||
: m_values(values) {}
|
: m_values(values) {}
|
||||||
|
|
||||||
QVariant ModelItem::data(int role) const { return m_values.value(role); }
|
QVariant ModelItem::data(int role) const { return m_values.value(role); }
|
||||||
|
|||||||
@ -3,9 +3,11 @@
|
|||||||
|
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
|
|
||||||
|
typedef QHash<int, QVariant> ModelItemValues;
|
||||||
|
|
||||||
class ModelItem {
|
class ModelItem {
|
||||||
public:
|
public:
|
||||||
ModelItem(const QHash<int, QVariant> values);
|
ModelItem(const ModelItemValues values);
|
||||||
|
|
||||||
QVariant data(int role) const;
|
QVariant data(int role) const;
|
||||||
bool setData(const QVariant& value, int role);
|
bool setData(const QVariant& value, int role);
|
||||||
|
|||||||
@ -120,8 +120,8 @@ bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<int, QVariant> TableModel::getItemValues(const QModelIndex& index) const {
|
ModelItemValues TableModel::getItemValues(const QModelIndex& index) const {
|
||||||
QHash<int, QVariant> values;
|
ModelItemValues values;
|
||||||
|
|
||||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
@ -169,14 +169,13 @@ void TableModel::appendItems(const QByteArray& jsonDoc) { insertItems(-1, jsonDo
|
|||||||
void TableModel::insertItems(int startPosition,
|
void TableModel::insertItems(int startPosition,
|
||||||
const QByteArray& jsonDoc,
|
const QByteArray& jsonDoc,
|
||||||
const QModelIndex& parentIndex) {
|
const QModelIndex& parentIndex) {
|
||||||
const QList<QHash<int, QVariant>> valueList =
|
const QList<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonDoc, ITEM_KEY_STRING);
|
||||||
JsonParser::toItemValuesList(jsonDoc, ITEM_KEY_STRING);
|
|
||||||
|
|
||||||
insertItems(startPosition, valueList, parentIndex);
|
insertItems(startPosition, valueList, parentIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TableModel::insertItems(int startPosition,
|
void TableModel::insertItems(int startPosition,
|
||||||
const QList<QHash<int, QVariant>>& itemValuesList,
|
const QList<ModelItemValues>& itemValuesList,
|
||||||
const QModelIndex& parentIndex) {
|
const QModelIndex& parentIndex) {
|
||||||
qInfo() << "Inserting item(s) into model...";
|
qInfo() << "Inserting item(s) into model...";
|
||||||
if (parentIndex != QModelIndex()) {
|
if (parentIndex != QModelIndex()) {
|
||||||
@ -192,7 +191,7 @@ void TableModel::insertItems(int startPosition,
|
|||||||
m_undoStack->push(insertCommand);
|
m_undoStack->push(insertCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TableModel::execInsertItems(const int firstRow, const QList<QHash<int, QVariant>> valueList) {
|
void TableModel::execInsertItems(const int firstRow, const QList<ModelItemValues> valueList) {
|
||||||
const int nRows = valueList.size();
|
const int nRows = valueList.size();
|
||||||
qDebug() << "Inserting" << nRows << "items...";
|
qDebug() << "Inserting" << nRows << "items...";
|
||||||
|
|
||||||
|
|||||||
@ -8,6 +8,8 @@ class ModelItem;
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
typedef QHash<int, QVariant> ModelItemValues;
|
||||||
|
|
||||||
class TableModel : public QAbstractTableModel {
|
class TableModel : public QAbstractTableModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@ -32,7 +34,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;
|
ModelItemValues getItemValues(const QModelIndex& index) const;
|
||||||
QJsonDocument getAllItemsAsJsonDoc() const;
|
QJsonDocument getAllItemsAsJsonDoc() const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
@ -44,7 +46,7 @@ class TableModel : public QAbstractTableModel {
|
|||||||
const QByteArray& jsonDoc,
|
const QByteArray& jsonDoc,
|
||||||
const QModelIndex& parentIndex = QModelIndex());
|
const QModelIndex& parentIndex = QModelIndex());
|
||||||
void insertItems(int startPosition,
|
void insertItems(int startPosition,
|
||||||
const QList<QHash<int, QVariant>>& itemValuesList,
|
const QList<ModelItemValues>& itemValuesList,
|
||||||
const QModelIndex& parentIndex = QModelIndex());
|
const QModelIndex& parentIndex = QModelIndex());
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -55,7 +57,7 @@ class TableModel : public QAbstractTableModel {
|
|||||||
|
|
||||||
/// *** functions ***
|
/// *** functions ***
|
||||||
/// undo/redo functions
|
/// undo/redo functions
|
||||||
void execInsertItems(const int firstRow, const QList<QHash<int, QVariant>> valueList);
|
void execInsertItems(const int firstRow, const QList<ModelItemValues> valueList);
|
||||||
void execRemoveItems(const int firstRow, const int nRows);
|
void execRemoveItems(const int firstRow, const int nRows);
|
||||||
void execEditItemData(const int row, const QMap<int, QVariant>& changedValues);
|
void execEditItemData(const int row, const QMap<int, QVariant>& changedValues);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user