Skip to content

Commit f5c4b26

Browse files
Moved focus related functionality into CDockFocusController class to keep the dock manager code clean
1 parent c4d2d72 commit f5c4b26

File tree

6 files changed

+18
-243
lines changed

6 files changed

+18
-243
lines changed

demo/MainWindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ CMainWindow::CMainWindow(QWidget *parent) :
571571
// dock widget.
572572
// CDockManager::setConfigFlag(CDockManager::HideSingleCentralWidgetTitleBar, true);
573573

574-
CDockManager::setConfigFlag(CDockManager::AlwaysShowTabs, true);
574+
//CDockManager::setConfigFlag(CDockManager::AlwaysShowTabs, true);
575575
CDockManager::setConfigFlag(CDockManager::FocusStyling, true);
576576

577577
// Now create the dock manager and its content

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set(ads_SRCS
1717
DockWidget.cpp
1818
DockWidgetTab.cpp
1919
DockingStateReader.cpp
20+
DockFocusController.cpp
2021
ElidingLabel.cpp
2122
FloatingDockContainer.cpp
2223
FloatingDragPreview.cpp
@@ -37,6 +38,7 @@ set(ads_INSTALL_INCLUDE
3738
DockWidget.h
3839
DockWidgetTab.h
3940
DockingStateReader.h
41+
DockFocusController.h
4042
ElidingLabel.h
4143
FloatingDockContainer.h
4244
FloatingDragPreview.h

src/DockManager.cpp

Lines changed: 8 additions & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
#include <QSettings>
4646
#include <QMenu>
4747
#include <QApplication>
48-
#include <QGuiApplication>
49-
#include <QPointer>
5048

5149
#include "FloatingDockContainer.h"
5250
#include "DockOverlay.h"
@@ -56,6 +54,7 @@
5654
#include "IconProvider.h"
5755
#include "DockingStateReader.h"
5856
#include "DockAreaTitleBar.h"
57+
#include "DockFocusController.h"
5958

6059
#ifdef Q_OS_LINUX
6160
#include "linux/FloatingWidgetTitleBar.h"
@@ -108,9 +107,7 @@ struct DockManagerPrivate
108107
CDockManager::eViewMenuInsertionOrder MenuInsertionOrder = CDockManager::MenuAlphabeticallySorted;
109108
bool RestoringState = false;
110109
QVector<CFloatingDockContainer*> UninitializedFloatingWidgets;
111-
QPointer<CDockWidget> FocusedDockWidget = nullptr;
112-
QPointer<CDockAreaWidget> FocusedArea = nullptr;
113-
QPointer<CFloatingDockContainer> FloatingWidget = nullptr;
110+
CDockFocusController* FocusController = nullptr;
114111

115112
/**
116113
* Private data constructor
@@ -168,12 +165,6 @@ struct DockManagerPrivate
168165
* Adds action to menu - optionally in sorted order
169166
*/
170167
void addActionToMenu(QAction* Action, QMenu* Menu, bool InsertSorted);
171-
172-
/**
173-
* This function updates the focus style of the given dock widget and
174-
* the dock area that it belongs to
175-
*/
176-
void updateDockWidgetFocus(CDockWidget* DockWidget);
177168
};
178169
// struct DockManagerPrivate
179170

@@ -458,111 +449,6 @@ void DockManagerPrivate::addActionToMenu(QAction* Action, QMenu* Menu, bool Inse
458449
}
459450

460451

461-
//===========================================================================
462-
void updateDockWidgetFocusStyle(CDockWidget* DockWidget, bool Focused)
463-
{
464-
DockWidget->setProperty("focused", Focused);
465-
DockWidget->tabWidget()->setProperty("focused", Focused);
466-
DockWidget->tabWidget()->updateStyle();
467-
internal::repolishStyle(DockWidget);
468-
}
469-
470-
471-
//===========================================================================
472-
void updateDockAreaFocusStyle(CDockAreaWidget* DockArea, bool Focused)
473-
{
474-
DockArea->setProperty("focused", Focused);
475-
internal::repolishStyle(DockArea);
476-
internal::repolishStyle(DockArea->titleBar());
477-
}
478-
479-
480-
//===========================================================================
481-
void updateFloatingWidgetFocusStyle(CFloatingDockContainer* FloatingWidget, bool Focused)
482-
{
483-
#ifdef Q_OS_LINUX
484-
auto TitleBar = qobject_cast<CFloatingWidgetTitleBar*>(FloatingWidget->titleBarWidget());
485-
if (!TitleBar)
486-
{
487-
return;
488-
}
489-
TitleBar->setProperty("focused", Focused);
490-
TitleBar->updateStyle();
491-
#else
492-
Q_UNUSED(FloatingWidget)
493-
Q_UNUSED(Focused)
494-
#endif
495-
}
496-
497-
498-
//============================================================================
499-
void DockManagerPrivate::updateDockWidgetFocus(CDockWidget* DockWidget)
500-
{
501-
CDockAreaWidget* NewFocusedDockArea = nullptr;
502-
if (FocusedDockWidget)
503-
{
504-
updateDockWidgetFocusStyle(FocusedDockWidget, false);
505-
}
506-
507-
CDockWidget* old = FocusedDockWidget;
508-
if (DockWidget != FocusedDockWidget)
509-
{
510-
std::cout << "!!!!!!!!!!!! focusedDockWidgetChanged " << (FocusedDockWidget ? FocusedDockWidget->objectName().toStdString() : "-")
511-
<< " -> " << (DockWidget ? DockWidget->objectName().toStdString() : "-") << std::endl;
512-
}
513-
FocusedDockWidget = DockWidget;
514-
updateDockWidgetFocusStyle(FocusedDockWidget, true);
515-
NewFocusedDockArea = FocusedDockWidget->dockAreaWidget();
516-
if (NewFocusedDockArea && (FocusedArea != NewFocusedDockArea))
517-
{
518-
if (FocusedArea)
519-
{
520-
std::cout << "FocusedArea" << std::endl;
521-
QObject::disconnect(FocusedArea, SIGNAL(viewToggled(bool)), _this, SLOT(onFocusedDockAreaViewToggled(bool)));
522-
updateDockAreaFocusStyle(FocusedArea, false);
523-
}
524-
525-
FocusedArea = NewFocusedDockArea;
526-
updateDockAreaFocusStyle(FocusedArea, true);
527-
QObject::connect(FocusedArea, SIGNAL(viewToggled(bool)), _this, SLOT(onFocusedDockAreaViewToggled(bool)));
528-
}
529-
530-
531-
auto NewFloatingWidget = FocusedDockWidget->dockContainer()->floatingWidget();
532-
if (NewFloatingWidget)
533-
{
534-
std::cout << "NewFloatingWidget->setProperty(FocusedDockWidget)" << std::endl;
535-
NewFloatingWidget->setProperty("FocusedDockWidget", QVariant::fromValue(DockWidget));
536-
}
537-
538-
539-
#ifdef Q_OS_LINUX
540-
// This code is required for styling the floating widget titlebar for linux
541-
// depending on the current focus state
542-
if (FloatingWidget == NewFloatingWidget)
543-
{
544-
return;
545-
}
546-
547-
if (FloatingWidget)
548-
{
549-
updateFloatingWidgetFocusStyle(FloatingWidget, false);
550-
}
551-
FloatingWidget = NewFloatingWidget;
552-
553-
if (FloatingWidget)
554-
{
555-
updateFloatingWidgetFocusStyle(FloatingWidget, true);
556-
}
557-
#endif
558-
559-
if (old != DockWidget)
560-
{
561-
emit _this->focusedDockWidgetChanged(old, DockWidget);
562-
}
563-
}
564-
565-
566452
//============================================================================
567453
CDockManager::CDockManager(QWidget *parent) :
568454
CDockContainerWidget(this, parent),
@@ -583,8 +469,7 @@ CDockManager::CDockManager(QWidget *parent) :
583469

584470
if (CDockManager::configFlags().testFlag(CDockManager::FocusStyling))
585471
{
586-
connect(QApplication::instance(), SIGNAL(focusChanged(QWidget*, QWidget*)),
587-
this, SLOT(onApplicationFocusChanged(QWidget*, QWidget*)));
472+
d->FocusController = new CDockFocusController(this);
588473
}
589474
}
590475

