Simple implementation of CSV file import. Successfully imported items are appended to the model.
This commit is contained in:
141
formats/csvparser.cpp
Normal file
141
formats/csvparser.cpp
Normal file
@ -0,0 +1,141 @@
|
||||
#include "csvparser.h"
|
||||
|
||||
#include <QHash>
|
||||
#include <QLocale>
|
||||
#include <QVariant>
|
||||
|
||||
#include "../../3rdParty/rapidcsv/src/rapidcsv.h"
|
||||
#include "../model/metadata.h"
|
||||
|
||||
using namespace rapidcsv;
|
||||
|
||||
QList<QHash<int, QVariant>> CsvParser::getItemsFromCSVFile(const QString& fileName) {
|
||||
Document doc(fileName.toStdString());
|
||||
|
||||
const bool isCompatible = isCsvCompatible(doc);
|
||||
if (isCompatible) {
|
||||
const QList<QHash<int, QVariant>> result = createListItemsFromCsvEntries(doc);
|
||||
return result;
|
||||
} else {
|
||||
return QList<QHash<int, QVariant>>();
|
||||
}
|
||||
}
|
||||
|
||||
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<QHash<int, QVariant>> CsvParser::createListItemsFromCsvEntries(
|
||||
const rapidcsv::Document& doc) {
|
||||
QList<QHash<int, QVariant>> 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 QHash<int, QVariant> 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;
|
||||
}
|
||||
|
||||
QHash<int, QVariant> CsvParser::getItemValuesForRow(
|
||||
const QList<QString>& headerNames,
|
||||
const QHash<QString, std::vector<std::string>>& columnValueMap,
|
||||
const int row) {
|
||||
QHash<int, QVariant> 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;
|
||||
}
|
||||
29
formats/csvparser.h
Normal file
29
formats/csvparser.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef CSVPARSER_H
|
||||
#define CSVPARSER_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace rapidcsv {
|
||||
class Document;
|
||||
}
|
||||
|
||||
class CsvParser {
|
||||
public:
|
||||
static QList<QHash<int, QVariant>> getItemsFromCSVFile(const QString& fileName);
|
||||
|
||||
private:
|
||||
explicit CsvParser();
|
||||
|
||||
static bool isCsvCompatible(const rapidcsv::Document& doc);
|
||||
static QList<QHash<int, QVariant>> createListItemsFromCsvEntries(const rapidcsv::Document& doc);
|
||||
static QHash<QString, std::vector<std::string>> extractColumnValues(
|
||||
const QList<QString> headerNames,
|
||||
const rapidcsv::Document& doc);
|
||||
static QHash<int, QVariant> 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
|
||||
Reference in New Issue
Block a user