Commit 6c8a198c authored by Ronan Abhamon's avatar Ronan Abhamon

feat(App): svg parser in progress

parent 323dcb16
...@@ -20,9 +20,12 @@ ...@@ -20,9 +20,12 @@
* Author: Ronan Abhamon * Author: Ronan Abhamon
*/ */
#include <QFile>
#include <QImage> #include <QImage>
#include <QPainter> #include <QPainter>
#include <QSvgRenderer> #include <QSvgRenderer>
#include <QXmlStreamReader>
#include <QtDebug>
#include "ImageProvider.hpp" #include "ImageProvider.hpp"
...@@ -38,19 +41,56 @@ ImageProvider::ImageProvider () : QQuickImageProvider( ...@@ -38,19 +41,56 @@ 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); const QString path = QStringLiteral(":/assets/images/%1").arg(id);
// 1. Build svg renderer. // 1. Read and update XML content.
QSvgRenderer renderer(path); QFile file(path);
if (!file.open(QIODevice::ReadOnly))
return QImage(); // Invalid file.
QString content;
QXmlStreamReader reader(&file);
bool soFarSoGood = true;
while (soFarSoGood && !reader.atEnd())
switch (reader.readNext()) {
case QXmlStreamReader::NoToken:
case QXmlStreamReader::Invalid:
break;
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
case QXmlStreamReader::StartElement: {
QXmlStreamAttributes attributes = reader.attributes();
} break;
case QXmlStreamReader::EndElement:
content.append(QStringLiteral("</%1>").arg(reader.name().toString()));
break;
case QXmlStreamReader::Characters:
case QXmlStreamReader::Comment:
case QXmlStreamReader::DTD:
case QXmlStreamReader::EntityReference:
case QXmlStreamReader::ProcessingInstruction:
break;
}
qDebug() << content;
if (!soFarSoGood || reader.hasError())
return QImage(); // Invalid file.
// 2. Build svg renderer.
QSvgRenderer renderer(&reader);
if (!renderer.isValid()) if (!renderer.isValid())
return QImage(path); // Not a svg file? return QImage(path); // Not a svg file.
// 2. 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())
return QImage(); // Memory cannot be allocated. return QImage(); // Memory cannot be allocated.
image.fill(0x00000000); image.fill(0x00000000);
// 3. Paint! // 4. Paint!
QPainter painter(&image); QPainter painter(&image);
renderer.render(&painter); renderer.render(&painter);
......
...@@ -35,8 +35,6 @@ public: ...@@ -35,8 +35,6 @@ public:
QImage requestImage (const QString &id, QSize *size, const QSize &requestedSize) override; QImage requestImage (const QString &id, QSize *size, const QSize &requestedSize) override;
static const QString PROVIDER_ID; static const QString PROVIDER_ID;
private:
}; };
#endif // IMAGE_PROVIDER_H_ #endif // IMAGE_PROVIDER_H_
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