@@ -720,16 +605,11 @@ bool CDockManager::restoreState(const QByteArray &state, int version)
720605
emit restoringState();
721606
bool Result = d->restoreState(state, version);
722607
d->RestoringState = false;
723-
emit stateRestored();
724608
if (!IsHidden)
725609
{
726610
show();
727611
}
728-
729-
if (d->FocusedDockWidget)
730-
{
731-
updateDockWidgetFocusStyle(d->FocusedDockWidget, false);
732-
}
612+
emit stateRestored();
733613
return Result;
734614
}
735615

@@ -1026,127 +906,23 @@ CIconProvider& CDockManager::iconProvider()
1026906
}
1027907

1028908

1029-
//===========================================================================
1030-
void CDockManager::onApplicationFocusChanged(QWidget* focusedOld, QWidget* focusedNow)
1031-
{
1032-
if (isRestoringState())
1033-
{
1034-
return;
1035-
}
1036-
std::cout << "CDockManager::onFocusChanged" << std::endl;
1037-
std::cout << "focusedNow " << focusedNow << std::endl;
1038-
Q_UNUSED(focusedOld)
1039-
if (!focusedNow)
1040-
{
1041-
return;
1042-
}
1043-
1044-
CDockWidget* DockWidget = nullptr;
1045-
auto DockWidgetTab = qobject_cast<CDockWidgetTab*>(focusedNow);
1046-
std::cout << "FocuseNow " << focusedNow->metaObject()->className() << std::endl;
1047-
if (DockWidgetTab)
1048-
{
1049-
DockWidget = DockWidgetTab->dockWidget();
1050-
}
1051-
else
1052-
{
1053-
DockWidget = internal::findParent<CDockWidget*>(focusedNow);
1054-
}
1055-
1056-
#ifdef Q_OS_LINUX
1057-
if (!DockWidget)
1058-
{
1059-
return;
1060-
}
1061-
#else
1062-
if (!DockWidget || DockWidget->tabWidget()->isHidden())
1063-
{
1064-
std::cout << "!DockWidget || !DockWidget->tabWidget()->isVisible() " << (DockWidget ? DockWidget->objectName().toStdString() : "0") << std::endl;
1065-
std::cout << "DockWidget->tabWidget()->isHidden() " << (DockWidget ? DockWidget->tabWidget()->isHidden() : false) << std::endl;
1066-
return;
1067-
}
1068-
#endif
1069-
1070-
std::cout << "CDockManager::onFocusChanged " << DockWidget->tabWidget()->text().toStdString() << std::endl;
1071-
d->updateDockWidgetFocus(DockWidget);
1072-
}
1073-
1074-
1075-
//===========================================================================
1076-
void CDockManager::onFocusedDockAreaViewToggled(bool Open)
1077-
{
1078-
if (isRestoringState())
1079-
{
1080-
return;
1081-
}
1082-
1083-
CDockAreaWidget* DockArea = qobject_cast<CDockAreaWidget*>(sender());
1084-
if (!DockArea || Open)
1085-
{
1086-
return;
1087-
}
1088-
auto Container = DockArea->dockContainer();
1089-
auto OpenedDockAreas = Container->openedDockAreas();
1090-
if (OpenedDockAreas.isEmpty())
1091-
{
1092-
return;
1093-
}
1094-
1095-
CDockManager::setWidgetFocus(OpenedDockAreas[0]->currentDockWidget()->tabWidget());
1096-
}
1097-
1098-
1099909
//===========================================================================
1100910
void CDockManager::notifyWidgetOrAreaRelocation(QWidget* DroppedWidget)
1101911
{
1102-
if (isRestoringState())
1103-
{
1104-
return;
1105-
}
1106-
std::cout << "\n\nCDockManager::notifyWidgetDrop" << std::endl;
1107-
CDockWidget* DockWidget = qobject_cast<CDockWidget*>(DroppedWidget);
1108-
if (DockWidget)
1109-
{
1110-
std::cout << "CDockManager::setWidgetFocus " << DockWidget->objectName().toStdString() << std::endl;
1111-
CDockManager::setWidgetFocus(DockWidget->tabWidget());
1112-
return;
1113-
}
1114-
1115-
CDockAreaWidget* DockArea = qobject_cast<CDockAreaWidget*>(DroppedWidget);
1116-
if (!DockArea)
912+
if (d->FocusController)
1117913
{
1118-
return;
914+
d->FocusController->notifyWidgetOrAreaRelocation(DroppedWidget);
1119915
}
1120-
1121-
DockWidget = DockArea->currentDockWidget();
1122-
CDockManager::setWidgetFocus(DockWidget->tabWidget());
1123-
std::cout << "\n\n" << std::endl;
1124916
}
1125917

