dolphin/Source/Core/DolphinQt2/Debugger/RegisterColumn.cpp

124 lines
2.6 KiB
C++

// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "DolphinQt2/Debugger/RegisterColumn.h"
#include <cstring>
#include <utility>
#include <QMessageBox>
RegisterColumn::RegisterColumn(RegisterType type, std::function<u64()> get,
std::function<void(u64)> set)
: m_type(type), m_get_register(std::move(get)), m_set_register(std::move(set))
{
RefreshValue();
Update();
setFlags(set == nullptr ? flags() ^ Qt::ItemIsEditable : flags());
setData(DATA_TYPE, static_cast<quint32>(type));
}
RegisterDisplay RegisterColumn::GetDisplay() const
{
return m_display;
}
void RegisterColumn::SetDisplay(RegisterDisplay display)
{
m_display = display;
Update();
}
void RegisterColumn::RefreshValue()
{
QBrush brush = QPalette().brush(QPalette::Text);
if (m_value != m_get_register())
{
m_value = m_get_register();
brush.setColor(Qt::red);
}
setForeground(brush);
Update();
}
u64 RegisterColumn::GetValue() const
{
return m_value;
}
void RegisterColumn::SetValue()
{
u64 value = 0;
bool valid = false;
switch (m_display)
{
case RegisterDisplay::Hex:
value = text().toULongLong(&valid, 16);
break;
case RegisterDisplay::SInt32:
value = text().toInt(&valid);
break;
case RegisterDisplay::UInt32:
value = text().toUInt(&valid);
break;
case RegisterDisplay::Float:
{
float f = text().toFloat(&valid);
std::memcpy(&value, &f, sizeof(u32));
break;
}
}
if (!valid)
{
QMessageBox::critical(nullptr, QObject::tr("Invalid input"),
QObject::tr("Bad input for field"));
}
else
{
m_set_register(value);
}
RefreshValue();
}
void RegisterColumn::Update()
{
QString text;
switch (m_display)
{
case RegisterDisplay::Hex:
text = QStringLiteral("%1").arg(m_value,
(m_type == RegisterType::ibat || m_type == RegisterType::dbat ||
m_type == RegisterType::fpr ?
sizeof(u64) :
sizeof(u32)) *
2,
16, QLatin1Char('0'));
break;
case RegisterDisplay::SInt32:
text = QString::number(static_cast<qint32>(m_value));
break;
case RegisterDisplay::UInt32:
text = QString::number(static_cast<quint32>(m_value));
break;
case RegisterDisplay::Float:
{
float tmp;
std::memcpy(&tmp, &m_value, sizeof(float));
text = QString::number(tmp);
break;
}
}
setText(text);
}