summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2024-04-10 16:30:42 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2024-05-07 12:28:59 -0700
commitab2f4ef8fad82fd07dac8768b8dcb94b11b271b6 (patch)
treeb24d4c9f6e65150566b195a545794386b30114cc /tests/auto
parentfb2ea1e52a03f87e041a61b936183175df37b5bf (diff)
QTextDocument: Add support for responsive images
Add support for the max-width css attribute in image. This allows images to be responsive: it adapts their size to the size of the QTextDocument so that they never grow bigger than the QTextDocument pageSize. This is implemented for the image handler used in QTextEdit and other QtWidget text related classes. [ChangeLog][QtGui][CSS] The max-width style can now be applied to <img/> to set the maximum width in pixels or percentage. Task-number: QTBUG-12283 Change-Id: Ic94e16279a1240ab4a509823de59dc0bfc920bb9 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp66
1 files changed, 65 insertions, 1 deletions
diff --git a/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp b/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp
index 8327a5d187..0048623d0e 100644
--- a/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp
+++ b/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp
@@ -6,6 +6,10 @@
#include <QPainter>
#include <private/qtextimagehandler_p.h>
+using namespace Qt::StringLiterals;
+
+// #define DEBUG_WRITE_HTML
+
class tst_QTextImageHandler : public QObject
{
Q_OBJECT
@@ -20,6 +24,8 @@ private slots:
void loadAtNImages_data();
#ifndef QT_NO_TEXTHTMLPARSER
void loadAtNImages();
+ void maxWidth_data();
+ void maxWidth();
#endif
};
@@ -61,7 +67,7 @@ void tst_QTextImageHandler::loadAtNImages()
const auto it = std::find_if(formats.begin(), formats.end(), [](const auto &format){
return format.objectType() == QTextFormat::ImageObject;
});
- QVERIFY(it != formats.end());
+ QCOMPARE_NE(it, formats.end());
const QTextImageFormat format = (*it).toImageFormat();
QTextImageHandler handler;
@@ -77,6 +83,64 @@ void tst_QTextImageHandler::loadAtNImages()
QCOMPARE(img.pixelColor(0, 0), expectedColor);
}
}
+
+void tst_QTextImageHandler::maxWidth_data()
+{
+ QTest::addColumn<QString>("imageFile");
+ QTest::addColumn<QSizeF>("pageSize");
+ QTest::addColumn<QTextLength>("maxWidth");
+ QTest::addColumn<QSizeF>("expectedSize");
+
+ QTest::addRow("constrained-percentage") << QFINDTESTDATA("data/image.png") << QSizeF(16, 16) << QTextLength(QTextLength::PercentageLength, 100) << QSizeF(12, 12);
+ QTest::addRow("not-constrained-percentage") << QFINDTESTDATA("data/image.png") << QSizeF(200, 200) << QTextLength(QTextLength::PercentageLength, 100) << QSizeF(16, 16);
+ QTest::addRow("constrained-fixed") << QFINDTESTDATA("data/image.png") << QSizeF(16, 16) << QTextLength(QTextLength::FixedLength, 5) << QSizeF(5, 5);
+ QTest::addRow("not-constrained-fixed") << QFINDTESTDATA("data/image.png") << QSizeF(200, 200) << QTextLength(QTextLength::FixedLength, 5) << QSizeF(5, 5);
+ QTest::addRow("not-constrained-default") << QFINDTESTDATA("data/image.png") << QSizeF(200, 200) << QTextLength(QTextLength::VariableLength, 5) << QSizeF(16, 16);
+}
+
+void tst_QTextImageHandler::maxWidth()
+{
+ QFETCH(QString, imageFile);
+ QFETCH(QSizeF, pageSize);
+ QFETCH(QTextLength, maxWidth);
+ QFETCH(QSizeF, expectedSize);
+
+ QTextDocument doc;
+ doc.setPageSize(pageSize);
+ doc.setDocumentMargin(2);
+ QTextCursor c(&doc);
+ QString style;
+ if (maxWidth.type() == QTextLength::PercentageLength)
+ style = " style=\"max-width:"_L1 + QString::number(maxWidth.rawValue()) + "%;\""_L1;
+ else if (maxWidth.type() == QTextLength::FixedLength)
+ style = " style=\"max-width:"_L1 + QString::number(maxWidth.rawValue()) + "px;\""_L1;
+ const QString html = "<img src=\"" + imageFile + u'\"' + style + "\">";
+ c.insertHtml(html);
+
+#ifdef DEBUG_WRITE_HTML
+ {
+ QFile out("/tmp/" + QLatin1String(QTest::currentDataTag()) + ".html");
+ out.open(QFile::WriteOnly);
+ out.write(html.toLatin1());
+ out.close();
+ }
+ {
+ QFile out("/tmp/" + QLatin1String(QTest::currentDataTag()) + "_rewrite.html");
+ out.open(QFile::WriteOnly);
+ out.write(doc.toHtml().toLatin1());
+ out.close();
+ }
+#endif
+ const auto formats = doc.allFormats();
+ const auto it = std::find_if(formats.begin(), formats.end(), [](const auto &format){
+ return format.objectType() == QTextFormat::ImageObject;
+ });
+ QCOMPARE_NE(it, formats.end());
+ const QTextImageFormat format = (*it).toImageFormat();
+ QTextImageHandler handler;
+
+ QCOMPARE(handler.intrinsicSize(&doc, 0, format), expectedSize);
+}
#endif
QTEST_MAIN(tst_QTextImageHandler)