Commit b5be92e0 authored by Ronan Abhamon's avatar Ronan Abhamon

feat(ImageProvider): supports style property

parent 3865f59e
...@@ -66,52 +66,71 @@ static QByteArray parseFillAndStroke ( ...@@ -66,52 +66,71 @@ static QByteArray parseFillAndStroke (
QByteArray attributes; QByteArray attributes;
const QByteArray classAttr = readerAttributes.value("class").toLatin1(); for (const auto &classValue : readerAttributes.value("class").toLatin1().split(' ')) {
if (!classAttr.length())
return attributes;
for (const auto &classValue : classAttr.split(' ')) {
regex.indexIn(classValue.trimmed()); regex.indexIn(classValue.trimmed());
const QStringList list = regex.capturedTexts(); if (regex.pos() == -1)
if (list.length() != 3)
continue; continue;
const QStringList list = regex.capturedTexts();
const QVariant colorValue = colors.property(list[1].toStdString().c_str()); const QVariant colorValue = colors.property(list[1].toStdString().c_str());
if (!colorValue.isValid()) { if (!colorValue.isValid()) {
qWarning() << QStringLiteral("Color name `%1` does not exist.").arg(list[1]); qWarning() << QStringLiteral("Color name `%1` does not exist.").arg(list[1]);
continue; continue;
} }
removeAttribute(readerAttributes, list[2]); ::removeAttribute(readerAttributes, list[2]);
attributes.append(buildByteArrayAttribute(list[2].toLatin1(), colorValue.value<QColor>().name().toLatin1())); attributes.append(::buildByteArrayAttribute(list[2].toLatin1(), colorValue.value<QColor>().name().toLatin1()));
} }
return attributes; return attributes;
} }
static QByteArray parseStyle ( static QByteArray parseStyle (
const QXmlStreamAttributes &readerAttributes, QXmlStreamAttributes &readerAttributes,
const Colors &colors const Colors &colors
) { ) {
return QByteArray();
// TODO.
static QRegExp regex("^color-([^-]+)-style-(fill|stroke)$"); static QRegExp regex("^color-([^-]+)-style-(fill|stroke)$");
QByteArray attribute = "style=\""; QByteArray attribute;
QByteArray fill;
QByteArray stroke;
const QByteArray classAttr = readerAttributes.value("class").toLatin1(); QSet<QString> overrode;
for (const auto &classValue : classAttr.split(' ')) { for (const auto &classValue : readerAttributes.value("class").toLatin1().split(' ')) {
regex.indexIn(classValue.trimmed()); regex.indexIn(classValue.trimmed());
if (regex.pos() == -1)
continue;
const QStringList list = regex.capturedTexts(); const QStringList list = regex.capturedTexts();
if (list.length() != 3)
overrode.insert(list[2]);
const QVariant colorValue = colors.property(list[1].toStdString().c_str());
if (!colorValue.isValid()) {
qWarning() << QStringLiteral("Color name `%1` does not exist.").arg(list[1]);
continue; continue;
} }
attribute.append(list[2].toLatin1());
attribute.append(":");
attribute.append(colorValue.value<QColor>().name().toLatin1());
attribute.append(";");
}
const QByteArrayList styleValues = readerAttributes.value("style").toLatin1().split(';');
for (const auto &styleValue : styleValues) {
const QByteArrayList list = styleValue.split(':');
if (list.length() > 0 && !overrode.contains(list[0])) {
attribute.append(styleValue);
attribute.append(";");
}
}
::removeAttribute(readerAttributes, "style");
if (attribute.length() > 0) {
attribute.prepend("style=\"");
attribute.append("\" "); attribute.append("\" ");
}
return attribute; return attribute;
} }
...@@ -119,20 +138,19 @@ static QByteArray parseStyle ( ...@@ -119,20 +138,19 @@ static QByteArray parseStyle (
static QByteArray parseAttributes (const QXmlStreamReader &reader, const Colors &colors) { static QByteArray parseAttributes (const QXmlStreamReader &reader, const Colors &colors) {
QXmlStreamAttributes readerAttributes = reader.attributes(); QXmlStreamAttributes readerAttributes = reader.attributes();
QByteArray attributes = parseFillAndStroke(readerAttributes, colors); QByteArray attributes = ::parseFillAndStroke(readerAttributes, colors);
attributes.append(parseStyle(readerAttributes, colors)); attributes.append(::parseStyle(readerAttributes, colors));
for (const auto &attribute : readerAttributes) { 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();
if (prefix.length() > 0) { if (prefix.length() > 0) {
attributes.append(prefix); attributes.append(prefix);
attributes.append(":"); attributes.append(":");
} }
attributes.append(buildByteArrayAttribute(name, value)); attributes.append(
::buildByteArrayAttribute(attribute.name().toLatin1(), attribute.value().toLatin1())
);
} }
return attributes; return attributes;
...@@ -169,9 +187,9 @@ static QByteArray parseStartElement (const QXmlStreamReader &reader, const Color ...@@ -169,9 +187,9 @@ static QByteArray parseStartElement (const QXmlStreamReader &reader, const Color
QByteArray startElement = "<"; QByteArray startElement = "<";
startElement.append(reader.name().toLatin1()); startElement.append(reader.name().toLatin1());
startElement.append(" "); startElement.append(" ");
startElement.append(parseAttributes(reader, colors)); startElement.append(::parseAttributes(reader, colors));
startElement.append(" "); startElement.append(" ");
startElement.append(parseDeclarations(reader)); startElement.append(::parseDeclarations(reader));
startElement.append(">"); startElement.append(">");
return startElement; return startElement;
} }
...@@ -201,15 +219,15 @@ static QByteArray computeContent (QFile &file) { ...@@ -201,15 +219,15 @@ static QByteArray computeContent (QFile &file) {
break; break;
case QXmlStreamReader::StartDocument: case QXmlStreamReader::StartDocument:
content.append(parseStartDocument(reader)); content.append(::parseStartDocument(reader));
break; break;
case QXmlStreamReader::StartElement: case QXmlStreamReader::StartElement:
content.append(parseStartElement(reader, *colors)); content.append(::parseStartElement(reader, *colors));
break; break;
case QXmlStreamReader::EndElement: case QXmlStreamReader::EndElement:
content.append(parseEndElement(reader)); content.append(::parseEndElement(reader));
break; break;
case QXmlStreamReader::Characters: case QXmlStreamReader::Characters:
...@@ -254,7 +272,7 @@ QImage ImageProvider::requestImage (const QString &id, QSize *, const QSize &) { ...@@ -254,7 +272,7 @@ QImage ImageProvider::requestImage (const QString &id, QSize *, const QSize &) {
return QImage(); return QImage();
} }
const QByteArray content = computeContent(file); const QByteArray content = ::computeContent(file);
if (!content.length()) { if (!content.length()) {
qWarning() << QStringLiteral("Unable to parse file: `%1`.").arg(path); qWarning() << QStringLiteral("Unable to parse file: `%1`.").arg(path);
return QImage(); return QImage();
......
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