summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorten Johan Sørvig <morten.sorvig@qt.io>2021-02-21 20:46:22 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2021-03-08 08:27:41 +0000
commit1471f2ae0924e5d124178d1953a14db889348da0 (patch)
treedef325a49c4caced38619631008b2ccc62c02967
parent4f37cf2ce544dad5b207161536566627b6b0b1ef (diff)
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 <volker.hilsheimer@qt.io>
-rw-r--r--src/gui/image/qimage.cpp20
-rw-r--r--src/gui/image/qimage.h1
-rw-r--r--src/gui/image/qpixmap.cpp17
-rw-r--r--src/gui/image/qpixmap.h1
-rw-r--r--tests/auto/gui/image/qimage/tst_qimage.cpp10
-rw-r--r--tests/auto/gui/image/qpixmap/tst_qpixmap.cpp10
6 files changed, 56 insertions, 3 deletions
diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp
index 33c12993d7..fadfd5be73 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)
{
@@ -1465,6 +1465,22 @@ void QImage::setDevicePixelRatio(qreal 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 ae63c758f4..59e07e7dd0 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 d8473001aa..2f9c4fd617 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 42d4e27102..9d4d8e582d 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 1554ac3602..12345b2a97 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 e6123decb2..a70305bb14 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"