From 3bd22bd2d7ef756bfcba4e12c7a043444f6b3d1d Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Fri, 5 Jun 2020 17:46:02 +0300 Subject: [PATCH] QmlDesigner: Persist navigator expand state between model changes Each qml document has its navigator tree expand state cached at model detach and the state is restored at subsequent mode attach on the same document. Change-Id: I93ff71f500abde44fcc829f53baefc40b68981f6 Fixes: QDS-2222 Reviewed-by: Thomas Hartmann --- .../components/navigator/navigatorview.cpp | 39 +++++++++++++++++++ .../components/navigator/navigatorview.h | 4 ++ 2 files changed, 43 insertions(+) diff --git a/src/plugins/qmldesigner/components/navigator/navigatorview.cpp b/src/plugins/qmldesigner/components/navigator/navigatorview.cpp index 043d1198a82..a38564873f7 100644 --- a/src/plugins/qmldesigner/components/navigator/navigatorview.cpp +++ b/src/plugins/qmldesigner/components/navigator/navigatorview.cpp @@ -117,7 +117,20 @@ void NavigatorView::modelAttached(Model *model) QTimer::singleShot(0, this, [this, treeView]() { m_currentModelInterface->setFilter( DesignerSettings::getValue(DesignerSettingsKey::NAVIGATOR_SHOW_ONLY_VISIBLE_ITEMS).toBool()); + + // Expand everything to begin with to ensure model node to index cache is populated treeView->expandAll(); + + if (AbstractView::model() && m_expandMap.contains(AbstractView::model()->fileUrl())) { + const QHash localExpandMap = m_expandMap[AbstractView::model()->fileUrl()]; + auto it = localExpandMap.constBegin(); + while (it != localExpandMap.constEnd()) { + const QModelIndex index = indexForModelNode(modelNodeForId(it.key())); + if (index.isValid()) + treeWidget()->setExpanded(index, it.value()); + ++it; + } + } }); #ifdef _LOCK_ITEMS_ @@ -127,6 +140,32 @@ void NavigatorView::modelAttached(Model *model) void NavigatorView::modelAboutToBeDetached(Model *model) { + m_expandMap.remove(model->fileUrl()); + + if (currentModel()) { + // Store expand state of the navigator tree + QHash localExpandMap; + const ModelNode rootNode = rootModelNode(); + const QModelIndex rootIndex = indexForModelNode(rootNode); + + std::function gatherExpandedState; + gatherExpandedState = [&](const QModelIndex &index) { + if (index.isValid()) { + const int rowCount = currentModel()->rowCount(index); + for (int i = 0; i < rowCount; ++i) { + const QModelIndex childIndex = currentModel()->index(i, 0, index); + const ModelNode node = modelNodeForIndex(childIndex); + // Just store collapsed states as everything is expanded by default + if (node.isValid() && !treeWidget()->isExpanded(childIndex)) + localExpandMap.insert(node.id(), false); + gatherExpandedState(childIndex); + } + } + }; + gatherExpandedState(rootIndex); + m_expandMap[model->fileUrl()].insert(localExpandMap); + } + AbstractView::modelAboutToBeDetached(model); } diff --git a/src/plugins/qmldesigner/components/navigator/navigatorview.h b/src/plugins/qmldesigner/components/navigator/navigatorview.h index 3bafe0fa80b..72e47935748 100644 --- a/src/plugins/qmldesigner/components/navigator/navigatorview.h +++ b/src/plugins/qmldesigner/components/navigator/navigatorview.h @@ -30,6 +30,8 @@ #include #include +#include +#include QT_BEGIN_NAMESPACE class QTreeView; @@ -120,6 +122,8 @@ private: QPointer m_widget; QPointer m_treeModel; + QHash> m_expandMap; + NavigatorModelInterface *m_currentModelInterface = nullptr; friend class TestNavigator;