Skip to content

Commit

Permalink
Merge pull request #158 from cristian64/persistent_expansion_state
Browse files Browse the repository at this point in the history
Persistent expansion state.
  • Loading branch information
dreamsyntax authored May 30, 2024
2 parents 7e15e25 + 5e87af3 commit 82e1314
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
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
12 changes: 10 additions & 2 deletions Source/MemoryWatch/MemWatchTreeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void MemWatchTreeNode::setValueEditing(const bool valueEditing)
m_isValueEditing = valueEditing;
}

QString MemWatchTreeNode::getGroupName() const
const QString& MemWatchTreeNode::getGroupName() const
{
return m_groupName;
}
Expand All @@ -67,7 +67,7 @@ void MemWatchTreeNode::setEntry(MemWatchEntry* entry)
m_entry = entry;
}

QVector<MemWatchTreeNode*> MemWatchTreeNode::getChildren() const
const QVector<MemWatchTreeNode*>& MemWatchTreeNode::getChildren() const
{
return m_children;
}
Expand Down Expand Up @@ -152,6 +152,10 @@ void MemWatchTreeNode::readFromJson(const QJsonObject& json, MemWatchTreeNode* p
{
m_isGroup = true;
m_groupName = json["groupName"].toString();
if (json.contains("expanded"))
{
m_expanded = json["expanded"].toBool();
}
QJsonArray groupEntries = json["groupEntries"].toArray();
for (auto i : groupEntries)
{
Expand Down Expand Up @@ -200,6 +204,10 @@ void MemWatchTreeNode::writeToJson(QJsonObject& json) const
if (isGroup())
{
json["groupName"] = m_groupName;
if (m_expanded)
{
json["expanded"] = m_expanded;
}
QJsonArray entries;
for (MemWatchTreeNode* const child : m_children)
{
Expand Down
7 changes: 5 additions & 2 deletions Source/MemoryWatch/MemWatchTreeNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class MemWatchTreeNode
MemWatchTreeNode& operator=(MemWatchTreeNode&&) = delete;

bool isGroup() const;
QString getGroupName() const;
bool isExpanded() const { return m_expanded; }
void setExpanded(const bool expanded) { m_expanded = expanded; };
const QString& getGroupName() const;
void setGroupName(const QString& groupName);
MemWatchEntry* getEntry() const;
void setEntry(MemWatchEntry* entry);
QVector<MemWatchTreeNode*> getChildren() const;
const QVector<MemWatchTreeNode*>& getChildren() const;
void setChildren(QVector<MemWatchTreeNode*> children);
MemWatchTreeNode* getParent() const;
void setParent(MemWatchTreeNode* parent);
Expand All @@ -46,6 +48,7 @@ class MemWatchTreeNode

private:
bool m_isGroup;
bool m_expanded{};
bool m_isValueEditing = false;
QString m_groupName;
MemWatchEntry* m_entry;
Expand Down

0 comments on commit 82e1314

Please sign in to comment.