Merge pull request #10542 from TryTwo/PR_MemoryViewWidget_Spacing

Debugger MemoryViewWidget: fixed, tighter spacing
This commit is contained in:
Admiral H. Curtiss 2022-03-30 22:53:05 +02:00 committed by GitHub
commit cc3f8204da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 15 deletions

View File

@ -9,6 +9,7 @@
#include <QMenu> #include <QMenu>
#include <QMouseEvent> #include <QMouseEvent>
#include <QScrollBar> #include <QScrollBar>
#include <QtGlobal>
#include <cmath> #include <cmath>
@ -32,17 +33,32 @@ MemoryViewWidget::MemoryViewWidget(QWidget* parent) : QTableWidget(parent)
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setShowGrid(false); setShowGrid(false);
setFont(Settings::Instance().GetDebugFont()); setContextMenuPolicy(Qt::CustomContextMenu);
connect(&Settings::Instance(), &Settings::DebugFontChanged, this, &QWidget::setFont); connect(&Settings::Instance(), &Settings::DebugFontChanged, this, &MemoryViewWidget::UpdateFont);
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this] { Update(); }); connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, [this] { Update(); });
connect(Host::GetInstance(), &Host::UpdateDisasmDialog, this, &MemoryViewWidget::Update); connect(Host::GetInstance(), &Host::UpdateDisasmDialog, this, &MemoryViewWidget::Update);
connect(this, &MemoryViewWidget::customContextMenuRequested, this, connect(this, &MemoryViewWidget::customContextMenuRequested, this,
&MemoryViewWidget::OnContextMenu); &MemoryViewWidget::OnContextMenu);
connect(&Settings::Instance(), &Settings::ThemeChanged, this, &MemoryViewWidget::Update); connect(&Settings::Instance(), &Settings::ThemeChanged, this, &MemoryViewWidget::Update);
setContextMenuPolicy(Qt::CustomContextMenu); // Also calls update.
UpdateFont();
}
void MemoryViewWidget::UpdateFont()
{
const QFontMetrics fm(Settings::Instance().GetDebugFont());
m_font_vspace = fm.lineSpacing();
// BoundingRect is too unpredictable, a custom one would be needed for each view type. Different
// fonts have wildly different spacing between two characters and horizontalAdvance includes
// spacing.
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
m_font_width = fm.horizontalAdvance(QLatin1Char('0'));
#else
m_font_width = fm.width(QLatin1Char('0'));
#endif
setFont(Settings::Instance().GetDebugFont());
Update(); Update();
} }
@ -72,7 +88,10 @@ void MemoryViewWidget::Update()
if (rowCount() == 0) if (rowCount() == 0)
setRowCount(1); setRowCount(1);
setRowHeight(0, 24); // This sets all row heights and determines horizontal ascii spacing.
verticalHeader()->setDefaultSectionSize(m_font_vspace - 1);
verticalHeader()->setMinimumSectionSize(m_font_vspace - 1);
horizontalHeader()->setMinimumSectionSize(m_font_width * 2);
const AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_address_space); const AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_address_space);
@ -83,8 +102,6 @@ void MemoryViewWidget::Update()
for (int i = 0; i < rows; i++) for (int i = 0; i < rows; i++)
{ {
setRowHeight(i, 24);
u32 addr = m_address - ((rowCount() / 2) * 16) + i * 16; u32 addr = m_address - ((rowCount() / 2) * 16) + i * 16;
auto* bp_item = new QTableWidgetItem; auto* bp_item = new QTableWidgetItem;
@ -187,26 +204,49 @@ void MemoryViewWidget::Update()
}); });
break; break;
case Type::Float32: case Type::Float32:
update_values( update_values([&accessors](u32 address) {
[&accessors](u32 address) { return QString::number(accessors->ReadF32(address)); }); QString string = QString::number(accessors->ReadF32(address), 'g', 4);
// Align to first digit.
if (!string.startsWith(QLatin1Char('-')))
string.prepend(QLatin1Char(' '));
return string;
});
break; break;
} }
if (row_breakpoint) if (row_breakpoint)
{ {
bp_item->setData(Qt::DecorationRole, bp_item->setData(Qt::DecorationRole, Resources::GetScaledThemeIcon("debugger_breakpoint")
Resources::GetScaledThemeIcon("debugger_breakpoint").pixmap(QSize(24, 24))); .pixmap(QSize(rowHeight(0) - 3, rowHeight(0) - 3)));
} }
} }
setColumnWidth(0, 24 + 5); setColumnWidth(0, rowHeight(0));
for (int i = 1; i < columnCount(); i++) int width = 0;
switch (m_type)
{ {
resizeColumnToContents(i); case Type::U8:
// Add some extra spacing because the default width is too small in most cases width = m_font_width * 3;
setColumnWidth(i, columnWidth(i) * 1.1); break;
case Type::ASCII:
width = m_font_width * 2;
break;
case Type::U16:
width = m_font_width * 5;
break;
case Type::U32:
width = m_font_width * 10;
break;
case Type::Float32:
width = m_font_width * 12;
break;
} }
for (int i = 2; i < columnCount(); i++)
setColumnWidth(i, width);
viewport()->update(); viewport()->update();
update(); update();
} }

View File

@ -35,6 +35,7 @@ public:
explicit MemoryViewWidget(QWidget* parent = nullptr); explicit MemoryViewWidget(QWidget* parent = nullptr);
void Update(); void Update();
void UpdateFont();
void ToggleBreakpoint(); void ToggleBreakpoint();
void ToggleRowBreakpoint(bool row); void ToggleRowBreakpoint(bool row);
@ -69,4 +70,6 @@ private:
bool m_do_log = true; bool m_do_log = true;
u32 m_context_address; u32 m_context_address;
u32 m_address = 0; u32 m_address = 0;
int m_font_width = 0;
int m_font_vspace = 0;
}; };