Skip to content

Commit

Permalink
Port to QImage and QPixmap deviceIndependentSize()
Browse files Browse the repository at this point in the history
Replace the “size() / devicePixelRatio()” pattern with
a call to deviceIndependentSize().

Change-Id: I9d9359e80b9e6643e7395028cd43e3261d449ae7
Reviewed-by: Qt CI Bot <[email protected]>
Reviewed-by: Tor Arne Vestbø <[email protected]>
  • Loading branch information
Morten Sørvig committed Sep 1, 2021
1 parent 4e460aa commit 81a7344
Show file tree
Hide file tree
Showing 20 changed files with 50 additions and 60 deletions.
4 changes: 2 additions & 2 deletions examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
//! [7] //! [8]
auto previewPixmap = qFuzzyCompare(pixmap.devicePixelRatio(), qreal(1))
? pixmap
: pixmap.scaled(pixmap.size() / pixmap.devicePixelRatio(), Qt::KeepAspectRatio,
: pixmap.scaled(pixmap.deviceIndependentSize().toSize(), Qt::KeepAspectRatio,
Qt::SmoothTransformation);
double scaleFactor = pixmapScale / curScale;
int newWidth = int(previewPixmap.width() * scaleFactor);
Expand Down Expand Up @@ -216,7 +216,7 @@ void MandelbrotWidget::mouseReleaseEvent(QMouseEvent *event)
pixmapOffset += event->position().toPoint() - lastDragPos;
lastDragPos = QPoint();

const auto pixmapSize = pixmap.size() / pixmap.devicePixelRatio();
const auto pixmapSize = pixmap.deviceIndependentSize().toSize();
int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x();
int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y();
scroll(deltaX, deltaY);
Expand Down
4 changes: 1 addition & 3 deletions src/gui/kernel/qshapedpixmapdndwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ void QShapedPixmapWindow::updateGeometry(const QPoint &pos)
{
QSize size(1, 1);
if (!m_pixmap.isNull()) {
size = qFuzzyCompare(m_pixmap.devicePixelRatio(), qreal(1.0))
? m_pixmap.size()
: (QSizeF(m_pixmap.size()) / m_pixmap.devicePixelRatio()).toSize();
size = m_pixmap.deviceIndependentSize().toSize();
}
setGeometry(QRect(pos - m_hotSpot, size));
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/painting/qcoregraphics.mm
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ + (instancetype)imageFromQImage:(const QImage &)image
// NSImage.
auto nsImage = [[NSImage alloc] initWithSize:NSZeroSize];
auto *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
imageRep.size = (image.size() / image.devicePixelRatio()).toCGSize();
imageRep.size = image.deviceIndependentSize().toCGSize();
[nsImage addRepresentation:[imageRep autorelease]];
Q_ASSERT(CGSizeEqualToSize(nsImage.size, imageRep.size));

Expand Down Expand Up @@ -178,7 +178,7 @@ + (instancetype)imageFromQIcon:(const QIcon &)icon withSize:(int)size
continue;

auto *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
imageRep.size = (image.size() / image.devicePixelRatio()).toCGSize();
imageRep.size = image.deviceIndependentSize().toCGSize();
[nsImage addRepresentation:[imageRep autorelease]];
}

Expand Down
4 changes: 2 additions & 2 deletions src/gui/painting/qpaintengineex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,12 +967,12 @@ void QPaintEngineEx::drawPolygon(const QPoint *points, int pointCount, PolygonDr

void QPaintEngineEx::drawPixmap(const QPointF &pos, const QPixmap &pm)
{
drawPixmap(QRectF(pos, pm.size() / pm.devicePixelRatio()), pm, pm.rect());
drawPixmap(QRectF(pos, pm.deviceIndependentSize()), pm, pm.rect());
}

void QPaintEngineEx::drawImage(const QPointF &pos, const QImage &image)
{
drawImage(QRectF(pos, image.size() / image.devicePixelRatio()), image, image.rect());
drawImage(QRectF(pos, image.deviceIndependentSize()), image, image.rect());
}

void QPaintEngineEx::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
Expand Down
16 changes: 8 additions & 8 deletions src/gui/text/qtextimagehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,19 @@ static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format)
QSize size(width, height);
if (!hasWidth || !hasHeight) {
pm = getPixmap(doc, format);
const int pmWidth = pm.width() / pm.devicePixelRatio();
const int pmHeight = pm.height() / pm.devicePixelRatio();
const QSizeF pmSize = pm.deviceIndependentSize();

if (!hasWidth) {
if (!hasHeight)
size.setWidth(pmWidth);
size.setWidth(pmSize.width());
else
size.setWidth(qRound(height * (pmWidth / (qreal) pmHeight)));
size.setWidth(qRound(height * (pmSize.width() / (qreal) pmSize.height())));
}
if (!hasHeight) {
if (!hasWidth)
size.setHeight(pmHeight);
size.setHeight(pmSize.height());
else
size.setHeight(qRound(width * (pmHeight / (qreal) pmWidth)));
size.setHeight(qRound(width * (pmSize.height() / (qreal) pmSize.width())));
}
}

Expand Down Expand Up @@ -171,10 +170,11 @@ static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format)
QSize size(width, height);
if (!hasWidth || !hasHeight) {
image = getImage(doc, format);
QSizeF imageSize = image.deviceIndependentSize();
if (!hasWidth)
size.setWidth(image.width() / image.devicePixelRatio());
size.setWidth(imageSize.width());
if (!hasHeight)
size.setHeight(image.height() / image.devicePixelRatio());
size.setHeight(imageSize.height());
}

