diff options
Diffstat (limited to 'src/widgets/widgets/qlabel.cpp')
-rw-r--r-- | src/widgets/widgets/qlabel.cpp | 172 |
1 files changed, 63 insertions, 109 deletions
diff --git a/src/widgets/widgets/qlabel.cpp b/src/widgets/widgets/qlabel.cpp index 2d7b7f79b1..f4155de0a0 100644 --- a/src/widgets/widgets/qlabel.cpp +++ b/src/widgets/widgets/qlabel.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qpainter.h" #include "qevent.h" @@ -54,22 +18,24 @@ #include "private/qstylesheetstyle_p.h" #include <qmath.h> -#ifndef QT_NO_ACCESSIBILITY +#if QT_CONFIG(accessibility) #include <qaccessible.h> #endif QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + QLabelPrivate::QLabelPrivate() : QFramePrivate(), sh(), msh(), text(), - pixmap(nullptr), - scaledpixmap(nullptr), - cachedimage(nullptr), + pixmap(), + scaledpixmap(), + cachedimage(), #ifndef QT_NO_PICTURE - picture(nullptr), + picture(), #endif #if QT_CONFIG(movie) movie(), @@ -183,14 +149,13 @@ QLabelPrivate::~QLabelPrivate() was a button (inheriting from QAbstractButton), triggering the mnemonic would emulate a button click. - \sa QLineEdit, QTextEdit, QPixmap, QMovie, - {fowler}{GUI Design Handbook: Label} + \sa QLineEdit, QTextEdit, QPixmap, QMovie */ #ifndef QT_NO_PICTURE /*! \fn QPicture QLabel::picture(Qt::ReturnByValueConstant) const - \obsolete Use the overload without argument instead. + \deprecated Use the overload without argument instead. \since 5.15 Returns the label's picture. @@ -339,7 +304,7 @@ void QLabel::setText(const QString &text) d->updateLabel(); -#ifndef QT_NO_ACCESSIBILITY +#if QT_CONFIG(accessibility) if (accessibleName().isEmpty()) { QAccessibleEvent event(this, QAccessible::NameChanged); QAccessible::updateAccessibility(&event); @@ -376,12 +341,9 @@ void QLabel::setPixmap(const QPixmap &pixmap) Q_D(QLabel); if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) { d->clearContents(); - d->pixmap = new QPixmap(pixmap); + d->pixmap = pixmap; } - if (d->pixmap->depth() == 1 && !d->pixmap->mask()) - d->pixmap->setMask(*((QBitmap *)d->pixmap)); - d->updateLabel(); } @@ -396,7 +358,7 @@ QPixmap QLabel::pixmap() const /*! \fn QPixmap QLabel::pixmap(Qt::ReturnByValueConstant) const - \obsolete Use the overload without argument instead. + \deprecated Use the overload without argument instead. \since 5.15 Returns the label's pixmap. @@ -424,7 +386,7 @@ void QLabel::setPicture(const QPicture &picture) { Q_D(QLabel); d->clearContents(); - d->picture = new QPicture(picture); + d->picture = picture; d->updateLabel(); } @@ -604,7 +566,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 +574,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)); @@ -620,7 +582,7 @@ QSize QLabelPrivate::sizeForWidth(int w) const int m = indent; if (m < 0 && q->frameWidth()) // no indent, but we do have a frame - m = fm.horizontalAdvance(QLatin1Char('x')) - margin*2; + m = fm.horizontalAdvance(u'x') - margin*2; if (m > 0) { if ((align & Qt::AlignLeft) || (align & Qt::AlignRight)) hextra += m; @@ -1129,17 +1091,19 @@ void QLabel::paintEvent(QPaintEvent *) #endif if (d->pixmap && !d->pixmap->isNull()) { QPixmap pix; - if (d->scaledcontents) { - QSize scaledSize = cr.size() * devicePixelRatio(); + 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) { if (!d->cachedimage) - d->cachedimage = new QImage(d->pixmap->toImage()); - delete d->scaledpixmap; + d->cachedimage = d->pixmap->toImage(); + d->scaledpixmap.reset(); QImage scaledImage = d->cachedimage->scaled(scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); - d->scaledpixmap = new QPixmap(QPixmap::fromImage(std::move(scaledImage))); - d->scaledpixmap->setDevicePixelRatio(devicePixelRatio()); + d->scaledpixmap = QPixmap::fromImage(std::move(scaledImage)); + d->scaledpixmap->setDevicePixelRatio(dpr); } pix = *d->scaledpixmap; } else @@ -1207,12 +1171,14 @@ void QLabel::setBuddy(QWidget *buddy) Q_D(QLabel); if (d->buddy) - disconnect(d->buddy, SIGNAL(destroyed()), this, SLOT(_q_buddyDeleted())); + QObjectPrivate::disconnect(d->buddy, &QObject::destroyed, + d, &QLabelPrivate::buddyDeleted); d->buddy = buddy; if (buddy) - connect(buddy, SIGNAL(destroyed()), this, SLOT(_q_buddyDeleted())); + QObjectPrivate::connect(buddy, &QObject::destroyed, + d, &QLabelPrivate::buddyDeleted); if (d->isTextLabel) { if (d->shortcutId) @@ -1248,14 +1214,14 @@ void QLabelPrivate::updateShortcut() // But then we do want to hide the ampersands, so we can't use shortcutId. hasShortcut = false; - if (!text.contains(QLatin1Char('&'))) + if (!text.contains(u'&')) return; hasShortcut = true; shortcutId = q->grabShortcut(QKeySequence::mnemonic(text)); } -void QLabelPrivate::_q_buddyDeleted() +void QLabelPrivate::buddyDeleted() { Q_Q(QLabel); q->setBuddy(nullptr); @@ -1264,7 +1230,7 @@ void QLabelPrivate::_q_buddyDeleted() #endif // QT_NO_SHORTCUT #if QT_CONFIG(movie) -void QLabelPrivate::_q_movieUpdated(const QRect& rect) +void QLabelPrivate::movieUpdated(const QRect &rect) { Q_Q(QLabel); if (movie && movie->isValid()) { @@ -1287,12 +1253,12 @@ void QLabelPrivate::_q_movieUpdated(const QRect& rect) } } -void QLabelPrivate::_q_movieResized(const QSize& size) +void QLabelPrivate::movieResized(const QSize &size) { Q_Q(QLabel); - q->update(); //we need to refresh the whole background in case the new size is smaler + q->update(); //we need to refresh the whole background in case the new size is smaller valid_hints = false; - _q_movieUpdated(QRect(QPoint(0,0), size)); + movieUpdated(QRect(QPoint(0,0), size)); q->updateGeometry(); } @@ -1314,8 +1280,10 @@ void QLabel::setMovie(QMovie *movie) return; d->movie = movie; - connect(movie, SIGNAL(resized(QSize)), this, SLOT(_q_movieResized(QSize))); - connect(movie, SIGNAL(updated(QRect)), this, SLOT(_q_movieUpdated(QRect))); + d->movieConnections = { + QObjectPrivate::connect(movie, &QMovie::resized, d, &QLabelPrivate::movieResized), + QObjectPrivate::connect(movie, &QMovie::updated, d, &QLabelPrivate::movieUpdated), + }; // Assume that if the movie is running, // resize/update signals will come soon enough @@ -1339,15 +1307,11 @@ void QLabelPrivate::clearContents() hasShortcut = false; #ifndef QT_NO_PICTURE - delete picture; - picture = nullptr; + picture.reset(); #endif - delete scaledpixmap; - scaledpixmap = nullptr; - delete cachedimage; - cachedimage = nullptr; - delete pixmap; - pixmap = nullptr; + scaledpixmap.reset(); + cachedimage.reset(); + pixmap.reset(); text.clear(); Q_Q(QLabel); @@ -1357,10 +1321,8 @@ void QLabelPrivate::clearContents() shortcutId = 0; #endif #if QT_CONFIG(movie) - if (movie) { - QObject::disconnect(movie, SIGNAL(resized(QSize)), q, SLOT(_q_movieResized(QSize))); - QObject::disconnect(movie, SIGNAL(updated(QRect)), q, SLOT(_q_movieUpdated(QRect))); - } + for (const auto &conn : std::as_const(movieConnections)) + QObject::disconnect(conn); movie = nullptr; #endif #ifndef QT_NO_CURSOR @@ -1429,7 +1391,7 @@ void QLabel::setTextFormat(Qt::TextFormat format) Returns the resource provider for rich text of this label. */ -QUrlResourceProvider *QLabel::resourceProvider() const +QTextDocument::ResourceProvider QLabel::resourceProvider() const { Q_D(const QLabel); return d->control ? d->control->document()->resourceProvider() : d->resourceProvider; @@ -1442,7 +1404,7 @@ QUrlResourceProvider *QLabel::resourceProvider() const \note The label \e{does not} take ownership of the \a provider. */ -void QLabel::setResourceProvider(QUrlResourceProvider *provider) +void QLabel::setResourceProvider(const QTextDocument::ResourceProvider &provider) { Q_D(QLabel); d->resourceProvider = provider; @@ -1493,10 +1455,8 @@ void QLabel::setScaledContents(bool enable) return; d->scaledcontents = enable; if (!enable) { - delete d->scaledpixmap; - d->scaledpixmap = nullptr; - delete d->cachedimage; - d->cachedimage = nullptr; + d->scaledpixmap.reset(); + d->cachedimage.reset(); } update(contentsRect()); } @@ -1523,7 +1483,7 @@ QRect QLabelPrivate::documentRect() const : q->layoutDirection(), QFlag(this->align)); int m = indent; if (m < 0 && q->frameWidth()) // no indent, but we do have a frame - m = q->fontMetrics().horizontalAdvance(QLatin1Char('x')) / 2 - margin; + m = q->fontMetrics().horizontalAdvance(u'x') / 2 - margin; if (m > 0) { if (align & Qt::AlignLeft) cr.setLeft(cr.left() + m); @@ -1565,11 +1525,11 @@ void QLabelPrivate::ensureTextPopulated() const int from = 0; bool found = false; QTextCursor cursor; - while (!(cursor = control->document()->find((QLatin1String("&")), from)).isNull()) { + while (!(cursor = control->document()->find(("&"_L1), from)).isNull()) { cursor.deleteChar(); // remove the ampersand cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); from = cursor.position(); - if (!found && cursor.selectedText() != QLatin1String("&")) { //not a second & + if (!found && cursor.selectedText() != "&"_L1) { //not a second & found = true; shortcutCursor = cursor; } @@ -1622,12 +1582,12 @@ void QLabelPrivate::ensureTextControl() const control->setOpenExternalLinks(openExternalLinks); control->setPalette(q->palette()); control->setFocus(q->hasFocus()); - QObject::connect(control, SIGNAL(updateRequest(QRectF)), - q, SLOT(update())); - QObject::connect(control, SIGNAL(linkHovered(QString)), - q, SLOT(_q_linkHovered(QString))); - QObject::connect(control, SIGNAL(linkActivated(QString)), - q, SIGNAL(linkActivated(QString))); + QObject::connect(control, &QWidgetTextControl::updateRequest, + q, qOverload<>(&QLabel::update)); + QObject::connect(control, &QWidgetTextControl::linkActivated, + q, &QLabel::linkActivated); + QObjectPrivate::connect(control, &QWidgetTextControl::linkHovered, + this, &QLabelPrivate::linkHovered); textLayoutDirty = true; textDirty = true; } @@ -1643,7 +1603,7 @@ void QLabelPrivate::sendControlEvent(QEvent *e) control->processEvent(e, -layoutRect().topLeft(), q); } -void QLabelPrivate::_q_linkHovered(const QString &anchor) +void QLabelPrivate::linkHovered(const QString &anchor) { Q_Q(QLabel); #ifndef QT_NO_CURSOR @@ -1674,7 +1634,7 @@ QRectF QLabelPrivate::layoutRect() const if (!control) return cr; ensureTextLayouted(); - // Caculate y position manually + // Calculate y position manually qreal rh = control->document()->documentLayout()->documentSize().height(); qreal yo = 0; if (align & Qt::AlignVCenter) @@ -1694,16 +1654,10 @@ QPoint QLabelPrivate::layoutPoint(const QPoint& p) const #ifndef QT_NO_CONTEXTMENU QMenu *QLabelPrivate::createStandardContextMenu(const QPoint &pos) { - QString linkToCopy; - QPoint p; - if (control && effectiveTextFormat != Qt::PlainText) { - p = layoutPoint(pos); - linkToCopy = control->document()->documentLayout()->anchorAt(p); - } - - if (linkToCopy.isEmpty() && !control) + if (!control) return nullptr; + const QPoint p = layoutPoint(pos); return control->createStandardContextMenu(p, q_func()); } #endif |