1126918

1127919
//===========================================================================
1128920
void CDockManager::notifyFloatingWidgetDrop(CFloatingDockContainer* FloatingWidget)
1129921
{
1130-
std::cout << "\n\nCDockManager::notifyFloatingWidgetDrop" << std::endl;
1131-
if (!FloatingWidget || isRestoringState())
1132-
{
1133-
return;
1134-
}
1135-
1136-
auto vDockWidget = FloatingWidget->property("FocusedDockWidget");
1137-
if (!vDockWidget.isValid())
1138-
{
1139-
return;
1140-
}
1141-
std::cout << "vDockWidget.isValid()" << std::endl;
1142-
auto DockWidget = vDockWidget.value<CDockWidget*>();
1143-
if (DockWidget)
922+
if (d->FocusController)
1144923
{
1145-
std::cout << "Dropped focus dock widget " << DockWidget->objectName().toStdString() << std::endl;
1146-
DockWidget->dockAreaWidget()->setCurrentDockWidget(DockWidget);
1147-
CDockManager::setWidgetFocus(DockWidget->tabWidget());
924+
d->FocusController->notifyFloatingWidgetDrop(FloatingWidget);
1148925
}
1149-
std::cout << "\n\n" << std::endl;
1150926
}
1151927

1152928

src/DockManager.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ class ADS_EXPORT CDockManager : public CDockContainerWidget
8484
friend struct FloatingDragPreviewPrivate;
8585
friend class CDockAreaTitleBar;
8686

