Improve image width rendering by limiting the image width to the label's width

This commit is contained in:
Tobias Fella 2020-06-07 17:14:13 +02:00
parent 5d8865a8f1
commit 2c4effe372
3 changed files with 38 additions and 1 deletions

View File

@ -20,6 +20,7 @@
#include "entry.h"
#include <QRegularExpression>
#include <QSqlQuery>
#include <QUrl>
@ -116,3 +117,37 @@ void Entry::setRead(bool read)
query.bindValue(QStringLiteral(":read"), m_read);
Database::instance().execute(query);
}
QString Entry::adjustedContent(int width, int fontSize)
{
QString ret(m_content);
QRegularExpression imgRegex(QStringLiteral("<img ((?!width=\"[0-9]+(px)?\").)*(width=\"([0-9]+)(px)?\")?[^>]*>"));
QRegularExpressionMatchIterator i = imgRegex.globalMatch(ret);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString imgTag(match.captured());
if (imgTag.contains(QStringLiteral("wp-smiley")))
imgTag.insert(4, QStringLiteral(" width=\"%1\"").arg(fontSize));
QString widthParameter = match.captured(4);
if (widthParameter.length() != 0) {
if (widthParameter.toInt() > width)
imgTag.replace(match.captured(3), QStringLiteral("width=\"%1\"").arg(width));
} else {
imgTag.insert(4, QStringLiteral(" width=\"%1\"").arg(width));
}
ret.replace(match.captured(), imgTag);
}
ret.replace(QRegularExpression(QStringLiteral("<ul[^>]*>")), QLatin1String(""));
ret.replace(QRegularExpression(QStringLiteral("</ul>")), QLatin1String(""));
ret.replace(QRegularExpression(QStringLiteral("<li[^>]*>")), QLatin1String(""));
ret.replace(QRegularExpression(QStringLiteral("</li>")), QLatin1String(""));
ret.replace(QStringLiteral("<img"), QStringLiteral("<br /> <img"));
return ret;
}

View File

@ -61,6 +61,8 @@ public:
void setRead(bool read);
Q_INVOKABLE QString adjustedContent(int width, int fontSize);
Q_SIGNALS:
void readChanged(bool read);

View File

@ -34,13 +34,13 @@ Kirigami.ScrollablePage {
title: entry.title
Controls.Label {
//anchors.fill: parent
text: page.entry.content
baseUrl: page.entry.baseUrl
textFormat: Text.RichText
wrapMode: Text.WordWrap
Layout.fillWidth: true
onLinkActivated: Qt.openUrlExternally(link)
onWidthChanged: text = entry.adjustedContent(width, font.pixelSize)
}
}