Skip to content

Commit

Permalink
Add **Group** context menu action to group currently selected entries.
Browse files Browse the repository at this point in the history
A new group node is added in the place of the first selected entry, and
all the selected entries are moved from their current parents (which
can be different parents) into the newly created group node.
  • Loading branch information
cristian64 committed May 24, 2024
1 parent 8cea2ab commit e162fbe
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Source/GUI/MemWatcher/MemWatchModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,52 @@ void MemWatchModel::deleteNode(const QModelIndex& index)
}
}

void MemWatchModel::groupSelection(const QModelIndexList& indexes)
{
if (indexes.isEmpty())
return;

// Collect nodes from indexes; indexes will be invalidated shortly as nodes are removed from their
// parents.
std::vector<MemWatchTreeNode*> nodes;
for (const QModelIndex& index : indexes)
{
nodes.push_back(static_cast<MemWatchTreeNode*>(index.internalPointer()));
}

MemWatchTreeNode* newParent{};
int newRow{std::numeric_limits<int>::max()};

// Extract nodes from their current parent.
for (MemWatchTreeNode* const node : nodes)
{
MemWatchTreeNode* const parent{node->getParent()};
const int row{static_cast<int>(parent->getChildren().indexOf(node))};

if (!newParent || newParent == parent)
{
newParent = parent;
newRow = std::min(newRow, row);
}

beginRemoveRows(getIndexFromTreeNode(parent), row, row);
parent->removeChild(node);
endRemoveRows();
}

beginInsertRows(getIndexFromTreeNode(newParent), newRow, newRow);

// Create new group with all the extracted nodes, and insert in the new parent.
MemWatchTreeNode* const group{new MemWatchTreeNode(nullptr, nullptr, true, "New Group")};
for (MemWatchTreeNode* const node : nodes)
{
group->appendChild(node);
}
newParent->insertChild(newRow, group);

endInsertRows();
}

int MemWatchModel::columnCount(const QModelIndex& parent) const
{
(void)parent;
Expand Down Expand Up @@ -630,3 +676,15 @@ MemWatchTreeNode* MemWatchModel::getTreeNodeFromIndex(const QModelIndex& index)
{
return static_cast<MemWatchTreeNode*>(index.internalPointer());
}

QModelIndex MemWatchModel::getIndexFromTreeNode(const MemWatchTreeNode* const node)
{
if (node == m_rootNode)
{
return createIndex(0, 0, m_rootNode);
}

const MemWatchTreeNode* const parent{node->getParent()};
return index(static_cast<int>(parent->getChildren().indexOf(node)), 0,
getIndexFromTreeNode(parent));
}
2 changes: 2 additions & 0 deletions Source/GUI/MemWatcher/MemWatchModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class MemWatchModel : public QAbstractItemModel
void editEntry(MemWatchEntry* entry, const QModelIndex& index);
void clearRoot();
void deleteNode(const QModelIndex& index);
void groupSelection(const QModelIndexList& indexes);
void onUpdateTimer();
void onFreezeTimer();
void loadRootFromJsonRecursive(const QJsonObject& json);
Expand All @@ -71,6 +72,7 @@ class MemWatchModel : public QAbstractItemModel
bool hasAnyNodes() const;
MemWatchTreeNode* getRootNode() const;
static MemWatchTreeNode* getTreeNodeFromIndex(const QModelIndex& index);
QModelIndex getIndexFromTreeNode(const MemWatchTreeNode* node);
bool editData(const QModelIndex& index, const QVariant& value, int role, bool emitEdit = false);

signals:
Expand Down
14 changes: 14 additions & 0 deletions Source/GUI/MemWatcher/MemWatchWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ void MemWatchWidget::onMemWatchContextMenuRequested(const QPoint& pos)
node = m_watchModel->getRootNode();
}

const QModelIndexList simplifiedSelection{simplifySelection()};
if (!simplifiedSelection.empty())
{
QAction* const groupAction{new QAction(tr("&Group"), this)};
connect(groupAction, &QAction::triggered, this, &MemWatchWidget::groupCurrentSelection);
contextMenu->addAction(groupAction);
contextMenu->addSeparator();
}

QAction* cut = new QAction(tr("Cu&t"), this);
connect(cut, &QAction::triggered, this, [this] { cutSelectedWatchesToClipBoard(); });
contextMenu->addAction(cut);
Expand Down Expand Up @@ -293,6 +302,11 @@ void MemWatchWidget::setSelectedWatchesBase(MemWatchEntry* entry, Common::MemBas
m_hasUnsavedChanges = true;
}

void MemWatchWidget::groupCurrentSelection()
{
m_watchModel->groupSelection(simplifySelection());
}

void MemWatchWidget::cutSelectedWatchesToClipBoard()
{
copySelectedWatchesToClipBoard();
Expand Down
1 change: 1 addition & 0 deletions Source/GUI/MemWatcher/MemWatchWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MemWatchWidget : public QWidget
void onRowsInserted(const QModelIndex& parent, int first, int last);
void openWatchFile();
void setSelectedWatchesBase(MemWatchEntry* entry, Common::MemBase base);
void groupCurrentSelection();
void copySelectedWatchesToClipBoard();
void cutSelectedWatchesToClipBoard();
void pasteWatchFromClipBoard(const QModelIndex& referenceIndex);
Expand Down
10 changes: 10 additions & 0 deletions Source/MemoryWatch/MemWatchTreeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,19 @@ void MemWatchTreeNode::insertChild(const int row, MemWatchTreeNode* node)

void MemWatchTreeNode::removeChild(const int row)
{
m_children[row]->m_parent = nullptr;
m_children.remove(row);
}

void MemWatchTreeNode::removeChild(MemWatchTreeNode* const child)
{
m_children.removeAll(child);
if (child->m_parent == this)
{
child->m_parent = nullptr;
}
}

void MemWatchTreeNode::removeChildren()
{
m_children.clear();
Expand Down
1 change: 1 addition & 0 deletions Source/MemoryWatch/MemWatchTreeNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MemWatchTreeNode
void appendChild(MemWatchTreeNode* node);
void insertChild(int row, MemWatchTreeNode* node);
void removeChild(int row);
void removeChild(MemWatchTreeNode* child);
void removeChildren();
void deleteChildren();

Expand Down

0 comments on commit e162fbe

Please sign in to comment.