Commit d611f97e5909eb97df0091a6f71518ecaf6c24f7

Authored by Scott Klum
2 parents 12647ef8 d425956d

Merge pull request #292 from biometrics/csv_fix

Fix issue #265 (csv format broken with FTE templates)
Showing 1 changed file with 47 additions and 12 deletions
openbr/plugins/gallery.cpp
@@ -887,20 +887,27 @@ class csvGallery : public FileGallery @@ -887,20 +887,27 @@ class csvGallery : public FileGallery
887 QStringList lines; 887 QStringList lines;
888 lines.reserve(files.size()+1); 888 lines.reserve(files.size()+1);
889 889
  890 + QMap<QString, int> columnCounts;
  891 +
890 { // Make header 892 { // Make header
891 QStringList words; 893 QStringList words;
892 words.append("File"); 894 words.append("File");
893 - foreach (const QString &key, samples.keys())  
894 - words.append(getCSVElement(key, samples[key], true)); 895 + foreach (const QString &key, samples.keys()) {
  896 + int count = 0;
  897 + words.append(getCSVElement(key, samples[key], true, count));
  898 + columnCounts.insert(key, count);
  899 + }
895 lines.append(words.join(",")); 900 lines.append(words.join(","));
896 } 901 }
897 - 902 +
898 // Make table 903 // Make table
899 foreach (const File &file, files) { 904 foreach (const File &file, files) {
900 QStringList words; 905 QStringList words;
901 words.append(file.name); 906 words.append(file.name);
902 - foreach (const QString &key, samples.keys())  
903 - words.append(getCSVElement(key, file.value(key), false)); 907 + foreach (const QString &key, samples.keys()) {
  908 + int count = columnCounts[key];
  909 + words.append(getCSVElement(key, file.value(key), false, count));
  910 + }
904 lines.append(words.join(",")); 911 lines.append(words.join(","));
905 } 912 }
906 913
@@ -950,22 +957,50 @@ class csvGallery : public FileGallery @@ -950,22 +957,50 @@ class csvGallery : public FileGallery
950 files.append(t.file); 957 files.append(t.file);
951 } 958 }
952 959
953 - static QString getCSVElement(const QString &key, const QVariant &value, bool header) 960 + static QString getCSVElement(const QString &key, const QVariant &value, bool header, int & columnCount)
954 { 961 {
  962 + if (header)
  963 + columnCount = 1;
  964 +
955 if (value.canConvert<QString>()) { 965 if (value.canConvert<QString>()) {
956 if (header) return key; 966 if (header) return key;
957 - else return value.value<QString>(); 967 + else {
  968 + if (columnCount != 1)
  969 + qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key));
  970 + return value.value<QString>();
  971 + }
958 } else if (value.canConvert<QPointF>()) { 972 } else if (value.canConvert<QPointF>()) {
959 const QPointF point = value.value<QPointF>(); 973 const QPointF point = value.value<QPointF>();
960 - if (header) return key+"_X,"+key+"_Y";  
961 - else return QString::number(point.x())+","+QString::number(point.y()); 974 + if (header) {
  975 + columnCount = 2;
  976 + return key+"_X,"+key+"_Y";
  977 + }
  978 + else {
  979 + if (columnCount != 2)
  980 + qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key));
  981 +
  982 + return QString::number(point.x())+","+QString::number(point.y());
  983 + }
962 } else if (value.canConvert<QRectF>()) { 984 } else if (value.canConvert<QRectF>()) {
963 const QRectF rect = value.value<QRectF>(); 985 const QRectF rect = value.value<QRectF>();
964 - if (header) return key+"_X,"+key+"_Y,"+key+"_Width,"+key+"_Height";  
965 - else return QString::number(rect.x())+","+QString::number(rect.y())+","+QString::number(rect.width())+","+QString::number(rect.height()); 986 + if (header) {
  987 + columnCount = 4;
  988 + return key+"_X,"+key+"_Y,"+key+"_Width,"+key+"_Height";
  989 + }
  990 + else {
  991 + if (columnCount != 4)
  992 + qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key));
  993 +
  994 + return QString::number(rect.x())+","+QString::number(rect.y())+","+QString::number(rect.width())+","+QString::number(rect.height());
  995 + }
966 } else { 996 } else {
967 if (header) return key; 997 if (header) return key;
968 - else return QString::number(std::numeric_limits<float>::quiet_NaN()); 998 + else {
  999 + QString output = QString::number(std::numeric_limits<float>::quiet_NaN());
  1000 + for (int i = 1; i < columnCount; i++)
  1001 + output += "," + QString::number(std::numeric_limits<float>::quiet_NaN());
  1002 + return output;
  1003 + }
969 } 1004 }
970 } 1005 }
971 }; 1006 };