Skip to content

Commit

Permalink
Merge pull request #139 from cristian64/revised_watch_list_item_inser…
Browse files Browse the repository at this point in the history
…tion

Revised watch list item insertion.
  • Loading branch information
dreamsyntax authored May 16, 2024
2 parents ec97744 + e9a5b08 commit ae7c268
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 54 deletions.
67 changes: 50 additions & 17 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 Expand Up @@ -454,8 +487,7 @@ QMimeData* MemWatchModel::mimeData(const QModelIndexList& indexes) const
nodes << node;
}
MemWatchTreeNode* leastDeepNode = getLeastDeepNodeFromList(nodes);
const qulonglong leastDeepPointer{
Common::bit_cast<qulonglong, MemWatchTreeNode*>(leastDeepNode)};
const qulonglong leastDeepPointer{Common::bit_cast<qulonglong, MemWatchTreeNode*>(leastDeepNode)};
stream << leastDeepPointer;
stream << static_cast<int>(nodes.count());
foreach (MemWatchTreeNode* node, nodes)
Expand Down Expand Up @@ -661,8 +693,9 @@ void MemWatchModel::loadRootFromJsonRecursive(const QJsonObject& json)
emit layoutChanged();
}

MemWatchModel::CTParsingErrors
MemWatchModel::importRootFromCTFile(QFile* const CTFile, const bool useDolphinPointer, const u32 CEStart)
MemWatchModel::CTParsingErrors MemWatchModel::importRootFromCTFile(QFile* const CTFile,
const bool useDolphinPointer,
const u32 CEStart)
{
CheatEngineParser parser = CheatEngineParser();
parser.setTableStartAddress(CEStart);
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
68 changes: 34 additions & 34 deletions Source/GUI/MemWatcher/MemWatchWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void MemWatchWidget::initialiseWidgets()
connect(m_watchModel, &MemWatchModel::writeFailed, this, &MemWatchWidget::onValueWriteError);
connect(m_watchModel, &MemWatchModel::dropSucceeded, this, &MemWatchWidget::onDropSucceeded);
connect(m_watchModel, &MemWatchModel::readFailed, this, &MemWatchWidget::mustUnhook);
connect(m_watchModel, &MemWatchModel::rowsInserted, this, &MemWatchWidget::onRowsInserted);

m_watchDelegate = new MemWatchDelegate();

Expand Down Expand Up @@ -86,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 @@ -255,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 @@ -345,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 Expand Up @@ -506,7 +491,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 +506,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 Expand Up @@ -615,6 +602,19 @@ void MemWatchWidget::onDropSucceeded()
m_hasUnsavedChanges = true;
}

void MemWatchWidget::onRowsInserted(const QModelIndex& parent, const int first, const int last)
{
const QModelIndex firstIndex{m_watchModel->index(first, 0, parent)};
const QModelIndex lastIndex{m_watchModel->index(last, 0, parent)};
const QItemSelection selection{firstIndex, lastIndex};

QItemSelectionModel* const selectionModel{m_watchView->selectionModel()};
selectionModel->select(selection, QItemSelectionModel::ClearAndSelect);
selectionModel->setCurrentIndex(lastIndex, QItemSelectionModel::Current);

QTimer::singleShot(0, [this, lastIndex] { m_watchView->scrollTo(lastIndex); });
}

QTimer* MemWatchWidget::getUpdateTimer() const
{
return m_updateTimer;
Expand Down
3 changes: 2 additions & 1 deletion Source/GUI/MemWatcher/MemWatchWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class MemWatchWidget : public QWidget
void onLockSelection(bool lockStatus);
void onDeleteSelection();
void onDropSucceeded();
void onRowsInserted(const QModelIndex& parent, int first, int last);
void openWatchFile();
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 ae7c268

Please sign in to comment.