From ffde7e8995be207d3a3d628a31dbb19ba683a1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Thu, 6 Aug 2020 16:14:30 +0200 Subject: QIcon: Add pixmap() overload which take devicePixelRatio Add new QIcon::pixmap() overload: QPixmap pixmap(const QSize &size, qreal devicePixelRatio, ...) This function replaces the existing pixmap() function which take a QWindow pointer, and should be more convenient in use. Task-number: QTBUG-85885 Change-Id: Ie4ca96a266d9278864678dc61bdfc2836cabdb93 Reviewed-by: Richard Moe Gustavsen --- src/gui/image/qicon.cpp | 62 +++++++++++++++++++++++++++++++++---------------- src/gui/image/qicon.h | 1 + 2 files changed, 43 insertions(+), 20 deletions(-) (limited to 'src/gui/image') diff --git a/src/gui/image/qicon.cpp b/src/gui/image/qicon.cpp index 92d17b368d..e7e13860d8 100644 --- a/src/gui/image/qicon.cpp +++ b/src/gui/image/qicon.cpp @@ -820,7 +820,8 @@ QPixmap QIcon::pixmap(const QSize &size, Mode mode, State state) const { if (!d) return QPixmap(); - return pixmap(nullptr, size, mode, state); + const qreal dpr = -1; // don't know target dpr + return pixmap(size, dpr, mode, state); } /*! @@ -849,18 +850,36 @@ QPixmap QIcon::pixmap(const QSize &size, Mode mode, State state) const images will have a devicePixelRatio larger than 1. */ -/*! Returns the actual size of the icon for the requested \a size, \a - mode, and \a state. The result might be smaller than requested, but - never larger. The returned size is in device-independent pixels (This - is relevant for high-dpi pixmaps.) +/*! + \overload + \since 6.0 - \sa pixmap(), paint() + Returns a pixmap with the requested \a size, \a devicePixelRatio, \a mode, and \a + state, generating one if necessary. + + \sa actualSize(), paint() */ -QSize QIcon::actualSize(const QSize &size, Mode mode, State state) const +QPixmap QIcon::pixmap(const QSize &size, qreal devicePixelRatio, Mode mode, State state) const { if (!d) - return QSize(); - return actualSize(nullptr, size, mode, state); + return QPixmap(); + + // Use the global devicePixelRatio if the caller does not know the target dpr + if (devicePixelRatio == -1) + devicePixelRatio = qApp->devicePixelRatio(); + + // Handle the simple normal-dpi case + if (!(devicePixelRatio > 1.0)) { + QPixmap pixmap = d->engine->pixmap(size, mode, state); + pixmap.setDevicePixelRatio(1.0); + return pixmap; + } + + // Try get a pixmap that is big enough to be displayed at device pixel resolution. + QIconEngine::ScaledPixmapArgument scalePixmapArg = { size * devicePixelRatio, mode, state, devicePixelRatio, QPixmap() }; + d->engine->virtual_hook(QIconEngine::ScaledPixmapHook, reinterpret_cast(&scalePixmapArg)); + scalePixmapArg.pixmap.setDevicePixelRatio(d->pixmapDevicePixelRatio(devicePixelRatio, size, scalePixmapArg.pixmap.size())); + return scalePixmapArg.pixmap; } /*! @@ -881,19 +900,22 @@ QPixmap QIcon::pixmap(QWindow *window, const QSize &size, Mode mode, State state return QPixmap(); qreal devicePixelRatio = qt_effective_device_pixel_ratio(window); + return pixmap(size, devicePixelRatio, mode, state); +} - // Handle the simple normal-dpi case: - if (!(devicePixelRatio > 1.0)) { - QPixmap pixmap = d->engine->pixmap(size, mode, state); - pixmap.setDevicePixelRatio(1.0); - return pixmap; - } - // Try get a pixmap that is big enough to be displayed at device pixel resolution. - QIconEngine::ScaledPixmapArgument scalePixmapArg = { size * devicePixelRatio, mode, state, devicePixelRatio, QPixmap() }; - d->engine->virtual_hook(QIconEngine::ScaledPixmapHook, reinterpret_cast(&scalePixmapArg)); - scalePixmapArg.pixmap.setDevicePixelRatio(d->pixmapDevicePixelRatio(devicePixelRatio, size, scalePixmapArg.pixmap.size())); - return scalePixmapArg.pixmap; +/*! Returns the actual size of the icon for the requested \a size, \a + mode, and \a state. The result might be smaller than requested, but + never larger. The returned size is in device-independent pixels (This + is relevant for high-dpi pixmaps.) + + \sa pixmap(), paint() +*/ +QSize QIcon::actualSize(const QSize &size, Mode mode, State state) const +{ + if (!d) + return QSize(); + return actualSize(nullptr, size, mode, state); } /*! diff --git a/src/gui/image/qicon.h b/src/gui/image/qicon.h index 18c6f6d083..2ae9678e5a 100644 --- a/src/gui/image/qicon.h +++ b/src/gui/image/qicon.h @@ -82,6 +82,7 @@ public: { return pixmap(QSize(w, h), mode, state); } inline QPixmap pixmap(int extent, Mode mode = Normal, State state = Off) const { return pixmap(QSize(extent, extent), mode, state); } + QPixmap pixmap(const QSize &size, qreal devicePixelRatio, Mode mode = Normal, State state = Off) const; QPixmap pixmap(QWindow *window, const QSize &size, Mode mode = Normal, State state = Off) const; QSize actualSize(const QSize &size, Mode mode = Normal, State state = Off) const; -- cgit v1.2.3