From 068a545339a3ec14187e6b9b5ea05c6ffbda05a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Fri, 2 Oct 2015 23:48:17 +0200 Subject: Add support for "@3x" image loading. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement as generic "@Nx" support in an exported qt_findAtNxFile function. 3x devices now get one extra file existence test in cases where @3x versions are not present. 1x devices are still on the fast path where there are no extra file system accesses. Add an @3x image to the highdpi manual test. Change-Id: I4ce3fc245ada01ea410abe1443ceb1e3abf7c17f Reviewed-by: Timur Pocheptsov Reviewed-by: Tor Arne Vestbø --- src/gui/image/qicon.cpp | 51 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) (limited to 'src/gui/image/qicon.cpp') diff --git a/src/gui/image/qicon.cpp b/src/gui/image/qicon.cpp index 7a59adffb8..af3af516db 100644 --- a/src/gui/image/qicon.cpp +++ b/src/gui/image/qicon.cpp @@ -45,6 +45,7 @@ #include "qcache.h" #include "qdebug.h" #include "qpalette.h" +#include "qmath.h" #include "private/qhexstring_p.h" #include "private/qguiapplication_p.h" @@ -1026,19 +1027,13 @@ void QIcon::addFile(const QString &fileName, const QSize &size, Mode mode, State } else { detach(); } + d->engine->addFile(fileName, size, mode, state); - // Check if a "@2x" file exists and add it. - static bool disable2xImageLoading = !qEnvironmentVariableIsEmpty("QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING"); - if (!disable2xImageLoading && qApp->devicePixelRatio() > 1.0) { - QString at2xfileName = fileName; - int dotIndex = fileName.lastIndexOf(QLatin1Char('.')); - if (dotIndex == -1) /* no dot */ - dotIndex = fileName.size(); /* append */ - at2xfileName.insert(dotIndex, QStringLiteral("@2x")); - if (QFile::exists(at2xfileName)) - d->engine->addFile(at2xfileName, size, mode, state); - } + // Check if a "@Nx" file exists and add it. + QString atNxFileName = qt_findAtNxFile(fileName, qApp->devicePixelRatio()); + if (atNxFileName != fileName) + d->engine->addFile(atNxFileName, size, mode, state); } /*! @@ -1375,5 +1370,39 @@ QDebug operator<<(QDebug dbg, const QIcon &i) \internal */ +/*! + \internal + \since 5.6 + Attempts to find a suitable @Nx file for the given \a targetDevicePixelRatio + Returns the the \a baseFileName if no such file was found. + + Given base foo.png and a target dpr of 2.5, this function will look for + foo@3x.png, then foo@2x, then fall back to foo.png if not found. +*/ +QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio) +{ + if (targetDevicePixelRatio <= 1.0) + return baseFileName; + + static bool disableNxImageLoading = !qEnvironmentVariableIsEmpty("QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING"); + if (disableNxImageLoading) + return baseFileName; + + QString atNx = QLatin1String("@%1x"); + int dotIndex = baseFileName.lastIndexOf(QLatin1Char('.')); + if (dotIndex == -1) /* no dot */ + dotIndex = baseFileName.size(); /* append */ + + // Check for @Nx, ..., @3x, @2x file versions, + for (int n = qCeil(targetDevicePixelRatio); n > 1; --n) { + QString atNxfileName = baseFileName; + atNxfileName.insert(dotIndex, atNx.arg(n)); + if (QFile::exists(atNxfileName)) + return atNxfileName; + } + + return baseFileName; +} + QT_END_NAMESPACE #endif //QT_NO_ICON -- cgit v1.2.3