diff options
Diffstat (limited to 'src/widgets/widgets/qtoolbarlayout.cpp')
-rw-r--r-- | src/widgets/widgets/qtoolbarlayout.cpp | 167 |
1 files changed, 69 insertions, 98 deletions
diff --git a/src/widgets/widgets/qtoolbarlayout.cpp b/src/widgets/widgets/qtoolbarlayout.cpp index f2d329d59d..cc842e2657 100644 --- a/src/widgets/widgets/qtoolbarlayout.cpp +++ b/src/widgets/widgets/qtoolbarlayout.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 <qapplication.h> #include <qaction.h> @@ -48,7 +12,7 @@ #include <qmenu.h> #include <qdebug.h> #include <qmath.h> -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS #include <qpa/qplatformnativeinterface.h> #endif @@ -69,13 +33,13 @@ extern QMainWindowLayout *qt_mainwindow_layout(const QMainWindow *window); */ QToolBarItem::QToolBarItem(QWidget *widget) - : QWidgetItem(widget), action(0), customWidget(false) + : QWidgetItem(widget), action(nullptr), customWidget(false) { } bool QToolBarItem::isEmpty() const { - return action == 0 || !action->isVisible(); + return action == nullptr || !action->isVisible(); } /****************************************************************************** @@ -84,7 +48,7 @@ bool QToolBarItem::isEmpty() const QToolBarLayout::QToolBarLayout(QWidget *parent) : QLayout(parent), expanded(false), animating(false), dirty(true), - expanding(false), empty(true), expandFlag(false), popupMenu(0) + expanding(false), empty(true), expandFlag(false), popupMenu(nullptr) { QToolBar *tb = qobject_cast<QToolBar*>(parent); if (!tb) @@ -119,8 +83,9 @@ void QToolBarLayout::updateMarginAndSpacing() QStyle *style = tb->style(); QStyleOptionToolBar opt; tb->initStyleOption(&opt); - setMargin(style->pixelMetric(QStyle::PM_ToolBarItemMargin, &opt, tb) - + style->pixelMetric(QStyle::PM_ToolBarFrameWidth, &opt, tb)); + const int margin = style->pixelMetric(QStyle::PM_ToolBarItemMargin, &opt, tb) + + style->pixelMetric(QStyle::PM_ToolBarFrameWidth, &opt, tb); + setContentsMargins(margin, margin, margin, margin); setSpacing(style->pixelMetric(QStyle::PM_ToolBarItemSpacing, &opt, tb)); } @@ -131,15 +96,15 @@ bool QToolBarLayout::hasExpandFlag() const void QToolBarLayout::setUsePopupMenu(bool set) { - if (!dirty && ((popupMenu == 0) == set)) + if (!dirty && ((popupMenu == nullptr) == set)) invalidate(); if (!set) { QObject::connect(extension, SIGNAL(clicked(bool)), this, SLOT(setExpanded(bool)), Qt::UniqueConnection); extension->setPopupMode(QToolButton::DelayedPopup); - extension->setMenu(0); + extension->setMenu(nullptr); delete popupMenu; - popupMenu = 0; + popupMenu = nullptr; } else { QObject::disconnect(extension, SIGNAL(clicked(bool)), this, SLOT(setExpanded(bool))); @@ -167,22 +132,22 @@ void QToolBarLayout::addItem(QLayoutItem*) QLayoutItem *QToolBarLayout::itemAt(int index) const { - if (index < 0 || index >= items.count()) - return 0; + if (index < 0 || index >= items.size()) + return nullptr; return items.at(index); } QLayoutItem *QToolBarLayout::takeAt(int index) { - if (index < 0 || index >= items.count()) - return 0; + if (index < 0 || index >= items.size()) + return nullptr; QToolBarItem *item = items.takeAt(index); if (popupMenu) popupMenu->removeAction(item->action); QWidgetAction *widgetAction = qobject_cast<QWidgetAction*>(item->action); - if (widgetAction != 0 && item->customWidget) { + if (widgetAction != nullptr && item->customWidget) { widgetAction->releaseWidget(item->widget()); } else { // destroy the QToolButton/QToolBarSeparator @@ -197,7 +162,7 @@ QLayoutItem *QToolBarLayout::takeAt(int index) void QToolBarLayout::insertAction(int index, QAction *action) { index = qMax(0, index); - index = qMin(items.count(), index); + index = qMin(items.size(), index); QToolBarItem *item = createItem(action); if (item) { @@ -206,9 +171,9 @@ void QToolBarLayout::insertAction(int index, QAction *action) } } -int QToolBarLayout::indexOf(QAction *action) const +int QToolBarLayout::indexOf(const QAction *action) const { - for (int i = 0; i < items.count(); ++i) { + for (int i = 0; i < items.size(); ++i) { if (items.at(i)->action == action) return i; } @@ -217,7 +182,7 @@ int QToolBarLayout::indexOf(QAction *action) const int QToolBarLayout::count() const { - return items.count(); + return items.size(); } bool QToolBarLayout::isEmpty() const @@ -239,9 +204,9 @@ Qt::Orientations QToolBarLayout::expandingDirections() const updateGeomArray(); QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); if (!tb) - return Qt::Orientations(0); + return {}; Qt::Orientation o = tb->orientation(); - return expanding ? Qt::Orientations(o) : Qt::Orientations(0); + return expanding ? Qt::Orientations(o) : Qt::Orientations{}; } bool QToolBarLayout::movable() const @@ -250,7 +215,7 @@ bool QToolBarLayout::movable() const if (!tb) return false; QMainWindow *win = qobject_cast<QMainWindow*>(tb->parentWidget()); - return tb->isMovable() && win != 0; + return tb->isMovable() && win != nullptr; } void QToolBarLayout::updateGeomArray() const @@ -268,7 +233,7 @@ void QToolBarLayout::updateGeomArray() const tb->initStyleOption(&opt); const int handleExtent = movable() ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0; - const int margin = this->margin(); + const QMargins margins = contentsMargins(); const int spacing = this->spacing(); const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb); Qt::Orientation o = tb->orientation(); @@ -281,10 +246,10 @@ void QToolBarLayout::updateGeomArray() const that->expanding = false; that->empty = false; - QVector<QLayoutStruct> a(items.count() + 1); // + 1 for the stretch + QList<QLayoutStruct> a(items.size() + 1); // + 1 for the stretch int count = 0; - for (int i = 0; i < items.count(); ++i) { + for (int i = 0; i < items.size(); ++i) { QToolBarItem *item = items.at(i); QSize max = item->maximumSize(); @@ -330,24 +295,24 @@ void QToolBarLayout::updateGeomArray() const that->empty = count == 0; rpick(o, that->minSize) += handleExtent; - that->minSize += QSize(2*margin, 2*margin); - if (items.count() > 1) + that->minSize += QSize(pick(Qt::Horizontal, margins), pick(Qt::Vertical, margins)); + if (items.size() > 1) rpick(o, that->minSize) += spacing + extensionExtent; rpick(o, that->hint) += handleExtent; - that->hint += QSize(2*margin, 2*margin); + that->hint += QSize(pick(Qt::Horizontal, margins), pick(Qt::Vertical, margins)); that->dirty = false; } static bool defaultWidgetAction(QToolBarItem *item) { QWidgetAction *a = qobject_cast<QWidgetAction*>(item->action); - return a != 0 && a->defaultWidget() == item->widget(); + return a != nullptr && a->defaultWidget() == item->widget(); } void QToolBarLayout::updateMacBorderMetrics() { -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); if (!tb) return; @@ -359,6 +324,8 @@ void QToolBarLayout::updateMacBorderMetrics() return; QPlatformNativeInterface *nativeInterface = QApplication::platformNativeInterface(); + if (!nativeInterface) + return; // Not Cocoa platform plugin. QPlatformNativeInterface::NativeResourceForIntegrationFunction function = nativeInterface->nativeResourceFunctionForIntegration("registerContentBorderArea"); if (!function) @@ -384,7 +351,7 @@ void QToolBarLayout::setGeometry(const QRect &rect) QStyle *style = tb->style(); QStyleOptionToolBar opt; tb->initStyleOption(&opt); - const int margin = this->margin(); + const QMargins margins = contentsMargins(); const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb); Qt::Orientation o = tb->orientation(); @@ -403,14 +370,18 @@ void QToolBarLayout::setGeometry(const QRect &rect) QSize hint = sizeHint(); QPoint pos; - rpick(o, pos) = pick(o, rect.bottomRight()) - margin - extensionExtent + 2; + rpick(o, pos) = pick(o, rect.bottomRight()) - + pick(o, QSize(margins.bottom(), margins.right())) - extensionExtent + 2; if (area == Qt::LeftToolBarArea || area == Qt::TopToolBarArea) - rperp(o, pos) = perp(o, rect.topLeft()) + margin; + rperp(o, pos) = perp(o, rect.topLeft()) + + perp(o, QSize(margins.top(), margins.left())); else - rperp(o, pos) = perp(o, rect.bottomRight()) - margin - (perp(o, hint) - 2*margin) + 1; + rperp(o, pos) = perp(o, rect.bottomRight()) - + perp(o, QSize(margins.bottom(), margins.right())) - + (perp(o, hint) - perp(o, margins)) + 1; QSize size; rpick(o, size) = extensionExtent; - rperp(o, size) = perp(o, hint) - 2*margin; + rperp(o, size) = perp(o, hint) - perp(o, margins); QRect r(pos, size); if (o == Qt::Horizontal) @@ -443,25 +414,25 @@ bool QToolBarLayout::layoutActions(const QSize &size) tb->initStyleOption(&opt); const int handleExtent = movable() ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0; - const int margin = this->margin(); + const QMargins margins = contentsMargins(); const int spacing = this->spacing(); const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb); Qt::Orientation o = tb->orientation(); bool extensionMenuContainsOnlyWidgetActions = true; - int space = pick(o, rect.size()) - 2*margin - handleExtent; + int space = pick(o, rect.size()) - pick(o, margins) - handleExtent; if (space <= 0) return false; // nothing to do. - if(popupMenu) + if (popupMenu) popupMenu->clear(); bool ranOutOfSpace = false; int rows = 0; - int rowPos = perp(o, rect.topLeft()) + margin; + int rowPos = perp(o, rect.topLeft()) + perp(o, QSize(margins.top(), margins.left())); int i = 0; - while (i < items.count()) { - QVector<QLayoutStruct> a = geomArray; + while (i < items.size()) { + QList<QLayoutStruct> a = geomArray; int start = i; int size = 0; @@ -470,7 +441,7 @@ bool QToolBarLayout::layoutActions(const QSize &size) int count = 0; int maximumSize = 0; bool expansiveRow = false; - for (; i < items.count(); ++i) { + for (; i < items.size(); ++i) { if (a[i].empty) continue; @@ -521,14 +492,14 @@ bool QToolBarLayout::layoutActions(const QSize &size) } QPoint pos; - rpick(o, pos) = margin + handleExtent + a[j].pos; + rpick(o, pos) = pick(o, QSize(margins.top(), margins.left())) + handleExtent + a[j].pos; rperp(o, pos) = rowPos; QSize size; rpick(o, size) = a[j].size; if (expanded) rperp(o, size) = rowHeight; else - rperp(o, size) = perp(o, rect.size()) - 2*margin; + rperp(o, size) = perp(o, rect.size()) - perp(o, margins); QRect r(pos, size); if (o == Qt::Horizontal) @@ -541,7 +512,7 @@ bool QToolBarLayout::layoutActions(const QSize &size) } if (!expanded) { - for (int j = i; j < items.count(); ++j) { + for (int j = i; j < items.size(); ++j) { QToolBarItem *item = items.at(j); if (!item->widget()->isHidden()) hideWidgets << item->widget(); @@ -563,12 +534,12 @@ bool QToolBarLayout::layoutActions(const QSize &size) // widgets into the menu. If only custom widget actions are chopped off, the popup menu // is empty. So we show the little extension button to show something is chopped off, // but we make it disabled. - extension->setEnabled(popupMenu == 0 || !extensionMenuContainsOnlyWidgetActions); + extension->setEnabled(popupMenu == nullptr || !extensionMenuContainsOnlyWidgetActions); // we have to do the show/hide here, because it triggers more calls to setGeometry :( - for (int i = 0; i < showWidgets.count(); ++i) + for (int i = 0; i < showWidgets.size(); ++i) showWidgets.at(i)->show(); - for (int i = 0; i < hideWidgets.count(); ++i) + for (int i = 0; i < hideWidgets.size(); ++i) hideWidgets.at(i)->hide(); return ranOutOfSpace; @@ -589,13 +560,13 @@ QSize QToolBarLayout::expandedSize(const QSize &size) const tb->initStyleOption(&opt); const int handleExtent = movable() ? style->pixelMetric(QStyle::PM_ToolBarHandleExtent, &opt, tb) : 0; - const int margin = this->margin(); + const QMargins margins = contentsMargins(); const int spacing = this->spacing(); const int extensionExtent = style->pixelMetric(QStyle::PM_ToolBarExtensionExtent, &opt, tb); int total_w = 0; int count = 0; - for (int x = 0; x < items.count(); ++x) { + for (int x = 0; x < items.size(); ++x) { if (!geomArray[x].empty) { total_w += (count == 0 ? 0 : spacing) + geomArray[x].minimumSize; ++count; @@ -609,19 +580,19 @@ QSize QToolBarLayout::expandedSize(const QSize &size) const if (rows == 1) ++rows; // we want to expand to at least two rows int space = total_w/rows + spacing + extensionExtent; - space = qMax(space, min_w - 2*margin - handleExtent); - if (win != 0) - space = qMin(space, pick(o, win->size()) - 2*margin - handleExtent); + space = qMax(space, min_w - pick(o, margins) - handleExtent); + if (win != nullptr) + space = qMin(space, pick(o, win->size()) - pick(o, margins) - handleExtent); int w = 0; int h = 0; int i = 0; - while (i < items.count()) { + while (i < items.size()) { int count = 0; int size = 0; int prev = -1; int rowHeight = 0; - for (; i < items.count(); ++i) { + for (; i < items.size(); ++i) { if (geomArray[i].empty) continue; @@ -644,11 +615,11 @@ QSize QToolBarLayout::expandedSize(const QSize &size) const h += rowHeight + spacing; } - w += 2*margin + handleExtent + spacing + extensionExtent; + w += pick(Qt::Horizontal, margins) + handleExtent + spacing + extensionExtent; w = qMax(w, min_w); - if (win != 0) + if (win != nullptr) w = qMin(w, pick(o, win->size())); - h += 2*margin - spacing; //there is no spacing before the first row + h += pick(Qt::Vertical, margins) - spacing; //there is no spacing before the first row QSize result; rpick(o, result) = w; @@ -705,14 +676,14 @@ QToolBarItem *QToolBarLayout::createItem(QAction *action) { bool customWidget = false; bool standardButtonWidget = false; - QWidget *widget = 0; + QWidget *widget = nullptr; QToolBar *tb = qobject_cast<QToolBar*>(parentWidget()); if (!tb) - return (QToolBarItem *)0; + return (QToolBarItem *)nullptr; if (QWidgetAction *widgetAction = qobject_cast<QWidgetAction *>(action)) { widget = widgetAction->requestWidget(tb); - if (widget != 0) { + if (widget != nullptr) { widget->setAttribute(Qt::WA_LayoutUsesWidgetRect); customWidget = true; } |