Commit cfe25ddaf69fd2ca0da9c1d495ebcf1d2b187c4a
1 parent
ddbf56c6
More work
Showing
3 changed files
with
46 additions
and
62 deletions
openbr/core/qtutils.cpp
| @@ -245,10 +245,10 @@ QStringList parse(QString args, char split, bool *ok) | @@ -245,10 +245,10 @@ QStringList parse(QString args, char split, bool *ok) | ||
| 245 | QStack<QChar> subexpressions; | 245 | QStack<QChar> subexpressions; |
| 246 | for (int i=0; i<args.size(); i++) { | 246 | for (int i=0; i<args.size(); i++) { |
| 247 | if (inQuote) { | 247 | if (inQuote) { |
| 248 | - if (args[i] == '\'') | 248 | + if (args[i] == '\'' || args[i] == '\"') |
| 249 | inQuote = false; | 249 | inQuote = false; |
| 250 | } else { | 250 | } else { |
| 251 | - if (args[i] == '\'') { | 251 | + if (args[i] == '\'' || args[i] == '\"') { |
| 252 | inQuote = true; | 252 | inQuote = true; |
| 253 | } else if ((args[i] == '(') || (args[i] == '[') || (args[i] == '<') || (args[i] == '{')) { | 253 | } else if ((args[i] == '(') || (args[i] == '[') || (args[i] == '<') || (args[i] == '{')) { |
| 254 | subexpressions.push(args[i]); | 254 | subexpressions.push(args[i]); |
| @@ -277,7 +277,10 @@ QStringList parse(QString args, char split, bool *ok) | @@ -277,7 +277,10 @@ QStringList parse(QString args, char split, bool *ok) | ||
| 277 | return words; | 277 | return words; |
| 278 | } | 278 | } |
| 279 | } else if (subexpressions.isEmpty() && (args[i] == split)) { | 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 | start = i+1; | 284 | start = i+1; |
| 282 | } | 285 | } |
| 283 | } | 286 | } |
| @@ -512,6 +515,13 @@ QVariantMap fromJsonObject(const QJsonObject &object) | @@ -512,6 +515,13 @@ QVariantMap fromJsonObject(const QJsonObject &object) | ||
| 512 | 515 | ||
| 513 | QVariant fromString(const QString &value) | 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 | bool ok = false; | 525 | bool ok = false; |
| 516 | const QPointF point = QtUtils::toPoint(value, &ok); | 526 | const QPointF point = QtUtils::toPoint(value, &ok); |
| 517 | if (ok) return point; | 527 | if (ok) return point; |
openbr/openbr_plugin.cpp
| @@ -156,30 +156,12 @@ QVariant File::value(const QString &key) const | @@ -156,30 +156,12 @@ QVariant File::value(const QString &key) const | ||
| 156 | 156 | ||
| 157 | QVariant File::parse(const QString &value) | 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 | void File::set(const QString &key, const QString &value) | 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 | bool File::getBool(const QString &key, bool defaultValue) const | 167 | bool File::getBool(const QString &key, bool defaultValue) const |
openbr/plugins/gallery/csv.cpp
| @@ -157,21 +157,31 @@ class csvGallery : public FileGallery | @@ -157,21 +157,31 @@ class csvGallery : public FileGallery | ||
| 157 | QtUtils::writeFile(file, lines); | 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 | TemplateList readBlock(bool *done) | 186 | TemplateList readBlock(bool *done) |
| 177 | { | 187 | { |
| @@ -196,7 +206,10 @@ class csvGallery : public FileGallery | @@ -196,7 +206,10 @@ class csvGallery : public FileGallery | ||
| 196 | QMap<QString, File> combinedFiles; | 206 | QMap<QString, File> combinedFiles; |
| 197 | 207 | ||
| 198 | while (!f.atEnd()) { | 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 | const QString name = values.first().toString(); | 213 | const QString name = values.first().toString(); |
| 201 | File &in = combinedFiles[name]; | 214 | File &in = combinedFiles[name]; |
| 202 | in.name = name; | 215 | in.name = name; |
| @@ -207,7 +220,9 @@ class csvGallery : public FileGallery | @@ -207,7 +220,9 @@ class csvGallery : public FileGallery | ||
| 207 | templates.append(in); | 220 | templates.append(in); |
| 208 | } else { | 221 | } else { |
| 209 | for (qint64 i = 0; i < this->readBlockSize && !f.atEnd(); i++) { | 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 | File in; | 227 | File in; |
| 213 | in.name = values.first().toString(); | 228 | in.name = values.first().toString(); |
| @@ -250,29 +265,6 @@ class csvGallery : public FileGallery | @@ -250,29 +265,6 @@ class csvGallery : public FileGallery | ||
| 250 | } | 265 | } |
| 251 | return words.join(","); | 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 | BR_REGISTER(Gallery, csvGallery) | 270 | BR_REGISTER(Gallery, csvGallery) |