diff --git a/textdocument/lib/abstractmarkupbuilder.h b/textdocument/lib/abstractmarkupbuilder.h index c6316804..f0798f83 100644 --- a/textdocument/lib/abstractmarkupbuilder.h +++ b/textdocument/lib/abstractmarkupbuilder.h @@ -157,6 +157,14 @@ class GRANTLEE_TEXTDOCUMENT_EXPORT AbstractMarkupBuilder */ virtual void insertImage(const QString &url, qreal width, qreal height) = 0; + /** + Insert a new inline image into the markup + @param image The image itself + @param width The width of the image + @param height The height of the image + */ + virtual void insertImage(const QImage &image, qreal width, qreal height) = 0; + /** Begin a new list element in the markup. A list element contains list items, and may contain other lists. diff --git a/textdocument/lib/bbcodebuilder.cpp b/textdocument/lib/bbcodebuilder.cpp index 15d47a13..625d45f9 100644 --- a/textdocument/lib/bbcodebuilder.cpp +++ b/textdocument/lib/bbcodebuilder.cpp @@ -100,6 +100,13 @@ void BBCodeBuilder::insertImage(const QString &src, qreal width, qreal height) m_text.append(QStringLiteral("[IMG]%1[/IMG]").arg(src)); } +void BBCodeBuilder::insertImage(const QImage &image, qreal width, qreal height) +{ + Q_UNUSED(image); + Q_UNUSED(width); + Q_UNUSED(height); +} + void BBCodeBuilder::beginList(QTextListFormat::Style type) { switch (type) { diff --git a/textdocument/lib/bbcodebuilder.h b/textdocument/lib/bbcodebuilder.h index f8126a6b..ddf58946 100644 --- a/textdocument/lib/bbcodebuilder.h +++ b/textdocument/lib/bbcodebuilder.h @@ -73,6 +73,7 @@ class BBCodeBuilder : public AbstractMarkupBuilder void addNewline() override; void insertImage(const QString &src, qreal width, qreal height) override; + void insertImage(const QImage &image, qreal width, qreal height) override; void beginList(QTextListFormat::Style type) override; diff --git a/textdocument/lib/markupdirector.cpp b/textdocument/lib/markupdirector.cpp index 41347202..9a6c7f1a 100644 --- a/textdocument/lib/markupdirector.cpp +++ b/textdocument/lib/markupdirector.cpp @@ -477,8 +477,13 @@ MarkupDirector::processImage(QTextBlock::iterator it, { Q_UNUSED(doc) // TODO: Close any open format elements? - m_builder->insertImage(imageFormat.name(), imageFormat.width(), - imageFormat.height()); + QImage image = doc->resource(QTextDocument::ImageResource, QUrl(imageFormat.name())).value(); + if (image.isNull()) { + m_builder->insertImage(imageFormat.name(), imageFormat.width(), + imageFormat.height()); + } else { + m_builder->insertImage(image, imageFormat.width(), imageFormat.height()); + } if (!it.atEnd()) return ++it; return it; diff --git a/textdocument/lib/plaintextmarkupbuilder.cpp b/textdocument/lib/plaintextmarkupbuilder.cpp index c219ef44..41d05707 100644 --- a/textdocument/lib/plaintextmarkupbuilder.cpp +++ b/textdocument/lib/plaintextmarkupbuilder.cpp @@ -256,6 +256,15 @@ void PlainTextMarkupBuilder::insertImage(const QString &src, qreal width, d->m_text.append(QStringLiteral("[%1]").arg(ref)); } +void PlainTextMarkupBuilder::insertImage(const QImage &image, qreal width, + qreal height) +{ + Q_D(PlainTextMarkupBuilder); + Q_UNUSED(image) + Q_UNUSED(width) + Q_UNUSED(height) +} + void PlainTextMarkupBuilder::beginList(QTextListFormat::Style style) { Q_D(PlainTextMarkupBuilder); diff --git a/textdocument/lib/plaintextmarkupbuilder.h b/textdocument/lib/plaintextmarkupbuilder.h index bee0dfc8..a8d7a0b9 100644 --- a/textdocument/lib/plaintextmarkupbuilder.h +++ b/textdocument/lib/plaintextmarkupbuilder.h @@ -148,6 +148,7 @@ class GRANTLEE_TEXTDOCUMENT_EXPORT PlainTextMarkupBuilder void insertHorizontalRule(int width = -1) override; void insertImage(const QString &src, qreal width, qreal height) override; + void insertImage(const QImage &image, qreal width, qreal height) override; void beginList(QTextListFormat::Style style) override; diff --git a/textdocument/lib/texthtmlbuilder.cpp b/textdocument/lib/texthtmlbuilder.cpp index 1d3d0d0b..b3883688 100644 --- a/textdocument/lib/texthtmlbuilder.cpp +++ b/textdocument/lib/texthtmlbuilder.cpp @@ -21,6 +21,7 @@ #include "texthtmlbuilder.h" #include +#include #include namespace Grantlee @@ -304,6 +305,20 @@ void TextHTMLBuilder::insertImage(const QString &src, qreal width, qreal height) d->m_text.append(QStringLiteral("/>")); } +void TextHTMLBuilder::insertImage(const QImage &image, qreal width, qreal height) +{ + Q_D(TextHTMLBuilder); + QByteArray data; + QBuffer buffer(&data); + image.save(&buffer, "PNG"); + d->m_text.append(QStringLiteral("m_text.append(QStringLiteral("width=\"%2\" ").arg(width)); + if (height != 0) + d->m_text.append(QStringLiteral("height=\"%2\" ").arg(height)); + d->m_text.append(QStringLiteral("/>")); +} + void TextHTMLBuilder::beginList(QTextListFormat::Style type) { Q_D(TextHTMLBuilder); diff --git a/textdocument/lib/texthtmlbuilder.h b/textdocument/lib/texthtmlbuilder.h index 22dec91f..b9e3d211 100644 --- a/textdocument/lib/texthtmlbuilder.h +++ b/textdocument/lib/texthtmlbuilder.h @@ -173,6 +173,7 @@ class GRANTLEE_TEXTDOCUMENT_EXPORT TextHTMLBuilder void insertHorizontalRule(int width = -1) override; void insertImage(const QString &src, qreal width, qreal height) override; + void insertImage(const QImage &image, qreal width, qreal height) override; void beginList(QTextListFormat::Style type) override;