Skip to content

Commit 83cdc69

Browse files
Merge branch 'improve_focus_highlighting'
2 parents 2c8abee + 1e3af06 commit 83cdc69

File tree

6 files changed

+90
-62
lines changed

6 files changed

+90
-62
lines changed

src/DockAreaTitleBar.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "DockAreaTabBar.h"
5252
#include "IconProvider.h"
5353
#include "DockComponentsFactory.h"
54+
#include "DockFocusController.h"
5455

5556
#include <iostream>
5657

@@ -471,7 +472,8 @@ void CDockAreaTitleBar::mousePressEvent(QMouseEvent* ev)
471472

472473
if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting))
473474
{
474-
d->TabBar->currentTab()->setFocus(Qt::OtherFocusReason);
475+
//d->TabBar->currentTab()->setFocus(Qt::OtherFocusReason);
476+
d->dockManager()->dockFocusController()->setDockWidgetTabFocused(d->TabBar->currentTab());
475477
}
476478
return;
477479
}

src/DockFocusController.cpp

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <QPointer>
1717
#include <QApplication>
1818
#include <QAbstractButton>
19+
#include <QWindow>
1920

2021
#include "DockWidget.h"
2122
#include "DockAreaWidget.h"
@@ -115,6 +116,11 @@ void DockFocusControllerPrivate::updateDockWidgetFocus(CDockWidget* DockWidget)
115116
return;
116117
}
117118

119+
auto Window = DockWidget->dockContainer()->window()->windowHandle();
120+
if (Window)
121+
{
122+
Window->setProperty("FocusedDockWidget", QVariant::fromValue<CDockWidget*>(DockWidget));
123+
}
118124
CDockAreaWidget* NewFocusedDockArea = nullptr;
119125
if (FocusedDockWidget)
120126
{
@@ -206,6 +212,8 @@ CDockFocusController::CDockFocusController(CDockManager* DockManager) :
206212
d->DockManager = DockManager;
207213
connect(QApplication::instance(), SIGNAL(focusChanged(QWidget*, QWidget*)),
208214
this, SLOT(onApplicationFocusChanged(QWidget*, QWidget*)));
215+
connect(QApplication::instance(), SIGNAL(focusWindowChanged(QWindow*)),
216+
this, SLOT(onFocusWindowChanged(QWindow*)));
209217
connect(d->DockManager, SIGNAL(stateRestored()), SLOT(onStateRestored()));
210218
}
211219

@@ -216,62 +224,48 @@ CDockFocusController::~CDockFocusController()
216224
}
217225

218226

219-
//===========================================================================
220-
void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidget* focusedNow)
227+
//============================================================================
228+
void CDockFocusController::onFocusWindowChanged(QWindow *focusWindow)
221229
{
222-
if (d->DockManager->isRestoringState())
230+
if (!focusWindow)
223231
{
224232
return;
225233
}
226234

227-
ADS_PRINT("CDockFocusController::onApplicationFocusChanged "
228-
<< " old: " << focusedOld << " new: " << focusedNow);
229-
if (!focusedNow)
235+
auto vDockWidget = focusWindow->property("FocusedDockWidget");
236+
if (!vDockWidget.isValid())
230237
{
231238
return;
232239
}
233240

234-
// If the close button in another tab steals the focus from the current
235-
// active dock widget content, i.e. if the user clicks its close button,
236-
// then we immediately give the focus back to the previous focused widget
237-
// focusedOld
238-
if (CDockManager::testConfigFlag(CDockManager::AllTabsHaveCloseButton))
241+
auto DockWidget = vDockWidget.value<CDockWidget*>();
242+
if (!DockWidget)
239243
{
240-
auto OtherDockWidgetTab = internal::findParent<CDockWidgetTab*>(focusedNow);
241-
if (OtherDockWidgetTab && focusedOld)
242-
{
243-
auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld);
244-
if (OldFocusedDockWidget)
245-
{
246-
focusedOld->setFocus();
247-
}
248-
return;
249-
}
244+
return;
250245
}
251246

