From 8dd8acf8bbfde548756ccde52cb14c848f2d8739 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Wed, 14 Jun 2017 10:58:38 +0200 Subject: [PATCH] qt_findAtNxFile(): account for .9 (9-patch image) extensions Currently a file with a .9.png extension will only have its @2x variant found if it follows this format: foo.9@2x.png Since ".9" should be considered part of the file suffix, it should ideally be able to look like this and still be picked up: foo@2x.9.png This patch makes qt_findAtNxFile() account for .9.* extensions. This is needed for the image-based style support in Qt Quick Controls 2, which uses 9-patch images. qmlbench benchmark results using benchmarks\auto\creation\quick.image\delegates_image.qml with QT_SCALE_FACTOR=2 show no difference in performance after this patch is applied. [ChangeLog][QtGui] High DPI variants of 9-patch images can now be loaded using the following syntax: "foo@2x.9.png" Change-Id: I6d1384113bef21b4fe85a104ee6b16869c93b077 Reviewed-by: J-P Nurmi Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/gui/image/qicon.cpp | 7 ++++++- .../qiconhighdpi/icons/misc/button.9.png | Bin 0 -> 329 bytes .../qiconhighdpi/icons/misc/button@2x.9.png | Bin 0 -> 616 bytes .../image/qiconhighdpi/tst_qiconhighdpi.cpp | 19 ++++++++++++++++++ .../image/qiconhighdpi/tst_qiconhighdpi.qrc | 2 ++ 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/auto/gui/image/qiconhighdpi/icons/misc/button.9.png create mode 100644 tests/auto/gui/image/qiconhighdpi/icons/misc/button@2x.9.png diff --git a/src/gui/image/qicon.cpp b/src/gui/image/qicon.cpp index fa4b4e01af..9b2e96d4b0 100644 --- a/src/gui/image/qicon.cpp +++ b/src/gui/image/qicon.cpp @@ -1469,8 +1469,13 @@ QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRati return baseFileName; int dotIndex = baseFileName.lastIndexOf(QLatin1Char('.')); - if (dotIndex == -1) /* no dot */ + if (dotIndex == -1) { /* no dot */ dotIndex = baseFileName.size(); /* append */ + } else if (dotIndex >= 2 && baseFileName[dotIndex - 1] == QLatin1Char('9') + && baseFileName[dotIndex - 2] == QLatin1Char('.')) { + // If the file has a .9.* (9-patch image) extension, we must ensure that the @nx goes before it. + dotIndex -= 2; + } QString atNxfileName = baseFileName; atNxfileName.insert(dotIndex, QLatin1String("@2x")); diff --git a/tests/auto/gui/image/qiconhighdpi/icons/misc/button.9.png b/tests/auto/gui/image/qiconhighdpi/icons/misc/button.9.png new file mode 100644 index 0000000000000000000000000000000000000000..1a560a1d7400423f1aca7bf70b2a4ad201ddbd70 GIT binary patch literal 329 zcmeAS@N?(olHy`uVBq!ia0vp^S|H591|*LjJ{b+9I14-?iy0XBj({-ZRBb+Kpx|#$ z7srr@*0(nmxtIb48Xne%`7cb^ES=JQ^%3v3N>ToYtU0%P#8x|Qym3^ig5|ZuJz+6l<8{Bf}1&47Cy7QV$~k?Jm9rV)?DYc#Vbld(#JgYF65ZK3i!OKiR+M_>66rp zVrHxAd9J!^1T9_@TqPBJO~LfY>Jv{ZEXsd13m5c?&)YJ6;)k8nJI=g+K0AL&&BFy{ Z%&R$!KW@nrxCaad22WQ%mvv4FO#p6*jI#g$ literal 0 HcmV?d00001 diff --git a/tests/auto/gui/image/qiconhighdpi/icons/misc/button@2x.9.png b/tests/auto/gui/image/qiconhighdpi/icons/misc/button@2x.9.png new file mode 100644 index 0000000000000000000000000000000000000000..f010dc55c7c8c0769055aac10c85fb3a0c7b64bc GIT binary patch literal 616 zcmeAS@N?(olHy`uVBq!ia0vp^V9db=WF6OOo(-fp3p^r=85sDEfH31!Z9ZuR1|}&_ z7srr_IdAVS40;?O(f08DoSeJo9u%B02w8hDv5IkTi`F+L1CKAWO!qRL+2#6GsEX0F zRDvh4@O7ZiIY$*uoy#*@Kb>tm@Oy<>?~mtmO**aAU!;EGKL2~gq5Xb8Hobjt@=M92 zE(`8U=AOyFBMuj9uRL?fcg^Lby?df;?H{hcH2LNCzrU5@Ql8FS(){1_>46Z(M8j2= zYqtLRyWBdWM1EGsL-%RtRNsCzdSvj%bJh}xeR6WL>sNpJwvt2Ns{vZ;Be ze|O#UVxRlUo$A}_)Au-q&714vuO1|ABs#JBQ6Ar|GT#&5UiQeAyxmo;vfg61wD;NS zC+YP!>sD|4@?>T2cEu%}ioP6~P9DNevji@+Oi*hvfe5{v$caUidP25L*zE<{FnL89 z=i5#19og1Qkz4QK^zNNu%Cx*+b5nQhDhr&HG*@iWsyKD2Wvg;;_(?(}8oJ-Ti(xjyJTjw!ETbkDsBNY4`5U@rTPo1AdsU z`h0%*?6{n}%TrIA>CCU4`zdJs&9devicePixelRatio()); + + switch (dpr) { + case 1: + QCOMPARE(icon.availableSizes().size(), 1); + QCOMPARE(icon.availableSizes().at(0), QSize(42, 42)); + break; + case 2: + QCOMPARE(icon.availableSizes().size(), 2); + QCOMPARE(icon.availableSizes().at(0), QSize(42, 42)); + QCOMPARE(icon.availableSizes().at(1), QSize(82, 82)); + break; + } +} + int main(int argc, char *argv[]) { QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); diff --git a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc index 80b5e38ee6..5cc1c6d9b1 100644 --- a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc +++ b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.qrc @@ -4,5 +4,7 @@ icons/testtheme/22x22/actions/appointment-new.png icons/testtheme/index.theme icons/testtheme/22x22@2/actions/appointment-new.png + icons/misc/button.9.png + icons/misc/button@2x.9.png