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>
bb10
Morten Johan Sørvig 2021-02-21 20:46:22 +01:00 committed by Volker Hilsheimer
parent 4f37cf2ce5
commit 1471f2ae09
6 changed files with 56 additions and 3 deletions

View File

@ -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.

View File

@ -219,6 +219,7 @@ public:
qreal devicePixelRatio() const;
void setDevicePixelRatio(qreal scaleFactor);
QSizeF deviceIndependentSize() const;
void fill(uint pixel);
void fill(const QColor &color);

View File

@ -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.

View File

@ -101,6 +101,7 @@ public:
qreal devicePixelRatio() const;
void setDevicePixelRatio(qreal scaleFactor);
QSizeF deviceIndependentSize() const;
bool hasAlpha() const;
bool hasAlphaChannel() const;

View File

@ -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);

View File

@ -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"