qreal scale = 1.0;
Expand Down
26 changes: 13 additions & 13 deletions src/plugins/styles/mac/qmacstyle_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3568,9 +3568,10 @@ static QPixmap darkenPixmap(const QPixmap &pixmap)
QPixmap pixmap = header->icon.pixmap(QSize(iconExtent, iconExtent), p->device()->devicePixelRatio(), mode);

QRect pixr = header->rect;
pixr.setY(header->rect.center().y() - (pixmap.height() / pixmap.devicePixelRatio() - 1) / 2);
QSizeF size = pixmap.deviceIndependentSize();
pixr.setY(header->rect.center().y() - (size.height() - 1) / 2);
proxy()->drawItemPixmap(p, pixr, Qt::AlignVCenter, pixmap);
textr.translate(pixmap.width() / pixmap.devicePixelRatio() + 2, 0);
textr.translate(size.width() + 2, 0);
}
QString text = header->text;
if (const QStyleOptionHeaderV2 *headerV2 = qstyleoption_cast<const QStyleOptionHeaderV2 *>(header)) {
Expand Down Expand Up @@ -3627,12 +3628,13 @@ static QPixmap darkenPixmap(const QPixmap &pixmap)
// Draw the text if it's needed.
if (tb->toolButtonStyle != Qt::ToolButtonIconOnly) {
needText = true;
QSizeF size = pixmap.deviceIndependentSize();
if (tb->toolButtonStyle == Qt::ToolButtonTextUnderIcon) {
pr.setHeight(pixmap.size().height() / pixmap.devicePixelRatio() + 6);
pr.setHeight(size.height() + 6);
cr.adjust(0, pr.bottom(), 0, -3);
alignment |= Qt::AlignCenter;
} else {
pr.setWidth(pixmap.width() / pixmap.devicePixelRatio() + 8);
pr.setWidth(size.width() + 8);
cr.adjust(pr.right(), 0, 0, 0);
alignment |= Qt::AlignLeft | Qt::AlignVCenter;
}
Expand Down Expand Up @@ -3812,12 +3814,11 @@ static QPixmap darkenPixmap(const QPixmap &pixmap)
if (btn.state & State_On)
state = QIcon::On;
QPixmap pixmap = btn.icon.pixmap(btn.iconSize, p->device()->devicePixelRatio(), mode, state);
int pixmapWidth = pixmap.width() / pixmap.devicePixelRatio();
int pixmapHeight = pixmap.height() / pixmap.devicePixelRatio();
contentW += pixmapWidth + QMacStylePrivate::PushButtonContentPadding;
QSizeF pixmapSize = pixmap.deviceIndependentSize();
contentW += pixmapSize.width() + QMacStylePrivate::PushButtonContentPadding;
int iconLeftOffset = freeContentRect.x() + (freeContentRect.width() - contentW) / 2;
int iconTopOffset = freeContentRect.y() + (freeContentRect.height() - pixmapHeight) / 2;
QRect iconDestRect(iconLeftOffset, iconTopOffset, pixmapWidth, pixmapHeight);
int iconTopOffset = freeContentRect.y() + (freeContentRect.height() - pixmapSize.height()) / 2;
QRect iconDestRect(iconLeftOffset, iconTopOffset, pixmapSize.width(), pixmapSize.height());
QRect visualIconDestRect = visualRect(btn.direction, freeContentRect, iconDestRect);
proxy()->drawItemPixmap(p, visualIconDestRect, Qt::AlignLeft | Qt::AlignVCenter, pixmap);
int newOffset = iconDestRect.x() + iconDestRect.width()
Expand Down Expand Up @@ -4314,13 +4315,12 @@ static QPixmap darkenPixmap(const QPixmap &pixmap)
}
#endif
QPixmap pixmap = mi->icon.pixmap(iconSize, p->device()->devicePixelRatio(), mode);
int pixw = pixmap.width() / pixmap.devicePixelRatio();
int pixh = pixmap.height() / pixmap.devicePixelRatio();
QRect cr(xpos, mi->rect.y(), checkcol, mi->rect.height());
QRect pmr(0, 0, pixw, pixh);
QSize size = pixmap.deviceIndependentSize().toSize();
QRect pmr(QPoint(0, 0), size);
pmr.moveCenter(cr.center());
p->drawPixmap(pmr.topLeft(), pixmap);
xpos += pixw + 6;
xpos += size.width() + 6;
}

