From 5e87af3fc7217cbd9ce30abb346648f97db01c66 Mon Sep 17 00:00:00 2001 From: cristian64 Date: Mon, 27 May 2024 22:51:34 +0100 Subject: [PATCH] Restore expansion state when save file is loaded. Also when pasted rows are inserted. --- Source/GUI/MemWatcher/MemWatchWidget.cpp | 60 +++++++++++++++++++++++- Source/GUI/MemWatcher/MemWatchWidget.h | 3 ++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/Source/GUI/MemWatcher/MemWatchWidget.cpp b/Source/GUI/MemWatcher/MemWatchWidget.cpp index 0c5bb74..47397fb 100644 --- a/Source/GUI/MemWatcher/MemWatchWidget.cpp +++ b/Source/GUI/MemWatcher/MemWatchWidget.cpp @@ -65,6 +65,8 @@ void MemWatchWidget::initialiseWidgets() &MemWatchWidget::onMemWatchContextMenuRequested); connect(m_watchView, &QAbstractItemView::doubleClicked, this, &MemWatchWidget::onWatchDoubleClicked); + connect(m_watchView, &QTreeView::collapsed, this, &MemWatchWidget::onCollapsed); + connect(m_watchView, &QTreeView::expanded, this, &MemWatchWidget::onExpanded); m_watchView->setItemDelegate(m_watchDelegate); m_watchView->setModel(m_watchModel); @@ -651,7 +653,34 @@ void MemWatchWidget::onRowsInserted(const QModelIndex& parent, const int first, selectionModel->select(selection, QItemSelectionModel::ClearAndSelect); selectionModel->setCurrentIndex(lastIndex, QItemSelectionModel::Current); - QTimer::singleShot(0, [this, lastIndex] { m_watchView->scrollTo(lastIndex); }); + QTimer::singleShot(0, [this, parent, first, last, lastIndex] { + for (int i{first}; i <= last; ++i) + { + const MemWatchTreeNode* const node{ + MemWatchModel::getTreeNodeFromIndex(m_watchModel->index(i, 0, parent))}; + updateExpansionState(node); + } + + m_watchView->scrollTo(lastIndex); + }); +} + +void MemWatchWidget::onCollapsed(const QModelIndex& index) +{ + MemWatchTreeNode* const node{MemWatchModel::getTreeNodeFromIndex(index)}; + if (!node) + return; + node->setExpanded(false); + m_hasUnsavedChanges = true; +} + +void MemWatchWidget::onExpanded(const QModelIndex& index) +{ + MemWatchTreeNode* const node{MemWatchModel::getTreeNodeFromIndex(index)}; + if (!node) + return; + node->setExpanded(true); + m_hasUnsavedChanges = true; } QTimer* MemWatchWidget::getUpdateTimer() const @@ -704,6 +733,7 @@ void MemWatchWidget::openWatchFile() watchFile.close(); QJsonDocument loadDoc(QJsonDocument::fromJson(bytes)); m_watchModel->loadRootFromJsonRecursive(loadDoc.object()); + updateExpansionState(); m_watchListFile = fileName; m_hasUnsavedChanges = false; } @@ -897,6 +927,7 @@ void MemWatchWidget::restoreWatchModel(const QString& json) { const QJsonDocument loadDoc(QJsonDocument::fromJson(json.toUtf8())); m_watchModel->loadRootFromJsonRecursive(loadDoc.object()); + updateExpansionState(); } QString MemWatchWidget::saveWatchModel() @@ -906,3 +937,30 @@ QString MemWatchWidget::saveWatchModel() QJsonDocument saveDoc(root); return saveDoc.toJson(); } + +void MemWatchWidget::updateExpansionState(const MemWatchTreeNode* const node) +{ + QSignalBlocker signalBlocker(*m_watchView); + + std::vector nodes; + nodes.push_back(node ? node : m_watchModel->getRootNode()); + + while (!nodes.empty()) + { + const MemWatchTreeNode* const node{nodes.back()}; + nodes.pop_back(); + + if (!node) + continue; + + if (node->isExpanded()) + { + m_watchView->setExpanded(m_watchModel->getIndexFromTreeNode(node), true); + } + + for (const MemWatchTreeNode* const child : node->getChildren()) + { + nodes.push_back(child); + } + } +} diff --git a/Source/GUI/MemWatcher/MemWatchWidget.h b/Source/GUI/MemWatcher/MemWatchWidget.h index cb8098d..194500d 100644 --- a/Source/GUI/MemWatcher/MemWatchWidget.h +++ b/Source/GUI/MemWatcher/MemWatchWidget.h @@ -31,6 +31,8 @@ class MemWatchWidget : public QWidget void onDeleteSelection(); void onDropSucceeded(); void onRowsInserted(const QModelIndex& parent, int first, int last); + void onCollapsed(const QModelIndex& index); + void onExpanded(const QModelIndex& index); void openWatchFile(); void setSelectedWatchesBase(MemWatchEntry* entry, Common::MemBase base); void groupCurrentSelection(); @@ -55,6 +57,7 @@ class MemWatchWidget : public QWidget private: void initialiseWidgets(); void makeLayouts(); + void updateExpansionState(const MemWatchTreeNode* node = nullptr); QTreeView* m_watchView{}; MemWatchModel* m_watchModel{};