Skip to content

Commit 8d1465a

Browse files
Added initial support for setting focus highlighting without uisng setFocus
1 parent 5ead468 commit 8d1465a

File tree

7 files changed

+64
-26
lines changed

7 files changed

+64
-26
lines changed

demo/MainWindow.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ CMainWindow::CMainWindow(QWidget *parent) :
609609
// uncomment the following line to enable focus highlighting of the dock
610610
// widget that has the focus
611611
CDockManager::setConfigFlag(CDockManager::FocusHighlighting, true);
612+
CDockManager::setConfigFlag(CDockManager::AllTabsHaveCloseButton, true);
612613

613614
// uncomment if you would like to enable an equal distribution of the
614615
// available size of a splitter to all contained dock widgets

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: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge
231231
return;
232232
}
233233

234+
/*
234235
// If the close button in another tab steals the focus from the current
235236
// active dock widget content, i.e. if the user clicks its close button,
236237
// then we immediately give the focus back to the previous focused widget
@@ -243,17 +244,18 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge
243244
auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld);
244245
if (OldFocusedDockWidget)
245246
{
246-
focusedOld->setFocus();
247+
//focusedOld->setFocus();
247248
}
248249
return;
249250
}
250-
}
251+
}*/
251252

252253
CDockWidget* DockWidget = nullptr;
253-
auto DockWidgetTab = qobject_cast<CDockWidgetTab*>(focusedNow);
254+
/*auto DockWidgetTab = qobject_cast<CDockWidgetTab*>(focusedNow);
254255
if (DockWidgetTab)
255256
{
256-
DockWidget = DockWidgetTab->dockWidget();
257+
DockWidget = DockWidgetTab->dockWidget();
258+
257259
// If the DockWidgetTab "steals" the focus from a widget in the same
258260
// DockWidget, then we immediately give the focus back to the previous
259261
// focused widget focusedOld
@@ -262,10 +264,10 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge
262264
auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld);
263265
if (OldFocusedDockWidget && OldFocusedDockWidget == DockWidget)
264266
{
265-
focusedOld->setFocus();
267+
//focusedOld->setFocus();
266268
}
267269
}
268-
}
270+
}*/
269271

270272
if (!DockWidget)
271273
{
@@ -293,6 +295,18 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge
293295
}
294296

295297

298+
//===========================================================================
299+
void CDockFocusController::setDockWidgetTabFocused(CDockWidgetTab* Tab)
300+
{
301+
std::cout << "setDockWidgetTabFocused " << Tab->text().toStdString() << std::endl;
302+
auto DockWidget = Tab->dockWidget();
303+
if (DockWidget)
304+
{
305+
d->updateDockWidgetFocus(DockWidget);
306+
}
307+
}
308+
309+
296310
//===========================================================================
297311
void CDockFocusController::setDockWidgetFocused(CDockWidget* focusedNow)
298312
{

src/DockFocusController.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,6 @@ private Q_SLOTS:
4848
*/
4949
virtual ~CDockFocusController();
5050

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-
6651
/**
6752
* A container needs to call this function if a widget has been dropped
6853
* into it
@@ -83,6 +68,12 @@ private Q_SLOTS:
8368
*/
8469
CDockWidget* focusedDockWidget() const;
8570

71+
/**
72+
* Request focus highlighting for the given dock widget assigned to the tab
73+
* given in Tab parameter
74+
*/
75+
void setDockWidgetTabFocused(CDockWidgetTab* Tab);
76+
8677
public Q_SLOTS:
8778
/**
8879
* 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)