Skip to content

Commit

Permalink
Insert new watch items below (or inside) the currently selected item.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristian64 committed May 16, 2024
1 parent e16847b commit a6f5455
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
59 changes: 46 additions & 13 deletions Source/GUI/MemWatcher/MemWatchModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,57 @@ MemWatchEntry* MemWatchModel::getEntryFromIndex(const QModelIndex& index)
return node->getEntry();
}

void MemWatchModel::addGroup(const QString& name)
void MemWatchModel::addNodes(const std::vector<MemWatchTreeNode*>& nodes,
const QModelIndex& referenceIndex)
{
const QModelIndex rootIndex = index(0, 0, QModelIndex{});
MemWatchTreeNode* node = new MemWatchTreeNode(nullptr, m_rootNode, true, name);
beginInsertRows(rootIndex, rowCount(rootIndex), rowCount(rootIndex));
m_rootNode->appendChild(node);
if (nodes.empty())
return;

QModelIndex targetIndex;
MemWatchTreeNode* parentNode{};
int rowIndex{};

if (referenceIndex.isValid())
{
parentNode = static_cast<MemWatchTreeNode*>(referenceIndex.internalPointer());
if (parentNode->isGroup())
{
targetIndex = referenceIndex.siblingAtColumn(0);
rowIndex = parentNode->childrenCount();
}
else
{
parentNode = parentNode->getParent();
targetIndex = referenceIndex.parent();
rowIndex = parentNode->hasChildren() ? referenceIndex.row() + 1 : 0;
}
}
else
{
targetIndex = QModelIndex{};
parentNode = m_rootNode;
rowIndex = parentNode->childrenCount();
}

const int first{rowIndex};
const int last{rowIndex + static_cast<int>(nodes.size()) - 1};

beginInsertRows(targetIndex, first, last);
for (int i{first}; i <= last; ++i)
{
parentNode->insertChild(i, nodes[static_cast<size_t>(i - first)]);
}
endInsertRows();
emit layoutChanged();
}

void MemWatchModel::addEntry(MemWatchEntry* entry)
void MemWatchModel::addGroup(const QString& name, const QModelIndex& referenceIndex)
{
MemWatchTreeNode* newNode = new MemWatchTreeNode(entry);
QModelIndex idx = index(0, 0, QModelIndex{});
beginInsertRows(idx, rowCount(QModelIndex()), rowCount(QModelIndex()));
m_rootNode->appendChild(newNode);
endInsertRows();
emit layoutChanged();
addNodes({new MemWatchTreeNode(nullptr, m_rootNode, true, name)}, referenceIndex);
}

void MemWatchModel::addEntry(MemWatchEntry* const entry, const QModelIndex& referenceIndex)
{
addNodes({new MemWatchTreeNode(entry)}, referenceIndex);
}

void MemWatchModel::editEntry(MemWatchEntry* entry, const QModelIndex& index)
Expand Down
7 changes: 5 additions & 2 deletions Source/GUI/MemWatcher/MemWatchModel.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <vector>

#include <QAbstractItemModel>
#include <QFile>
#include <QJsonObject>
Expand Down Expand Up @@ -54,8 +56,9 @@ class MemWatchModel : public QAbstractItemModel

void changeType(const QModelIndex& index, Common::MemType type, size_t length);
static MemWatchEntry* getEntryFromIndex(const QModelIndex& index);
void addGroup(const QString& name);
void addEntry(MemWatchEntry* entry);
void addNodes(const std::vector<MemWatchTreeNode*>& nodes, const QModelIndex& referenceIndex = QModelIndex{});
void addGroup(const QString& name, const QModelIndex& referenceIndex = QModelIndex{});
void addEntry(MemWatchEntry* entry, const QModelIndex& referenceIndex = QModelIndex{});
void editEntry(MemWatchEntry* entry, const QModelIndex& index);
void sortRecursive(int column, Qt::SortOrder order, MemWatchTreeNode* parent);
void clearRoot();
Expand Down
6 changes: 4 additions & 2 deletions Source/GUI/MemWatcher/MemWatchWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ void MemWatchWidget::onAddGroup()
"Enter the group name:", QLineEdit::Normal, "", &ok);
if (ok && !text.isEmpty())
{
m_watchModel->addGroup(text);
const QModelIndexList selectedIndexes{m_watchView->selectionModel()->selectedIndexes()};
m_watchModel->addGroup(text, selectedIndexes.empty() ? QModelIndex{} : selectedIndexes.back());
m_hasUnsavedChanges = true;
}
}
Expand All @@ -520,7 +521,8 @@ void MemWatchWidget::onAddWatchEntry()

void MemWatchWidget::addWatchEntry(MemWatchEntry* entry)
{
m_watchModel->addEntry(entry);
const QModelIndexList selectedIndexes{m_watchView->selectionModel()->selectedIndexes()};
m_watchModel->addEntry(entry, selectedIndexes.empty() ? QModelIndex{} : selectedIndexes.back());
m_hasUnsavedChanges = true;
}

Expand Down

0 comments on commit a6f5455

Please sign in to comment.