87-
private slots:
88-
void onApplicationFocusChanged(QWidget *old, QWidget *now);
89-
void onFocusedDockAreaViewToggled(bool Open);
9087

9188
protected:
9289
/**
@@ -122,6 +119,7 @@ private slots:
122119
*/
123120
CDockOverlay* dockAreaOverlay() const;
124121

122+
125123
/**
126124
* A container needs to call this function if a widget has been dropped
127125
* into it
@@ -136,6 +134,7 @@ private slots:
136134
*/
137135
void notifyFloatingWidgetDrop(CFloatingDockContainer* FloatingWidget);
138136

137+
139138
/**
140139
* Show the floating widgets that has been created floating
141140
*/

src/DockWidgetTab.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,7 @@ void CDockWidgetTab::setActiveTab(bool active)
467467
bool TabHasCloseButton = (ActiveTabHasCloseButton && active) | AllTabsHaveCloseButton;
468468
d->CloseButton->setVisible(DockWidgetClosable && TabHasCloseButton);
469469

470-
/*if (d->IsActiveTab == active)
471-
{
472-
return;
473-
}*/
474-
470+
// Focus related stuff
475471
if (CDockManager::configFlags().testFlag(CDockManager::FocusStyling) && !d->DockWidget->dockManager()->isRestoringState())
476472
{
477473
bool UpdateFocusStyle = false;

src/src.pro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ HEADERS += \
4545
DockAreaTitleBar.h \
4646
ElidingLabel.h \
4747
IconProvider.h \
48-
DockComponentsFactory.h
48+
DockComponentsFactory.h \
49+
DockFocusController.h
4950

5051

5152
SOURCES += \
@@ -64,7 +65,8 @@ SOURCES += \
6465
DockAreaTitleBar.cpp \
6566
ElidingLabel.cpp \
6667
IconProvider.cpp \
67-
DockComponentsFactory.cpp
68+
DockComponentsFactory.cpp \
69+
DockFocusController.cpp
6870

6971

7072
unix {

0 commit comments

Comments
 (0)