diff --git a/openbr/plugins/gallery/csv.cpp b/openbr/plugins/gallery/csv.cpp index eef367b..c9b2a9b 100644 --- a/openbr/plugins/gallery/csv.cpp +++ b/openbr/plugins/gallery/csv.cpp @@ -16,10 +16,101 @@ #include #include +#include namespace br { +struct CSVHeader +{ + QList indices; + + CSVHeader() + {} + + CSVHeader(const QString &key) + : key(key) + {} + + QString key; + QStringList subKeys; +}; + +class CSVHeaderList : public QList +{ +public: + CSVHeaderList() + {} + + CSVHeaderList(const QList &headers) + { + foreach (const CSVHeader &header, headers) + append(header); + } + + CSVHeaderList(const QStringList &keys) + { + foreach (const QString &key, keys) + append(CSVHeader(key)); + } + + void sort() + { + typedef QPair IndexPair; + QList sortedKeys = Common::Sort(keys()); + + CSVHeaderList sortedList; + foreach (const IndexPair sortedKey, sortedKeys) + sortedList.append((*this)[sortedKey.second]); + *this = sortedList; + } + + QStringList keys() const + { + QStringList keys; + for (int i=0; isize(); i++) + keys.append((*this)[i].key); + return keys; + } + + static CSVHeaderList fromHeaders(const QStringList &headers) + { + CSVHeaderList csvHeaders; + QStringList processedKeys; + + for (int i=0; ireadBlockSize && !f.atEnd(); i++) { - const QVariantList values = parseLine(f.readLine()); - if (values.size() != headers.size()) continue; + if (combineFiles) { + *done = true; + QMap combinedFiles; + + for (qint64 i = 0; i < !f.atEnd(); i++) { + const QVariantList values = parseLine(f.readLine()); + const QString name = values.first().toString(); + File &in = combinedFiles[name]; + in.name = name; + setValuesFromHeaders(in, headers, values.mid(1)); + } - File in; - for (int j=0; jreadBlockSize && !f.atEnd(); i++) { + const QVariantList values = parseLine(f.readLine()); + + File in; + in.name = values.first().toString(); + setValuesFromHeaders(in, headers, values.mid(1)); + in.set("progress", f.pos()); + templates.append(in); + } + *done = f.atEnd(); } - *done = f.atEnd(); + return templates; } @@ -104,10 +231,10 @@ class csvGallery : public FileGallery writeOpen(); if (headers.isEmpty()) { foreach (const QString &key, t.file.localKeys()) - headers.append(key); + headers.append(CSVHeader(key)); headers.sort(); - const QString header = QString(QStringList(QStringList("File") + headers).join(",") + "\n"); + const QString header = QString(QStringList(QStringList("File") + headers.keys()).join(",") + "\n"); f.write(header.toLocal8Bit()); } f.write(QString(lineFromFile(t.file) + "\n").toLocal8Bit()); @@ -119,7 +246,7 @@ class csvGallery : public FileGallery { QStringList words; words.append(file.name); - foreach (const QString &key, headers) { + foreach (const QString &key, headers.keys()) { QString value = QtUtils::toString(file.value(key)); if (value.contains(",")) value = '"' + value + '"';