Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions textdocument/lib/abstractmarkupbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions textdocument/lib/bbcodebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions textdocument/lib/bbcodebuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 7 additions & 2 deletions textdocument/lib/markupdirector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QImage>();
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;
Expand Down
9 changes: 9 additions & 0 deletions textdocument/lib/plaintextmarkupbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions textdocument/lib/plaintextmarkupbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 15 additions & 0 deletions textdocument/lib/texthtmlbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "texthtmlbuilder.h"

#include <QtCore/QList>
#include <QtCore/QBuffer>
#include <QtGui/QTextDocument>

namespace Grantlee
Expand Down Expand Up @@ -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("<img src=\"data:image/png;base64,%1\" ").arg(QString::fromLatin1(data.toBase64())));
if (width != 0)
d->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);
Expand Down
1 change: 1 addition & 0 deletions textdocument/lib/texthtmlbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down