diff --git a/src/DockAreaWidget.cpp b/src/DockAreaWidget.cpp index b681ede..292d54b 100644 --- a/src/DockAreaWidget.cpp +++ b/src/DockAreaWidget.cpp @@ -414,14 +414,15 @@ void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget) auto TabWidget = DockWidget->tabWidget(); TabWidget->hide(); d->tabBar()->removeTab(TabWidget); + CDockContainerWidget* DockContainer = dockContainer(); if (NextOpenDockWidget) { setCurrentDockWidget(NextOpenDockWidget); } - else if (d->ContentsLayout->isEmpty()) + else if (d->ContentsLayout->isEmpty() && DockContainer->dockAreaCount() > 1) { qDebug() << "Dock Area empty"; - dockContainer()->removeDockArea(this); + DockContainer->removeDockArea(this); this->deleteLater(); } else @@ -434,14 +435,13 @@ void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget) d->updateCloseButtonState(); updateTitleBarVisibility(); - auto TopLevelDockWidget = dockContainer()->topLevelDockWidget(); + auto TopLevelDockWidget = DockContainer->topLevelDockWidget(); if (TopLevelDockWidget) { TopLevelDockWidget->emitTopLevelChanged(true); } #if (ADS_DEBUG_LEVEL > 0) - CDockContainerWidget* DockContainer = dockContainer(); DockContainer->dumpLayout(); #endif } diff --git a/src/DockContainerWidget.cpp b/src/DockContainerWidget.cpp index 6cc3a13..3539406 100644 --- a/src/DockContainerWidget.cpp +++ b/src/DockContainerWidget.cpp @@ -998,6 +998,15 @@ CDockAreaWidget* CDockContainerWidget::addDockWidget(DockWidgetArea area, CDockW } } +//============================================================================ +void CDockContainerWidget::removeDockWidget(CDockWidget* Dockwidget) +{ + CDockAreaWidget* Area = Dockwidget->dockAreaWidget(); + if (Area) + { + Area->removeDockWidget(Dockwidget); + } +} //============================================================================ unsigned int CDockContainerWidget::zOrderIndex() const @@ -1057,6 +1066,12 @@ void CDockContainerWidget::removeDockArea(CDockAreaWidget* area) area->setParent(nullptr); internal::hideEmptyParentSplitters(Splitter); + // Remove this area from cached areas + const auto& cache = d->LastAddedAreaCache; + if (auto p = std::find(cache, cache+sizeof(cache)/sizeof(cache[0]), area)) { + d->LastAddedAreaCache[std::distance(cache, p)] = nullptr; + } + // If splitter has more than 1 widgets, we are finished and can leave if (Splitter->count() > 1) { @@ -1274,6 +1289,7 @@ bool CDockContainerWidget::restoreState(QXmlStreamReader& s, bool Testing) { d->VisibleDockAreaCount = -1;// invalidate the dock area count d->DockAreas.clear(); + std::fill(std::begin(d->LastAddedAreaCache),std::end(d->LastAddedAreaCache), nullptr); } if (IsFloating) diff --git a/src/DockContainerWidget.h b/src/DockContainerWidget.h index 0483137..c923ecc 100644 --- a/src/DockContainerWidget.h +++ b/src/DockContainerWidget.h @@ -166,6 +166,11 @@ public: CDockAreaWidget* addDockWidget(DockWidgetArea area, CDockWidget* Dockwidget, CDockAreaWidget* DockAreaWidget = nullptr); + /** + * Removes dockwidget + */ + void removeDockWidget(CDockWidget* Dockwidget); + /** * Returns the current zOrderIndex */ diff --git a/src/DockManager.cpp b/src/DockManager.cpp index 44a0a2b..c3b3ace 100644 --- a/src/DockManager.cpp +++ b/src/DockManager.cpp @@ -565,9 +565,13 @@ CDockAreaWidget* CDockManager::addDockWidgetTab(DockWidgetArea area, { return addDockWidget(ads::CenterDockWidgetArea, Dockwidget, AreaWidget); } + else if (!openedDockAreas().isEmpty()) + { + return addDockWidget(area, Dockwidget, openedDockAreas().last()); + } else { - return addDockWidget(area, Dockwidget, AreaWidget); + return addDockWidget(area, Dockwidget, nullptr); } } @@ -586,6 +590,12 @@ CDockWidget* CDockManager::findDockWidget(const QString& ObjectName) const return d->DockWidgetsMap.value(ObjectName, nullptr); } +//============================================================================ +void CDockManager::removeDockWidget(CDockWidget* Dockwidget) +{ + d->DockWidgetsMap.remove(Dockwidget->objectName()); + CDockContainerWidget::removeDockWidget(Dockwidget); +} //============================================================================ QMap CDockManager::dockWidgetsMap() const diff --git a/src/DockManager.h b/src/DockManager.h index a7c088c..72ab026 100644 --- a/src/DockManager.h +++ b/src/DockManager.h @@ -192,6 +192,11 @@ public: */ CDockWidget* findDockWidget(const QString& ObjectName) const; + /** + * Remove the given Dock from the dock manager + */ + void removeDockWidget(CDockWidget* Dockwidget); + /** * This function returns a readable reference to the internal dock * widgets map so that it is possible to iterate over all dock widgets diff --git a/src/DockWidget.cpp b/src/DockWidget.cpp index c8ca3bb..08c47e4 100644 --- a/src/DockWidget.cpp +++ b/src/DockWidget.cpp @@ -262,6 +262,16 @@ void CDockWidget::setWidget(QWidget* widget, eInsertMode InsertMode) } +//============================================================================ +QWidget* CDockWidget::takeWidget() +{ + d->ScrollArea->takeWidget(); + d->Layout->removeWidget(d->Widget); + d->Widget->setParent(nullptr); + return d->Widget; +} + + //============================================================================ QWidget* CDockWidget::widget() const { diff --git a/src/DockWidget.h b/src/DockWidget.h index e083137..658372f 100644 --- a/src/DockWidget.h +++ b/src/DockWidget.h @@ -238,6 +238,11 @@ public: */ void setWidget(QWidget* widget, eInsertMode InsertMode = AutoScrollArea); + /** + * Remove the widget from the dock and give ownership back to the caller + */ + QWidget* takeWidget(); + /** * Returns the widget for the dock widget. This function returns zero if * the widget has not been set.