Skip to content

Commit 87b0596

Browse files
rolivavRodrigo Oliva
andauthored
Add feature to close tabs with the middle mouse button. (#360)
Co-authored-by: Rodrigo Oliva <[email protected]>
1 parent dbca6d7 commit 87b0596

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

demo/MainWindow.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,9 @@ CMainWindow::CMainWindow(QWidget *parent) :
630630
// uncomment if you would like to enable an equal distribution of the
631631
// available size of a splitter to all contained dock widgets
632632
// CDockManager::setConfigFlag(CDockManager::EqualSplitOnInsertion, true);
633+
634+
// uncomment if you would like to close tabs with the middle mouse button, web browser style
635+
// CDockManager::setConfigFlag(CDockManager::MiddleMouseButtonClosesTab, true);
633636

634637
// Now create the dock manager and its content
635638
d->DockManager = new CDockManager(this);

src/DockManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class ADS_EXPORT CDockManager : public CDockContainerWidget
195195
FloatingContainerForceQWidgetTitleBar = 0x1000000,//!< Linux only ! Forces all FloatingContainer to use a QWidget based title bar.
196196
//!< If neither this nor FloatingContainerForceNativeTitleBar is set (the default) native titlebars are used except on known bad systems.
197197
//! Users can overwrite this by setting the environment variable ADS_UseNativeTitle to "1" or "0".
198+
MiddleMouseButtonClosesTab = 0x2000000, //! If the flag is set, the user can use the mouse middle button to close the tab under the mouse
198199

199200
DefaultDockAreaButtons = DockAreaHasCloseButton
200201
| DockAreaHasUndockButton

src/DockWidgetTab.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,30 @@ void CDockWidgetTab::mouseReleaseEvent(QMouseEvent* ev)
392392
// End of tab moving, emit signal
393393
if (d->DockArea)
394394
{
395+
ev->accept();
395396
Q_EMIT moved(internal::globalPositionOf(ev));
396397
}
397398
break;
398399

399400
case DraggingFloatingWidget:
401+
ev->accept();
400402
d->FloatingWidget->finishDragging();
401403
break;
402404

403405
default:; // do nothing
404406
}
407+
}
408+
else if (ev->button() == Qt::MiddleButton)
409+
{
410+
if (CDockManager::testConfigFlag(CDockManager::MiddleMouseButtonClosesTab))
411+
{
412+
// Only attempt to close if the mouse is still
413+
// on top of the widget, to allow the user to cancel.
414+
if (rect().contains(mapFromGlobal(QCursor::pos()))) {
415+
ev->accept();
416+
Q_EMIT closeRequested();
417+
}
418+
}
405419
}
406420

407421
Super::mouseReleaseEvent(ev);
@@ -628,14 +642,18 @@ QString CDockWidgetTab::text() const
628642
//============================================================================
629643
void CDockWidgetTab::mouseDoubleClickEvent(QMouseEvent *event)
630644
{
631-
// If this is the last dock area in a dock container it does not make
632-
// sense to move it to a new floating widget and leave this one
633-
// empty
634-
if ((!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1)
635-
&& d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable))
645+
if (event->button() == Qt::LeftButton)
636646
{
637-
d->saveDragStartMousePosition(internal::globalPositionOf(event));
638-
d->startFloating(DraggingInactive);
647+
// If this is the last dock area in a dock container it does not make
648+
// sense to move it to a new floating widget and leave this one
649+
// empty
650+
if ((!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1)
651+
&& d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable))
652+
{
653+
event->accept();
654+
d->saveDragStartMousePosition(internal::globalPositionOf(event));
655+
d->startFloating(DraggingInactive);
656+
}
639657
}
640658

641659
Super::mouseDoubleClickEvent(event);

0 commit comments

Comments
 (0)