diff --git a/src/plugins/autotest/testnavigationwidget.cpp b/src/plugins/autotest/testnavigationwidget.cpp index 7eb210eb767..d64fc81fed7 100644 --- a/src/plugins/autotest/testnavigationwidget.cpp +++ b/src/plugins/autotest/testnavigationwidget.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -106,6 +107,14 @@ TestNavigationWidget::TestNavigationWidget(QWidget *parent) : this, [this] (int numberOfActive) { m_missingFrameworksWidget->setVisible(numberOfActive == 0); }); + ProjectExplorer::SessionManager *sm = ProjectExplorer::SessionManager::instance(); + connect(sm, &ProjectExplorer::SessionManager::startupProjectChanged, + [this](ProjectExplorer::Project * /*project*/) { + m_expandedStateCache.clear(); + m_itemUseCache.clear(); + }); + connect(m_model, &TestTreeModel::testTreeModelChanged, + this, &TestNavigationWidget::reapplyCachedExpandedState); connect(m_progressTimer, &QTimer::timeout, m_progressIndicator, &Utils::ProgressIndicator::show); } @@ -226,6 +235,28 @@ QList TestNavigationWidget::createToolButtons() return list; } +void TestNavigationWidget::updateExpandedStateCache() +{ + // raise generation for cached items and drop anything reaching 10th generation + const QList cachedNames = m_itemUseCache.keys(); + for (const QString &cachedName : cachedNames) { + auto it = m_itemUseCache.find(cachedName); + if (it.value()++ >= 10) { + m_itemUseCache.erase(it); + m_expandedStateCache.remove(cachedName); + } + } + + for (Utils::TreeItem *rootNode : *m_model->rootItem()) { + rootNode->forAllChildren([this](Utils::TreeItem *child) { + auto childItem = static_cast(child); + const QString cacheName = childItem->cacheName(); + m_expandedStateCache.insert(cacheName, m_view->isExpanded(childItem->index())); + m_itemUseCache[cacheName] = 0; // explicitly mark as 0-generation + }); + } +} + void TestNavigationWidget::onItemActivated(const QModelIndex &index) { const Utils::Link link = index.data(LinkRole).value(); @@ -295,6 +326,22 @@ void TestNavigationWidget::onRunThisTestTriggered(TestRunMode runMode) TestRunner::instance()->runTest(runMode, item); } +void TestNavigationWidget::reapplyCachedExpandedState() +{ + for (Utils::TreeItem *rootNode : *m_model->rootItem()) { + rootNode->forAllChildren([this](Utils::TreeItem *child) { + auto childItem = static_cast(child); + const QString cacheName = childItem->cacheName(); + const auto it = m_expandedStateCache.find(cacheName); + if (it == m_expandedStateCache.end()) + return; + QModelIndex index = child->index(); + if (m_view->isExpanded(index) != it.value()) + m_view->setExpanded(index, it.value()); + }); + } +} + TestNavigationWidgetFactory::TestNavigationWidgetFactory() { setDisplayName(tr("Tests")); diff --git a/src/plugins/autotest/testnavigationwidget.h b/src/plugins/autotest/testnavigationwidget.h index 1160046b0bc..23a12d42548 100644 --- a/src/plugins/autotest/testnavigationwidget.h +++ b/src/plugins/autotest/testnavigationwidget.h @@ -64,7 +64,7 @@ public: void contextMenuEvent(QContextMenuEvent *event) override; QList createToolButtons(); -signals: + void updateExpandedStateCache(); private: void onItemActivated(const QModelIndex &index); @@ -74,6 +74,7 @@ private: void onParsingFinished(); void initializeFilterMenu(); void onRunThisTestTriggered(TestRunMode runMode); + void reapplyCachedExpandedState(); TestTreeModel *m_model; TestTreeSortFilterModel *m_sortFilterModel; @@ -85,6 +86,8 @@ private: Utils::ProgressIndicator *m_progressIndicator; QTimer *m_progressTimer; QFrame *m_missingFrameworksWidget; + QHash m_expandedStateCache; + QHash m_itemUseCache; }; class TestNavigationWidgetFactory : public Core::INavigationWidgetFactory diff --git a/src/plugins/autotest/testtreemodel.h b/src/plugins/autotest/testtreemodel.h index 909ab3fac45..20ef8e01369 100644 --- a/src/plugins/autotest/testtreemodel.h +++ b/src/plugins/autotest/testtreemodel.h @@ -104,7 +104,7 @@ private: QList testItemsByName(TestTreeItem *root, const QString &testName); Internal::TestCodeParser *m_parser = nullptr; - QHash m_checkStateCache; // could be enhanced to store expanded as well + QHash m_checkStateCache; QHash m_itemUseCache; };