diff options
Diffstat (limited to 'src/widgets/widgets/qtoolbar.cpp')
-rw-r--r-- | src/widgets/widgets/qtoolbar.cpp | 213 |
1 files changed, 57 insertions, 156 deletions
diff --git a/src/widgets/widgets/qtoolbar.cpp b/src/widgets/widgets/qtoolbar.cpp index c76c7f6c70..5e5ef8e8d5 100644 --- a/src/widgets/widgets/qtoolbar.cpp +++ b/src/widgets/widgets/qtoolbar.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 "qtoolbar.h" @@ -43,6 +7,9 @@ #if QT_CONFIG(combobox) #include <qcombobox.h> #endif +#if QT_CONFIG(draganddrop) +#include <qdrag.h> +#endif #include <qevent.h> #include <qlayout.h> #include <qmainwindow.h> @@ -50,6 +17,7 @@ #if QT_CONFIG(menubar) #include <qmenubar.h> #endif +#include <qmimedata.h> #if QT_CONFIG(rubberband) #include <qrubberband.h> #endif @@ -60,6 +28,7 @@ #include <qtimer.h> #include <private/qwidgetaction_p.h> #include <private/qmainwindowlayout_p.h> +#include <private/qhighdpiscaling_p.h> #ifdef Q_OS_MACOS #include <qpa/qplatformnativeinterface.h> @@ -75,6 +44,8 @@ QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + // qmainwindow.cpp extern QMainWindowLayout *qt_mainwindow_layout(const QMainWindow *window); @@ -140,8 +111,12 @@ void QToolBarPrivate::updateWindowFlags(bool floating, bool unplug) flags |= Qt::FramelessWindowHint; - if (unplug) +#if QT_CONFIG(draganddrop) + // If we are performing a platform drag the flag is not needed and we want to avoid recreating + // the platform window when it would be removed later + if (unplug && !QMainWindowLayout::needsPlatformDrag()) flags |= Qt::X11BypassWindowManagerHint; +#endif q->setWindowFlags(flags); } @@ -152,8 +127,6 @@ void QToolBarPrivate::setWindowState(bool floating, bool unplug, const QRect &re bool visible = !q->isHidden(); bool wasFloating = q->isFloating(); // ...is also currently using popup menus - q->hide(); - updateWindowFlags(floating, unplug); if (floating != wasFloating) @@ -207,12 +180,29 @@ void QToolBarPrivate::startDrag(bool moving) QMainWindowLayout *layout = qt_mainwindow_layout(win); Q_ASSERT(layout != nullptr); +#if QT_CONFIG(draganddrop) + const bool wasFloating = q->isFloating(); +#endif + if (!moving) { - state->widgetItem = layout->unplug(q); + state->widgetItem = layout->unplug(q, QDockWidgetPrivate::DragScope::Group); Q_ASSERT(state->widgetItem != nullptr); } state->dragging = !moving; state->moving = moving; + +#if QT_CONFIG(draganddrop) + if (QMainWindowLayout::needsPlatformDrag() && state->dragging) { + auto result = layout->performPlatformWidgetDrag(state->widgetItem, state->pressPos); + if (result == Qt::IgnoreAction && !wasFloating) { + layout->revert(state->widgetItem); + delete state; + state = nullptr; + } else { + endDrag(); + } + } +#endif } void QToolBarPrivate::endDrag() @@ -278,6 +268,13 @@ bool QToolBarPrivate::mousePressEvent(QMouseEvent *event) bool QToolBarPrivate::mouseReleaseEvent(QMouseEvent*) { +#if QT_CONFIG(draganddrop) + // if we are peforming a platform drag ignore the release here and end the drag when the actual + // drag ends. + if (QMainWindowLayout::needsPlatformDrag()) + return false; +#endif + if (state != nullptr) { endDrag(); return true; @@ -328,6 +325,11 @@ bool QToolBarPrivate::mouseMoveEvent(QMouseEvent *event) q->grabMouse(); } + if (!state) { + q->releaseMouse(); + return true; + } + if (state->dragging) { QPoint pos = event->globalPosition().toPoint(); // if we are right-to-left, we move so as to keep the right edge the same distance @@ -345,7 +347,12 @@ bool QToolBarPrivate::mouseMoveEvent(QMouseEvent *event) const QPoint globalPressPos = q->mapToGlobal(q->isRightToLeft() ? rtl : state->pressPos); int pos = 0; - QPoint delta = event->globalPosition().toPoint() - globalPressPos; + const QWindow *handle = q->window() ? q->window()->windowHandle() : nullptr; + const QPoint delta = handle + ? QHighDpi::fromNativePixels(event->globalPosition(), handle).toPoint() + - QHighDpi::fromNativePixels(globalPressPos, handle) + : event->globalPosition().toPoint() - globalPressPos; + if (orientation == Qt::Vertical) { pos = q->y() + delta.y(); } else { @@ -388,6 +395,10 @@ void QToolBarPrivate::plug(const QRect &r) \ingroup mainwindow-classes \inmodule QtWidgets + A toolbar is typically created by calling + \l QMainWindow::addToolBar(const QString &title), but it can also + be added as the first widget in a QVBoxLayout, for example. + Toolbar buttons are added by adding \e actions, using addAction() or insertAction(). Groups of buttons can be separated using addSeparator() or insertSeparator(). If a toolbar button is not @@ -411,7 +422,7 @@ void QToolBarPrivate::plug(const QRect &r) addWidget(). Please use widget actions created by inheriting QWidgetAction and implementing QWidgetAction::createWidget() instead. - \sa QToolButton, QMenu, QAction, {Application Example} + \sa QToolButton, QMenu, QAction */ /*! @@ -737,117 +748,6 @@ void QToolBar::clear() } /*! - Creates a new action with the given \a text. This action is added to - the end of the toolbar. -*/ -QAction *QToolBar::addAction(const QString &text) -{ - QAction *action = new QAction(text, this); - addAction(action); - return action; -} - -/*! - \overload - - Creates a new action with the given \a icon and \a text. This - action is added to the end of the toolbar. -*/ -QAction *QToolBar::addAction(const QIcon &icon, const QString &text) -{ - QAction *action = new QAction(icon, text, this); - addAction(action); - return action; -} - -/*! - \overload - - Creates a new action with the given \a text. This action is added to - the end of the toolbar. The action's \l{QAction::triggered()}{triggered()} - signal is connected to \a member in \a receiver. -*/ -QAction *QToolBar::addAction(const QString &text, - const QObject *receiver, const char* member) -{ - QAction *action = new QAction(text, this); - QObject::connect(action, SIGNAL(triggered(bool)), receiver, member); - addAction(action); - return action; -} - -/*! - \overload - - Creates a new action with the given \a icon and \a text. This - action is added to the end of the toolbar. The action's - \l{QAction::triggered()}{triggered()} signal is connected to \a - member in \a receiver. -*/ -QAction *QToolBar::addAction(const QIcon &icon, const QString &text, - const QObject *receiver, const char* member) -{ - QAction *action = new QAction(icon, text, this); - QObject::connect(action, SIGNAL(triggered(bool)), receiver, member); - addAction(action); - return action; -} - -/*!\fn template<typename Functor> QAction *QToolBar::addAction(const QString &text, Functor functor) - - \since 5.6 - - \overload - - Creates a new action with the given \a text. This action is added to - the end of the toolbar. The action's - \l{QAction::triggered()}{triggered()} signal is connected to the - \a functor. -*/ - -/*!\fn template<typename Functor> QAction *QToolBar::addAction(const QString &text, const QObject *context, Functor functor) - - \since 5.6 - - \overload - - Creates a new action with the given \a text. This action is added - to the end of the toolbar. The action's - \l{QAction::triggered()}{triggered()} signal is connected to the - \a functor. The \a functor can be a pointer to a member function - in the \a context object. - - If the \a context object is destroyed, the \a functor will not be called. -*/ - -/*!\fn template<typename Functor> QAction *QToolBar::addAction(const QIcon &icon, const QString &text, Functor functor) - - \since 5.6 - - \overload - - Creates a new action with the given \a icon and \a text. This - action is added to the end of the toolbar. The action's - \l{QAction::triggered()}{triggered()} signal is connected to the - \a functor. -*/ - -/*!\fn template<typename Functor> QAction *QToolBar::addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor) - - \since 5.6 - - \overload - - Creates a new action with the given \a icon and \a text. This - action is added to the end of the toolbar. The action's - \l{QAction::triggered()}{triggered()} signal is connected to the - \a functor. The \a functor can be a pointer to a member function - of the \a context object. - - If the \a context object is destroyed, the \a functor will not be called. -*/ - -/*! Adds a separator to the end of the toolbar. \sa insertSeparator() @@ -1164,7 +1064,7 @@ bool QToolBar::event(QEvent *event) case QEvent::Leave: if (d->state != nullptr && d->state->dragging) { #ifdef Q_OS_WIN - // This is a workaround for loosing the mouse on Vista. + // This is a workaround for losing the mouse on Vista. QPoint pos = QCursor::pos(); QMouseEvent fake(QEvent::MouseMove, mapFromGlobal(pos), pos, Qt::NoButton, QGuiApplication::mouseButtons(), QGuiApplication::keyboardModifiers()); @@ -1184,6 +1084,7 @@ bool QToolBar::event(QEvent *event) d->layout->setExpanded(false); break; } + break; default: break; } |