QLabel: use QIcon instead QPixmap in internal data structure

... and let QIcon do all the scaling (and caching) stuff. This allows
us to pass a QIcon to QLabel.

Task-number: QTBUG-122403
Change-Id: I40f6338a38b75ddbdf909f98a8d18d6369d62acb
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Christian Ehrlicher 2024-06-16 21:02:29 +02:00
parent 02bf12072f
commit 81e74cfa26
2 changed files with 21 additions and 33 deletions

View File

@ -304,20 +304,18 @@ void QLabel::clear()
void QLabel::setPixmap(const QPixmap &pixmap)
{
Q_D(QLabel);
if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) {
d->clearContents();
d->pixmap = pixmap;
}
if (d->icon && d->icon->availableSizes().contains(pixmap.size()) &&
d->icon->pixmap(pixmap.size()).cacheKey() == pixmap.cacheKey())
return;
d->icon = QIcon(pixmap);
d->pixmapSize = pixmap.deviceIndependentSize().toSize();
d->updateLabel();
}
QPixmap QLabel::pixmap() const
{
Q_D(const QLabel);
if (d->pixmap)
return *(d->pixmap);
return QPixmap();
return d->icon ? d->icon->pixmap(d->pixmapSize) : QPixmap();
}
/*!
@ -525,9 +523,8 @@ QSize QLabelPrivate::sizeForWidth(int w) const
int vextra = hextra;
QFontMetrics fm = q->fontMetrics();
if (pixmap && !pixmap->isNull()) {
br = pixmap->rect();
br.setSize(pixmap->deviceIndependentSize().toSize());
if (icon && !icon->isNull()) {
br = QRect(QPoint(0, 0), pixmapSize);
#ifndef QT_NO_PICTURE
} else if (picture && !picture->isNull()) {
br = picture->boundingRect();
@ -1050,25 +1047,18 @@ void QLabel::paintEvent(QPaintEvent *)
}
} else
#endif
if (d->pixmap && !d->pixmap->isNull()) {
QPixmap pix;
if (d->icon && !d->icon->isNull()) {
const qreal dpr = 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 || d->scaledpixmap->size() != scaledSize) {
d->scaledpixmap =
d->pixmap->scaled(scaledSize,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
d->scaledpixmap->setDevicePixelRatio(dpr);
}
pix = *d->scaledpixmap;
} else
pix = *d->pixmap;
const QSize size = d->scaledcontents ? cr.size() : d->pixmapSize;
const auto mode = isEnabled() ? QIcon::Normal : QIcon::Disabled;
QPixmap pix = d->icon->pixmap(size, dpr, mode);
if (d->scaledcontents && pix.size() != size * dpr) {
pix = pix.scaled(size * dpr, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
pix.setDevicePixelRatio(dpr);
d->icon->addPixmap(pix, mode);
}
QStyleOption opt;
opt.initFrom(this);
if (!isEnabled())
pix = style->generatedIconPixmap(QIcon::Disabled, pix, &opt);
style->drawItemPixmap(&painter, cr, align, pix);
}
}
@ -1266,8 +1256,8 @@ void QLabelPrivate::clearContents()
#ifndef QT_NO_PICTURE
picture.reset();
#endif
scaledpixmap.reset();
pixmap.reset();
icon.reset();
pixmapSize = QSize();
text.clear();
Q_Q(QLabel);
@ -1410,8 +1400,6 @@ void QLabel::setScaledContents(bool enable)
if ((bool)d->scaledcontents == enable)
return;
d->scaledcontents = enable;
if (!enable)
d->scaledpixmap.reset();
update(contentsRect());
}

View File

@ -85,8 +85,8 @@ public:
mutable QSize sh;
mutable QSize msh;
QString text;
std::optional<QPixmap> pixmap;
std::optional<QPixmap> scaledpixmap;
std::optional<QIcon> icon;
QSize pixmapSize;
#ifndef QT_NO_PICTURE
std::optional<QPicture> picture;
#endif