From cfaf169ea1e967b02a023cd38799ae7381f673d3 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Tue, 23 Nov 2021 14:15:15 +0200 Subject: [PATCH] QmlDesigner: Fix node expand state caching for subcomponent edit case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When detaching subcomponent edit model, we just update node expand state cache for the file instead of recreating it from scratch. Also, never collapse the current root node when attaching the model. Fixes: QDS-5557 Change-Id: Id107940daccd9663ec4216de7dc8ae6a5bee8974 Reviewed-by: Samuel Ghinet Reviewed-by: Mahmoud Badri Reviewed-by: Qt CI Bot Reviewed-by: Henning Gründl Reviewed-by: Thomas Hartmann --- .../components/navigator/navigatorview.cpp | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/plugins/qmldesigner/components/navigator/navigatorview.cpp b/src/plugins/qmldesigner/components/navigator/navigatorview.cpp index 2606ea83a49..514bf2c75b1 100644 --- a/src/plugins/qmldesigner/components/navigator/navigatorview.cpp +++ b/src/plugins/qmldesigner/components/navigator/navigatorview.cpp @@ -28,6 +28,7 @@ #include "navigatorwidget.h" #include "qmldesignerconstants.h" #include "qmldesignericons.h" +#include "qmldesignerplugin.h" #include "nameitemdelegate.h" #include "iconcheckboxitemdelegate.h" @@ -166,9 +167,14 @@ void NavigatorView::modelAttached(Model *model) 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()); + const ModelNode node = modelNodeForId(it.key()); + // When editing subcomponent, the current root node may be marked collapsed in the + // full file view, but we never want to actually collapse it, so skip it. + if (!node.isRootNode()) { + const QModelIndex index = indexForModelNode(node); + if (index.isValid()) + treeWidget()->setExpanded(index, it.value()); + } ++it; } } @@ -200,11 +206,18 @@ void NavigatorView::addNodeAndSubModelNodesToList(const ModelNode &node, QListfileUrl()); + QHash &localExpandMap = m_expandMap[model->fileUrl()]; + + // If detaching full document model, recreate expand map from scratch to remove stale entries. + // Otherwise just update it (subcomponent edit case). + bool fullUpdate = true; + if (DesignDocument *document = QmlDesignerPlugin::instance()->currentDesignDocument()) + fullUpdate = !document->inFileComponentModelActive(); + if (fullUpdate) + localExpandMap.clear(); if (currentModel()) { // Store expand state of the navigator tree - QHash localExpandMap; const ModelNode rootNode = rootModelNode(); const QModelIndex rootIndex = indexForModelNode(rootNode); @@ -215,15 +228,18 @@ void NavigatorView::modelAboutToBeDetached(Model *model) 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); + if (node.isValid()) { + // Just store collapsed states as everything is expanded by default + if (!treeWidget()->isExpanded(childIndex)) + localExpandMap.insert(node.id(), false); + else if (!fullUpdate) + localExpandMap.remove(node.id()); + } gatherExpandedState(childIndex); } } }; gatherExpandedState(rootIndex); - m_expandMap[model->fileUrl()] = localExpandMap; } AbstractView::modelAboutToBeDetached(model);