From 5af7492b67041c9875dbf2c81cbc9dcca046e7e8 Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Tue, 27 Jul 2021 12:22:31 +0200 Subject: [PATCH] Changed DockFocusController to properly handle window activation --- demo/MainWindow.cpp | 1 - src/DockFocusController.cpp | 89 +++++++++++++++++-------------------- src/DockFocusController.h | 1 + 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index 21df25c..63eaa45 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -609,7 +609,6 @@ CMainWindow::CMainWindow(QWidget *parent) : // uncomment the following line to enable focus highlighting of the dock // widget that has the focus CDockManager::setConfigFlag(CDockManager::FocusHighlighting, true); - CDockManager::setConfigFlag(CDockManager::AllTabsHaveCloseButton, true); // uncomment if you would like to enable an equal distribution of the // available size of a splitter to all contained dock widgets diff --git a/src/DockFocusController.cpp b/src/DockFocusController.cpp index 44d452f..4490f87 100644 --- a/src/DockFocusController.cpp +++ b/src/DockFocusController.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "DockWidget.h" #include "DockAreaWidget.h" @@ -115,6 +116,11 @@ void DockFocusControllerPrivate::updateDockWidgetFocus(CDockWidget* DockWidget) return; } + auto Window = DockWidget->dockContainer()->window()->windowHandle(); + if (Window) + { + Window->setProperty("FocusedDockWidget", QVariant::fromValue(DockWidget)); + } CDockAreaWidget* NewFocusedDockArea = nullptr; if (FocusedDockWidget) { @@ -206,6 +212,8 @@ CDockFocusController::CDockFocusController(CDockManager* DockManager) : d->DockManager = DockManager; connect(QApplication::instance(), SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(onApplicationFocusChanged(QWidget*, QWidget*))); + connect(QApplication::instance(), SIGNAL(focusWindowChanged(QWindow*)), + this, SLOT(onFocusWindowChanged(QWindow*))); connect(d->DockManager, SIGNAL(stateRestored()), SLOT(onStateRestored())); } @@ -216,6 +224,30 @@ CDockFocusController::~CDockFocusController() } +//============================================================================ +void CDockFocusController::onFocusWindowChanged(QWindow *focusWindow) +{ + if (!focusWindow) + { + return; + } + + auto vDockWidget = focusWindow->property("FocusedDockWidget"); + if (!vDockWidget.isValid()) + { + return; + } + + auto DockWidget = vDockWidget.value(); + if (!DockWidget) + { + return; + } + + d->updateDockWidgetFocus(DockWidget); +} + + //=========================================================================== void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidget* focusedNow) { @@ -231,49 +263,7 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge return; } - /* - // If the close button in another tab steals the focus from the current - // active dock widget content, i.e. if the user clicks its close button, - // then we immediately give the focus back to the previous focused widget - // focusedOld - if (CDockManager::testConfigFlag(CDockManager::AllTabsHaveCloseButton)) - { - auto OtherDockWidgetTab = internal::findParent(focusedNow); - if (OtherDockWidgetTab && focusedOld) - { - auto OldFocusedDockWidget = internal::findParent(focusedOld); - if (OldFocusedDockWidget) - { - //focusedOld->setFocus(); - } - return; - } - }*/ - - CDockWidget* DockWidget = nullptr; - /*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 (focusedOld) - { - auto OldFocusedDockWidget = internal::findParent(focusedOld); - if (OldFocusedDockWidget && OldFocusedDockWidget == DockWidget) - { - //focusedOld->setFocus(); - } - } - }*/ - - if (!DockWidget) - { - DockWidget = qobject_cast(focusedNow); - } - + CDockWidget* DockWidget = qobject_cast(focusedNow); if (!DockWidget) { DockWidget = internal::findParent(focusedNow); @@ -298,7 +288,6 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge //=========================================================================== void CDockFocusController::setDockWidgetTabFocused(CDockWidgetTab* Tab) { - std::cout << "setDockWidgetTabFocused " << Tab->text().toStdString() << std::endl; auto DockWidget = Tab->dockWidget(); if (DockWidget) { @@ -334,7 +323,7 @@ void CDockFocusController::onFocusedDockAreaViewToggled(bool Open) return; } - CDockManager::setWidgetFocus(OpenedDockAreas[0]->currentDockWidget()->tabWidget()); + d->updateDockWidgetFocus(OpenedDockAreas[0]->currentDockWidget()); } @@ -362,7 +351,7 @@ void CDockFocusController::notifyWidgetOrAreaRelocation(QWidget* DroppedWidget) } d->ForceFocusChangedSignal = true; - CDockManager::setWidgetFocus(DockWidget->tabWidget()); + CDockManager::setWidgetFocus(DockWidget); } @@ -383,9 +372,15 @@ void CDockFocusController::notifyFloatingWidgetDrop(CFloatingDockContainer* Floa auto DockWidget = vDockWidget.value(); if (DockWidget) { + /*auto Window = DockWidget->dockContainer()->window()->windowHandle(); + DockWidget->dockContainer()->window()->clearFocus(); + if (Window) + { + Window->setProperty("FocusedDockWidget", QVariant::fromValue(DockWidget)); + }*/ d->FocusedDockWidget = nullptr; DockWidget->dockAreaWidget()->setCurrentDockWidget(DockWidget); - CDockManager::setWidgetFocus(DockWidget->tabWidget()); + CDockManager::setWidgetFocus(DockWidget); } } diff --git a/src/DockFocusController.h b/src/DockFocusController.h index e5502c6..b080f3b 100644 --- a/src/DockFocusController.h +++ b/src/DockFocusController.h @@ -32,6 +32,7 @@ private: private Q_SLOTS: void onApplicationFocusChanged(QWidget *old, QWidget *now); + void onFocusWindowChanged(QWindow *focusWindow); void onFocusedDockAreaViewToggled(bool Open); void onStateRestored(); void onDockWidgetVisibilityChanged(bool Visible);