Simple implementation of CSV export.
This commit is contained in:
@ -50,6 +50,10 @@ QList<ModelItemValues> FileHandler::getItemValuesFromCSVFile(const QString& file
|
||||
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
|
||||
|
||||
@ -17,6 +17,7 @@ class FileHandler {
|
||||
|
||||
/// CSV
|
||||
static QList<ModelItemValues> getItemValuesFromCSVFile(const QString& filePath);
|
||||
static bool exportToCSVFile(const QList<QStringList>& rows, const QString& filePath);
|
||||
|
||||
private:
|
||||
explicit FileHandler();
|
||||
|
||||
@ -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() {}
|
||||
|
||||
/** A CSV file is compatible if the following is true:
|
||||
|
||||
@ -12,6 +12,7 @@ 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();
|
||||
|
||||
@ -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<QStringList> itemsAsStringLists = m_mainModel->getItemsAsStringLists();
|
||||
return FileHandler::exportToCSVFile(itemsAsStringLists, filePath);
|
||||
}
|
||||
|
||||
void GenericCore::setupModels() {
|
||||
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
|
||||
// TODO add QAbstractItemModelTester
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -146,6 +146,19 @@ QJsonDocument TableModel::getAllItemsAsJsonDoc() const {
|
||||
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) {
|
||||
if (parentIndex != QModelIndex()) {
|
||||
qWarning() << "Removing of child rows is not supported yet!";
|
||||
|
||||
@ -36,6 +36,7 @@ class TableModel : public QAbstractTableModel {
|
||||
|
||||
ModelItemValues getItemValues(const QModelIndex& index) const;
|
||||
QJsonDocument getAllItemsAsJsonDoc() const;
|
||||
QList<QStringList> getItemsAsStringLists() const;
|
||||
|
||||
public slots:
|
||||
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
||||
|
||||
Reference in New Issue
Block a user