1
0
mirror of https://github.com/githubuser0xFFFF/Qt-Advanced-Docking-System.git synced 2025-03-16 02:59:51 +08:00

Added initial support for setting focus highlighting without uisng setFocus

This commit is contained in:
Uwe Kindler 2021-07-25 18:12:27 +02:00
parent 5ead4684f5
commit 8d1465a81f
7 changed files with 64 additions and 26 deletions

View File

@ -609,6 +609,7 @@ CMainWindow::CMainWindow(QWidget *parent) :
// uncomment the following line to enable focus highlighting of the dock // uncomment the following line to enable focus highlighting of the dock
// widget that has the focus // widget that has the focus
CDockManager::setConfigFlag(CDockManager::FocusHighlighting, true); CDockManager::setConfigFlag(CDockManager::FocusHighlighting, true);
CDockManager::setConfigFlag(CDockManager::AllTabsHaveCloseButton, true);
// uncomment if you would like to enable an equal distribution of the // uncomment if you would like to enable an equal distribution of the
// available size of a splitter to all contained dock widgets // available size of a splitter to all contained dock widgets

View File

@ -51,6 +51,7 @@
#include "DockAreaTabBar.h" #include "DockAreaTabBar.h"
#include "IconProvider.h" #include "IconProvider.h"
#include "DockComponentsFactory.h" #include "DockComponentsFactory.h"
#include "DockFocusController.h"
#include <iostream> #include <iostream>
@ -471,7 +472,8 @@ void CDockAreaTitleBar::mousePressEvent(QMouseEvent* ev)
if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting)) 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; return;
} }

View File

