Skip to content

Commit

Permalink
Qt/CodeViewWidget: Implement branch arrows.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdmiralCurtiss committed Feb 24, 2020
1 parent c70e004 commit a7a84cc
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 5 deletions.
103 changes: 98 additions & 5 deletions Source/Core/DolphinQt/Debugger/CodeViewWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
#include <QKeyEvent>
#include <QMenu>
#include <QMouseEvent>
#include <QPainter>
#include <QResizeEvent>
#include <QScrollBar>
#include <QStyledItemDelegate>
#include <QTableWidgetItem>
#include <QWheelEvent>

Expand All @@ -30,6 +32,74 @@
#include "DolphinQt/Resources.h"
#include "DolphinQt/Settings.h"

struct CodeViewBranch
{
u32 src_addr;
u32 dst_addr;
};

constexpr u32 WIDTH_PER_BRANCH_ARROW = 16;

class BranchDisplayDelegate : public QStyledItemDelegate
{
public:
BranchDisplayDelegate(CodeViewWidget* parent) : m_parent(parent) {}

private:
CodeViewWidget* m_parent;

void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const override
{
QStyledItemDelegate::paint(painter, option, index);

painter->save();

painter->setPen(m_parent->palette().text().color());

constexpr u32 x_offset_in_branch_for_vertical_line = 10;
const u32 addr = m_parent->AddressForRow(index.row());
u32 current_branch_index = 0;
for (const CodeViewBranch& branch : m_parent->m_branches)
{
const u32 addr_lower = std::min(branch.src_addr, branch.dst_addr);
const u32 addr_higher = std::max(branch.src_addr, branch.dst_addr);
const bool in_range = addr >= addr_lower && addr <= addr_higher;

if (in_range)
{
const bool is_lowest = addr == addr_lower;
const bool is_highest = addr == addr_higher;
const int y_center = option.rect.top() + option.rect.height() / 2;
const int top = (is_lowest ? y_center : option.rect.top());
const int bottom = (is_highest ? y_center : option.rect.bottom());

// draw vertical part of the branch line
const int x_left = option.rect.left() + WIDTH_PER_BRANCH_ARROW * current_branch_index;
const int x_right = x_left + x_offset_in_branch_for_vertical_line;
painter->drawLine(x_right, top, x_right, bottom);

if (is_lowest || is_highest)
{
// draw horizontal part of the branch line if this is either the source or destination
painter->drawLine(x_left, y_center, x_right, y_center);
}

if (addr == branch.dst_addr)
{
// draw arrow if this is the destination address
painter->drawLine(x_left, y_center, x_left + 6, y_center - 3);
painter->drawLine(x_left, y_center, x_left + 6, y_center + 3);
}
}

++current_branch_index;
}

painter->restore();
}
};

// "Most mouse types work in steps of 15 degrees, in which case the delta value is a multiple of
// 120; i.e., 120 units * 1/8 = 15 degrees." (http://doc.qt.io/qt-5/qwheelevent.html#angleDelta)
constexpr double SCROLL_FRACTION_DEGREES = 15.;
Expand All @@ -41,7 +111,8 @@ constexpr int CODE_VIEW_COLUMN_ADDRESS = 1;
constexpr int CODE_VIEW_COLUMN_INSTRUCTION = 2;
constexpr int CODE_VIEW_COLUMN_PARAMETERS = 3;
constexpr int CODE_VIEW_COLUMN_DESCRIPTION = 4;
constexpr int CODE_VIEW_COLUMNCOUNT = 5;
constexpr int CODE_VIEW_COLUMN_BRANCH_ARROWS = 5;
constexpr int CODE_VIEW_COLUMNCOUNT = 6;

CodeViewWidget::CodeViewWidget()
{
Expand All @@ -58,6 +129,7 @@ CodeViewWidget::CodeViewWidget()
horizontalHeader()->hide();

setFont(Settings::Instance().GetDebugFont());
setItemDelegateForColumn(CODE_VIEW_COLUMN_BRANCH_ARROWS, new BranchDisplayDelegate(this));

FontBasedSizing();

Expand All @@ -75,6 +147,8 @@ CodeViewWidget::CodeViewWidget()
connect(&Settings::Instance(), &Settings::ThemeChanged, this, &CodeViewWidget::Update);
}

CodeViewWidget::~CodeViewWidget() = default;

static u32 GetBranchFromAddress(u32 addr)
{
std::string disasm = PowerPC::debug_interface.Disassemble(addr);
Expand All @@ -97,6 +171,14 @@ void CodeViewWidget::FontBasedSizing()
Update();
}

