Port to QImage and QPixmap deviceIndependentSize()
Replace the “size() / devicePixelRatio()” pattern with a call to deviceIndependentSize(). Change-Id: I9d9359e80b9e6643e7395028cd43e3261d449ae7 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
parent
4e460aa3f7
commit
81a7344e1d
@ -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);
|
||||
@ -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);
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ QT_END_NAMESPACE
|
||||
// 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));
|
||||
|
||||
@ -178,7 +178,7 @@ QT_END_NAMESPACE
|
||||
continue;
|
||||
|
||||
auto *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
|
||||
imageRep.size = (image.size() / image.devicePixelRatio()).toCGSize();
|
||||
imageRep.size = image.deviceIndependentSize().toCGSize();
|
||||
[nsImage addRepresentation:[imageRep autorelease]];
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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())));
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
@ -3568,9 +3568,10 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
|
||||
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)) {
|
||||
@ -3627,12 +3628,13 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
|
||||
// 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;
|
||||
}
|
||||
@ -3812,12 +3814,11 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
|
||||
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()
|
||||
@ -4314,13 +4315,12 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
|
||||
}
|
||||
#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;
|
||||
|
@ -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);
|
||||
|
@ -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());
|
||||
|
@ -452,7 +452,7 @@ public:
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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:
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -604,7 +604,7 @@ 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();
|
||||
@ -612,7 +612,7 @@ QSize QLabelPrivate::sizeForWidth(int w) const
|
||||
#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));
|
||||
|
@ -422,7 +422,7 @@ ControlLabel::ControlLabel(QMdiSubWindow *subWindow, QWidget *parent)
|
||||
Q_UNUSED(subWindow);
|
||||
setFocusPolicy(Qt::NoFocus);
|
||||
updateWindowIcon();
|
||||
setFixedSize(label.size() / label.devicePixelRatio());
|
||||
setFixedSize(label.deviceIndependentSize().toSize());
|
||||
}
|
||||
|
||||
/*
|
||||
@ -430,7 +430,7 @@ ControlLabel::ControlLabel(QMdiSubWindow *subWindow, QWidget *parent)
|
||||
*/
|
||||
QSize ControlLabel::sizeHint() const
|
||||
{
|
||||
return label.size() / label.devicePixelRatio();
|
||||
return label.deviceIndependentSize().toSize();
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -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());
|
||||
|
@ -584,7 +584,7 @@ void tst_QLabel::taskQTBUG_48157_dprPixmap()
|
||||
pixmap.load(QFINDTESTDATA(QStringLiteral("red@2x.png")));
|
||||
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()
|
||||
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user