From 99ed398c2fecbd016dacedd99b33deb398937ee7 Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Tue, 6 Jan 2026 10:04:26 +0100 Subject: [PATCH] Simple implementation of CSV export. --- data/filehandler.cpp | 4 ++++ data/filehandler.h | 1 + formats/csvparser.cpp | 18 ++++++++++++++++++ formats/csvparser.h | 1 + genericcore.cpp | 7 +++++++ genericcore.h | 1 + model/tablemodel.cpp | 13 +++++++++++++ model/tablemodel.h | 1 + 8 files changed, 46 insertions(+) diff --git a/data/filehandler.cpp b/data/filehandler.cpp index dc99a9b..c405e55 100644 --- a/data/filehandler.cpp +++ b/data/filehandler.cpp @@ -50,6 +50,10 @@ QList FileHandler::getItemValuesFromCSVFile(const QString& file return result; } +bool FileHandler::exportToCSVFile(const QList& rows, const QString& filePath) { + return CsvParser::exportToCSVFile(rows, filePath); +} + FileHandler::FileHandler() {} /** Tries to open the file specified by the file path & returns the content diff --git a/data/filehandler.h b/data/filehandler.h index 32ba140..6fd6019 100644 --- a/data/filehandler.h +++ b/data/filehandler.h @@ -17,6 +17,7 @@ class FileHandler { /// CSV static QList getItemValuesFromCSVFile(const QString& filePath); + static bool exportToCSVFile(const QList& rows, const QString& filePath); private: explicit FileHandler(); diff --git a/formats/csvparser.cpp b/formats/csvparser.cpp index 1b6c943..365de1d 100644 --- a/formats/csvparser.cpp +++ b/formats/csvparser.cpp @@ -21,6 +21,24 @@ QList CsvParser::getItemsFromCSVFile(const QString& fileName) { } } +bool CsvParser::exportToCSVFile(const QList& rows, const QString& filePath) { + Document doc(std::string(), LabelParams(0, -1)); + const QList 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 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: diff --git a/formats/csvparser.h b/formats/csvparser.h index 71986c9..7695156 100644 --- a/formats/csvparser.h +++ b/formats/csvparser.h @@ -12,6 +12,7 @@ class Document; class CsvParser { public: static QList getItemsFromCSVFile(const QString& fileName); + static bool exportToCSVFile(const QList& rows, const QString& filePath); private: explicit CsvParser(); diff --git a/genericcore.cpp b/genericcore.cpp index b75a225..96f07a3 100644 --- a/genericcore.cpp +++ b/genericcore.cpp @@ -121,6 +121,13 @@ void GenericCore::importCSVFile(const QString& filePath) { m_mainModel->insertItems(m_mainModel->rowCount(), itemValuesList); } +bool GenericCore::exportCSVFile(const QString& filePath) { + qInfo() << "exporting items to CSV..."; + qDebug() << "filePath:" << filePath; + const QList itemsAsStringLists = m_mainModel->getItemsAsStringLists(); + return FileHandler::exportToCSVFile(itemsAsStringLists, filePath); +} + void GenericCore::setupModels() { m_mainModel = make_shared(m_modelUndoStack, this); // TODO add QAbstractItemModelTester diff --git a/genericcore.h b/genericcore.h index b4a9776..e3c1e5f 100644 --- a/genericcore.h +++ b/genericcore.h @@ -27,6 +27,7 @@ class GenericCore : public QObject { void saveItems(); void importCSVFile(const QString& filePath); + bool exportCSVFile(const QString& filePath); signals: void displayStatusMessage(QString message); diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index 09011c8..4895e9a 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -146,6 +146,19 @@ QJsonDocument TableModel::getAllItemsAsJsonDoc() const { return doc; } +QList TableModel::getItemsAsStringLists() const { + QList result; + foreach (shared_ptr 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) { if (parentIndex != QModelIndex()) { qWarning() << "Removing of child rows is not supported yet!"; diff --git a/model/tablemodel.h b/model/tablemodel.h index 0627d13..4bd2dcd 100644 --- a/model/tablemodel.h +++ b/model/tablemodel.h @@ -36,6 +36,7 @@ class TableModel : public QAbstractTableModel { ModelItemValues getItemValues(const QModelIndex& index) const; QJsonDocument getAllItemsAsJsonDoc() const; + QList getItemsAsStringLists() const; public slots: // bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())