Commit cfe25ddaf69fd2ca0da9c1d495ebcf1d2b187c4a

Authored by Scott Klum
1 parent ddbf56c6

More work

openbr/core/qtutils.cpp
... ... @@ -245,10 +245,10 @@ QStringList parse(QString args, char split, bool *ok)
245 245 QStack<QChar> subexpressions;
246 246 for (int i=0; i<args.size(); i++) {
247 247 if (inQuote) {
248   - if (args[i] == '\'')
  248 + if (args[i] == '\'' || args[i] == '\"')
249 249 inQuote = false;
250 250 } else {
251   - if (args[i] == '\'') {
  251 + if (args[i] == '\'' || args[i] == '\"') {
252 252 inQuote = true;
253 253 } else if ((args[i] == '(') || (args[i] == '[') || (args[i] == '<') || (args[i] == '{')) {
254 254 subexpressions.push(args[i]);
... ... @@ -277,7 +277,10 @@ QStringList parse(QString args, char split, bool *ok)
277 277 return words;
278 278 }
279 279 } else if (subexpressions.isEmpty() && (args[i] == split)) {
280   - words.append(args.mid(start, i-start).trimmed());
  280 + QString word = args.mid(start, i-start).trimmed();
  281 + if (word.contains('\'') || word.contains('\"'))
  282 + word = word.mid(1, word.size()-2);
  283 + words.append(word);
281 284 start = i+1;
282 285 }
283 286 }
... ... @@ -512,6 +515,13 @@ QVariantMap fromJsonObject(const QJsonObject &amp;object)
512 515  
513 516 QVariant fromString(const QString &value)
514 517 {
  518 + if (value.startsWith('[') && value.endsWith(']')) {
  519 + QVariantList variants;
  520 + foreach (const QString &value, QtUtils::parse(value.mid(1, value.size()-2)))
  521 + variants.append(fromString(value));
  522 + return variants;
  523 + }
  524 +
515 525 bool ok = false;
516 526 const QPointF point = QtUtils::toPoint(value, &ok);
517 527 if (ok) return point;
... ...
openbr/openbr_plugin.cpp
... ... @@ -156,30 +156,12 @@ QVariant File::value(const QString &amp;key) const
156 156  
157 157 QVariant File::parse(const QString &value)
158 158 {
159   - bool ok = false;
160   - const QPointF point = QtUtils::toPoint(value, &ok);
161   - if (ok) return point;
162   - const QRectF rect = QtUtils::toRect(value, &ok);
163   - if (ok) return rect;
164   - const cv::RotatedRect rotatedRect = OpenCVUtils::rotateRectFromString(value, &ok);
165   - if (ok) return QVariant::fromValue(rotatedRect);
166   - const int i = value.toInt(&ok);
167   - if (ok) return i;
168   - const float f = value.toFloat(&ok);
169   - if (ok) return f;
170   - return value;
  159 + return QtUtils::fromString(value);
171 160 }
172 161  
173 162 void File::set(const QString &key, const QString &value)
174 163 {
175   - if (value.startsWith('[') && value.endsWith(']')) {
176   - QVariantList variants;
177   - foreach (const QString &value, QtUtils::parse(value.mid(1, value.size()-2)))
178   - variants.append(parse(value));
179   - set(key, variants);
180   - } else {
181   - set(key, QVariant(parse(value)));
182   - }
  164 + set(key, QtUtils::fromString(value));
183 165 }
184 166  
185 167 bool File::getBool(const QString &key, bool defaultValue) const
... ...
openbr/plugins/gallery/csv.cpp
... ... @@ -157,21 +157,31 @@ class csvGallery : public FileGallery
157 157 QtUtils::writeFile(file, lines);
158 158 }
159 159  
160   - void setValuesFromHeaders(File &f, const CSVHeaderList &headers, const QVariantList &values)
161   - {
162   - foreach (const CSVHeader &header, headers) {
163   - if (header.indices.size() == 1)
164   - f.set(header.key, values[header.indices.first()]);
165   - else if (header.indices.size() == 2) // QPointF
166   - f.appendPoint(QPointF(values[header.indices[header.subKeys.indexOf("X")]].toFloat(),
167   - values[header.indices[header.subKeys.indexOf("Y")]].toFloat()));
168   - else if (header.indices.size() == 4) // QRectF
169   - f.appendRect(QRectF(values[header.indices[header.subKeys.indexOf("X")]].toFloat(),
170   - values[header.indices[header.subKeys.indexOf("Y")]].toFloat(),
171   - values[header.indices[header.subKeys.indexOf("Width")]].toFloat(),
172   - values[header.indices[header.subKeys.indexOf("Height")]].toFloat()));
  160 + void setValuesFromHeaders(File &f, const CSVHeaderList &headers, const QVariantList &values)
  161 + {
  162 + foreach (const CSVHeader &header, headers) {
  163 + if (header.indices.size() == 1) {
  164 + if (header.key == "Rects")
  165 + foreach(const QVariant &rect, values[header.indices.first()].toList())
  166 + f.appendRect(rect.toRectF());
  167 + else if (header.key == "Points")
  168 + foreach(const QVariant &point, values[header.indices.first()].toList())
  169 + f.appendPoint(point.toPointF());
  170 + else {
  171 + const QVariant value = values[header.indices.first()];
  172 + if (!value.canConvert<QString>() || !value.toString().isEmpty())
  173 + f.set(header.key, values[header.indices.first()]);
  174 + }
  175 + } else if (header.indices.size() == 2) // QPointF
  176 + f.appendPoint(QPointF(values[header.indices[header.subKeys.indexOf("X")]].toFloat(),
  177 + values[header.indices[header.subKeys.indexOf("Y")]].toFloat()));
  178 + else if (header.indices.size() == 4) // QRectF
  179 + f.appendRect(QRectF(values[header.indices[header.subKeys.indexOf("X")]].toFloat(),
  180 + values[header.indices[header.subKeys.indexOf("Y")]].toFloat(),
  181 + values[header.indices[header.subKeys.indexOf("Width")]].toFloat(),
  182 + values[header.indices[header.subKeys.indexOf("Height")]].toFloat()));
  183 + }
173 184 }
174   - }
175 185  
176 186 TemplateList readBlock(bool *done)
177 187 {
... ... @@ -196,7 +206,10 @@ class csvGallery : public FileGallery
196 206 QMap<QString, File> combinedFiles;
197 207  
198 208 while (!f.atEnd()) {
199   - const QVariantList values = parseLine(f.readLine());
  209 + QVariantList values;
  210 + foreach (const QString &value, QtUtils::parse(f.readLine(), ','))
  211 + values.append(QtUtils::fromString(value));
  212 +
200 213 const QString name = values.first().toString();
201 214 File &in = combinedFiles[name];
202 215 in.name = name;
... ... @@ -207,7 +220,9 @@ class csvGallery : public FileGallery
207 220 templates.append(in);
208 221 } else {
209 222 for (qint64 i = 0; i < this->readBlockSize && !f.atEnd(); i++) {
210   - const QVariantList values = parseLine(f.readLine());
  223 + QVariantList values;
  224 + foreach (const QString &value, QtUtils::parse(f.readLine(), ','))
  225 + values.append(QtUtils::fromString(value));
211 226  
212 227 File in;
213 228 in.name = values.first().toString();
... ... @@ -250,29 +265,6 @@ class csvGallery : public FileGallery
250 265 }
251 266 return words.join(",");
252 267 }
253   -
254   - static QVariantList parseLine(const QByteArray bytes)
255   - {
256   - bool inQuote(false);
257   - QVariantList values;
258   - QString value = QString();
259   - const QString line = QString::fromLocal8Bit(bytes).trimmed();
260   - for (int i=0; i<line.size(); i++) {
261   - const QChar c = line[i];
262   - if (c == '"') {
263   - inQuote = !inQuote;
264   - continue;
265   - } else if (c == ',' && !inQuote) {
266   - values.append(QVariant(value));
267   - value = QString();
268   - } else
269   - value.append(c);
270   - }
271   - if (!value.isEmpty())
272   - values.append(QVariant(value));
273   -
274   - return values;
275   - }
276 268 };
277 269  
278 270 BR_REGISTER(Gallery, csvGallery)
... ...