Skip to content

Commit

Permalink
Restore expansion state when save file is loaded.
Browse files Browse the repository at this point in the history
Also when pasted rows are inserted.
  • Loading branch information
cristian64 committed May 29, 2024
1 parent eec4356 commit 5e87af3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
60 changes: 59 additions & 1 deletion Source/GUI/MemWatcher/MemWatchWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()
Expand All @@ -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<const MemWatchTreeNode*> 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);
}
}
}
3 changes: 3 additions & 0 deletions Source/GUI/MemWatcher/MemWatchWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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{};
Expand Down

0 comments on commit 5e87af3

Please sign in to comment.