Commit 3865f59e authored by Ronan Abhamon's avatar Ronan Abhamon

feat(ImageProvider): refactoring

parent 3086732c
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
* Author: Ronan Abhamon * Author: Ronan Abhamon
*/ */
#include <algorithm>
#include <QElapsedTimer> #include <QElapsedTimer>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
...@@ -36,8 +38,18 @@ ...@@ -36,8 +38,18 @@
// Max image size in bytes. (100Kb) // Max image size in bytes. (100Kb)
#define MAX_IMAGE_SIZE 102400 #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<int>(distance(readerAttributes.cbegin(), it)));
}
static QByteArray buildByteArrayAttribute (const QByteArray &name, const QByteArray &value) { static QByteArray buildByteArrayAttribute (const QByteArray &name, const QByteArray &value) {
QByteArray attribute = name; QByteArray attribute = name;
attribute.append("=\""); attribute.append("=\"");
...@@ -46,58 +58,73 @@ static QByteArray buildByteArrayAttribute (const QByteArray &name, const QByteAr ...@@ -46,58 +58,73 @@ static QByteArray buildByteArrayAttribute (const QByteArray &name, const QByteAr
return attribute; 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)$"); static QRegExp regex("^color-([^-]+)-(fill|stroke)$");
QByteArray attributes; QByteArray attributes;
const QByteArray value = readerAttributes.value("class").toLatin1(); const QByteArray classAttr = readerAttributes.value("class").toLatin1();
if (!value.length()) if (!classAttr.length())
return attributes; return attributes;
for (const auto &subValue : value.split(' ')) { for (const auto &classValue : classAttr.split(' ')) {
regex.indexIn(subValue.trimmed()); regex.indexIn(classValue.trimmed());
const QStringList list = regex.capturedTexts(); const QStringList list = regex.capturedTexts();
if (list.length() != 3) if (list.length() != 3)
continue; continue;
const QString colorName = list[1]; const QVariant colorValue = colors.property(list[1].toStdString().c_str());
const QVariant colorValue = colors.property(colorName.toStdString().c_str());
if (!colorValue.isValid()) { 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; continue;
} }
const QByteArray property = list[2].toLatin1(); removeAttribute(readerAttributes, list[2]);
if (property == QStringLiteral("fill")) attributes.append(buildByteArrayAttribute(list[2].toLatin1(), colorValue.value<QColor>().name().toLatin1()));
fill = true;
else
stroke = true;
attributes.append(
buildByteArrayAttribute(
property,
colorValue.value<QColor>().name().toLatin1()
)
);
} }
return attributes; return attributes;
} }
static QByteArray parseAttributes (const QXmlStreamReader &reader, const Colors &colors) { static QByteArray parseStyle (
const QXmlStreamAttributes readerAttributes = reader.attributes(); const QXmlStreamAttributes &readerAttributes,
bool fill = false, stroke = false; const Colors &colors
QByteArray attributes = fillFillAndStroke(readerAttributes, fill, stroke, colors); ) {
return QByteArray();
// TODO.
for (const auto &attribute : readerAttributes) { static QRegExp regex("^color-([^-]+)-style-(fill|stroke)$");
const QByteArray name = attribute.name().toLatin1();
if (fill && name == QStringLiteral("fill")) QByteArray attribute = "style=\"";
continue;
if (stroke && name == QStringLiteral("stroke")) 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; continue;
}
attribute.append("\" ");
return attribute;
}
static QByteArray parseAttributes (const QXmlStreamReader &reader, const Colors &colors) {
QXmlStreamAttributes readerAttributes = reader.attributes();
QByteArray attributes = parseFillAndStroke(readerAttributes, colors);
attributes.append(parseStyle(readerAttributes, colors));
for (const auto &attribute : readerAttributes) {
const QByteArray prefix = attribute.prefix().toLatin1(); const QByteArray prefix = attribute.prefix().toLatin1();
const QByteArray name = attribute.name().toLatin1();
const QByteArray value = attribute.value().toLatin1(); const QByteArray value = attribute.value().toLatin1();
if (prefix.length() > 0) { if (prefix.length() > 0) {
...@@ -209,11 +236,12 @@ ImageProvider::ImageProvider () : QQuickImageProvider( ...@@ -209,11 +236,12 @@ ImageProvider::ImageProvider () : QQuickImageProvider(
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
QImage ImageProvider::requestImage (const QString &id, QSize *, const QSize &) { 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; QElapsedTimer timer;
timer.start(); timer.start();
const QString path = QStringLiteral(":/assets/images/%1").arg(id);
// 1. Read and update XML content. // 1. Read and update XML content.
QFile file(path); QFile file(path);
if (QFileInfo(file).size() > MAX_IMAGE_SIZE) { if (QFileInfo(file).size() > MAX_IMAGE_SIZE) {
...@@ -242,8 +270,11 @@ QImage ImageProvider::requestImage (const QString &id, QSize *, const QSize &) { ...@@ -242,8 +270,11 @@ QImage ImageProvider::requestImage (const QString &id, QSize *, const QSize &) {
// 3. Create en empty image. // 3. Create en empty image.
const QRectF viewBox = renderer.viewBoxF(); const QRectF viewBox = renderer.viewBoxF();
QImage image(static_cast<int>(viewBox.width()), static_cast<int>(viewBox.height()), QImage::Format_ARGB32); QImage image(static_cast<int>(viewBox.width()), static_cast<int>(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. return QImage(); // Memory cannot be allocated.
}
image.fill(0x00000000); image.fill(0x00000000);
// 4. Paint! // 4. Paint!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment