Skip to content

Commit

Permalink
Added inlineImageTag() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
treefrogframework committed Mar 12, 2016
1 parent d6a52ae commit 24a2eb1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/tviewhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,44 @@ QString TViewHelper::imageTag(const QString &src, bool withTimestamp,
return selfClosingTag("img", attr);
}


QString TViewHelper::inlineImageTag(const QFileInfo &file, const QString &mediaType,
const QSize &size, const QString &alt,
const THtmlAttribute &attributes) const
{
QByteArray data;
QFile img(file.absoluteFilePath());
if (img.open(QIODevice::ReadOnly)) {
data = img.readAll();
img.close();
}
return inlineImageTag(data, mediaType, size, alt, attributes);
}


QString TViewHelper::inlineImageTag(const QByteArray &data, const QString &mediaType,
const QSize &size, const QString &alt,
const THtmlAttribute &attributes) const
{
THtmlAttribute attr = attributes;
if (!alt.isEmpty()) {
attr.prepend("alt", alt);
} else {
attr.prepend("alt", ""); // output 'alt' always
}

if (!size.isEmpty()) {
attr.prepend("height", QString::number(size.height()));
attr.prepend("width", QString::number(size.width()));
}

QByteArray dataUrl = "data:";
dataUrl += mediaType.toLatin1() + ";base64,";
dataUrl += data.toBase64();
attr.prepend("src", dataUrl);
return selfClosingTag("img", attr);
}

/*!
Creates a \<link\> link tag for a style sheet with href=\a "src". The
\a src must be one of URL, a absolute path or a relative path. If \a src
Expand Down
35 changes: 35 additions & 0 deletions src/tviewhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QPoint>
#include <QSize>
#include <QPair>
#include <QFileInfo>
#include <TGlobal>
#include <THtmlAttribute>

Expand Down Expand Up @@ -142,6 +143,20 @@ class T_CORE_EXPORT TViewHelper

QString imageTag(const QString &src, const THtmlAttribute &attributes) const;

QString inlineImageTag(const QFileInfo &file, const QString &mediaType,
const QSize &size = QSize(), const QString &alt = QString(),
const THtmlAttribute &attributes = THtmlAttribute()) const;

QString inlineImageTag(const QFileInfo &file, const QString &mediaType,
const THtmlAttribute &attributes) const;

QString inlineImageTag(const QByteArray &data, const QString &mediaType,
const QSize &size = QSize(), const QString &alt = QString(),
const THtmlAttribute &attributes = THtmlAttribute()) const;

QString inlineImageTag(const QByteArray &data, const QString &mediaType,
const THtmlAttribute &attributes) const;

QString imageLinkTo(const QString &src, const QUrl &url, const QSize &size = QSize(),
const QString &alt = QString(), const THtmlAttribute &attributes = THtmlAttribute()) const; // obsolete
QString imageLinkTo(const QUrl &url, const QString &src, bool withTimestamp,
Expand Down Expand Up @@ -348,4 +363,24 @@ inline QString TViewHelper::imageLinkTo(const QUrl &url, const QString &src, con
return imageLinkTo(url, src, false, QString(), attributes);
}

/*!
\fn QString TViewHelper::inlineImageTag(const QString &filePath, const QString &mediaType, const THtmlAttribute &attributes) const
Creates a inline image tag with data of \a filePath and \a mediaType.
*/
inline QString TViewHelper::inlineImageTag(const QFileInfo &file, const QString &mediaType,
const THtmlAttribute &attributes) const
{
return inlineImageTag(file, mediaType, QSize(), QString(), attributes);
}

/*!
\fn QString TViewHelper::inlineImageTag(const QByteArray &data, const QString &mediaType, const THtmlAttribute &attributes) const
Creates a inline image tag with \a data and \a mediaType.
*/
inline QString TViewHelper::inlineImageTag(const QByteArray &data, const QString &mediaType,
const THtmlAttribute &attributes) const
{
return inlineImageTag(data, mediaType, QSize(), QString(), attributes);
}

#endif // TVIEWHELPER_H

0 comments on commit 24a2eb1

Please sign in to comment.