Skip to content

Commit

Permalink
Add support for "@3x" image loading.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Tor Arne Vestbø <[email protected]>
  • Loading branch information
msorvig authored and Tor Arne Vestbø committed Oct 16, 2015
1 parent 9d1fab4 commit 068a545
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
51 changes: 40 additions & 11 deletions src/gui/image/qicon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}

/*!
Expand Down Expand Up @@ -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
[email protected], 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
2 changes: 2 additions & 0 deletions src/gui/image/qicon.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QIcon &);
Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QIcon &);
#endif

Q_GUI_EXPORT QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio);

QT_END_NAMESPACE

#endif // QICON_H
16 changes: 3 additions & 13 deletions src/gui/text/qtextimagehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

QT_BEGIN_NAMESPACE

extern QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio);
static QString resolveFileName(QString fileName, QUrl *url, qreal targetDevicePixelRatio)
{
// We might use the fileName for loading if url loading fails
Expand All @@ -62,19 +63,8 @@ static QString resolveFileName(QString fileName, QUrl *url, qreal targetDevicePi
if (targetDevicePixelRatio <= 1.0)
return fileName;

// try to find a 2x version

const int dotIndex = fileName.lastIndexOf(QLatin1Char('.'));
if (dotIndex != -1) {
QString at2xfileName = fileName;
at2xfileName.insert(dotIndex, QStringLiteral("@2x"));
if (QFile::exists(at2xfileName)) {
fileName = at2xfileName;
*url = QUrl(fileName);
}
}

return fileName;
// try to find a Nx version
return qt_findAtNxFile(fileName, targetDevicePixelRatio);
}


Expand Down
1 change: 1 addition & 0 deletions tests/manual/highdpi/highdpi.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<qresource prefix="/">
<file>qticon16.png</file>
<file>[email protected]</file>
<file>[email protected]</file>
<file>qticon32.png</file>
<file>[email protected]</file>
<file>qticon64.png</file>
Expand Down
Binary file added tests/manual/highdpi/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 068a545

Please sign in to comment.