252-
CDockWidget* DockWidget = nullptr;
253-
auto DockWidgetTab = qobject_cast<CDockWidgetTab*>(focusedNow);
254-
if (DockWidgetTab)
247+
d->updateDockWidgetFocus(DockWidget);
248+
}
249+
250+
251+
//===========================================================================
252+
void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidget* focusedNow)
253+
{
254+
Q_UNUSED(focusedOld);
255+
256+
if (d->DockManager->isRestoringState())
255257
{
256-
DockWidget = DockWidgetTab->dockWidget();
257-
// If the DockWidgetTab "steals" the focus from a widget in the same
258-
// DockWidget, then we immediately give the focus back to the previous
259-
// focused widget focusedOld
260-
if (focusedOld)
261-
{
262-
auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld);
263-
if (OldFocusedDockWidget && OldFocusedDockWidget == DockWidget)
264-
{
265-
focusedOld->setFocus();
266-
}
267-
}
258+
return;
268259
}
269260

270-
if (!DockWidget)
261+
ADS_PRINT("CDockFocusController::onApplicationFocusChanged "
262+
<< " old: " << focusedOld << " new: " << focusedNow);
263+
if (!focusedNow)
271264
{
272-
DockWidget = qobject_cast<CDockWidget*>(focusedNow);
265+
return;
273266
}
274267

268+
CDockWidget* DockWidget = qobject_cast<CDockWidget*>(focusedNow);
275269
if (!DockWidget)
276270
{
277271
DockWidget = internal::findParent<CDockWidget*>(focusedNow);
@@ -293,6 +287,17 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge
293287
}
294288

295289

290+
//===========================================================================
291+
void CDockFocusController::setDockWidgetTabFocused(CDockWidgetTab* Tab)
292+
{
293+
auto DockWidget = Tab->dockWidget();
294+
if (DockWidget)
295+
{
296+
d->updateDockWidgetFocus(DockWidget);
297+
}
298+
}
299+
300+
296301
//===========================================================================
297302
void CDockFocusController::setDockWidgetFocused(CDockWidget* focusedNow)
298303
{
@@ -320,7 +325,7 @@ void CDockFocusController::onFocusedDockAreaViewToggled(bool Open)
320325
return;
321326
}
322327

323-
CDockManager::setWidgetFocus(OpenedDockAreas[0]->currentDockWidget()->tabWidget());
328+
d->updateDockWidgetFocus(OpenedDockAreas[0]->currentDockWidget());
324329
}
325330

326331

@@ -348,7 +353,7 @@ void CDockFocusController::notifyWidgetOrAreaRelocation(QWidget* DroppedWidget)
348353
}
349354

350355
d->ForceFocusChangedSignal = true;
351-
CDockManager::setWidgetFocus(DockWidget->tabWidget());
356+
CDockManager::setWidgetFocus(DockWidget);
352357
}
353358

354359

@@ -369,9 +374,8 @@ void CDockFocusController::notifyFloatingWidgetDrop(CFloatingDockContainer* Floa
369374
auto DockWidget = vDockWidget.value<CDockWidget*>();
370375
if (DockWidget)
371376
{
372-
d->FocusedDockWidget = nullptr;
373377
DockWidget->dockAreaWidget()->setCurrentDockWidget(DockWidget);
374-
CDockManager::setWidgetFocus(DockWidget->tabWidget());
378+
CDockManager::setWidgetFocus(DockWidget);
375379
}
376380
}
377381

src/DockFocusController.h

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ADS_EXPORT CDockFocusController : public QObject
3232

3333
private Q_SLOTS:
3434
void onApplicationFocusChanged(QWidget *old, QWidget *now);
35+
void onFocusWindowChanged(QWindow *focusWindow);
3536
void onFocusedDockAreaViewToggled(bool Open);
3637
void onStateRestored();
3738
void onDockWidgetVisibilityChanged(bool Visible);
@@ -48,21 +49,6 @@ private Q_SLOTS:
4849
*/
4950
virtual ~CDockFocusController();
5051

51-
/**
52-
* Helper function to set focus depending on the configuration of the
53-
* FocusStyling flag
54-
*/
55-
template <class QWidgetPtr>
56-
static void setWidgetFocus(QWidgetPtr widget)
57-
{
58-
if (!CDockManager::testConfigFlag(CDockManager::FocusHighlighting))
59-
{
60-
return;
61-
}
62-
63-
widget->setFocus(Qt::OtherFocusReason);
64-
}
65-
6652
/**
6753
* A container needs to call this function if a widget has been dropped
6854
* into it
@@ -83,6 +69,12 @@ private Q_SLOTS:
8369
*/
8470
CDockWidget* focusedDockWidget() const;
8571

