Simple implementation of CSV export.

This commit is contained in:
2026-01-06 10:04:26 +01:00
parent 3e6273cb7d
commit 99ed398c2f
8 changed files with 46 additions and 0 deletions

View File

@ -50,6 +50,10 @@ QList<ModelItemValues> FileHandler::getItemValuesFromCSVFile(const QString& file
return result; return result;
} }
bool FileHandler::exportToCSVFile(const QList<QStringList>& rows, const QString& filePath) {
return CsvParser::exportToCSVFile(rows, filePath);
}
FileHandler::FileHandler() {} FileHandler::FileHandler() {}
/** Tries to open the file specified by the file path & returns the content /** Tries to open the file specified by the file path & returns the content

View File

@ -17,6 +17,7 @@ class FileHandler {
/// CSV /// CSV
static QList<ModelItemValues> getItemValuesFromCSVFile(const QString& filePath); static QList<ModelItemValues> getItemValuesFromCSVFile(const QString& filePath);
static bool exportToCSVFile(const QList<QStringList>& rows, const QString& filePath);
private: private:
explicit FileHandler(); explicit FileHandler();

View File

@ -21,6 +21,24 @@ QList<ModelItemValues> CsvParser::getItemsFromCSVFile(const QString& fileName) {
} }
} }
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() {} CsvParser::CsvParser() {}
/** A CSV file is compatible if the following is true: /** A CSV file is compatible if the following is true:

View File

@ -12,6 +12,7 @@ class Document;
class CsvParser { class CsvParser {
public: public:
static QList<ModelItemValues> getItemsFromCSVFile(const QString& fileName); static QList<ModelItemValues> getItemsFromCSVFile(const QString& fileName);
static bool exportToCSVFile(const QList<QStringList>& rows, const QString& filePath);
private: private:
explicit CsvParser(); explicit CsvParser();

View File

@ -121,6 +121,13 @@ void GenericCore::importCSVFile(const QString& filePath) {
m_mainModel->insertItems(m_mainModel->rowCount(), 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

View File

@ -27,6 +27,7 @@ class GenericCore : public QObject {
void saveItems(); void saveItems();
void importCSVFile(const QString& filePath); void importCSVFile(const QString& filePath);
bool exportCSVFile(const QString& filePath);
signals: signals:
void displayStatusMessage(QString message); void displayStatusMessage(QString message);

View File

@ -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!";

View File

@ -36,6 +36,7 @@ class TableModel : public QAbstractTableModel {
ModelItemValues 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())