diff options
Diffstat (limited to 'src/widgets/widgets/qstatusbar.cpp')
-rw-r--r-- | src/widgets/widgets/qstatusbar.cpp | 213 |
1 files changed, 74 insertions, 139 deletions
diff --git a/src/widgets/widgets/qstatusbar.cpp b/src/widgets/widgets/qstatusbar.cpp index 33b0d24d60..e398f52ac4 100644 --- a/src/widgets/widgets/qstatusbar.cpp +++ b/src/widgets/widgets/qstatusbar.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 "qstatusbar.h" @@ -54,7 +18,7 @@ #include "qmainwindow.h" #endif -#ifndef QT_NO_ACCESSIBILITY +#if QT_CONFIG(accessibility) #include "qaccessible.h" #endif @@ -69,22 +33,27 @@ class QStatusBarPrivate : public QWidgetPrivate public: QStatusBarPrivate() {} + enum ItemCategory + { + Normal, + Permanent + }; + struct SBItem { - SBItem(QWidget* widget, int stretch, bool permanent) - : s(stretch), w(widget), p(permanent) {} - int s; - QWidget * w; - bool p; + QWidget *widget = nullptr; + int stretch = 0; + ItemCategory category = Normal; + bool isPermanent() const { return category == Permanent; } }; - QList<SBItem *> items; + QList<SBItem> items; QString tempItem; - QBoxLayout * box; - QTimer * timer; + QBoxLayout *box; + QTimer *timer; #if QT_CONFIG(sizegrip) - QSizeGrip * resizer; + QSizeGrip *resizer; bool showSizeGrip; #endif @@ -94,8 +63,8 @@ public: { int i = items.size() - 1; for (; i >= 0; --i) { - SBItem *item = items.at(i); - if (!(item && item->p)) + const SBItem &item = items.at(i); + if (!item.isPermanent()) break; } return i; @@ -122,7 +91,7 @@ public: QRect QStatusBarPrivate::messageRect() const { Q_Q(const QStatusBar); - bool rtl = q->layoutDirection() == Qt::RightToLeft; + const bool rtl = q->layoutDirection() == Qt::RightToLeft; int left = 6; int right = q->width() - 12; @@ -136,18 +105,13 @@ QRect QStatusBarPrivate::messageRect() const } #endif - for (int i=0; i<items.size(); ++i) { - QStatusBarPrivate::SBItem* item = items.at(i); - if (!item) + for (const auto &item : items) { + if (item.isPermanent() && item.widget->isVisible()) { + if (rtl) + left = qMax(left, item.widget->x() + item.widget->width() + 2); + else + right = qMin(right, item.widget->x() - 2); break; - if (item->p && item->w->isVisible()) { - if (item->p) { - if (rtl) - left = qMax(left, item->w->x() + item->w->width() + 2); - else - right = qMin(right, item->w->x() - 2); - } - break; } } return QRect(left, 0, right-left, q->height()); @@ -187,12 +151,12 @@ QRect QStatusBarPrivate::messageRect() const Use the showMessage() slot to display a \e temporary message: - \snippet mainwindows/dockwidgets/mainwindow.cpp 8 + \snippet code/src_gui_widgets_qstatusbar.cpp 1 To remove a temporary message, use the clearMessage() slot, or set a time limit when calling showMessage(). For example: - \snippet mainwindows/dockwidgets/mainwindow.cpp 3 + \snippet code/src_gui_widgets_qstatusbar.cpp 2 Use the currentMessage() function to retrieve the temporary message currently shown. The QStatusBar class also provide the @@ -215,8 +179,7 @@ QRect QStatusBarPrivate::messageRect() const \image fusion-statusbar-sizegrip.png A status bar shown in the Fusion widget style - \sa QMainWindow, QStatusTipEvent, {fowler}{GUI Design Handbook: - Status Bar}, {Qt Widgets - Application Example} + \sa QMainWindow, QStatusTipEvent */ @@ -246,9 +209,6 @@ QStatusBar::QStatusBar(QWidget * parent) */ QStatusBar::~QStatusBar() { - Q_D(QStatusBar); - while (!d->items.isEmpty()) - delete d->items.takeFirst(); } @@ -298,7 +258,7 @@ int QStatusBar::insertWidget(int index, QWidget *widget, int stretch) return -1; Q_D(QStatusBar); - QStatusBarPrivate::SBItem* item = new QStatusBarPrivate::SBItem(widget, stretch, false); + QStatusBarPrivate::SBItem item{widget, stretch, QStatusBarPrivate::Normal}; int idx = d->indexToLastNonPermanentWidget(); if (Q_UNLIKELY(index < 0 || index > d->items.size() || (idx >= 0 && index > idx + 1))) { @@ -311,7 +271,7 @@ int QStatusBar::insertWidget(int index, QWidget *widget, int stretch) widget->hide(); reformat(); - if (!widget->isHidden() || !widget->testAttribute(Qt::WA_WState_ExplicitShowHide)) + if (!QWidgetPrivate::get(widget)->isExplicitlyHidden()) widget->show(); return index; @@ -326,7 +286,7 @@ int QStatusBar::insertWidget(int index, QWidget *widget, int stretch) minimum of space. Permanently means that the widget may not be obscured by temporary - messages. It is is located at the far right of the status bar. + messages. It is located at the far right of the status bar. \sa insertPermanentWidget(), removeWidget(), addWidget() */ @@ -338,7 +298,6 @@ void QStatusBar::addPermanentWidget(QWidget * widget, int stretch) insertPermanentWidget(d_func()->items.size(), widget, stretch); } - /*! \since 4.2 @@ -353,7 +312,7 @@ void QStatusBar::addPermanentWidget(QWidget * widget, int stretch) minimum of space. Permanently means that the widget may not be obscured by temporary - messages. It is is located at the far right of the status bar. + messages. It is located at the far right of the status bar. \sa addPermanentWidget(), removeWidget(), addWidget() */ @@ -363,7 +322,7 @@ int QStatusBar::insertPermanentWidget(int index, QWidget *widget, int stretch) return -1; Q_D(QStatusBar); - QStatusBarPrivate::SBItem* item = new QStatusBarPrivate::SBItem(widget, stretch, true); + QStatusBarPrivate::SBItem item{widget, stretch, QStatusBarPrivate::Permanent}; int idx = d->indexToLastNonPermanentWidget(); if (Q_UNLIKELY(index < 0 || index > d->items.size() || (idx >= 0 && index <= idx))) { @@ -373,7 +332,7 @@ int QStatusBar::insertPermanentWidget(int index, QWidget *widget, int stretch) d->items.insert(index, item); reformat(); - if (!widget->isHidden() || !widget->testAttribute(Qt::WA_WState_ExplicitShowHide)) + if (!QWidgetPrivate::get(widget)->isExplicitlyHidden()) widget->show(); return index; @@ -395,23 +354,10 @@ void QStatusBar::removeWidget(QWidget *widget) return; Q_D(QStatusBar); - bool found = false; - QStatusBarPrivate::SBItem* item; - for (int i=0; i<d->items.size(); ++i) { - item = d->items.at(i); - if (!item) - break; - if (item->w == widget) { - d->items.removeAt(i); - item->w->hide(); - delete item; - found = true; - break; - } - } - - if (found) + if (d->items.removeIf([widget](const auto &item) { return item.widget == widget; })) { + widget->hide(); reformat(); + } #if defined(QT_DEBUG) else qDebug("QStatusBar::removeWidget(): Widget not found."); @@ -495,25 +441,22 @@ void QStatusBar::reformat() int maxH = fontMetrics().height(); - int i; - QStatusBarPrivate::SBItem* item; - for (i=0,item=nullptr; i<d->items.size(); ++i) { - item = d->items.at(i); - if (!item || item->p) + qsizetype i; + for (i = 0; i < d->items.size(); ++i) { + const auto &item = d->items.at(i); + if (item.isPermanent()) break; - l->addWidget(item->w, item->s); - int itemH = qMin(qSmartMinSize(item->w).height(), item->w->maximumHeight()); + l->addWidget(item.widget, item.stretch); + int itemH = qMin(qSmartMinSize(item.widget).height(), item.widget->maximumHeight()); maxH = qMax(maxH, itemH); } l->addStretch(0); - for (item=nullptr; i<d->items.size(); ++i) { - item = d->items.at(i); - if (!item) - break; - l->addWidget(item->w, item->s); - int itemH = qMin(qSmartMinSize(item->w).height(), item->w->maximumHeight()); + for (; i < d->items.size(); ++i) { + const auto &item = d->items.at(i); + l->addWidget(item.widget, item.stretch); + int itemH = qMin(qSmartMinSize(item.widget).height(), item.widget->maximumHeight()); maxH = qMax(maxH, itemH); } #if QT_CONFIG(sizegrip) @@ -551,7 +494,7 @@ void QStatusBar::showMessage(const QString &message, int timeout) if (timeout > 0) { if (!d->timer) { d->timer = new QTimer(this); - connect(d->timer, SIGNAL(timeout()), this, SLOT(clearMessage())); + connect(d->timer, &QTimer::timeout, this, &QStatusBar::clearMessage); } d->timer->start(timeout); } else if (d->timer) { @@ -577,7 +520,7 @@ void QStatusBar::clearMessage() if (d->tempItem.isEmpty()) return; if (d->timer) { - qDeleteInEventHandler(d->timer); + delete d->timer; d->timer = nullptr; } d->tempItem.clear(); @@ -617,22 +560,20 @@ void QStatusBar::hideOrShow() Q_D(QStatusBar); bool haveMessage = !d->tempItem.isEmpty(); - QStatusBarPrivate::SBItem* item = nullptr; - for (int i=0; i<d->items.size(); ++i) { - item = d->items.at(i); - if (!item || item->p) + for (const auto &item : std::as_const(d->items)) { + if (item.isPermanent()) break; - if (haveMessage && item->w->isVisible()) { - item->w->hide(); - item->w->setAttribute(Qt::WA_WState_ExplicitShowHide, false); - } else if (!haveMessage && !item->w->testAttribute(Qt::WA_WState_ExplicitShowHide)) { - item->w->show(); + if (haveMessage && item.widget->isVisible()) { + item.widget->hide(); + item.widget->setAttribute(Qt::WA_WState_ExplicitShowHide, false); + } else if (!haveMessage && !item.widget->testAttribute(Qt::WA_WState_ExplicitShowHide)) { + item.widget->show(); } } emit messageChanged(d->tempItem); -#ifndef QT_NO_ACCESSIBILITY +#if QT_CONFIG(accessibility) if (QAccessible::isActive()) { QAccessibleEvent event(this, QAccessible::NameChanged); QAccessible::updateAccessibility(&event); @@ -671,16 +612,15 @@ void QStatusBar::paintEvent(QPaintEvent *event) opt.initFrom(this); style()->drawPrimitive(QStyle::PE_PanelStatusBar, &opt, &p, this); - for (int i=0; i<d->items.size(); ++i) { - QStatusBarPrivate::SBItem* item = d->items.at(i); - if (item && item->w->isVisible() && (!haveMessage || item->p)) { - QRect ir = item->w->geometry().adjusted(-2, -1, 2, 1); + for (const auto &item : std::as_const(d->items)) { + if (item.widget->isVisible() && (!haveMessage || item.isPermanent())) { + QRect ir = item.widget->geometry().adjusted(-2, -1, 2, 1); if (event->rect().intersects(ir)) { QStyleOption opt(0); opt.rect = ir; opt.palette = palette(); opt.state = QStyle::State_None; - style()->drawPrimitive(QStyle::PE_FrameStatusBarItem, &opt, &p, item->w); + style()->drawPrimitive(QStyle::PE_FrameStatusBarItem, &opt, &p, item.widget); } } } @@ -706,17 +646,13 @@ bool QStatusBar::event(QEvent *e) { Q_D(QStatusBar); - if (e->type() == QEvent::LayoutRequest - ) { + switch (e->type()) { + case QEvent::LayoutRequest: { // Calculate new strut height and call reformat() if it has changed int maxH = fontMetrics().height(); - QStatusBarPrivate::SBItem* item = nullptr; - for (int i=0; i<d->items.size(); ++i) { - item = d->items.at(i); - if (!item) - break; - int itemH = qMin(qSmartMinSize(item->w).height(), item->w->maximumHeight()); + for (const auto &item : std::as_const(d->items)) { + const int itemH = qMin(qSmartMinSize(item.widget).height(), item.widget->maximumHeight()); maxH = qMax(maxH, itemH); } @@ -729,18 +665,17 @@ bool QStatusBar::event(QEvent *e) reformat(); else update(); + break; } - if (e->type() == QEvent::ChildRemoved) { - QStatusBarPrivate::SBItem* item = nullptr; - for (int i=0; i<d->items.size(); ++i) { - item = d->items.at(i); - if (!item) - break; - if (item->w == ((QChildEvent*)e)->child()) { + case QEvent::ChildRemoved: + for (int i = 0; i < d->items.size(); ++i) { + const auto &item = d->items.at(i); + if (item.widget == static_cast<QChildEvent *>(e)->child()) d->items.removeAt(i); - delete item; - } } + break; + default: + break; } return QWidget::event(e); |