u32 CodeViewWidget::AddressForRow(int row) const
{
// m_address is defined as the center row of the table, so we have rowCount/2 instructions above
// it; an instruction is 4 bytes long on GC/Wii so we increment 4 bytes per row
const u32 row_zero_address = m_address - ((rowCount() / 2) * 4);
return row_zero_address + row * 4;
}

void CodeViewWidget::Update()
{
if (!isVisible())
Expand Down Expand Up @@ -129,9 +211,11 @@ void CodeViewWidget::Update()

const bool dark_theme = qApp->palette().color(QPalette::Base).valueF() < 0.5;

m_branches.clear();

for (int i = 0; i < rowCount(); i++)
{
const u32 addr = m_address - ((rowCount() / 2) * 4) + i * 4;
const u32 addr = AddressForRow(i);
const u32 color = PowerPC::debug_interface.GetColor(addr);
auto* bp_item = new QTableWidgetItem;
auto* addr_item = new QTableWidgetItem(QStringLiteral("%1").arg(addr, 8, 16, QLatin1Char('0')));
Expand All @@ -154,8 +238,9 @@ void CodeViewWidget::Update()
auto* ins_item = new QTableWidgetItem(ins_formatted);
auto* param_item = new QTableWidgetItem(param_formatted);
auto* description_item = new QTableWidgetItem(desc_formatted);
auto* branch_item = new QTableWidgetItem();

for (auto* item : {bp_item, addr_item, ins_item, param_item, description_item})
for (auto* item : {bp_item, addr_item, ins_item, param_item, description_item, branch_item})
{
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
item->setData(Qt::UserRole, addr);
Expand All @@ -181,8 +266,13 @@ void CodeViewWidget::Update()

if (hex_str.length() == VALID_BRANCH_LENGTH && desc != "---")
{
description_item->setText(tr("--> %1").arg(QString::fromStdString(
PowerPC::debug_interface.GetDescription(GetBranchFromAddress(addr)))));
u32 branch_addr = GetBranchFromAddress(addr);
CodeViewBranch& branch = m_branches.emplace_back();
branch.src_addr = addr;
branch.dst_addr = branch_addr;

description_item->setText(tr("--> %1").arg(
QString::fromStdString(PowerPC::debug_interface.GetDescription(branch_addr))));
param_item->setForeground(Qt::magenta);
}

Expand All @@ -201,6 +291,7 @@ void CodeViewWidget::Update()
setItem(i, CODE_VIEW_COLUMN_INSTRUCTION, ins_item);
setItem(i, CODE_VIEW_COLUMN_PARAMETERS, param_item);
setItem(i, CODE_VIEW_COLUMN_DESCRIPTION, description_item);
setItem(i, CODE_VIEW_COLUMN_BRANCH_ARROWS, branch_item);

if (addr == GetAddress())
{
Expand All @@ -209,6 +300,8 @@ void CodeViewWidget::Update()
}

resizeColumnsToContents();
setColumnWidth(CODE_VIEW_COLUMN_BRANCH_ARROWS,
static_cast<int>(WIDTH_PER_BRANCH_ARROW * m_branches.size()));
g_symbolDB.FillInCallers();

repaint();
Expand Down
10 changes: 10 additions & 0 deletions Source/Core/DolphinQt/Debugger/CodeViewWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class QMouseEvent;
class QResizeEvent;
class QShowEvent;

struct CodeViewBranch;
class BranchDisplayDelegate;

class CodeViewWidget : public QTableWidget
{
Q_OBJECT
Expand All @@ -26,6 +29,7 @@ class CodeViewWidget : public QTableWidget
};

explicit CodeViewWidget();
~CodeViewWidget() override;

u32 GetAddress() const;
u32 GetContextAddress() const;
Expand All @@ -38,6 +42,8 @@ class CodeViewWidget : public QTableWidget
void ToggleBreakpoint();
void AddBreakpoint();

u32 AddressForRow(int row) const;

signals:
void RequestPPCComparison(u32 addr);
void ShowMemory(u32 address);
Expand Down Expand Up @@ -83,4 +89,8 @@ class CodeViewWidget : public QTableWidget

u32 m_address = 0;
u32 m_context_address = 0;

std::vector<CodeViewBranch> m_branches;

friend class BranchDisplayDelegate;
};

0 comments on commit a7a84cc

Please sign in to comment.