Skip to content

Commit 9974256

Browse files
Fixed double emission of focusedDockWidgetChanged() signal
1 parent 8cf4134 commit 9974256

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

examples/deleteonclose/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ int main(int argc, char *argv[])
1414
ads::CDockManager::setConfigFlag(ads::CDockManager::AllTabsHaveCloseButton, true);
1515
auto dockManager = new ads::CDockManager(&w);
1616
QObject::connect(dockManager, &ads::CDockManager::focusedDockWidgetChanged, [] (ads::CDockWidget* old, ads::CDockWidget* now) {
17-
qDebug() << "CDockManager::focusedDockWidgetChanged: " << now->objectName() << " visible: " << now->isVisible();
17+
static int Count = 0;
18+
qDebug() << Count++ << " CDockManager::focusedDockWidgetChanged old: " << (old ? old->objectName() : "-") << " now: " << now->objectName() << " visible: " << now->isVisible();
1819
now->widget()->setFocus();
1920
});
2021

src/DockAreaWidget.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ void CDockAreaWidget::insertDockWidget(int index, CDockWidget* DockWidget,
447447
void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget)
448448
{
449449
ADS_PRINT("CDockAreaWidget::removeDockWidget");
450-
auto NextOpenDockWidget = nextOpenDockWidget(DockWidget);
450+
auto CurrentDockWidget = currentDockWidget();
451+
auto NextOpenDockWidget = (DockWidget == CurrentDockWidget) ? nextOpenDockWidget(DockWidget) : nullptr;
451452

452453
d->ContentsLayout->removeWidget(DockWidget);
453454
auto TabWidget = DockWidget->tabWidget();
@@ -466,7 +467,7 @@ void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget)
466467
DockContainer->removeDockArea(this);
467468
this->deleteLater();
468469
}
469-
else
470+
else if (DockWidget == CurrentDockWidget)
470471
{
471472
// if contents layout is not empty but there are no more open dock
472473
// widgets, then we need to hide the dock area because it does not

src/DockFocusController.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ void DockFocusControllerPrivate::updateDockWidgetFocus(CDockWidget* DockWidget)
160160
}
161161
#endif
162162

163+
if (old == DockWidget)
164+
{
165+
return;
166+
}
167+
163168
if (DockWidget->isVisible())
164169
{
165170
emit DockManager->focusedDockWidgetChanged(old, DockWidget);
@@ -212,6 +217,8 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge
212217
return;
213218
}
214219

220+
//qDebug() << "\n----------------------------";
221+
//qDebug() << "CDockFocusController::onApplicationFocusChanged " << " old: " << focusedOld << " new: " << focusedNow;
215222
Q_UNUSED(focusedOld)
216223
if (!focusedNow)
217224
{
@@ -223,6 +230,14 @@ void CDockFocusController::onApplicationFocusChanged(QWidget* focusedOld, QWidge
223230
if (DockWidgetTab)
224231
{
225232
DockWidget = DockWidgetTab->dockWidget();
233+
// If the DockWidgetTab "steals" the focus from a widget in the same
234+
// DockWidget, then we immediately give the focus back to the previous
235+
// focused widget focusedOld
236+
if (DockWidget == d->FocusedDockWidget && focusedOld && focusedOld != focusedNow)
237+
{
238+
focusedOld->setFocus();
239+
return;
240+
}
226241
}
227242

228243
if (!DockWidget)
@@ -278,7 +293,8 @@ void CDockFocusController::onFocusedDockAreaViewToggled(bool Open)
278293
return;
279294
}
280295

281-
CDockManager::setWidgetFocus(OpenedDockAreas[0]->currentDockWidget()->tabWidget());
296+
//CDockManager::setWidgetFocus(OpenedDockAreas[0]->currentDockWidget()->tabWidget());
297+
d->updateDockWidgetFocus(OpenedDockAreas[0]->currentDockWidget());
282298
}
283299

284300

@@ -293,7 +309,8 @@ void CDockFocusController::notifyWidgetOrAreaRelocation(QWidget* DroppedWidget)
293309
CDockWidget* DockWidget = qobject_cast<CDockWidget*>(DroppedWidget);
294310
if (DockWidget)
295311
{
296-
CDockManager::setWidgetFocus(DockWidget->tabWidget());
312+
//CDockManager::setWidgetFocus(DockWidget->tabWidget());
313+
d->updateDockWidgetFocus(DockWidget);
297314
return;
298315
}
299316

@@ -304,7 +321,8 @@ void CDockFocusController::notifyWidgetOrAreaRelocation(QWidget* DroppedWidget)
304321
}
305322

306323
DockWidget = DockArea->currentDockWidget();
307-
CDockManager::setWidgetFocus(DockWidget->tabWidget());
324+
//CDockManager::setWidgetFocus(DockWidget->tabWidget());
325+
d->updateDockWidgetFocus(DockWidget);
308326
}
309327

310328

@@ -325,8 +343,11 @@ void CDockFocusController::notifyFloatingWidgetDrop(CFloatingDockContainer* Floa
325343
auto DockWidget = vDockWidget.value<CDockWidget*>();
326344
if (DockWidget)
327345
{
346+
d->FocusedDockWidget = nullptr;
328347
DockWidget->dockAreaWidget()->setCurrentDockWidget(DockWidget);
329-
CDockManager::setWidgetFocus(DockWidget->tabWidget());
348+
//CDockManager::setWidgetFocus(DockWidget->tabWidget());
349+
//qDebug() << "CDockFocusController::notifyFloatingWidgetDrop";
350+
d->updateDockWidgetFocus(DockWidget);
330351
}
331352
}
332353

0 commit comments

Comments
 (0)