From 8d1465a81fa5c0328c025d1e7f561a1345948f87 Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Sun, 25 Jul 2021 18:12:27 +0200 Subject: [PATCH] Added initial support for setting focus highlighting without uisng setFocus --- demo/MainWindow.cpp | 1 + src/DockAreaTitleBar.cpp | 4 +++- src/DockFocusController.cpp | 26 ++++++++++++++++++++------ src/DockFocusController.h | 21 ++++++--------------- src/DockManager.cpp | 7 +++++++ src/DockManager.h | 9 ++++++++- src/DockWidgetTab.cpp | 22 +++++++++++++++++++--- 7 files changed, 64 insertions(+), 26 deletions(-) diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index 63eaa45..21df25c 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -609,6 +609,7 @@ 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/DockAreaTitleBar.cpp b/src/DockAreaTitleBar.cpp index 7fbb01a..8651d29 100644 --- a/src/DockAreaTitleBar.cpp +++ b/src/DockAreaTitleBar.cpp @@ -51,6 +51,7 @@ #include "DockAreaTabBar.h" #include "IconProvider.h" #include "DockComponentsFactory.h" +#include "DockFocusController.h" #include @@ -471,7 +472,8 @@ void CDockAreaTitleBar::mousePressEvent(QMouseEvent* ev) if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting)) { - d->TabBar->currentTab()->setFocus(Qt::OtherFocusReason); + //d->TabBar->currentTab()->setFocus(Qt::OtherFocusReason); + d->dockManager()->dockFocusController()->setDockWidgetTabFocused(d->TabBar->currentTab()); } return; } diff --git a/src/DockFocusController.cpp b/src/DockFocusController.cpp index c36f91b..44d452f 100644 --- a/src/DockFocusController.cpp +++ b/src/DockFocusController.cpp @@ -231,6 +231,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 @@ -243,17 +244,18 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge auto OldFocusedDockWidget = internal::findParent(focusedOld); if (OldFocusedDockWidget) { - focusedOld->setFocus(); + //focusedOld->setFocus(); } return; } - } + }*/ CDockWidget* DockWidget = nullptr; - auto DockWidgetTab = qobject_cast(focusedNow); + /*auto DockWidgetTab = qobject_cast(focusedNow); if (DockWidgetTab) { - DockWidget = DockWidgetTab->dockWidget(); + 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 @@ -262,10 +264,10 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge auto OldFocusedDockWidget = internal::findParent(focusedOld); if (OldFocusedDockWidget && OldFocusedDockWidget == DockWidget) { - focusedOld->setFocus(); + //focusedOld->setFocus(); } } - } + }*/ if (!DockWidget) { @@ -293,6 +295,18 @@ 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) + { + d->updateDockWidgetFocus(DockWidget); + } +} + + //=========================================================================== void CDockFocusController::setDockWidgetFocused(CDockWidget* focusedNow) { diff --git a/src/DockFocusController.h b/src/DockFocusController.h index 0a354ef..e5502c6 100644 --- a/src/DockFocusController.h +++ b/src/DockFocusController.h @@ -48,21 +48,6 @@ public: */ virtual ~CDockFocusController(); - /** - * Helper function to set focus depending on the configuration of the - * FocusStyling flag - */ - template - static void setWidgetFocus(QWidgetPtr widget) - { - if (!CDockManager::testConfigFlag(CDockManager::FocusHighlighting)) - { - return; - } - - widget->setFocus(Qt::OtherFocusReason); - } - /** * A container needs to call this function if a widget has been dropped * into it @@ -83,6 +68,12 @@ public: */ CDockWidget* focusedDockWidget() const; + /** + * Request focus highlighting for the given dock widget assigned to the tab + * given in Tab parameter + */ + void setDockWidgetTabFocused(CDockWidgetTab* Tab); + public Q_SLOTS: /** * Request a focus change to the given dock widget diff --git a/src/DockManager.cpp b/src/DockManager.cpp index 42f61b8..9df3a5a 100644 --- a/src/DockManager.cpp +++ b/src/DockManager.cpp @@ -1140,6 +1140,13 @@ void CDockManager::setSplitterSizes(CDockAreaWidget *ContainedArea, const QList< } } + +//=========================================================================== +CDockFocusController* CDockManager::dockFocusController() const +{ + return d->FocusController; +} + } // namespace ads //--------------------------------------------------------------------------- diff --git a/src/DockManager.h b/src/DockManager.h index 390e283..5941ea7 100644 --- a/src/DockManager.h +++ b/src/DockManager.h @@ -53,6 +53,7 @@ struct DockWidgetTabPrivate; struct DockAreaWidgetPrivate; class CIconProvider; class CDockComponentsFactory; +class CDockFocusController; /** * The central dock manager that maintains the complete docking system. @@ -134,12 +135,18 @@ protected: */ void notifyFloatingWidgetDrop(CFloatingDockContainer* FloatingWidget); - /** * Show the floating widgets that has been created floating */ virtual void showEvent(QShowEvent *event) override; + /** + * Acces for the internal dock focus controller. + * This function only returns a valid object, if the FocusHighlighting + * flag is set. + */ + CDockFocusController* dockFocusController() const; + public: using Super = CDockContainerWidget; diff --git a/src/DockWidgetTab.cpp b/src/DockWidgetTab.cpp index a43ba03..9eaa911 100644 --- a/src/DockWidgetTab.cpp +++ b/src/DockWidgetTab.cpp @@ -50,6 +50,7 @@ #include "DockOverlay.h" #include "DockManager.h" #include "IconProvider.h" +#include "DockFocusController.h" namespace ads @@ -207,6 +208,14 @@ struct DockWidgetTabPrivate IconLabel->setVisible(true); } + /** + * Convenience function for access to the dock manager dock focus controller + */ + CDockFocusController* focusController() const + { + return DockWidget->dockManager()->dockFocusController(); + } + }; // struct DockWidgetTabPrivate @@ -234,6 +243,7 @@ void DockWidgetTabPrivate::createLayout() CloseButton->setObjectName("tabCloseButton"); internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, TabCloseIcon); CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + CloseButton->setFocusPolicy(Qt::NoFocus); updateCloseButtonSizePolicy(); internal::setToolTip(CloseButton, QObject::tr("Close Tab")); _this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeRequested())); @@ -331,10 +341,11 @@ CDockWidgetTab::CDockWidgetTab(CDockWidget* DockWidget, QWidget *parent) : setAttribute(Qt::WA_NoMousePropagation, true); d->DockWidget = DockWidget; d->createLayout(); - if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting)) + setFocusPolicy(Qt::NoFocus); + /*if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting)) { setFocusPolicy(Qt::ClickFocus); - } + }*/ } //============================================================================ @@ -353,6 +364,10 @@ void CDockWidgetTab::mousePressEvent(QMouseEvent* ev) ev->accept(); d->saveDragStartMousePosition(internal::globalPositionOf(ev)); d->DragState = DraggingMousePressed; + if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting)) + { + d->focusController()->setDockWidgetTabFocused(this); + } Q_EMIT clicked(); return; } @@ -515,7 +530,8 @@ void CDockWidgetTab::setActiveTab(bool active) bool UpdateFocusStyle = false; if (active && !hasFocus()) { - setFocus(Qt::OtherFocusReason); + //setFocus(Qt::OtherFocusReason); + d->focusController()->setDockWidgetTabFocused(this); UpdateFocusStyle = true; }