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 <thomas.hartmann@qt.io>
This commit is contained in:
Miikka Heikkinen
2020-06-05 17:46:02 +03:00
parent 29ceaade9c
commit 3bd22bd2d7
2 changed files with 43 additions and 0 deletions

View File

@@ -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<QString, bool> 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<QString, bool> localExpandMap;
const ModelNode rootNode = rootModelNode();
const QModelIndex rootIndex = indexForModelNode(rootNode);
std::function<void(const QModelIndex &)> 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);
}

View File

@@ -30,6 +30,8 @@
#include <abstractview.h>
#include <QPointer>
#include <QHash>
#include <QUrl>
QT_BEGIN_NAMESPACE
class QTreeView;
@@ -120,6 +122,8 @@ private:
QPointer<NavigatorWidget> m_widget;
QPointer<NavigatorTreeModel> m_treeModel;
QHash<QUrl, QHash<QString, bool>> m_expandMap;
NavigatorModelInterface *m_currentModelInterface = nullptr;
friend class TestNavigator;