From aa25e1fd56cb48c2690d02a9d7139699d0a33222 Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Mon, 13 Jul 2020 08:41:30 +0200 Subject: [PATCH] Improved emission of focusedDockWidgetChanged signal to ensure, that the application can restore the focus of the focused application dock widget content --- src/DockAreaTabBar.cpp | 2 ++ src/DockAreaTitleBar.cpp | 4 +++- src/DockFocusController.cpp | 27 +++++++++++++++++---------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/DockAreaTabBar.cpp b/src/DockAreaTabBar.cpp index 2dfa2ec..842e3da 100644 --- a/src/DockAreaTabBar.cpp +++ b/src/DockAreaTabBar.cpp @@ -138,6 +138,8 @@ CDockAreaTabBar::CDockAreaTabBar(CDockAreaWidget* parent) : d->TabsLayout->addStretch(1); d->TabsContainerWidget->setLayout(d->TabsLayout); setWidget(d->TabsContainerWidget); + + setFocusPolicy(Qt::NoFocus); } diff --git a/src/DockAreaTitleBar.cpp b/src/DockAreaTitleBar.cpp index 99f977a..8d13690 100644 --- a/src/DockAreaTitleBar.cpp +++ b/src/DockAreaTitleBar.cpp @@ -269,6 +269,8 @@ CDockAreaTitleBar::CDockAreaTitleBar(CDockAreaWidget* parent) : d->createTabBar(); d->Layout->addWidget(new CSpacerWidget(this)); d->createButtons(); + + setFocusPolicy(Qt::NoFocus); } @@ -606,7 +608,7 @@ CTitleBarButton::CTitleBarButton(bool visible, QWidget* parent) Visible(visible), HideWhenDisabled(CDockManager::testConfigFlag(CDockManager::DockAreaHideDisabledButtons)) { - + setFocusPolicy(Qt::NoFocus); } //============================================================================ diff --git a/src/DockFocusController.cpp b/src/DockFocusController.cpp index a4a6eba..b3efcee 100644 --- a/src/DockFocusController.cpp +++ b/src/DockFocusController.cpp @@ -44,6 +44,7 @@ struct DockFocusControllerPrivate QPointer FloatingWidget = nullptr; #endif CDockManager* DockManager; + bool ForceFocusChangedSignal = false; /** * Private data constructor @@ -161,11 +162,12 @@ void DockFocusControllerPrivate::updateDockWidgetFocus(CDockWidget* DockWidget) } #endif - if (old == DockWidget) + if (old == DockWidget && !ForceFocusChangedSignal) { return; } + ForceFocusChangedSignal = false; if (DockWidget->isVisible()) { emit DockManager->focusedDockWidgetChanged(old, DockWidget); @@ -243,15 +245,18 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge auto DockWidgetTab = qobject_cast(focusedNow); if (DockWidgetTab) { - DockWidget = DockWidgetTab->dockWidget(); - // If the DockWidgetTab "steals" the focus from a widget in the same - // DockWidget, then we immediately give the focus back to the previous - // focused widget focusedOld - if (DockWidget == d->FocusedDockWidget && focusedOld && focusedOld != focusedNow) - { - focusedOld->setFocus(); - return; - } + DockWidget = DockWidgetTab->dockWidget(); + // If the DockWidgetTab "steals" the focus from a widget in the same + // DockWidget, then we immediately give the focus back to the previous + // focused widget focusedOld + if (focusedOld) + { + auto OldFocusedDockWidget = internal::findParent(focusedOld); + if (OldFocusedDockWidget && OldFocusedDockWidget == DockWidget) + { + focusedOld->setFocus(); + } + } } if (!DockWidget) @@ -322,6 +327,7 @@ void CDockFocusController::notifyWidgetOrAreaRelocation(QWidget* DroppedWidget) CDockWidget* DockWidget = qobject_cast(DroppedWidget); if (DockWidget) { + d->ForceFocusChangedSignal = true; CDockManager::setWidgetFocus(DockWidget->tabWidget()); return; } @@ -333,6 +339,7 @@ void CDockFocusController::notifyWidgetOrAreaRelocation(QWidget* DroppedWidget) } DockWidget = DockArea->currentDockWidget(); + d->ForceFocusChangedSignal = true; CDockManager::setWidgetFocus(DockWidget->tabWidget()); }