Skip to content

Commit 5af7492

Browse files
Changed DockFocusController to properly handle window activation
1 parent 8d1465a commit 5af7492

File tree

3 files changed

+42
-47
lines changed

3 files changed

+42
-47
lines changed

demo/MainWindow.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ 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);
613612

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

src/DockFocusController.cpp

Lines changed: 41 additions & 46 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,64 +224,46 @@ 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-
/*
235-
// If the close button in another tab steals the focus from the current
236-
// active dock widget content, i.e. if the user clicks its close button,
237-
// then we immediately give the focus back to the previous focused widget
238-
// focusedOld
239-
if (CDockManager::testConfigFlag(CDockManager::AllTabsHaveCloseButton))
241+
auto DockWidget = vDockWidget.value<CDockWidget*>();
242+
if (!DockWidget)
240243
{
241-
auto OtherDockWidgetTab = internal::findParent<CDockWidgetTab*>(focusedNow);
242-
if (OtherDockWidgetTab && focusedOld)
243-
{
244-
auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld);
245-
if (OldFocusedDockWidget)
246-
{
247-
//focusedOld->setFocus();
248-
}
249-
return;
250-
}
251-
}*/
244+
return;
245+
}
252246

253-
CDockWidget* DockWidget = nullptr;
254-
/*auto DockWidgetTab = qobject_cast<CDockWidgetTab*>(focusedNow);
255-
if (DockWidgetTab)
256-
{
257-
DockWidget = DockWidgetTab->dockWidget();
247+
d->updateDockWidgetFocus(DockWidget);
248+
}
258249

259-
// If the DockWidgetTab "steals" the focus from a widget in the same
260-
// DockWidget, then we immediately give the focus back to the previous
261-
// focused widget focusedOld
262-
if (focusedOld)
263-
{
264-
auto OldFocusedDockWidget = internal::findParent<CDockWidget*>(focusedOld);
265-
if (OldFocusedDockWidget && OldFocusedDockWidget == DockWidget)
266-
{
267-
//focusedOld->setFocus();
268-
}
269-
}
270-
}*/
271250

272-
if (!DockWidget)
251+
//===========================================================================
252+
void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidget* focusedNow)
253+
{
254+
if (d->DockManager->isRestoringState())
273255
{
274-
DockWidget = qobject_cast<CDockWidget*>(focusedNow);
256+
return;
275257
}
276258

259+
ADS_PRINT("CDockFocusController::onApplicationFocusChanged "
260+
<< " old: " << focusedOld << " new: " << focusedNow);
261+
if (!focusedNow)
262+
{
263+
return;
264+
}
265+
266+
CDockWidget* DockWidget = qobject_cast<CDockWidget*>(focusedNow);
277267
if (!DockWidget)
278268
{
279269
DockWidget = internal::findParent<CDockWidget*>(focusedNow);
@@ -298,7 +288,6 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge
298288
//===========================================================================
299289
void CDockFocusController::setDockWidgetTabFocused(CDockWidgetTab* Tab)
300290
{
301-
std::cout << "setDockWidgetTabFocused " << Tab->text().toStdString() << std::endl;
302291
auto DockWidget = Tab->dockWidget();
303292
if (DockWidget)
304293
{
@@ -334,7 +323,7 @@ void CDockFocusController::onFocusedDockAreaViewToggled(bool Open)
334323
return;
335324
}
336325

337-
CDockManager::setWidgetFocus(OpenedDockAreas[0]->currentDockWidget()->tabWidget());
326+
d->updateDockWidgetFocus(OpenedDockAreas[0]->currentDockWidget());
338327
}
339328

340329

@@ -362,7 +351,7 @@ void CDockFocusController::notifyWidgetOrAreaRelocation(QWidget* DroppedWidget)
362351
}
363352

364353
d->ForceFocusChangedSignal = true;
365-
CDockManager::setWidgetFocus(DockWidget->tabWidget());
354+
CDockManager::setWidgetFocus(DockWidget);
366355
}
367356

368357

@@ -383,9 +372,15 @@ void CDockFocusController::notifyFloatingWidgetDrop(CFloatingDockContainer* Floa
383372
auto DockWidget = vDockWidget.value<CDockWidget*>();
384373
if (DockWidget)
385374
{
375+
/*auto Window = DockWidget->dockContainer()->window()->windowHandle();
376+
DockWidget->dockContainer()->window()->clearFocus();
377+
if (Window)
378+
{
379+
Window->setProperty("FocusedDockWidget", QVariant::fromValue<CDockWidget*>(DockWidget));
380+
}*/
386381
d->FocusedDockWidget = nullptr;
387382
DockWidget->dockAreaWidget()->setCurrentDockWidget(DockWidget);
388-
CDockManager::setWidgetFocus(DockWidget->tabWidget());
383+
CDockManager::setWidgetFocus(DockWidget);
389384
}
390385
}
391386

src/DockFocusController.h

Lines changed: 1 addition & 0 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);

0 commit comments

Comments
 (0)