Skip to content

Commit

Permalink
Rework paste functionality.
Browse files Browse the repository at this point in the history
The newly introduced `MemWatchModel::addNodes()` method, which
efficiently inserts rows individually (as opposed to invalidating the
entire layout via `layoutChanged()`), is now used. This enables
`onRowsInserted()` to auto-select pasted rows.

Also, note that current selection (and not the current focus index) is
now used as insertion point when pasting items via `Ctrl+V`. On the
other hand, the **Paste** context menu action still uses the current
focus index (the index from which the context menu was deployed).

The memory leak around the `copiedRootNode` variable (which was never
freed before) has been addressed.
  • Loading branch information
cristian64 committed May 16, 2024
1 parent 43ec849 commit e9a5b08
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
48 changes: 16 additions & 32 deletions Source/GUI/MemWatcher/MemWatchWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ void MemWatchWidget::initialiseWidgets()
QShortcut* pasteWatchShortcut =
new QShortcut(QKeySequence(Qt::Modifier::CTRL | Qt::Key::Key_V), m_watchView);
connect(pasteWatchShortcut, &QShortcut::activated, this, [this] {
pasteWatchFromClipBoard(
MemWatchModel::getTreeNodeFromIndex(m_watchView->selectionModel()->currentIndex()),
m_watchView->selectionModel()->currentIndex().row() + 1);
const QModelIndexList selectedIndexes{m_watchView->selectionModel()->selectedIndexes()};
pasteWatchFromClipBoard(selectedIndexes.empty() ? QModelIndex{} : selectedIndexes.back());
});

m_btnAddGroup = new QPushButton(tr("Add group"), this);
Expand Down Expand Up @@ -256,9 +255,7 @@ void MemWatchWidget::onMemWatchContextMenuRequested(const QPoint& pos)
contextMenu->addAction(copy);

QAction* paste = new QAction(tr("&Paste"), this);
connect(paste, &QAction::triggered, this, [this, node] {
pasteWatchFromClipBoard(node, m_watchView->selectionModel()->currentIndex().row() + 1);
});
connect(paste, &QAction::triggered, this, [this, index] { pasteWatchFromClipBoard(index); });
contextMenu->addAction(paste);

contextMenu->addSeparator();
Expand Down Expand Up @@ -346,35 +343,22 @@ void MemWatchWidget::copySelectedWatchesToClipBoard()
clipboard->setText(nodeJsonStr);
}

void MemWatchWidget::pasteWatchFromClipBoard(MemWatchTreeNode* node, int row)
void MemWatchWidget::pasteWatchFromClipBoard(const QModelIndex& referenceIndex)
{
QClipboard* clipboard = QApplication::clipboard();
QString nodeStr = clipboard->text();

QJsonDocument loadDoc(QJsonDocument::fromJson(nodeStr.toUtf8()));
MemWatchTreeNode* copiedRootNode = new MemWatchTreeNode(nullptr);
copiedRootNode->readFromJson(loadDoc.object(), nullptr);
if (copiedRootNode->hasChildren())
MemWatchTreeNode copiedRootNode(nullptr);
{
int numberIterated = 0;
for (MemWatchTreeNode* const child : copiedRootNode->getChildren())
{
if (node == nullptr)
node = m_watchModel->getRootNode();
if (node->isGroup() || (node->getParent() == nullptr))
{
node->appendChild(child);
}
else
{
node->getParent()->insertChild(row + numberIterated, child);
}
numberIterated++;
}
emit m_watchModel->layoutChanged();

m_hasUnsavedChanges = true;
const QString nodeStr{QApplication::clipboard()->text()};
const QJsonDocument loadDoc{QJsonDocument::fromJson(nodeStr.toUtf8())};
copiedRootNode.readFromJson(loadDoc.object(), nullptr);
}

const QVector<MemWatchTreeNode*> children{copiedRootNode.getChildren()};
copiedRootNode.clearAllChild();

std::vector<MemWatchTreeNode*> childrenVec(children.constBegin(), children.constEnd());
m_watchModel->addNodes(childrenVec, referenceIndex);

m_hasUnsavedChanges = true;
}

void MemWatchWidget::onWatchDoubleClicked(const QModelIndex& index)
Expand Down
2 changes: 1 addition & 1 deletion Source/GUI/MemWatcher/MemWatchWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MemWatchWidget : public QWidget
void setSelectedWatchesBase(MemWatchEntry* entry, Common::MemBase base);
void copySelectedWatchesToClipBoard();
void cutSelectedWatchesToClipBoard();
void pasteWatchFromClipBoard(MemWatchTreeNode* node, int row);
void pasteWatchFromClipBoard(const QModelIndex& referenceIndex);
void saveWatchFile();
void saveAsWatchFile();
void clearWatchList();
Expand Down

0 comments on commit e9a5b08

Please sign in to comment.