Commit d425956d5a83a755b3fd7493c84d0490bf0a045c

Authored by Charles Otto
1 parent e095c645

Fix issue #265 (csv format broken with FTE templates)

Retain the number of columns generated per key output, so that the csv
layout will not be broken if point/rect metadata keys are missing (since
they are output as multiple columns of the csv).
Showing 1 changed file with 47 additions and 12 deletions
openbr/plugins/gallery.cpp
... ... @@ -857,20 +857,27 @@ class csvGallery : public FileGallery
857 857 QStringList lines;
858 858 lines.reserve(files.size()+1);
859 859  
  860 + QMap<QString, int> columnCounts;
  861 +
860 862 { // Make header
861 863 QStringList words;
862 864 words.append("File");
863   - foreach (const QString &key, samples.keys())
864   - words.append(getCSVElement(key, samples[key], true));
  865 + foreach (const QString &key, samples.keys()) {
  866 + int count = 0;
  867 + words.append(getCSVElement(key, samples[key], true, count));
  868 + columnCounts.insert(key, count);
  869 + }
865 870 lines.append(words.join(","));
866 871 }
867   -
  872 +
868 873 // Make table
869 874 foreach (const File &file, files) {
870 875 QStringList words;
871 876 words.append(file.name);
872   - foreach (const QString &key, samples.keys())
873   - words.append(getCSVElement(key, file.value(key), false));
  877 + foreach (const QString &key, samples.keys()) {
  878 + int count = columnCounts[key];
  879 + words.append(getCSVElement(key, file.value(key), false, count));
  880 + }
874 881 lines.append(words.join(","));
875 882 }
876 883  
... ... @@ -919,22 +926,50 @@ class csvGallery : public FileGallery
919 926 files.append(t.file);
920 927 }
921 928  
922   - static QString getCSVElement(const QString &key, const QVariant &value, bool header)
  929 + static QString getCSVElement(const QString &key, const QVariant &value, bool header, int & columnCount)
923 930 {
  931 + if (header)
  932 + columnCount = 1;
  933 +
924 934 if (value.canConvert<QString>()) {
925 935 if (header) return key;
926   - else return value.value<QString>();
  936 + else {
  937 + if (columnCount != 1)
  938 + qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key));
  939 + return value.value<QString>();
  940 + }
927 941 } else if (value.canConvert<QPointF>()) {
928 942 const QPointF point = value.value<QPointF>();
929   - if (header) return key+"_X,"+key+"_Y";
930   - else return QString::number(point.x())+","+QString::number(point.y());
  943 + if (header) {
  944 + columnCount = 2;
  945 + return key+"_X,"+key+"_Y";
  946 + }
  947 + else {
  948 + if (columnCount != 2)
  949 + qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key));
  950 +
  951 + return QString::number(point.x())+","+QString::number(point.y());
  952 + }
931 953 } else if (value.canConvert<QRectF>()) {
932 954 const QRectF rect = value.value<QRectF>();
933   - if (header) return key+"_X,"+key+"_Y,"+key+"_Width,"+key+"_Height";
934   - else return QString::number(rect.x())+","+QString::number(rect.y())+","+QString::number(rect.width())+","+QString::number(rect.height());
  955 + if (header) {
  956 + columnCount = 4;
  957 + return key+"_X,"+key+"_Y,"+key+"_Width,"+key+"_Height";
  958 + }
  959 + else {
  960 + if (columnCount != 4)
  961 + qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key));
  962 +
  963 + return QString::number(rect.x())+","+QString::number(rect.y())+","+QString::number(rect.width())+","+QString::number(rect.height());
  964 + }
935 965 } else {
936 966 if (header) return key;
937   - else return QString::number(std::numeric_limits<float>::quiet_NaN());
  967 + else {
  968 + QString output = QString::number(std::numeric_limits<float>::quiet_NaN());
  969 + for (int i = 1; i < columnCount; i++)
  970 + output += "," + QString::number(std::numeric_limits<float>::quiet_NaN());
  971 + return output;
  972 + }
938 973 }
939 974 }
940 975 };
... ...