72+
/**
73+
* Request focus highlighting for the given dock widget assigned to the tab
74+
* given in Tab parameter
75+
*/
76+
void setDockWidgetTabFocused(CDockWidgetTab* Tab);
77+
8678
public Q_SLOTS:
8779
/**
8880
* Request a focus change to the given dock widget

src/DockManager.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,13 @@ void CDockManager::setSplitterSizes(CDockAreaWidget *ContainedArea, const QList<
11401140
}
11411141
}
11421142

1143+
1144+
//===========================================================================
1145+
CDockFocusController* CDockManager::dockFocusController() const
1146+
{
1147+
return d->FocusController;
1148+
}
1149+
11431150
} // namespace ads
11441151

11451152
//---------------------------------------------------------------------------

src/DockManager.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct DockWidgetTabPrivate;
5353
struct DockAreaWidgetPrivate;
5454
class CIconProvider;
5555
class CDockComponentsFactory;
56+
class CDockFocusController;
5657

5758
/**
5859
* The central dock manager that maintains the complete docking system.
@@ -134,12 +135,18 @@ class ADS_EXPORT CDockManager : public CDockContainerWidget
134135
*/
135136
void notifyFloatingWidgetDrop(CFloatingDockContainer* FloatingWidget);
136137

137-
138138
/**
139139
* Show the floating widgets that has been created floating
140140
*/
141141
virtual void showEvent(QShowEvent *event) override;
142142

143+
/**
144+
* Acces for the internal dock focus controller.
145+
* This function only returns a valid object, if the FocusHighlighting
146+
* flag is set.
147+
*/
148+
CDockFocusController* dockFocusController() const;
149+
143150
public:
144151
using Super = CDockContainerWidget;
145152

src/DockWidgetTab.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "DockOverlay.h"
5151
#include "DockManager.h"
5252
#include "IconProvider.h"
53+
#include "DockFocusController.h"
5354

5455

5556
namespace ads
@@ -207,6 +208,14 @@ struct DockWidgetTabPrivate
207208
IconLabel->setVisible(true);
208209
}
209210

211+
/**
212+
* Convenience function for access to the dock manager dock focus controller
213+
*/
214+
CDockFocusController* focusController() const
215+
{
216+
return DockWidget->dockManager()->dockFocusController();
217+
}
218+
210219
};
211220
// struct DockWidgetTabPrivate
212221

@@ -234,6 +243,7 @@ void DockWidgetTabPrivate::createLayout()
234243
CloseButton->setObjectName("tabCloseButton");
235244
internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, TabCloseIcon);
236245
CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
246+
CloseButton->setFocusPolicy(Qt::NoFocus);
237247
updateCloseButtonSizePolicy();
238248
internal::setToolTip(CloseButton, QObject::tr("Close Tab"));
239249
_this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeRequested()));
@@ -331,10 +341,11 @@ CDockWidgetTab::CDockWidgetTab(CDockWidget* DockWidget, QWidget *parent) :
331341
setAttribute(Qt::WA_NoMousePropagation, true);
332342
d->DockWidget = DockWidget;
333343
d->createLayout();
334-
if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting))
344+
setFocusPolicy(Qt::NoFocus);
345+
/*if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting))
335346
{
336347
setFocusPolicy(Qt::ClickFocus);
337-
}
348+
}*/
338349
}
339350

340351
//============================================================================
@@ -353,6 +364,10 @@ void CDockWidgetTab::mousePressEvent(QMouseEvent* ev)
353364
ev->accept();
354365
d->saveDragStartMousePosition(internal::globalPositionOf(ev));
355366
d->DragState = DraggingMousePressed;
367+
if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting))
368+
{
369+
d->focusController()->setDockWidgetTabFocused(this);
370+
}
356371
Q_EMIT clicked();
357372
return;
358373
}
@@ -515,7 +530,8 @@ void CDockWidgetTab::setActiveTab(bool active)
515530
bool UpdateFocusStyle = false;
516531
if (active && !hasFocus())
517532
{
518-
setFocus(Qt::OtherFocusReason);
533+
//setFocus(Qt::OtherFocusReason);
534+
d->focusController()->setDockWidgetTabFocused(this);
519535
UpdateFocusStyle = true;
520536
}
521537

0 commit comments

Comments
 (0)