From 87b0596ebcda61e7f827c3d9fc9b96883a58d8c9 Mon Sep 17 00:00:00 2001 From: Rodrigo Oliva Date: Mon, 11 Oct 2021 20:33:56 +0200 Subject: [PATCH] Add feature to close tabs with the middle mouse button. (#360) Co-authored-by: Rodrigo Oliva --- demo/MainWindow.cpp | 3 +++ src/DockManager.h | 1 + src/DockWidgetTab.cpp | 32 +++++++++++++++++++++++++------- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index c8b323b..9745755 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -630,6 +630,9 @@ CMainWindow::CMainWindow(QWidget *parent) : // uncomment if you would like to enable an equal distribution of the // available size of a splitter to all contained dock widgets // CDockManager::setConfigFlag(CDockManager::EqualSplitOnInsertion, true); + + // uncomment if you would like to close tabs with the middle mouse button, web browser style + // CDockManager::setConfigFlag(CDockManager::MiddleMouseButtonClosesTab, true); // Now create the dock manager and its content d->DockManager = new CDockManager(this); diff --git a/src/DockManager.h b/src/DockManager.h index 5941ea7..d549a68 100644 --- a/src/DockManager.h +++ b/src/DockManager.h @@ -195,6 +195,7 @@ public: FloatingContainerForceQWidgetTitleBar = 0x1000000,//!< Linux only ! Forces all FloatingContainer to use a QWidget based title bar. //!< If neither this nor FloatingContainerForceNativeTitleBar is set (the default) native titlebars are used except on known bad systems. //! Users can overwrite this by setting the environment variable ADS_UseNativeTitle to "1" or "0". + MiddleMouseButtonClosesTab = 0x2000000, //! If the flag is set, the user can use the mouse middle button to close the tab under the mouse DefaultDockAreaButtons = DockAreaHasCloseButton | DockAreaHasUndockButton diff --git a/src/DockWidgetTab.cpp b/src/DockWidgetTab.cpp index 9eaa911..968bb6f 100644 --- a/src/DockWidgetTab.cpp +++ b/src/DockWidgetTab.cpp @@ -392,16 +392,30 @@ void CDockWidgetTab::mouseReleaseEvent(QMouseEvent* ev) // End of tab moving, emit signal if (d->DockArea) { + ev->accept(); Q_EMIT moved(internal::globalPositionOf(ev)); } break; case DraggingFloatingWidget: + ev->accept(); d->FloatingWidget->finishDragging(); break; default:; // do nothing } + } + else if (ev->button() == Qt::MiddleButton) + { + if (CDockManager::testConfigFlag(CDockManager::MiddleMouseButtonClosesTab)) + { + // Only attempt to close if the mouse is still + // on top of the widget, to allow the user to cancel. + if (rect().contains(mapFromGlobal(QCursor::pos()))) { + ev->accept(); + Q_EMIT closeRequested(); + } + } } Super::mouseReleaseEvent(ev); @@ -628,14 +642,18 @@ QString CDockWidgetTab::text() const //============================================================================ void CDockWidgetTab::mouseDoubleClickEvent(QMouseEvent *event) { - // If this is the last dock area in a dock container it does not make - // sense to move it to a new floating widget and leave this one - // empty - if ((!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1) - && d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable)) + if (event->button() == Qt::LeftButton) { - d->saveDragStartMousePosition(internal::globalPositionOf(event)); - d->startFloating(DraggingInactive); + // If this is the last dock area in a dock container it does not make + // sense to move it to a new floating widget and leave this one + // empty + if ((!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1) + && d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable)) + { + event->accept(); + d->saveDragStartMousePosition(internal::globalPositionOf(event)); + d->startFloating(DraggingInactive); + } } Super::mouseDoubleClickEvent(event);