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 <jens.bache-wiig@digia.com>
bb10
David Sansome 2012-03-22 11:28:29 +00:00 committed by The Qt Project
parent 3d52d67096
commit c3ffcbd9a2
2 changed files with 14 additions and 2 deletions

View 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);

View 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
{