Qt: "Show in Memory Viewer" context action

This commit is contained in:
trigger 2025-04-29 14:26:57 -07:00
parent e066735fe9
commit f950ee6a1e
3 changed files with 63 additions and 4 deletions

View file

@ -32,6 +32,8 @@
#include <QTimer>
#include <QCheckBox>
#include <QMessageBox>
#include <QMenu>
#include <QTextDocumentFragment>
#include <algorithm>
#include <functional>
@ -53,6 +55,9 @@ extern bool is_using_interpreter(thread_class t_class);
extern std::shared_ptr<CPUDisAsm> make_disasm(const cpu_thread* cpu, shared_ptr<cpu_thread> handle);
class CPUDisAsm;
std::shared_ptr<CPUDisAsm> make_basic_ppu_disasm();
debugger_frame::debugger_frame(std::shared_ptr<gui_settings> gui_settings, QWidget *parent)
: custom_dock_widget(tr("Debugger [Press F1 for Help]"), parent)
, m_gui_settings(std::move(gui_settings))
@ -125,7 +130,8 @@ debugger_frame::debugger_frame(std::shared_ptr<gui_settings> gui_settings, QWidg
m_regs = new QPlainTextEdit(this);
m_regs->setLineWrapMode(QPlainTextEdit::NoWrap);
m_regs->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
m_regs->setContextMenuPolicy(Qt::CustomContextMenu);
m_debugger_list->setFont(m_mono);
m_misc_state->setFont(m_mono);
m_regs->setFont(m_mono);
@ -158,6 +164,8 @@ debugger_frame::debugger_frame(std::shared_ptr<gui_settings> gui_settings, QWidg
body->setLayout(vbox_p_main);
setWidget(body);
connect(m_regs, &QPlainTextEdit::customContextMenuRequested, this, &debugger_frame::OnRegsContextMenu);
connect(m_go_to_addr, &QAbstractButton::clicked, this, &debugger_frame::ShowGotoAddressDialog);
connect(m_go_to_pc, &QAbstractButton::clicked, this, [this]() { ShowPC(true); });
@ -1702,3 +1710,52 @@ void debugger_frame::EnableButtons(bool enable)
m_btn_step_over->setEnabled(step);
m_btn_run->setEnabled(enable);
}
void debugger_frame::OnRegsContextMenu(const QPoint& pos)
{
QMenu* menu = m_regs->createStandardContextMenu();
QAction* memory_viewer_action = new QAction(tr("Show in Memory Viewer"), menu);
connect(memory_viewer_action, &QAction::triggered, this, [this]()
{
QTextCursor cursor = m_regs->textCursor();
if (!cursor.hasSelection()) {
QMessageBox::warning(this,tr("No Selection"), tr("Please select a hex value first."));
return;
}
QTextDocumentFragment frag(cursor);
QString selected = frag.toPlainText().trimmed();
int pos = 0;
HexValidator validator(this);
QValidator::State st = validator.validate(selected, pos);
if (st != QValidator::Acceptable) {
QMessageBox::critical(this, tr("Invalid Hex"), tr("“%1” is not a valid 32-bit hex value.").arg(selected));
return;
}
QString norm = normalize_hex_qstring(selected);
bool ok = false;
quint64 value = norm.toULongLong(&ok, 16);
auto pc = static_cast<uint32_t>(value);
const u32 id = idm::last_id();
auto handle_ptr = idm::get_unlocked<memory_viewer_handle>(id);
if (!handle_ptr)
{
idm::make<memory_viewer_handle>(this, make_basic_ppu_disasm(), pc);
return;
}
handle_ptr->m_mvp->SetPC(pc);
handle_ptr->m_mvp->raise();
handle_ptr->m_mvp->scroll(0);
handle_ptr->m_mvp->show();
});
menu->addSeparator();
menu->addAction(memory_viewer_action);
menu->exec(m_regs->mapToGlobal(pos));
}

View file

@ -137,6 +137,7 @@ public Q_SLOTS:
private Q_SLOTS:
void OnSelectUnit();
void OnSelectSPUDisassembler();
void OnRegsContextMenu(const QPoint& pos);
void ShowPC(bool user_requested = false);
void EnableUpdateTimer(bool enable) const;
void RunBtnPress();

View file

@ -55,6 +55,9 @@ public:
memory_viewer_panel(QWidget* parent, std::shared_ptr<CPUDisAsm> disasm, u32 addr = 0, std::function<cpu_thread*()> func = []() -> cpu_thread* { return {}; });
~memory_viewer_panel();
void scroll(s32 steps);
void SetPC(const uint pc);
enum class color_format : int
{
RGB,
@ -106,9 +109,7 @@ private:
search_mode m_modes{};
std::string getHeaderAtAddr(u32 addr) const;
void scroll(s32 steps);
void* to_ptr(u32 addr, u32 size = 1) const;
void SetPC(const uint pc);
void ShowMemory();
@ -134,6 +135,6 @@ struct memory_viewer_handle
~memory_viewer_handle() { m_mvp->close(); m_mvp->deleteLater(); }
private:
public:
const std::add_pointer_t<memory_viewer_panel> m_mvp;
};