diff --git a/examples/deleteonclose/main.cpp b/examples/deleteonclose/main.cpp index 74150db..b9a36de 100644 --- a/examples/deleteonclose/main.cpp +++ b/examples/deleteonclose/main.cpp @@ -14,7 +14,8 @@ int main(int argc, char *argv[]) ads::CDockManager::setConfigFlag(ads::CDockManager::AllTabsHaveCloseButton, true); auto dockManager = new ads::CDockManager(&w); QObject::connect(dockManager, &ads::CDockManager::focusedDockWidgetChanged, [] (ads::CDockWidget* old, ads::CDockWidget* now) { - qDebug() << "CDockManager::focusedDockWidgetChanged: " << now->objectName() << " visible: " << now->isVisible(); + static int Count = 0; + qDebug() << Count++ << " CDockManager::focusedDockWidgetChanged old: " << (old ? old->objectName() : "-") << " now: " << now->objectName() << " visible: " << now->isVisible(); now->widget()->setFocus(); }); diff --git a/src/DockAreaWidget.cpp b/src/DockAreaWidget.cpp index 325cf10..64dcf24 100644 --- a/src/DockAreaWidget.cpp +++ b/src/DockAreaWidget.cpp @@ -447,7 +447,8 @@ void CDockAreaWidget::insertDockWidget(int index, CDockWidget* DockWidget, void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget) { ADS_PRINT("CDockAreaWidget::removeDockWidget"); - auto NextOpenDockWidget = nextOpenDockWidget(DockWidget); + auto CurrentDockWidget = currentDockWidget(); + auto NextOpenDockWidget = (DockWidget == CurrentDockWidget) ? nextOpenDockWidget(DockWidget) : nullptr; d->ContentsLayout->removeWidget(DockWidget); auto TabWidget = DockWidget->tabWidget(); @@ -466,7 +467,7 @@ void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget) DockContainer->removeDockArea(this); this->deleteLater(); } - else + else if (DockWidget == CurrentDockWidget) { // if contents layout is not empty but there are no more open dock // widgets, then we need to hide the dock area because it does not diff --git a/src/DockFocusController.cpp b/src/DockFocusController.cpp index cb282de..0ecedc4 100644 --- a/src/DockFocusController.cpp +++ b/src/DockFocusController.cpp @@ -160,6 +160,11 @@ void DockFocusControllerPrivate::updateDockWidgetFocus(CDockWidget* DockWidget) } #endif + if (old == DockWidget) + { + return; + } + if (DockWidget->isVisible()) { emit DockManager->focusedDockWidgetChanged(old, DockWidget); @@ -212,6 +217,8 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge return; } + //qDebug() << "\n----------------------------"; + //qDebug() << "CDockFocusController::onApplicationFocusChanged " << " old: " << focusedOld << " new: " << focusedNow; Q_UNUSED(focusedOld) if (!focusedNow) { @@ -223,6 +230,14 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge 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; + } } if (!DockWidget) @@ -278,7 +293,8 @@ void CDockFocusController::onFocusedDockAreaViewToggled(bool Open) return; } - CDockManager::setWidgetFocus(OpenedDockAreas[0]->currentDockWidget()->tabWidget()); + //CDockManager::setWidgetFocus(OpenedDockAreas[0]->currentDockWidget()->tabWidget()); + d->updateDockWidgetFocus(OpenedDockAreas[0]->currentDockWidget()); } @@ -293,7 +309,8 @@ void CDockFocusController::notifyWidgetOrAreaRelocation(QWidget* DroppedWidget) CDockWidget* DockWidget = qobject_cast(DroppedWidget); if (DockWidget) { - CDockManager::setWidgetFocus(DockWidget->tabWidget()); + //CDockManager::setWidgetFocus(DockWidget->tabWidget()); + d->updateDockWidgetFocus(DockWidget); return; } @@ -304,7 +321,8 @@ void CDockFocusController::notifyWidgetOrAreaRelocation(QWidget* DroppedWidget) } DockWidget = DockArea->currentDockWidget(); - CDockManager::setWidgetFocus(DockWidget->tabWidget()); + //CDockManager::setWidgetFocus(DockWidget->tabWidget()); + d->updateDockWidgetFocus(DockWidget); } @@ -325,8 +343,11 @@ void CDockFocusController::notifyFloatingWidgetDrop(CFloatingDockContainer* Floa auto DockWidget = vDockWidget.value(); if (DockWidget) { + d->FocusedDockWidget = nullptr; DockWidget->dockAreaWidget()->setCurrentDockWidget(DockWidget); - CDockManager::setWidgetFocus(DockWidget->tabWidget()); + //CDockManager::setWidgetFocus(DockWidget->tabWidget()); + //qDebug() << "CDockFocusController::notifyFloatingWidgetDrop"; + d->updateDockWidgetFocus(DockWidget); } }