@ -231,6 +231,7 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge
return; return;
} }
/*
// If the close button in another tab steals the focus from the current // 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, // 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 // 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<CDockWidget*>(focusedOld); auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld);
if (OldFocusedDockWidget) if (OldFocusedDockWidget)
{ {
focusedOld->setFocus(); //focusedOld->setFocus();
} }
return; return;
} }
} }*/
CDockWidget* DockWidget = nullptr; CDockWidget* DockWidget = nullptr;
auto DockWidgetTab = qobject_cast<CDockWidgetTab*>(focusedNow); /*auto DockWidgetTab = qobject_cast<CDockWidgetTab*>(focusedNow);
if (DockWidgetTab) if (DockWidgetTab)
{ {
DockWidget = DockWidgetTab->dockWidget(); DockWidget = DockWidgetTab->dockWidget();
// If the DockWidgetTab "steals" the focus from a widget in the same // If the DockWidgetTab "steals" the focus from a widget in the same
// DockWidget, then we immediately give the focus back to the previous // DockWidget, then we immediately give the focus back to the previous
// focused widget focusedOld // focused widget focusedOld
@ -262,10 +264,10 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge
auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld); auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld);
if (OldFocusedDockWidget && OldFocusedDockWidget == DockWidget) if (OldFocusedDockWidget && OldFocusedDockWidget == DockWidget)
{ {
focusedOld->setFocus(); //focusedOld->setFocus();
} }
} }
} }*/
if (!DockWidget) 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) void CDockFocusController::setDockWidgetFocused(CDockWidget* focusedNow)
{ {

View File

@ -48,21 +48,6 @@ public:
*/ */
virtual ~CDockFocusController(); virtual ~CDockFocusController();
/**
* Helper function to set focus depending on the configuration of the
* FocusStyling flag
*/
template <class QWidgetPtr>
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 * A container needs to call this function if a widget has been dropped
* into it * into it
@ -83,6 +68,12 @@ public:
*/ */
CDockWidget* focusedDockWidget() const; 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: public Q_SLOTS:
/** /**
* Request a focus change to the given dock widget * Request a focus change to the given dock widget

View File

@ -1140,6 +1140,13 @@ void CDockManager::setSplitterSizes(CDockAreaWidget *ContainedArea, const QList<
} }
} }
//===========================================================================
CDockFocusController* CDockManager::dockFocusController() const
{
return d->FocusController;
}
} // namespace ads } // namespace ads
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -53,6 +53,7 @@ struct DockWidgetTabPrivate;
struct DockAreaWidgetPrivate; struct DockAreaWidgetPrivate;
class CIconProvider; class CIconProvider;
class CDockComponentsFactory; class CDockComponentsFactory;
class CDockFocusController;
/** /**
* The central dock manager that maintains the complete docking system. * The central dock manager that maintains the complete docking system.
@ -134,12 +135,18 @@ protected:
*/ */
void notifyFloatingWidgetDrop(CFloatingDockContainer* FloatingWidget); void notifyFloatingWidgetDrop(CFloatingDockContainer* FloatingWidget);
/** /**
* Show the floating widgets that has been created floating * Show the floating widgets that has been created floating
*/ */
virtual void showEvent(QShowEvent *event) override; 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: public:
using Super = CDockContainerWidget; using Super = CDockContainerWidget;

View File

@ -50,6 +50,7 @@
#include "DockOverlay.h" #include "DockOverlay.h"
#include "DockManager.h" #include "DockManager.h"
#include "IconProvider.h" #include "IconProvider.h"
#include "DockFocusController.h"
namespace ads namespace ads
@ -207,6 +208,14 @@ struct DockWidgetTabPrivate
IconLabel->setVisible(true); IconLabel->setVisible(true);
} }
/**
* Convenience function for access to the dock manager dock focus controller
*/
CDockFocusController* focusController() const
{
return DockWidget->dockManager()->dockFocusController();
}
}; };
// struct DockWidgetTabPrivate // struct DockWidgetTabPrivate
@ -234,6 +243,7 @@ void DockWidgetTabPrivate::createLayout()
CloseButton->setObjectName("tabCloseButton"); CloseButton->setObjectName("tabCloseButton");
internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, TabCloseIcon); internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, TabCloseIcon);
CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
CloseButton->setFocusPolicy(Qt::NoFocus);
updateCloseButtonSizePolicy(); updateCloseButtonSizePolicy();
internal::setToolTip(CloseButton, QObject::tr("Close Tab")); internal::setToolTip(CloseButton, QObject::tr("Close Tab"));
_this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeRequested())); _this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeRequested()));
@ -331,10 +341,11 @@ CDockWidgetTab::CDockWidgetTab(CDockWidget* DockWidget, QWidget *parent) :
setAttribute(Qt::WA_NoMousePropagation, true); setAttribute(Qt::WA_NoMousePropagation, true);
d->DockWidget = DockWidget; d->DockWidget = DockWidget;
d->createLayout(); d->createLayout();
if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting)) setFocusPolicy(Qt::NoFocus);
/*if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting))
{ {
setFocusPolicy(Qt::ClickFocus); setFocusPolicy(Qt::ClickFocus);
} }*/
} }
//============================================================================ //============================================================================
@ -353,6 +364,10 @@ void CDockWidgetTab::mousePressEvent(QMouseEvent* ev)
ev->accept(); ev->accept();
d->saveDragStartMousePosition(internal::globalPositionOf(ev)); d->saveDragStartMousePosition(internal::globalPositionOf(ev));
d->DragState = DraggingMousePressed; d->DragState = DraggingMousePressed;
if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting))
{
d->focusController()->setDockWidgetTabFocused(this);
}
Q_EMIT clicked(); Q_EMIT clicked();
return; return;
} }
@ -515,7 +530,8 @@ void CDockWidgetTab::setActiveTab(bool active)
bool UpdateFocusStyle = false; bool UpdateFocusStyle = false;
if (active && !hasFocus()) if (active && !hasFocus())
{ {
setFocus(Qt::OtherFocusReason); //setFocus(Qt::OtherFocusReason);
d->focusController()->setDockWidgetTabFocused(this);
UpdateFocusStyle = true; UpdateFocusStyle = true;
} }