From 3865f59ef016589068673a963536e9aec44a222f Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Tue, 20 Jun 2017 14:41:36 +0200 Subject: [PATCH] feat(ImageProvider): refactoring --- src/app/providers/ImageProvider.cpp | 95 +++++++++++++++++++---------- 1 file changed, 63 insertions(+), 32 deletions(-) diff --git a/src/app/providers/ImageProvider.cpp b/src/app/providers/ImageProvider.cpp index 4371d1b37..a23f44d91 100644 --- a/src/app/providers/ImageProvider.cpp +++ b/src/app/providers/ImageProvider.cpp @@ -20,6 +20,8 @@ * Author: Ronan Abhamon */ +#include + #include #include #include @@ -36,8 +38,18 @@ // Max image size in bytes. (100Kb) #define MAX_IMAGE_SIZE 102400 +using namespace std; + // ============================================================================= +static void removeAttribute (QXmlStreamAttributes &readerAttributes, const QString &name) { + auto it = find_if(readerAttributes.cbegin(), readerAttributes.cend(), [&name](const QXmlStreamAttribute &attribute) { + return name == attribute.name() && !attribute.prefix().length(); + }); + if (it != readerAttributes.cend()) + readerAttributes.remove(static_cast(distance(readerAttributes.cbegin(), it))); +} + static QByteArray buildByteArrayAttribute (const QByteArray &name, const QByteArray &value) { QByteArray attribute = name; attribute.append("=\""); @@ -46,58 +58,73 @@ static QByteArray buildByteArrayAttribute (const QByteArray &name, const QByteAr return attribute; } -static QByteArray fillFillAndStroke (const QXmlStreamAttributes &readerAttributes, bool &fill, bool &stroke, const Colors &colors) { +static QByteArray parseFillAndStroke ( + QXmlStreamAttributes &readerAttributes, + const Colors &colors +) { static QRegExp regex("^color-([^-]+)-(fill|stroke)$"); QByteArray attributes; - const QByteArray value = readerAttributes.value("class").toLatin1(); - if (!value.length()) + const QByteArray classAttr = readerAttributes.value("class").toLatin1(); + if (!classAttr.length()) return attributes; - for (const auto &subValue : value.split(' ')) { - regex.indexIn(subValue.trimmed()); + for (const auto &classValue : classAttr.split(' ')) { + regex.indexIn(classValue.trimmed()); const QStringList list = regex.capturedTexts(); if (list.length() != 3) continue; - const QString colorName = list[1]; - const QVariant colorValue = colors.property(colorName.toStdString().c_str()); + const QVariant colorValue = colors.property(list[1].toStdString().c_str()); if (!colorValue.isValid()) { - qWarning() << QStringLiteral("Color name `%1` does not exist.").arg(colorName); + qWarning() << QStringLiteral("Color name `%1` does not exist.").arg(list[1]); continue; } - const QByteArray property = list[2].toLatin1(); - if (property == QStringLiteral("fill")) - fill = true; - else - stroke = true; - - attributes.append( - buildByteArrayAttribute( - property, - colorValue.value().name().toLatin1() - ) - ); + removeAttribute(readerAttributes, list[2]); + attributes.append(buildByteArrayAttribute(list[2].toLatin1(), colorValue.value().name().toLatin1())); } return attributes; } +static QByteArray parseStyle ( + const QXmlStreamAttributes &readerAttributes, + const Colors &colors +) { + return QByteArray(); + // TODO. + + static QRegExp regex("^color-([^-]+)-style-(fill|stroke)$"); + + QByteArray attribute = "style=\""; + + QByteArray fill; + QByteArray stroke; + + const QByteArray classAttr = readerAttributes.value("class").toLatin1(); + for (const auto &classValue : classAttr.split(' ')) { + regex.indexIn(classValue.trimmed()); + const QStringList list = regex.capturedTexts(); + if (list.length() != 3) + continue; + } + + attribute.append("\" "); + + return attribute; +} + static QByteArray parseAttributes (const QXmlStreamReader &reader, const Colors &colors) { - const QXmlStreamAttributes readerAttributes = reader.attributes(); - bool fill = false, stroke = false; - QByteArray attributes = fillFillAndStroke(readerAttributes, fill, stroke, colors); + QXmlStreamAttributes readerAttributes = reader.attributes(); + + QByteArray attributes = parseFillAndStroke(readerAttributes, colors); + attributes.append(parseStyle(readerAttributes, colors)); for (const auto &attribute : readerAttributes) { - const QByteArray name = attribute.name().toLatin1(); - if (fill && name == QStringLiteral("fill")) - continue; - if (stroke && name == QStringLiteral("stroke")) - continue; - const QByteArray prefix = attribute.prefix().toLatin1(); + const QByteArray name = attribute.name().toLatin1(); const QByteArray value = attribute.value().toLatin1(); if (prefix.length() > 0) { @@ -209,11 +236,12 @@ ImageProvider::ImageProvider () : QQuickImageProvider( // ----------------------------------------------------------------------------- QImage ImageProvider::requestImage (const QString &id, QSize *, const QSize &) { + const QString path = QStringLiteral(":/assets/images/%1").arg(id); + qInfo() << QStringLiteral("Image `%1` requested.").arg(path); + QElapsedTimer timer; timer.start(); - const QString path = QStringLiteral(":/assets/images/%1").arg(id); - // 1. Read and update XML content. QFile file(path); if (QFileInfo(file).size() > MAX_IMAGE_SIZE) { @@ -242,8 +270,11 @@ QImage ImageProvider::requestImage (const QString &id, QSize *, const QSize &) { // 3. Create en empty image. const QRectF viewBox = renderer.viewBoxF(); QImage image(static_cast(viewBox.width()), static_cast(viewBox.height()), QImage::Format_ARGB32); - if (image.isNull()) + if (image.isNull()) { + qWarning() << QStringLiteral("Unable to create image of size `(%1, %2)` from path: `%3`.") + .arg(viewBox.width()).arg(viewBox.height()).arg(path); return QImage(); // Memory cannot be allocated. + } image.fill(0x00000000); // 4. Paint!