Partially revert "QLabel: cleanup QLabelPrivate"

This partially reverts commit 8bd532b1e.

Replacing the std::optional's with real objects led to unwanted
extra allocations, some even in the destructor.

Change-Id: Ib9f949b7d93352c9027ec8a162741d7830e9ec72
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 6f62f25daffafab750f84aec2b90e129e14c2cd9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
hjk 2024-08-02 12:02:55 +02:00 committed by Qt Cherry-pick Bot
parent 70d46c20f1
commit e2233061d2
2 changed files with 33 additions and 28 deletions

View File

@ -138,7 +138,9 @@ QLabelPrivate::~QLabelPrivate()
QPicture QLabel::picture() const
{
Q_D(const QLabel);
return d->picture;
if (d->picture)
return *(d->picture);
return QPicture();
}
#endif // QT_NO_PICTURE
@ -302,7 +304,7 @@ void QLabel::clear()
void QLabel::setPixmap(const QPixmap &pixmap)
{
Q_D(QLabel);
if (d->pixmap.cacheKey() != pixmap.cacheKey()) {
if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) {
d->clearContents();
d->pixmap = pixmap;
}
@ -313,7 +315,9 @@ void QLabel::setPixmap(const QPixmap &pixmap)
QPixmap QLabel::pixmap() const
{
Q_D(const QLabel);
return d->pixmap;
if (d->pixmap)
return *(d->pixmap);
return QPixmap();
}
/*!
@ -521,12 +525,12 @@ QSize QLabelPrivate::sizeForWidth(int w) const
int vextra = hextra;
QFontMetrics fm = q->fontMetrics();
if (!pixmap.isNull()) {
br = pixmap.rect();
br.setSize(pixmap.deviceIndependentSize().toSize());
if (pixmap && !pixmap->isNull()) {
br = pixmap->rect();
br.setSize(pixmap->deviceIndependentSize().toSize());
#ifndef QT_NO_PICTURE
} else if (!picture.isNull()) {
br = picture.boundingRect();
} else if (picture && !picture->isNull()) {
br = picture->boundingRect();
#endif
#if QT_CONFIG(movie)
} else if (movie && !movie->currentPixmap().isNull()) {
@ -1021,15 +1025,15 @@ void QLabel::paintEvent(QPaintEvent *)
}
} else
#ifndef QT_NO_PICTURE
if (!d->picture.isNull()) {
QRect br = d->picture.boundingRect();
if (d->picture) {
QRect br = d->picture->boundingRect();
int rw = br.width();
int rh = br.height();
if (d->scaledcontents) {
painter.save();
painter.translate(cr.x(), cr.y());
painter.scale((double)cr.width()/rw, (double)cr.height()/rh);
painter.drawPicture(-br.x(), -br.y(), d->picture);
painter.drawPicture(-br.x(), -br.y(), *d->picture);
painter.restore();
} else {
int xo = 0;
@ -1042,25 +1046,25 @@ void QLabel::paintEvent(QPaintEvent *)
xo = cr.width()-rw;
else if (align & Qt::AlignHCenter)
xo = (cr.width()-rw)/2;
painter.drawPicture(cr.x()+xo-br.x(), cr.y()+yo-br.y(), d->picture);
painter.drawPicture(cr.x()+xo-br.x(), cr.y()+yo-br.y(), *d->picture);
}
} else
#endif
if (!d->pixmap.isNull()) {
if (d->pixmap && !d->pixmap->isNull()) {
QPixmap pix;
const qreal dpr = devicePixelRatio();
if (d->scaledcontents || dpr != d->pixmap.devicePixelRatio()) {
if (d->scaledcontents || dpr != d->pixmap->devicePixelRatio()) {
QSize scaledSize = d->scaledcontents ? (cr.size() * dpr)
: (d->pixmap.size() * (dpr / d->pixmap.devicePixelRatio()));
if (d->scaledpixmap.size() != scaledSize) {
: (d->pixmap->size() * (dpr / d->pixmap->devicePixelRatio()));
if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) {
d->scaledpixmap =
d->pixmap.scaled(scaledSize,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
d->scaledpixmap.setDevicePixelRatio(dpr);
d->pixmap->scaled(scaledSize,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
d->scaledpixmap->setDevicePixelRatio(dpr);
}
pix = d->scaledpixmap;
pix = *d->scaledpixmap;
} else
pix = d->pixmap;
pix = *d->pixmap;
QStyleOption opt;
opt.initFrom(this);
if (!isEnabled())
@ -1260,10 +1264,10 @@ void QLabelPrivate::clearContents()
hasShortcut = false;
#ifndef QT_NO_PICTURE
picture = QPicture();
picture.reset();
#endif
scaledpixmap = QPixmap();
pixmap = QPixmap();
scaledpixmap.reset();
pixmap.reset();
text.clear();
Q_Q(QLabel);
@ -1407,7 +1411,7 @@ void QLabel::setScaledContents(bool enable)
return;
d->scaledcontents = enable;
if (!enable)
d->scaledpixmap = QPixmap();
d->scaledpixmap.reset();
update(contentsRect());
}

View File

@ -35,6 +35,7 @@
#include <QtCore/qpointer.h>
#include <array>
#include <optional>
QT_BEGIN_NAMESPACE
@ -84,10 +85,10 @@ public:
mutable QSize sh;
mutable QSize msh;
QString text;
QPixmap pixmap;
QPixmap scaledpixmap;
std::optional<QPixmap> pixmap;
std::optional<QPixmap> scaledpixmap;
#ifndef QT_NO_PICTURE
QPicture picture;
std::optional<QPicture> picture;
#endif
#if QT_CONFIG(movie)
QPointer<QMovie> movie;