From 3f5697554a82dcfb6b3d321d5d5a3da7debb3974 Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Thu, 1 Nov 2018 08:52:14 +0100 Subject: [PATCH] Changed store and restore functioality to save the current dock widget name of an dock area instead of the current index because if some dock widgets are missing when loading the configuration, the dock index might be wrong --- src/DockAreaWidget.cpp | 17 ++++++++++++----- src/DockAreaWidget.h | 8 ++++++++ src/DockContainerWidget.cpp | 11 ++++++----- src/DockManager.cpp | 13 +++++-------- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/DockAreaWidget.cpp b/src/DockAreaWidget.cpp index b498362..079abbd 100644 --- a/src/DockAreaWidget.cpp +++ b/src/DockAreaWidget.cpp @@ -451,13 +451,20 @@ CDockWidget* CDockAreaWidget::currentDockWidget() const //============================================================================ void CDockAreaWidget::setCurrentDockWidget(CDockWidget* DockWidget) { - int Index = index(DockWidget); - if (Index < 0) + if (dockManager()->isRestoringState()) { return; } - if (dockManager()->isRestoringState()) + internalSetCurrentDockWidget(DockWidget); +} + + +//============================================================================ +void CDockAreaWidget::internalSetCurrentDockWidget(CDockWidget* DockWidget) +{ + int Index = index(DockWidget); + if (Index < 0) { return; } @@ -607,9 +614,9 @@ void CDockAreaWidget::saveState(QXmlStreamWriter& s) const { s.writeStartElement("DockAreaWidget"); s.writeAttribute("Tabs", QString::number(d->ContentsLayout->count())); - s.writeAttribute("CurrentIndex", QString::number(d->ContentsLayout->currentIndex())); + s.writeAttribute("CurrentDockWidget", currentDockWidget()->objectName()); qDebug() << "CDockAreaWidget::saveState TabCount: " << d->ContentsLayout->count() - << " CurrentIndex: " << d->ContentsLayout->currentIndex(); + << " CurrentDockWidge: " << currentDockWidget()->objectName(); for (int i = 0; i < d->ContentsLayout->count(); ++i) { dockWidget(i)->saveState(s); diff --git a/src/DockAreaWidget.h b/src/DockAreaWidget.h index b5304d2..d845618 100644 --- a/src/DockAreaWidget.h +++ b/src/DockAreaWidget.h @@ -61,6 +61,7 @@ private: friend class CDockWidgetTab; friend struct DockWidgetPrivate; friend class CDockWidget; + friend struct DockManagerPrivate; private slots: void onTabCloseRequested(int Index); @@ -121,6 +122,13 @@ protected: */ void updateTabBarVisibility(); + /** + * This is the internal private function for setting the current widget. + * This function is called by the public setCurrentDockWidget() function + * and by the dock manager when restoring the state + */ + void internalSetCurrentDockWidget(CDockWidget* DockWidget); + public: using Super = QFrame; diff --git a/src/DockContainerWidget.cpp b/src/DockContainerWidget.cpp index c173484..ca2ac22 100644 --- a/src/DockContainerWidget.cpp +++ b/src/DockContainerWidget.cpp @@ -526,13 +526,14 @@ bool DockContainerWidgetPrivate::restoreDockArea(QXmlStreamReader& s, return false; } - int CurrentIndex = s.attributes().value("CurrentIndex").toInt(&Ok); - if (!Ok) + + QString CurrentDockWidget = s.attributes().value("CurrentDockWidget").toString(); + if (CurrentDockWidget.isEmpty()) { return false; } - qDebug() << "Restore NodeDockArea Tabs: " << Tabs << " CurrentIndex: " - << CurrentIndex; + qDebug() << "Restore NodeDockArea Tabs: " << Tabs << " CurrentDockWidget: " + << CurrentDockWidget; CDockAreaWidget* DockArea = nullptr; if (!Testing) @@ -590,7 +591,7 @@ bool DockContainerWidgetPrivate::restoreDockArea(QXmlStreamReader& s, } else { - DockArea->setProperty("currentIndex", CurrentIndex); + DockArea->setProperty("currentDockWidget", CurrentDockWidget); DockAreas.append(DockArea); } diff --git a/src/DockManager.cpp b/src/DockManager.cpp index 81135ea..55aaa62 100644 --- a/src/DockManager.cpp +++ b/src/DockManager.cpp @@ -269,15 +269,12 @@ bool DockManagerPrivate::restoreState(const QByteArray &state, int version) for (int i = 0; i < DockContainer->dockAreaCount(); ++i) { CDockAreaWidget* DockArea = DockContainer->dockArea(i); - int CurrentIndex = DockArea->property("currentIndex").toInt(); - int DockWidgetCount = DockArea->dockWidgetsCount(); - if (CurrentIndex < DockWidgetCount && DockWidgetCount > 1 && CurrentIndex > -1) + QString DockWidgetName = DockArea->property("currentDockWidget").toString(); + CDockWidget* DockWidget = _this->findDockWidget(DockWidgetName); + std::cout << "restore DockWIdgetName " << DockWidget->objectName().toStdString() << std::endl; + if (!DockWidget->isClosed()) { - auto DockWidget = DockArea->dockWidget(CurrentIndex); - if (!DockWidget->isClosed()) - { - DockArea->setCurrentIndex(CurrentIndex); - } + DockArea->internalSetCurrentDockWidget(DockWidget); } } }