From 46a552583f99ed04945ccf949afbdff58dcdfa1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Wed, 12 Aug 2020 15:19:54 +0200 Subject: Teach QPixmapIconEngine how to handle @Nx pixmaps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bring QPixmapIconEngine on par with QIconLoaderEngine when it comes to @Nx pixmap handling: Make the scale factor a test parameter during icon lookup. This allows storing e.g 16x16@1, 16x16@2, 16x16@3 versions of a pixmap in the icon, and then having QIcon select the correct one based on the target devicePixelRatio. Extend the qiconhighdpi test to also cover QPixmapIconEngine, via the addPixmap() API. The corner cases of pixmap lookup can be much complicated. QIconLoaderEngine and QPixmapIconEngine should ideally have identical behavior in order to avoid surprises. Change-Id: I17552cc61755bff9553c4a462e3983ac6759c13b Reviewed-by: Tor Arne Vestbø --- .../gui/image/qiconhighdpi/tst_qiconhighdpi.cpp | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'tests/auto/gui') diff --git a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp index 52f38419c7..62878926fa 100644 --- a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp +++ b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp @@ -39,6 +39,8 @@ private slots: void initTestCase(); void fromTheme_data(); void fromTheme(); + void addPixmap_data(); + void addPixmap(); void ninePatch(); }; @@ -173,6 +175,49 @@ void tst_QIconHighDpi::fromTheme() QCOMPARE(pixmap.devicePixelRatio(), expectedDpr); } +void tst_QIconHighDpi::addPixmap_data() +{ + // fromTheme() and addPixmap() test the QIconLoaderEngine and QPixmapIconEngine + // QIcon backend, repsectively. We want these to have identical behavior and + // re-use the same test data here. + fromTheme_data(); +} + +void tst_QIconHighDpi::addPixmap() +{ + QFETCH(int, requestedSize); + QFETCH(qreal, requestedDpr); + QFETCH(int, expectedSize); + QFETCH(qreal, expectedDpr); + + QIcon icon; + + // manual pixmap adder for full control of devicePixelRatio + auto addPixmapWithDpr = [&icon](const QString &path, qreal dpr) { + QImage image(path); + image.setDevicePixelRatio(dpr); + icon.addPixmap(QPixmap::fromImage(image)); + }; + + addPixmapWithDpr(":icons/testtheme/16x16/actions/appointment-new.png", 1); + addPixmapWithDpr(":icons/testtheme/22x22/actions/appointment-new.png", 1); + addPixmapWithDpr(":icons/testtheme/22x22@2/actions/appointment-new.png", 2); + + QVERIFY(icon.availableSizes().contains(QSize(16, 16))); + QVERIFY(icon.availableSizes().contains(QSize(22, 22))); + + if (qstrcmp(QTest::currentDataTag(), "16x16,dpr=2") == 0) + QSKIP("broken corner case"); // expect 22x22, get 32x32 + if (qstrcmp(QTest::currentDataTag(), "44x44,dpr=1") == 0) + QSKIP("broken corner case"); // expect 44x44, get 22x22 + if (qstrcmp(QTest::currentDataTag(), "8x8,dpr=3") == 0) + QSKIP("broken corner case");// expect 22x22, get 24x24 + + const QPixmap pixmap = icon.pixmap(QSize(requestedSize, requestedSize), requestedDpr); + QCOMPARE(pixmap.size(), QSize(expectedSize, expectedSize)); + QCOMPARE(pixmap.devicePixelRatio(), expectedDpr); +} + void tst_QIconHighDpi::ninePatch() { const QIcon icon(":/icons/misc/button.9.png"); -- cgit v1.2.3