From 1471f2ae0924e5d124178d1953a14db889348da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Sun, 21 Feb 2021 20:46:22 +0100 Subject: [PATCH] Add deviceIndependentSize() to QPixmap and QImage This function returns the size in device independent pixels, and should be used when calculating user interface sizes. Change-Id: I528123f962595a3da42438ca560289a29aca4917 Reviewed-by: Volker Hilsheimer --- src/gui/image/qimage.cpp | 20 ++++++++++++++++++-- src/gui/image/qimage.h | 1 + src/gui/image/qpixmap.cpp | 17 ++++++++++++++++- src/gui/image/qpixmap.h | 1 + tests/auto/gui/image/qimage/tst_qimage.cpp | 10 ++++++++++ tests/auto/gui/image/qpixmap/tst_qpixmap.cpp | 10 ++++++++++ 6 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp index 33c12993d71..fadfd5be73e 100644 --- a/src/gui/image/qimage.cpp +++ b/src/gui/image/qimage.cpp @@ -1316,7 +1316,7 @@ int QImage::height() const Returns the size of the image, i.e. its width() and height(). - \sa {QImage#Image Information}{Image Information} + \sa {QImage#Image Information}{Image Information}, deviceIndependentSize() */ QSize QImage::size() const { @@ -1449,7 +1449,7 @@ qreal QImage::devicePixelRatio() const high-DPI image rather than a large image (see \l{Drawing High Resolution Versions of Pixmaps and Images}). - \sa devicePixelRatio() + \sa devicePixelRatio(), deviceIndependentSize() */ void QImage::setDevicePixelRatio(qreal scaleFactor) { @@ -1464,6 +1464,22 @@ void QImage::setDevicePixelRatio(qreal scaleFactor) d->devicePixelRatio = scaleFactor; } +/*! + Returns the size of the pixmap in device independent pixels. + + This value should be used when using the pixmap size in user interface + size calculations. + + The return value is equivalent to pixmap.size() / pixmap.devicePixelRatio(), +*/ +QSizeF QImage::deviceIndependentSize() const +{ + if (!d) + return QSizeF(0, 0); + return QSizeF(d->width, d->height) / d->devicePixelRatio; +} + + /*! \since 5.10 Returns the image data size in bytes. diff --git a/src/gui/image/qimage.h b/src/gui/image/qimage.h index ae63c758f4e..59e07e7dd0c 100644 --- a/src/gui/image/qimage.h +++ b/src/gui/image/qimage.h @@ -219,6 +219,7 @@ public: qreal devicePixelRatio() const; void setDevicePixelRatio(qreal scaleFactor); + QSizeF deviceIndependentSize() const; void fill(uint pixel); void fill(const QColor &color); diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp index d8473001aad..2f9c4fd6176 100644 --- a/src/gui/image/qpixmap.cpp +++ b/src/gui/image/qpixmap.cpp @@ -633,7 +633,7 @@ qreal QPixmap::devicePixelRatio() const high-DPI pixmap rather than a large pixmap (see \l{Drawing High Resolution Versions of Pixmaps and Images}). - \sa devicePixelRatio() + \sa devicePixelRatio(), devicePixelSize() */ void QPixmap::setDevicePixelRatio(qreal scaleFactor) { @@ -647,6 +647,21 @@ void QPixmap::setDevicePixelRatio(qreal scaleFactor) data->setDevicePixelRatio(scaleFactor); } +/*! + Returns the size of the pixmap in device independent pixels. + + This value should be used when using the pixmap size in user interface + size calculations. + + The return value is equivalent to pixmap.size() / pixmap.devicePixelRatio(), +*/ +QSizeF QPixmap::deviceIndependentSize() const +{ + if (!data) + return QSizeF(0, 0); + return QSizeF(data->width(), data->height()) / data->devicePixelRatio(); +} + #ifndef QT_NO_IMAGE_HEURISTIC_MASK /*! Creates and returns a heuristic mask for this pixmap. diff --git a/src/gui/image/qpixmap.h b/src/gui/image/qpixmap.h index 42d4e271026..9d4d8e582d9 100644 --- a/src/gui/image/qpixmap.h +++ b/src/gui/image/qpixmap.h @@ -101,6 +101,7 @@ public: qreal devicePixelRatio() const; void setDevicePixelRatio(qreal scaleFactor); + QSizeF deviceIndependentSize() const; bool hasAlpha() const; bool hasAlphaChannel() const; diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index 1554ac36025..12345b2a977 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -212,6 +212,7 @@ private slots: void cleanupFunctions(); void devicePixelRatio(); + void deviceIndependentSize(); void rgb30Unpremul(); void rgb30Repremul_data(); void rgb30Repremul(); @@ -3473,6 +3474,15 @@ void tst_QImage::devicePixelRatio() QCOMPARE(b.devicePixelRatio(), qreal(1.0)); } +void tst_QImage::deviceIndependentSize() { + QImage a(64, 64, QImage::Format_ARGB32); + a.fill(Qt::white); + a.setDevicePixelRatio(1.0); + QCOMPARE(a.deviceIndependentSize(), QSizeF(64, 64)); + a.setDevicePixelRatio(2.0); + QCOMPARE(a.deviceIndependentSize(), QSizeF(32, 32)); +} + void tst_QImage::rgb30Unpremul() { QImage a(3, 1, QImage::Format_A2RGB30_Premultiplied); diff --git a/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp b/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp index e6123decb24..a70305bb147 100644 --- a/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp +++ b/tests/auto/gui/image/qpixmap/tst_qpixmap.cpp @@ -161,6 +161,7 @@ private slots: void copyOnNonAlignedBoundary(); void devicePixelRatio(); + void deviceIndependentSize(); private: const QString m_prefix; @@ -1678,5 +1679,14 @@ void tst_QPixmap::devicePixelRatio() QCOMPARE(b.devicePixelRatio(), qreal(1.0)); } +void tst_QPixmap::deviceIndependentSize() { + QPixmap a(64, 64); + a.fill(Qt::white); + a.setDevicePixelRatio(1.0); + QCOMPARE(a.deviceIndependentSize(), QSizeF(64, 64)); + a.setDevicePixelRatio(2.0); + QCOMPARE(a.deviceIndependentSize(), QSizeF(32, 32)); +} + QTEST_MAIN(tst_QPixmap) #include "tst_qpixmap.moc"