QLabel: cleanup QLabelPrivate

No need for std::optional<> here - simply use QPixmap/QPicture::isNull()
instead. Also use non-static member initialization instead
initialization list and cleanup the needed includes.

Change-Id: I255e9e068cff1e66fbaf35be51be64228e48ffac
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 8bd532b1e54e220c81cd742fabaa236b1e7679c4)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2024-06-19 21:09:24 +02:00 committed by Qt Cherry-pick Bot
parent 3852a6a779
commit ed5435877c
2 changed files with 46 additions and 91 deletions

View File

@ -3,21 +3,15 @@
#include "qpainter.h" #include "qpainter.h"
#include "qevent.h" #include "qevent.h"
#include "qdrawutil.h"
#include "qapplication.h"
#if QT_CONFIG(abstractbutton)
#include "qabstractbutton.h"
#endif
#include "qstyle.h" #include "qstyle.h"
#include "qstyleoption.h" #include "qstyleoption.h"
#include <limits.h>
#include "qclipboard.h"
#include <qdebug.h>
#include <qurl.h>
#include "qlabel_p.h" #include "qlabel_p.h"
#include "private/qstylesheetstyle_p.h" #include "private/qstylesheetstyle_p.h"
#include <qmath.h> #include <qmath.h>
#if QT_CONFIG(abstractbutton)
#include "qabstractbutton.h"
#endif
#if QT_CONFIG(accessibility) #if QT_CONFIG(accessibility)
#include <qaccessible.h> #include <qaccessible.h>
#endif #endif
@ -28,45 +22,17 @@ using namespace Qt::StringLiterals;
QLabelPrivate::QLabelPrivate() QLabelPrivate::QLabelPrivate()
: QFramePrivate(), : QFramePrivate(),
sh(),
msh(),
text(),
pixmap(),
scaledpixmap(),
#ifndef QT_NO_PICTURE
picture(),
#endif
#if QT_CONFIG(movie)
movie(),
#endif
control(nullptr),
shortcutCursor(),
#ifndef QT_NO_CURSOR
cursor(),
#endif
#ifndef QT_NO_SHORTCUT
buddy(),
shortcutId(0),
#endif
textformat(Qt::AutoText),
effectiveTextFormat(Qt::PlainText),
textInteractionFlags(Qt::LinksAccessibleByMouse),
sizePolicy(),
margin(0),
align(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextExpandTabs),
indent(-1),
valid_hints(false), valid_hints(false),
scaledcontents(false), scaledcontents(false),
textLayoutDirty(false), textLayoutDirty(false),
textDirty(false), textDirty(false),
isTextLabel(false), isTextLabel(false),
hasShortcut(/*???*/), hasShortcut(false),
#ifndef QT_NO_CURSOR #ifndef QT_NO_CURSOR
validCursor(false), validCursor(false),
onAnchor(false), onAnchor(false),
#endif #endif
openExternalLinks(false), openExternalLinks(false)
resourceProvider(nullptr)
{ {
} }
@ -172,9 +138,7 @@ QLabelPrivate::~QLabelPrivate()
QPicture QLabel::picture() const QPicture QLabel::picture() const
{ {
Q_D(const QLabel); Q_D(const QLabel);
if (d->picture) return d->picture;
return *(d->picture);
return QPicture();
} }
#endif // QT_NO_PICTURE #endif // QT_NO_PICTURE
@ -338,7 +302,7 @@ void QLabel::clear()
void QLabel::setPixmap(const QPixmap &pixmap) void QLabel::setPixmap(const QPixmap &pixmap)
{ {
Q_D(QLabel); Q_D(QLabel);
if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) { if (d->pixmap.cacheKey() != pixmap.cacheKey()) {
d->clearContents(); d->clearContents();
d->pixmap = pixmap; d->pixmap = pixmap;
} }
@ -349,9 +313,7 @@ void QLabel::setPixmap(const QPixmap &pixmap)
QPixmap QLabel::pixmap() const QPixmap QLabel::pixmap() const
{ {
Q_D(const QLabel); Q_D(const QLabel);
if (d->pixmap) return d->pixmap;
return *(d->pixmap);
return QPixmap();
} }
/*! /*!
@ -404,9 +366,7 @@ void QLabel::setPicture(const QPicture &picture)
void QLabel::setNum(int num) void QLabel::setNum(int num)
{ {
QString str; setText(QString::number(num));
str.setNum(num);
setText(str);
} }
/*! /*!
@ -424,9 +384,7 @@ void QLabel::setNum(int num)
void QLabel::setNum(double num) void QLabel::setNum(double num)
{ {
QString str; setText(QString::number(num));
str.setNum(num);
setText(str);
} }
/*! /*!
@ -563,12 +521,12 @@ QSize QLabelPrivate::sizeForWidth(int w) const
int vextra = hextra; int vextra = hextra;
QFontMetrics fm = q->fontMetrics(); QFontMetrics fm = q->fontMetrics();
if (pixmap && !pixmap->isNull()) { if (!pixmap.isNull()) {
br = pixmap->rect(); br = pixmap.rect();
br.setSize(pixmap->deviceIndependentSize().toSize()); br.setSize(pixmap.deviceIndependentSize().toSize());
#ifndef QT_NO_PICTURE #ifndef QT_NO_PICTURE
} else if (picture && !picture->isNull()) { } else if (!picture.isNull()) {
br = picture->boundingRect(); br = picture.boundingRect();
#endif #endif
#if QT_CONFIG(movie) #if QT_CONFIG(movie)
} else if (movie && !movie->currentPixmap().isNull()) { } else if (movie && !movie->currentPixmap().isNull()) {
@ -1063,15 +1021,15 @@ void QLabel::paintEvent(QPaintEvent *)
} }
} else } else
#ifndef QT_NO_PICTURE #ifndef QT_NO_PICTURE
if (d->picture) { if (!d->picture.isNull()) {
QRect br = d->picture->boundingRect(); QRect br = d->picture.boundingRect();
int rw = br.width(); int rw = br.width();
int rh = br.height(); int rh = br.height();
if (d->scaledcontents) { if (d->scaledcontents) {
painter.save(); painter.save();
painter.translate(cr.x(), cr.y()); painter.translate(cr.x(), cr.y());
painter.scale((double)cr.width()/rw, (double)cr.height()/rh); 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(); painter.restore();
} else { } else {
int xo = 0; int xo = 0;
@ -1084,25 +1042,25 @@ void QLabel::paintEvent(QPaintEvent *)
xo = cr.width()-rw; xo = cr.width()-rw;
else if (align & Qt::AlignHCenter) else if (align & Qt::AlignHCenter)
xo = (cr.width()-rw)/2; 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 } else
#endif #endif
if (d->pixmap && !d->pixmap->isNull()) { if (!d->pixmap.isNull()) {
QPixmap pix; QPixmap pix;
const qreal dpr = devicePixelRatio(); 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) QSize scaledSize = d->scaledcontents ? (cr.size() * dpr)
: (d->pixmap->size() * (dpr / d->pixmap->devicePixelRatio())); : (d->pixmap.size() * (dpr / d->pixmap.devicePixelRatio()));
if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) { if (d->scaledpixmap.size() != scaledSize) {
d->scaledpixmap = d->scaledpixmap =
d->pixmap->scaled(scaledSize, d->pixmap.scaled(scaledSize,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation); Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
d->scaledpixmap->setDevicePixelRatio(dpr); d->scaledpixmap.setDevicePixelRatio(dpr);
} }
pix = *d->scaledpixmap; pix = d->scaledpixmap;
} else } else
pix = *d->pixmap; pix = d->pixmap;
QStyleOption opt; QStyleOption opt;
opt.initFrom(this); opt.initFrom(this);
if (!isEnabled()) if (!isEnabled())
@ -1302,10 +1260,10 @@ void QLabelPrivate::clearContents()
hasShortcut = false; hasShortcut = false;
#ifndef QT_NO_PICTURE #ifndef QT_NO_PICTURE
picture.reset(); picture = QPicture();
#endif #endif
scaledpixmap.reset(); scaledpixmap = QPixmap();
pixmap.reset(); pixmap = QPixmap();
text.clear(); text.clear();
Q_Q(QLabel); Q_Q(QLabel);
@ -1448,9 +1406,8 @@ void QLabel::setScaledContents(bool enable)
if ((bool)d->scaledcontents == enable) if ((bool)d->scaledcontents == enable)
return; return;
d->scaledcontents = enable; d->scaledcontents = enable;
if (!enable) { if (!enable)
d->scaledpixmap.reset(); d->scaledpixmap = QPixmap();
}
update(contentsRect()); update(contentsRect());
} }

View File

@ -26,9 +26,8 @@
#if QT_CONFIG(movie) #if QT_CONFIG(movie)
#include "qmovie.h" #include "qmovie.h"
#endif #endif
#include "qimage.h"
#include "qbitmap.h"
#include "qpicture.h" #include "qpicture.h"
#include "qpixmap.h"
#if QT_CONFIG(menu) #if QT_CONFIG(menu)
#include "qmenu.h" #include "qmenu.h"
#endif #endif
@ -36,7 +35,6 @@
#include <QtCore/qpointer.h> #include <QtCore/qpointer.h>
#include <array> #include <array>
#include <optional>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -86,31 +84,31 @@ public:
mutable QSize sh; mutable QSize sh;
mutable QSize msh; mutable QSize msh;
QString text; QString text;
std::optional<QPixmap> pixmap; QPixmap pixmap;
std::optional<QPixmap> scaledpixmap; QPixmap scaledpixmap;
#ifndef QT_NO_PICTURE #ifndef QT_NO_PICTURE
std::optional<QPicture> picture; QPicture picture;
#endif #endif
#if QT_CONFIG(movie) #if QT_CONFIG(movie)
QPointer<QMovie> movie; QPointer<QMovie> movie;
std::array<QMetaObject::Connection, 2> movieConnections; std::array<QMetaObject::Connection, 2> movieConnections;
#endif #endif
mutable QWidgetTextControl *control; mutable QWidgetTextControl *control = nullptr;
mutable QTextCursor shortcutCursor; mutable QTextCursor shortcutCursor;
#ifndef QT_NO_CURSOR #ifndef QT_NO_CURSOR
QCursor cursor; QCursor cursor;
#endif #endif
#ifndef QT_NO_SHORTCUT #ifndef QT_NO_SHORTCUT
QPointer<QWidget> buddy; QPointer<QWidget> buddy;
int shortcutId; int shortcutId = 0;
#endif #endif
Qt::TextFormat textformat; Qt::TextFormat textformat = Qt::AutoText;
Qt::TextFormat effectiveTextFormat; Qt::TextFormat effectiveTextFormat = Qt::PlainText;
Qt::TextInteractionFlags textInteractionFlags; Qt::TextInteractionFlags textInteractionFlags = Qt::LinksAccessibleByMouse;
mutable QSizePolicy sizePolicy; mutable QSizePolicy sizePolicy;
int margin; int margin = 0;
ushort align; int align = Qt::AlignLeft | Qt::AlignVCenter | Qt::TextExpandTabs;
short indent; int indent = -1;
mutable uint valid_hints : 1; mutable uint valid_hints : 1;
uint scaledcontents : 1; uint scaledcontents : 1;
mutable uint textLayoutDirty : 1; mutable uint textLayoutDirty : 1;
@ -123,7 +121,7 @@ public:
#endif #endif
uint openExternalLinks : 1; uint openExternalLinks : 1;
// <-- space for more bit field values here // <-- space for more bit field values here
QTextDocument::ResourceProvider resourceProvider; QTextDocument::ResourceProvider resourceProvider = nullptr;
friend class QMessageBoxPrivate; friend class QMessageBoxPrivate;
}; };