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 23, 2020
1 parent edad018 commit ea3d8c5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
93 changes: 88 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,68 @@
#include "DolphinQt/Resources.h"
#include "DolphinQt/Settings.h"

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().shadow().color());

const u32 addr = m_parent->AddressForRow(index.row());
const u32 x_offset_in_branch_for_vertical_line = 10;
u32 current_branch_index = 0;
for (const CodeViewBranch& branch : m_parent->m_branches)
{
const u32 addr_lower = std::min(branch.m_src_addr, branch.m_dst_addr);
const u32 addr_higher = std::max(branch.m_src_addr, branch.m_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.m_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 @@ -38,7 +102,7 @@ constexpr size_t VALID_BRANCH_LENGTH = 10;

CodeViewWidget::CodeViewWidget()
{
setColumnCount(5);
setColumnCount(6);
setShowGrid(false);
setContextMenuPolicy(Qt::CustomContextMenu);
setSelectionMode(QAbstractItemView::SingleSelection);
Expand All @@ -51,6 +115,7 @@ CodeViewWidget::CodeViewWidget()
horizontalHeader()->hide();

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

FontBasedSizing();

Expand Down Expand Up @@ -90,6 +155,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 rowZeroAddress = m_address - ((rowCount() / 2) * 4);
return rowZeroAddress + row * 4;
}

void CodeViewWidget::Update()
{
if (!isVisible())
Expand Down Expand Up @@ -122,9 +195,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 @@ -147,8 +222,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 @@ -174,8 +250,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.m_src_addr = addr;
branch.m_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 @@ -194,6 +275,7 @@ void CodeViewWidget::Update()
setItem(i, 2, ins_item);
setItem(i, 3, param_item);
setItem(i, 4, description_item);
setItem(i, 5, branch_item);

if (addr == GetAddress())
{
Expand All @@ -202,6 +284,7 @@ void CodeViewWidget::Update()
}

resizeColumnsToContents();
setColumnWidth(5, 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,12 @@ class QMouseEvent;
class QResizeEvent;
class QShowEvent;

struct CodeViewBranch
{
u32 m_src_addr;
u32 m_dst_addr;
};

class CodeViewWidget : public QTableWidget
{
Q_OBJECT
Expand All @@ -38,6 +44,10 @@ class CodeViewWidget : public QTableWidget
void ToggleBreakpoint();
void AddBreakpoint();

u32 AddressForRow(int row) const;

std::vector<CodeViewBranch> m_branches;

signals:
void RequestPPCComparison(u32 addr);
void ShowMemory(u32 address);
Expand Down

0 comments on commit ea3d8c5

Please sign in to comment.