forked from qt-creator/qt-creator
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:
@@ -117,7 +117,20 @@ void NavigatorView::modelAttached(Model *model)
|
|||||||
QTimer::singleShot(0, this, [this, treeView]() {
|
QTimer::singleShot(0, this, [this, treeView]() {
|
||||||
m_currentModelInterface->setFilter(
|
m_currentModelInterface->setFilter(
|
||||||
DesignerSettings::getValue(DesignerSettingsKey::NAVIGATOR_SHOW_ONLY_VISIBLE_ITEMS).toBool());
|
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();
|
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_
|
#ifdef _LOCK_ITEMS_
|
||||||
@@ -127,6 +140,32 @@ void NavigatorView::modelAttached(Model *model)
|
|||||||
|
|
||||||
void NavigatorView::modelAboutToBeDetached(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);
|
AbstractView::modelAboutToBeDetached(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -30,6 +30,8 @@
|
|||||||
#include <abstractview.h>
|
#include <abstractview.h>
|
||||||
|
|
||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
|
#include <QHash>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QTreeView;
|
class QTreeView;
|
||||||
@@ -120,6 +122,8 @@ private:
|
|||||||
QPointer<NavigatorWidget> m_widget;
|
QPointer<NavigatorWidget> m_widget;
|
||||||
QPointer<NavigatorTreeModel> m_treeModel;
|
QPointer<NavigatorTreeModel> m_treeModel;
|
||||||
|
|
||||||
|
QHash<QUrl, QHash<QString, bool>> m_expandMap;
|
||||||
|
|
||||||
NavigatorModelInterface *m_currentModelInterface = nullptr;
|
NavigatorModelInterface *m_currentModelInterface = nullptr;
|
||||||
|
|
||||||
friend class TestNavigator;
|
friend class TestNavigator;
|
||||||
|
Reference in New Issue
Block a user