Skip to content

Commit

Permalink
Add feature to close tabs with the middle mouse button.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Oliva authored and Rodrigo Oliva committed Oct 6, 2021
1 parent 5f89dd0 commit 806f566
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions demo/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/DockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class ADS_EXPORT CDockManager : public CDockContainerWidget
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
Expand Down
32 changes: 24 additions & 8 deletions src/DockWidgetTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,16 +392,29 @@ 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);
Expand Down Expand Up @@ -628,14 +641,17 @@ 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))
{
d->saveDragStartMousePosition(internal::globalPositionOf(event));
d->startFloating(DraggingInactive);
if (event->button() == Qt::LeftButton) {
// 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);
Expand Down

0 comments on commit 806f566

Please sign in to comment.