QString s = mi->text;
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/styles/windowsvista/qwindowsvistastyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,9 +1273,7 @@ void QWindowsVistaStyle::drawControl(ControlElement element, const QStyleOption
pixmap = menuitem->icon.pixmap(proxy()->pixelMetric(PM_SmallIconSize, option, widget), mode, QIcon::On);
else
pixmap = menuitem->icon.pixmap(proxy()->pixelMetric(PM_SmallIconSize, option, widget), mode);
const int pixw = pixmap.width() / pixmap.devicePixelRatio();
const int pixh = pixmap.height() / pixmap.devicePixelRatio();
QRect pmr(0, 0, pixw, pixh);
QRect pmr(QPoint(0, 0), pixmap.deviceIndependentSize().toSize());
pmr.moveCenter(vCheckRect.center());
painter->setPen(menuitem->palette.text().color());
painter->drawPixmap(pmr.topLeft(), pixmap);
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/styles/windowsvista/qwindowsxpstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1974,9 +1974,7 @@ void QWindowsXPStyle::drawControl(ControlElement element, const QStyleOption *op
QPixmap pixmap = checked ?
menuitem->icon.pixmap(proxy()->pixelMetric(PM_SmallIconSize, option, widget), mode, QIcon::On) :
menuitem->icon.pixmap(proxy()->pixelMetric(PM_SmallIconSize, option, widget), mode);
const int pixw = pixmap.width() / pixmap.devicePixelRatio();
const int pixh = pixmap.height() / pixmap.devicePixelRatio();
QRect iconRect(0, 0, pixw, pixh);
QRect iconRect(QPoint(0, 0), pixmap.deviceIndependentSize().toSize());
iconRect.moveCenter(QRect(xpos, y, checkcol, h).center());
QRect vIconRect = visualRect(option->direction, option->rect, iconRect);
p->setPen(menuitem->palette.text().color());
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/dialogs/qwizard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class QWatermarkLabel : public QLabel

QSize minimumSizeHint() const override {
if (!pixmap(Qt::ReturnByValue).isNull())
return pixmap(Qt::ReturnByValue).size() / pixmap(Qt::ReturnByValue).devicePixelRatio();
return pixmap(Qt::ReturnByValue).deviceIndependentSize().toSize();
return QFrame::minimumSizeHint();
}

Expand Down
2 changes: 1 addition & 1 deletion src/widgets/effects/qpixmapfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ Q_WIDGETS_EXPORT void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius,
if (p) {
p->scale(scale, scale);
p->setRenderHint(QPainter::SmoothPixmapTransform);
p->drawImage(QRect(QPoint(0, 0), blurImage.size() / blurImage.devicePixelRatio()), blurImage);
p->drawImage(QRect(QPoint(0, 0), blurImage.deviceIndependentSize().toSize()), blurImage);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/widgets/graphicsview/qgraphicsitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9585,9 +9585,9 @@ QRectF QGraphicsPixmapItem::boundingRect() const
return QRectF();
if (d->flags & ItemIsSelectable) {
qreal pw = 1.0;
return QRectF(d->offset, QSizeF(d->pixmap.size()) / d->pixmap.devicePixelRatio()).adjusted(-pw/2, -pw/2, pw/2, pw/2);
return QRectF(d->offset, d->pixmap.deviceIndependentSize()).adjusted(-pw/2, -pw/2, pw/2, pw/2);
} else {
return QRectF(d->offset, QSizeF(d->pixmap.size()) / d->pixmap.devicePixelRatio());
return QRectF(d->offset, d->pixmap.deviceIndependentSize());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/widgets/itemviews/qitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,10 @@ QRect QItemDelegate::rect(const QStyleOptionViewItem &option,
break;
case QMetaType::QPixmap: {
const QPixmap &pixmap = qvariant_cast<QPixmap>(value);
return QRect(QPoint(0, 0), pixmap.size() / pixmap.devicePixelRatio() ); }
return QRect(QPoint(0, 0), pixmap.deviceIndependentSize().toSize()); }
case QMetaType::QImage: {
const QImage &image = qvariant_cast<QImage>(value);
return QRect(QPoint(0, 0), image.size() / image.devicePixelRatio() ); }
return QRect(QPoint(0, 0), image.deviceIndependentSize().toSize()); }
case QMetaType::QIcon: {
QIcon::Mode mode = d->iconMode(option.state);
QIcon::State state = d->iconState(option.state);
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/itemviews/qstyleditemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ void QStyledItemDelegate::initStyleOption(QStyleOptionViewItem *option,
case QMetaType::QImage: {
QImage image = qvariant_cast<QImage>(*value);
option->icon = QIcon(QPixmap::fromImage(image));
option->decorationSize = image.size() / image.devicePixelRatio();
option->decorationSize = image.deviceIndependentSize().toSize();
break;
}
case QMetaType::QPixmap: {
QPixmap pixmap = qvariant_cast<QPixmap>(*value);
option->icon = QIcon(pixmap);
option->decorationSize = pixmap.size() / pixmap.devicePixelRatio();
option->decorationSize = pixmap.deviceIndependentSize().toSize();
break;
}
default:
Expand Down
5 changes: 1 addition & 4 deletions src/widgets/styles/qfusionstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,10 +1652,7 @@ void QFusionStyle::drawControl(ControlElement element, const QStyleOption *optio
else
pixmap = menuItem->icon.pixmap(iconSize, painter->device()->devicePixelRatio(), mode);

const int pixw = pixmap.width() / pixmap.devicePixelRatio();
const int pixh = pixmap.height() / pixmap.devicePixelRatio();

QRect pmr(0, 0, pixw, pixh);
QRect pmr(QPoint(0, 0), pixmap.deviceIndependentSize().toSize());
pmr.moveCenter(vCheckRect.center());
painter->setPen(menuItem->palette.text().color());
if (!ignoreCheckMark && checkable && checked) {
Expand Down
5 changes: 3 additions & 2 deletions src/widgets/styles/qstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,9 @@ QRect QStyle::itemPixmapRect(const QRect &rect, int alignment, const QPixmap &pi
int x, y, w, h;
rect.getRect(&x, &y, &w, &h);

const int pixmapWidth = pixmap.width()/pixmap.devicePixelRatio();
const int pixmapHeight = pixmap.height()/pixmap.devicePixelRatio();
QSizeF pixmapSize = pixmap.deviceIndependentSize();
const int pixmapWidth = pixmapSize.width();
const int pixmapHeight = pixmapSize.height();

if ((alignment & Qt::AlignVCenter) == Qt::AlignVCenter)
y += h/2 - pixmapHeight/2;
Expand Down
4 changes: 1 addition & 3 deletions src/widgets/styles/qwindowsstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,9 +1157,7 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai
pixmap = menuitem->icon.pixmap(proxy()->pixelMetric(PM_SmallIconSize, opt, widget), mode, QIcon::On);
else
pixmap = menuitem->icon.pixmap(proxy()->pixelMetric(PM_SmallIconSize, opt, widget), mode);
const int pixw = pixmap.width() / pixmap.devicePixelRatio();
const int pixh = pixmap.height() / pixmap.devicePixelRatio();
QRect pmr(0, 0, pixw, pixh);
QRect pmr(QPoint(0, 0), pixmap.deviceIndependentSize().toSize());
pmr.moveCenter(vCheckRect.center());
p->setPen(menuitem->palette.text().color());
p->drawPixmap(pmr.topLeft(), pixmap);
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/widgets/qlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,15 +604,15 @@ QSize QLabelPrivate::sizeForWidth(int w) const

if (pixmap && !pixmap->isNull()) {
br = pixmap->rect();
br.setSize(br.size() / pixmap->devicePixelRatio());
br.setSize(pixmap->deviceIndependentSize().toSize());
#ifndef QT_NO_PICTURE
} else if (picture && !picture->isNull()) {
br = picture->boundingRect();
#endif
#if QT_CONFIG(movie)
} else if (movie && !movie->currentPixmap().isNull()) {
br = movie->currentPixmap().rect();
br.setSize(br.size() / movie->currentPixmap().devicePixelRatio());
br.setSize(movie->currentPixmap().deviceIndependentSize().toSize());
#endif
} else if (isTextLabel) {
int align = QStyle::visualAlignment(textDirection(), QFlag(this->align));
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/widgets/qmdisubwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,15 @@ ControlLabel::ControlLabel(QMdiSubWindow *subWindow, QWidget *parent)
Q_UNUSED(subWindow);
setFocusPolicy(Qt::NoFocus);
updateWindowIcon();
setFixedSize(label.size() / label.devicePixelRatio());
setFixedSize(label.deviceIndependentSize().toSize());
}

/*
\internal
*/
QSize ControlLabel::sizeHint() const
{
return label.size() / label.devicePixelRatio();
return label.deviceIndependentSize().toSize();
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/widgets/qsplashscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void QSplashScreen::setPixmap(const QPixmap &pixmap)
d->pixmap = pixmap;
setAttribute(Qt::WA_TranslucentBackground, pixmap.hasAlpha());

const QRect r(QPoint(), pixmap.size() / pixmap.devicePixelRatio());
const QRect r(QPoint(), pixmap.deviceIndependentSize().toSize());
resize(r.size());

move(screen()->geometry().center() - r.center());
Expand Down
4 changes: 2 additions & 2 deletions tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ void tst_QLabel::taskQTBUG_48157_dprPixmap()
pixmap.load(QFINDTESTDATA(QStringLiteral("[email protected]")));
QCOMPARE(pixmap.devicePixelRatio(), 2.0);
label.setPixmap(pixmap);
QCOMPARE(label.sizeHint(), pixmap.rect().size() / pixmap.devicePixelRatio());
QCOMPARE(label.sizeHint(), pixmap.deviceIndependentSize().toSize());
}

void tst_QLabel::taskQTBUG_48157_dprMovie()
Expand All @@ -595,7 +595,7 @@ void tst_QLabel::taskQTBUG_48157_dprMovie()
movie.start();
QCOMPARE(movie.currentPixmap().devicePixelRatio(), 2.0);
label.setMovie(&movie);
QCOMPARE(label.sizeHint(), movie.currentPixmap().size() / movie.currentPixmap().devicePixelRatio());
QCOMPARE(label.sizeHint(), movie.currentPixmap().deviceIndependentSize().toSize());
}

void tst_QLabel::resourceProvider()
Expand Down

0 comments on commit 81a7344

Please sign in to comment.