Scale icons returned by QIcon::fromTheme.
authorDavid Sansome <me@davidsansome.com>
Thu, 22 Mar 2012 11:28:29 +0000 (11:28 +0000)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Wed, 10 Oct 2012 06:45:22 +0000 (08:45 +0200)
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 <jens.bache-wiig@digia.com>
src/gui/image/qiconloader.cpp
tests/auto/gui/image/qicon/tst_qicon.cpp

index 7e62a58..ba2731b 100644 (file)
@@ -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<qint64>(basePixmap.cacheKey())
                   % HexString<int>(mode)
                   % HexString<qint64>(QGuiApplication::palette().cacheKey())
-                  % HexString<int>(actualSize);
+                  % HexString<int>(actualSize.width())
+                  % HexString<int>(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<QGuiApplication *>(qApp))
             cachedPixmap = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(mode, basePixmap);
index 3ada9b9..acbb50d 100644 (file)
@@ -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
     {