Merge branch 'feature/fileImportExportCsv' into develop
This commit is contained in:
@ -30,6 +30,9 @@ add_library(${TARGET_APP} STATIC
|
|||||||
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
|
model/metadata.h
|
||||||
|
formats/csvparser.h formats/csvparser.cpp
|
||||||
|
# 3rd party libraries
|
||||||
|
../3rdParty/rapidcsv/src/rapidcsv.h
|
||||||
)
|
)
|
||||||
|
|
||||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
|||||||
@ -5,6 +5,8 @@
|
|||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
|
||||||
|
#include "../formats/csvparser.h"
|
||||||
|
|
||||||
bool FileHandler::saveToFile(const QJsonDocument& doc, const QString& fileName) {
|
bool FileHandler::saveToFile(const QJsonDocument& doc, const QString& fileName) {
|
||||||
qDebug() << "saving file...";
|
qDebug() << "saving file...";
|
||||||
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
|
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
|
||||||
@ -29,25 +31,59 @@ bool FileHandler::saveToFile(const QJsonDocument& doc, const QString& fileName)
|
|||||||
}
|
}
|
||||||
|
|
||||||
QByteArray FileHandler::loadJSONDataFromFile(const QString fileName) {
|
QByteArray FileHandler::loadJSONDataFromFile(const QString fileName) {
|
||||||
QByteArray jsonData;
|
|
||||||
QFile file;
|
QFile file;
|
||||||
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
|
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
|
||||||
file.setFileName(path + "/" + fileName);
|
file.setFileName(path + "/" + fileName);
|
||||||
|
|
||||||
|
QPair<QString, QByteArray> fileContent = getFileContent(path + "/" + fileName);
|
||||||
|
|
||||||
|
return fileContent.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<ModelItemValues> FileHandler::getItemValuesFromCSVFile(const QString& filePath) {
|
||||||
|
QList<ModelItemValues> result;
|
||||||
|
QFile file;
|
||||||
|
file.setFileName(filePath);
|
||||||
|
if (file.exists()) {
|
||||||
|
result = CsvParser::getItemsFromCSVFile(filePath);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FileHandler::exportToCSVFile(const QList<QStringList>& rows, const QString& filePath) {
|
||||||
|
return CsvParser::exportToCSVFile(rows, filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
FileHandler::FileHandler() {}
|
||||||
|
|
||||||
|
/** Tries to open the file specified by the file path & returns the content
|
||||||
|
* @brief FileHandler::getFileContent
|
||||||
|
* @param filePath
|
||||||
|
* @return Returns an error string (empty if successful) and the file content
|
||||||
|
*/
|
||||||
|
QPair<QString, QByteArray> FileHandler::getFileContent(const QString& filePath) {
|
||||||
|
QString errorString = "";
|
||||||
|
|
||||||
|
QByteArray fileContent;
|
||||||
|
QFile file;
|
||||||
|
file.setFileName(filePath);
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
qDebug() << "File found, reading content...";
|
qDebug() << "File found, reading content...";
|
||||||
const bool successfulOpened = file.open(QIODevice::ReadOnly | QIODevice::Text);
|
const bool successfulOpened = file.open(QIODevice::ReadOnly | QIODevice::Text);
|
||||||
if (successfulOpened) {
|
if (successfulOpened) {
|
||||||
// TODO learn and decide on the differences between "readAll" and using
|
// TODO learn and decide on the differences between "readAll" and using
|
||||||
// streams
|
// streams
|
||||||
jsonData = file.readAll();
|
fileContent = file.readAll();
|
||||||
file.close();
|
file.close();
|
||||||
} else {
|
} else {
|
||||||
qWarning() << "File could not be opened!";
|
errorString = "File could not be opened!";
|
||||||
|
qWarning() << errorString;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qInfo() << "File not found. Returning empty result...";
|
errorString = "File not found. Returning empty result...";
|
||||||
|
qInfo() << errorString;
|
||||||
}
|
}
|
||||||
return jsonData;
|
|
||||||
}
|
|
||||||
|
|
||||||
FileHandler::FileHandler() {}
|
const QPair<QString, QByteArray> result(errorString, fileContent);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,17 +1,28 @@
|
|||||||
#ifndef FILEHANDLER_H
|
#ifndef FILEHANDLER_H
|
||||||
#define FILEHANDLER_H
|
#define FILEHANDLER_H
|
||||||
|
|
||||||
|
#include <QVariant>
|
||||||
|
|
||||||
|
typedef QHash<int, QVariant> ModelItemValues;
|
||||||
|
|
||||||
class QJsonDocument;
|
class QJsonDocument;
|
||||||
class QString;
|
class QString;
|
||||||
class QByteArray;
|
class QByteArray;
|
||||||
|
|
||||||
class FileHandler {
|
class FileHandler {
|
||||||
public:
|
public:
|
||||||
|
/// JSON
|
||||||
static bool saveToFile(const QJsonDocument& doc, const QString& fileName);
|
static bool saveToFile(const QJsonDocument& doc, const QString& fileName);
|
||||||
static QByteArray loadJSONDataFromFile(const QString fileName);
|
static QByteArray loadJSONDataFromFile(const QString fileName);
|
||||||
|
|
||||||
|
/// CSV
|
||||||
|
static QList<ModelItemValues> getItemValuesFromCSVFile(const QString& filePath);
|
||||||
|
static bool exportToCSVFile(const QList<QStringList>& rows, const QString& filePath);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit FileHandler();
|
explicit FileHandler();
|
||||||
|
|
||||||
|
static QPair<QString, QByteArray> getFileContent(const QString& filePath);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FILEHANDLER_H
|
#endif // FILEHANDLER_H
|
||||||
|
|||||||
158
formats/csvparser.cpp
Normal file
158
formats/csvparser.cpp
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
#include "csvparser.h"
|
||||||
|
|
||||||
|
#include <QHash>
|
||||||
|
#include <QLocale>
|
||||||
|
#include <QVariant>
|
||||||
|
|
||||||
|
#include "../../3rdParty/rapidcsv/src/rapidcsv.h"
|
||||||
|
#include "../model/metadata.h"
|
||||||
|
|
||||||
|
using namespace rapidcsv;
|
||||||
|
|
||||||
|
QList<ModelItemValues> CsvParser::getItemsFromCSVFile(const QString& fileName) {
|
||||||
|
Document doc(fileName.toStdString());
|
||||||
|
|
||||||
|
const bool isCompatible = isCsvCompatible(doc);
|
||||||
|
if (isCompatible) {
|
||||||
|
const QList<ModelItemValues> result = createListItemsFromCsvEntries(doc);
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
return QList<ModelItemValues>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CsvParser::exportToCSVFile(const QList<QStringList>& rows, const QString& filePath) {
|
||||||
|
Document doc(std::string(), LabelParams(0, -1));
|
||||||
|
const QList<QString> headerNames = GET_HEADER_NAMES();
|
||||||
|
for (int column = 0; column < headerNames.size(); ++column) {
|
||||||
|
doc.SetColumnName(column, headerNames.at(column).toStdString());
|
||||||
|
}
|
||||||
|
for (int row = 0; row < rows.size(); ++row) {
|
||||||
|
QStringList rowValues = rows.at(row);
|
||||||
|
std::vector<std::string> rowValueStrings;
|
||||||
|
for (int column = 0; column < rowValues.size(); ++column) {
|
||||||
|
rowValueStrings.push_back(rowValues.at(column).toStdString());
|
||||||
|
}
|
||||||
|
doc.InsertRow(row, rowValueStrings);
|
||||||
|
}
|
||||||
|
doc.Save(filePath.toStdString());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
CsvParser::CsvParser() {}
|
||||||
|
|
||||||
|
/** A CSV file is compatible if the following is true:
|
||||||
|
* - there is a CSV column for every column in the table model
|
||||||
|
* (except there are optional columns defined in the model)
|
||||||
|
* @brief CsvParser::isCsvCompatible
|
||||||
|
* @param doc
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
bool CsvParser::isCsvCompatible(const rapidcsv::Document& doc) {
|
||||||
|
qInfo() << "Checking CSV document for compatiblity...";
|
||||||
|
const std::vector<std::string> columnNames = doc.GetColumnNames();
|
||||||
|
for (const QString& headerName : GET_HEADER_NAMES()) {
|
||||||
|
bool isHeaderNameFound = false;
|
||||||
|
if (std::find(columnNames.begin(), columnNames.end(), headerName) != columnNames.end()) {
|
||||||
|
qDebug() << QString("Header found in column names: %1").arg(headerName);
|
||||||
|
} else {
|
||||||
|
const QString errorString =
|
||||||
|
QString("Couldn't find header name '%1' in CSV file. Aborting...").arg(headerName);
|
||||||
|
qWarning() << errorString;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<ModelItemValues> CsvParser::createListItemsFromCsvEntries(const rapidcsv::Document& doc) {
|
||||||
|
QList<ModelItemValues> result;
|
||||||
|
const int rowCount = doc.GetRowCount();
|
||||||
|
const QList<QString> headerNames = GET_HEADER_NAMES();
|
||||||
|
|
||||||
|
/// get the values for all columns
|
||||||
|
QHash<QString, std::vector<std::string>> columnValueMap = extractColumnValues(headerNames, doc);
|
||||||
|
|
||||||
|
/// get item values for each row
|
||||||
|
for (int row = 0; row < rowCount; ++row) {
|
||||||
|
const ModelItemValues itemValues = getItemValuesForRow(headerNames, columnValueMap, row);
|
||||||
|
result.append(itemValues);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
QHash<QString, std::vector<std::string>> CsvParser::extractColumnValues(
|
||||||
|
const QList<QString> headerNames,
|
||||||
|
const rapidcsv::Document& doc) {
|
||||||
|
QHash<QString, std::vector<std::string>> columnValueMap;
|
||||||
|
for (const QString& columnName : headerNames) {
|
||||||
|
// NEXT add support for optional columns
|
||||||
|
// if (optionalCsvHeaderNames.contains(columnName)) {
|
||||||
|
// const std::vector<std::string> columnNames = doc.GetColumnNames();
|
||||||
|
// int columnIdx = doc.GetColumnIdx(columnName.toStdString());
|
||||||
|
// if (columnIdx == -1) {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
const std::vector<std::string> columnValues =
|
||||||
|
doc.GetColumn<std::string>(columnName.toStdString());
|
||||||
|
columnValueMap.insert(columnName, columnValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
return columnValueMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelItemValues CsvParser::getItemValuesForRow(
|
||||||
|
const QList<QString>& headerNames,
|
||||||
|
const QHash<QString, std::vector<std::string>>& columnValueMap,
|
||||||
|
const int row) {
|
||||||
|
ModelItemValues result;
|
||||||
|
for (const QString& columnName : headerNames) {
|
||||||
|
if (!columnValueMap.contains(columnName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int role = ROLE_NAMES.key(columnName.toLatin1());
|
||||||
|
std::string valueString = columnValueMap.value(columnName).at(row);
|
||||||
|
|
||||||
|
QVariant value = parseItemValue(role, valueString);
|
||||||
|
if (value.isValid()) {
|
||||||
|
result[role] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant CsvParser::parseItemValue(const int role, const std::string& valueString) {
|
||||||
|
QVariant result;
|
||||||
|
if (STRING_ROLES.contains(role)) {
|
||||||
|
/// string values
|
||||||
|
result = QString::fromStdString(valueString);
|
||||||
|
} else if (INT_ROLES.contains(role)) {
|
||||||
|
/// int values
|
||||||
|
|
||||||
|
/// GetColumn<int> crashed (probably because of the empty values)
|
||||||
|
/// so the strings will be processed later
|
||||||
|
const QString intAsString = QString::fromStdString(valueString);
|
||||||
|
result = intAsString.toInt();
|
||||||
|
} else if (DOUBLE_ROLES.contains(role)) {
|
||||||
|
/// double values
|
||||||
|
const QString doubleAsString = QString::fromStdString(valueString);
|
||||||
|
double doubleValue;
|
||||||
|
if (doubleAsString.contains(',')) {
|
||||||
|
QLocale german(QLocale::German);
|
||||||
|
doubleValue = german.toDouble(doubleAsString);
|
||||||
|
} else {
|
||||||
|
doubleValue = doubleAsString.toDouble();
|
||||||
|
}
|
||||||
|
result = doubleValue;
|
||||||
|
|
||||||
|
// } else if (typeColumns.contains(columnName)) {
|
||||||
|
// // NEXT validate string is allowed
|
||||||
|
// values[role] = QString::fromStdString(columnValueMap.value(columnName).at(row));
|
||||||
|
} else {
|
||||||
|
/// no type recognized for column
|
||||||
|
QString errorString = QString("Couldn't find value type for role '%1'").arg(role);
|
||||||
|
qCritical() << errorString;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
32
formats/csvparser.h
Normal file
32
formats/csvparser.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef CSVPARSER_H
|
||||||
|
#define CSVPARSER_H
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
typedef QHash<int, QVariant> ModelItemValues;
|
||||||
|
|
||||||
|
namespace rapidcsv {
|
||||||
|
class Document;
|
||||||
|
}
|
||||||
|
|
||||||
|
class CsvParser {
|
||||||
|
public:
|
||||||
|
static QList<ModelItemValues> getItemsFromCSVFile(const QString& fileName);
|
||||||
|
static bool exportToCSVFile(const QList<QStringList>& rows, const QString& filePath);
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit CsvParser();
|
||||||
|
|
||||||
|
static bool isCsvCompatible(const rapidcsv::Document& doc);
|
||||||
|
static QList<ModelItemValues> createListItemsFromCsvEntries(const rapidcsv::Document& doc);
|
||||||
|
static QHash<QString, std::vector<std::string>> extractColumnValues(
|
||||||
|
const QList<QString> headerNames,
|
||||||
|
const rapidcsv::Document& doc);
|
||||||
|
static ModelItemValues getItemValuesForRow(
|
||||||
|
const QList<QString>& headerNames,
|
||||||
|
const QHash<QString, std::vector<std::string>>& columnValueMap,
|
||||||
|
const int row);
|
||||||
|
static QVariant parseItemValue(const int role, const std::string& valueString);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CSVPARSER_H
|
||||||
@ -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;
|
||||||
@ -17,13 +17,13 @@ QList<QHash<int, QVariant>> JsonParser::toItemValuesList(const QByteArray& jsonD
|
|||||||
|
|
||||||
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);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -76,12 +76,10 @@ bool GenericCore::isApplicationUpdateAvailable() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::triggerApplicationUpdate() {
|
void GenericCore::triggerApplicationUpdate(const bool saveChanges) {
|
||||||
// TODO include cleaness of undo stack
|
if (saveChanges && !m_modelUndoStack->isClean()) {
|
||||||
// if (!m_undoStack->isClean()) {
|
saveItems();
|
||||||
// saveItems();
|
}
|
||||||
// }
|
|
||||||
// QStringList args("update componentA componentB");
|
|
||||||
QStringList args("--start-updater");
|
QStringList args("--start-updater");
|
||||||
QString toolFilePath = getMaintenanceToolFilePath();
|
QString toolFilePath = getMaintenanceToolFilePath();
|
||||||
QProcess::startDetached(toolFilePath, args);
|
QProcess::startDetached(toolFilePath, args);
|
||||||
@ -101,8 +99,6 @@ void GenericCore::saveItems() {
|
|||||||
const QJsonDocument doc = m_mainModel->getAllItemsAsJsonDoc();
|
const QJsonDocument doc = m_mainModel->getAllItemsAsJsonDoc();
|
||||||
const bool successfulSave = FileHandler::saveToFile(doc, ITEM_FILE_NAME);
|
const bool successfulSave = FileHandler::saveToFile(doc, ITEM_FILE_NAME);
|
||||||
if (successfulSave) {
|
if (successfulSave) {
|
||||||
// QStringList completedTaskStrings = m_model->completedTasks();
|
|
||||||
// appendCompletedTasksToFile(completedTaskStrings, "completed.txt");
|
|
||||||
m_modelUndoStack->setClean();
|
m_modelUndoStack->setClean();
|
||||||
emit displayStatusMessage(QString("Items saved."));
|
emit displayStatusMessage(QString("Items saved."));
|
||||||
} else {
|
} else {
|
||||||
@ -110,6 +106,26 @@ void GenericCore::saveItems() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GenericCore::importCSVFile(const QString& filePath) {
|
||||||
|
qInfo() << "importing items from CSV...";
|
||||||
|
qDebug() << "filePath:" << filePath;
|
||||||
|
const QList<ModelItemValues> itemValuesList = FileHandler::getItemValuesFromCSVFile(filePath);
|
||||||
|
// NEXT inform UI on errors
|
||||||
|
if (itemValuesList.isEmpty()) {
|
||||||
|
qDebug() << "No items found. Doing nothing...";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// qDebug() << "CSV file content:" << itemValuesList;
|
||||||
|
m_mainModel->insertItems(m_mainModel->rowCount(), itemValuesList);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GenericCore::exportCSVFile(const QString& filePath) {
|
||||||
|
qInfo() << "exporting items to CSV...";
|
||||||
|
qDebug() << "filePath:" << filePath;
|
||||||
|
const QList<QStringList> itemsAsStringLists = m_mainModel->getItemsAsStringLists();
|
||||||
|
return FileHandler::exportToCSVFile(itemsAsStringLists, filePath);
|
||||||
|
}
|
||||||
|
|
||||||
void GenericCore::setupModels() {
|
void GenericCore::setupModels() {
|
||||||
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
|
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
|
||||||
// TODO add QAbstractItemModelTester
|
// TODO add QAbstractItemModelTester
|
||||||
|
|||||||
@ -20,12 +20,14 @@ class GenericCore : public QObject {
|
|||||||
void sayHello() const;
|
void sayHello() const;
|
||||||
|
|
||||||
bool isApplicationUpdateAvailable();
|
bool isApplicationUpdateAvailable();
|
||||||
void triggerApplicationUpdate();
|
void triggerApplicationUpdate(const bool saveChanges);
|
||||||
|
|
||||||
QUndoStack* getModelUndoStack() const;
|
QUndoStack* getModelUndoStack() const;
|
||||||
std::shared_ptr<TableModel> getModel() const;
|
std::shared_ptr<TableModel> getModel() const;
|
||||||
|
|
||||||
void saveItems();
|
void saveItems();
|
||||||
|
void importCSVFile(const QString& filePath);
|
||||||
|
bool exportCSVFile(const QString& filePath);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void displayStatusMessage(QString message);
|
void displayStatusMessage(QString message);
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
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)
|
||||||
|
|||||||
@ -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 {
|
||||||
@ -12,7 +14,7 @@ class InsertRowsCommand : public QUndoCommand {
|
|||||||
/// 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
|
||||||
@ -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
|
||||||
|
|||||||
@ -50,5 +50,13 @@ static int GET_ROLE_FOR_COLUMN(const int column) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
static QList<QString> GET_HEADER_NAMES() {
|
||||||
|
QList<QString> result;
|
||||||
|
for (const UserRoles& role : USER_FACING_ROLES) {
|
||||||
|
const QString headerName = ROLE_NAMES.value(role);
|
||||||
|
result.append(headerName);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // METADATA_H
|
#endif // METADATA_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()) {
|
||||||
@ -146,6 +146,19 @@ QJsonDocument TableModel::getAllItemsAsJsonDoc() const {
|
|||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QList<QStringList> TableModel::getItemsAsStringLists() const {
|
||||||
|
QList<QStringList> result;
|
||||||
|
foreach (shared_ptr<ModelItem> item, m_items) {
|
||||||
|
QStringList valueList;
|
||||||
|
for (int column = 0; column < columnCount(); ++column) {
|
||||||
|
QString value = item->data(GET_ROLE_FOR_COLUMN(column)).toString();
|
||||||
|
valueList.append(value);
|
||||||
|
}
|
||||||
|
result.append(valueList);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
|
bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
|
||||||
if (parentIndex != QModelIndex()) {
|
if (parentIndex != QModelIndex()) {
|
||||||
qWarning() << "Removing of child rows is not supported yet!";
|
qWarning() << "Removing of child rows is not supported yet!";
|
||||||
@ -169,22 +182,29 @@ 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<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonDoc, ITEM_KEY_STRING);
|
||||||
|
|
||||||
|
insertItems(startPosition, valueList, parentIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TableModel::insertItems(int startPosition,
|
||||||
|
const QList<ModelItemValues>& itemValuesList,
|
||||||
|
const QModelIndex& parentIndex) {
|
||||||
qInfo() << "Inserting item(s) into model...";
|
qInfo() << "Inserting item(s) into model...";
|
||||||
if (parentIndex != QModelIndex()) {
|
if (parentIndex != QModelIndex()) {
|
||||||
qWarning() << "Using invalid parent index (no child support for now)";
|
qWarning()
|
||||||
|
<< "Using invalid parent index (no child support for now)! Using root index as parent...";
|
||||||
}
|
}
|
||||||
if (startPosition == -1 || startPosition > m_items.size()) {
|
if (startPosition == -1 || startPosition > m_items.size()) {
|
||||||
/// Appending item(s)
|
/// Appending item(s)
|
||||||
startPosition = m_items.size();
|
startPosition = m_items.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QHash<int, QVariant>> valueList = JsonParser::toItemValuesList(jsonDoc, ITEM_KEY_STRING);
|
InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, itemValuesList);
|
||||||
|
|
||||||
InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, valueList);
|
|
||||||
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,8 +34,9 @@ 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;
|
||||||
|
QList<QStringList> getItemsAsStringLists() const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
||||||
@ -43,6 +46,9 @@ class TableModel : public QAbstractTableModel {
|
|||||||
void insertItems(int startPosition,
|
void insertItems(int startPosition,
|
||||||
const QByteArray& jsonDoc,
|
const QByteArray& jsonDoc,
|
||||||
const QModelIndex& parentIndex = QModelIndex());
|
const QModelIndex& parentIndex = QModelIndex());
|
||||||
|
void insertItems(int startPosition,
|
||||||
|
const QList<ModelItemValues>& itemValuesList,
|
||||||
|
const QModelIndex& parentIndex = QModelIndex());
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// *** members ***
|
/// *** members ***
|
||||||
@ -52,7 +58,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