From c3ffcbd9a2697f1799ba4afbdf14ca1210ac2bbd Mon Sep 17 00:00:00 2001 From: David Sansome Date: Thu, 22 Mar 2012 11:28:29 +0000 Subject: [PATCH] Scale icons returned by QIcon::fromTheme. The icon engine behind icons that are loaded from QIcon::fromTheme does not scale any pixmaps that it returns. When using an icon theme with an incomplete set of icons (for example, only a "128x128" folder), QIcon::pixmap will always return 128x128 pixmaps even if you ask for one of size 22x22. This is contrary to the QIcon::pixmap documentation that says "The pixmap might be smaller than requested, but never larger." This patch uses the same code that is in the main QIcon class in the PixmapEntry QIconLoaderEngineEntry to scale pixmaps if they are too big. Change-Id: Ic25a3628ac82cfb899574245f658490a2dd49d54 Reviewed-by: Jens Bache-Wiig --- src/gui/image/qiconloader.cpp | 10 ++++++++-- tests/auto/gui/image/qicon/tst_qicon.cpp | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/gui/image/qiconloader.cpp b/src/gui/image/qiconloader.cpp index 7e62a5846c..ba2731b210 100644 --- a/src/gui/image/qiconloader.cpp +++ b/src/gui/image/qiconloader.cpp @@ -504,17 +504,23 @@ QPixmap PixmapEntry::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State st if (basePixmap.isNull()) basePixmap.load(filename); - int actualSize = qMin(size.width(), size.height()); + QSize actualSize = basePixmap.size(); + if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height())) + actualSize.scale(size, Qt::KeepAspectRatio); + QString key = QLatin1String("$qt_theme_") % HexString(basePixmap.cacheKey()) % HexString(mode) % HexString(QGuiApplication::palette().cacheKey()) - % HexString(actualSize); + % HexString(actualSize.width()) + % HexString(actualSize.height()); QPixmap cachedPixmap; if (QPixmapCache::find(key, &cachedPixmap)) { return cachedPixmap; } else { + if (basePixmap.size() != actualSize) + basePixmap = basePixmap.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); cachedPixmap = basePixmap; if (QGuiApplication *guiApp = qobject_cast(qApp)) cachedPixmap = static_cast(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(mode, basePixmap); diff --git a/tests/auto/gui/image/qicon/tst_qicon.cpp b/tests/auto/gui/image/qicon/tst_qicon.cpp index 3ada9b9a80..acbb50d8f3 100644 --- a/tests/auto/gui/image/qicon/tst_qicon.cpp +++ b/tests/auto/gui/image/qicon/tst_qicon.cpp @@ -591,6 +591,12 @@ void tst_QIcon::fromTheme() noIcon = QIcon::fromTheme("svg-icon", abIcon); QVERIFY(!noIcon.availableSizes().isEmpty()); + // Pixmaps should be no larger than the requested size (QTBUG-17953) + QCOMPARE(appointmentIcon.pixmap(22).size(), QSize(22, 22)); // exact + QCOMPARE(appointmentIcon.pixmap(32).size(), QSize(32, 32)); // exact + QCOMPARE(appointmentIcon.pixmap(48).size(), QSize(32, 32)); // smaller + QCOMPARE(appointmentIcon.pixmap(8).size(), QSize(8, 8)); // scaled down + QByteArray ba; // write to QByteArray {