1
0
Fork 0

Add bytes to the CalculatorDialog and refactor

Thanks to @Rat431 for the idea, see #2872
This commit is contained in:
Duncan Ogilvie 2022-05-14 20:18:22 +02:00
parent d77f2d9b09
commit 101f4ae569
3 changed files with 307 additions and 219 deletions

View File

@ -2,17 +2,61 @@
#include "ui_CalculatorDialog.h"
#include "ValidateExpressionThread.h"
static duint bswap(duint value)
{
duint result = 0;
for(size_t i = 0; i < sizeof(value); i++)
((unsigned char*)&result)[sizeof(value) - i - 1] = ((unsigned char*)&value)[i];
return result;
}
QString CalculatorDialog::inFormat(const duint val, CalculatorDialog::Format format) const
{
switch(format)
{
default:
case Format::Hex:
return QString("%1").arg(val, 1, 16, QChar('0')).toUpper();
case Format::SignedDec:
return QString("%1").arg((dsint)val);
case Format::UnsignedDec:
return QString("%1").arg(val);
case Format::Binary:
{
QString binary = QString("%1").arg(val, 8 * sizeof(duint), 2, QChar('0')).toUpper();
QString ans = "";
for(int i = 0; i < sizeof(duint) * 8; i++)
{
if((i % 4 == 0) && (i != 0))
ans += " ";
ans += binary[i];
}
return ans;
}
case Format::Octal:
return QString("%1").arg(val, 1, 8, QChar('0')).toUpper();
case Format::Bytes:
return QString("%1").arg(bswap(val), 2 * sizeof(duint), 16, QChar('0')).toUpper();
}
}
CalculatorDialog::CalculatorDialog(QWidget* parent) : QDialog(parent), ui(new Ui::CalculatorDialog)
{
ui->setupUi(this);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint | Qt::MSWindowsFixedSizeDialogHint);
connect(this, SIGNAL(validAddress(bool)), ui->btnGoto, SLOT(setEnabled(bool)));
connect(this, SIGNAL(validAddress(bool)), ui->btnGotoDump, SLOT(setEnabled(bool)));
connect(this, SIGNAL(validAddress(bool)), ui->btnGotoMemoryMap, SLOT(setEnabled(bool)));
emit validAddress(false);
ui->txtBin->setInputMask(QString("bbbb ").repeated(sizeof(duint) * 2));
ui->txtBytes->setInputMask(QString("HH").repeated(sizeof(duint)));
ui->txtBin->setInputMask(QString("bbbb ").repeated(sizeof(duint) * 2).trimmed());
ui->txtExpression->setText("0");
ui->txtExpression->selectAll();
ui->txtExpression->setFocus();
mValidateThread = new ValidateExpressionThread(this);
mValidateThread->setOnExpressionChangedCallback(std::bind(&CalculatorDialog::validateExpression, this, std::placeholders::_1));
@ -35,6 +79,12 @@ void CalculatorDialog::validateExpression(QString expression)
this->mValidateThread->emitExpressionChanged(validExpression, validPointer, value);
}
void CalculatorDialog::setExpressionFocus()
{
ui->txtExpression->selectAll();
ui->txtExpression->setFocus();
}
void CalculatorDialog::showEvent(QShowEvent* event)
{
Q_UNUSED(event);
@ -48,36 +98,43 @@ void CalculatorDialog::hideEvent(QHideEvent* event)
mValidateThread->wait();
}
void CalculatorDialog::setExpressionFocus()
{
ui->txtExpression->selectAll();
ui->txtExpression->setFocus();
}
void CalculatorDialog::expressionChanged(bool validExpression, bool validPointer, dsint value)
{
if(!validExpression)
{
ui->txtBin->setText("");
ui->txtHex->setText("");
ui->txtSignedDec->setText("");
ui->txtUnsignedDec->setText("");
ui->txtHex->setText("");
ui->txtOct->setText("");
ui->txtBytes->setText("");
ui->txtBin->setText("");
ui->txtAscii->setText("");
ui->txtUnicode->setText("");
ui->txtExpression->setStyleSheet("border: 2px solid red");
emit validAddress(false);
}
else
{
ui->txtExpression->setStyleSheet("");
ui->txtHex->setText(inFormat(value, N_HEX));
ui->txtSignedDec->setText(inFormat(value, N_SDEC));
ui->txtUnsignedDec->setText(inFormat(value, N_UDEC));
int cursorpos = ui->txtBin->cursorPosition();
ui->txtBin->setText(inFormat(value, N_BIN));
ui->txtBin->setCursorPosition(cursorpos);
ui->txtOct->setText(inFormat(value, N_OCT));
ui->txtHex->setText(inFormat(value, Format::Hex));
ui->txtSignedDec->setText(inFormat(value, Format::SignedDec));
ui->txtUnsignedDec->setText(inFormat(value, Format::UnsignedDec));
ui->txtOct->setText(inFormat(value, Format::Octal));
{
int cursorpos = ui->txtBytes->cursorPosition();
ui->txtBytes->setText(inFormat(value, Format::Bytes));
ui->txtBytes->setCursorPosition(cursorpos);
}
{
int cursorpos = ui->txtBin->cursorPosition();
ui->txtBin->setText(inFormat(value, Format::Binary));
ui->txtBin->setCursorPosition(cursorpos);
}
if(value == (value & 0xFF))
{
QChar c((ushort)value);
@ -88,6 +145,7 @@ void CalculatorDialog::expressionChanged(bool validExpression, bool validPointer
}
else
ui->txtAscii->setText("???");
ui->txtAscii->setCursorPosition(0);
ui->txtAscii->selectAll();
if((value == (value & 0xFFFF))) //UNICODE?
@ -104,6 +162,7 @@ void CalculatorDialog::expressionChanged(bool validExpression, bool validPointer
}
ui->txtUnicode->setCursorPosition(0);
ui->txtUnicode->selectAll();
emit validAddress(validPointer);
}
}
@ -115,47 +174,13 @@ void CalculatorDialog::on_txtExpression_textChanged(const QString & arg1)
ui->txtSignedDec->setStyleSheet("");
ui->txtUnsignedDec->setStyleSheet("");
ui->txtOct->setStyleSheet("");
ui->txtBytes->setStyleSheet("");
ui->txtBin->setStyleSheet("");
ui->txtAscii->setStyleSheet("");
ui->txtUnicode->setStyleSheet("");
emit validAddress(false);
}
QString CalculatorDialog::inFormat(const duint val, CalculatorDialog::NUMBERFORMAT NF) const
{
switch(NF)
{
default:
case N_HEX:
return QString("%1").arg(val, 1, 16, QChar('0')).toUpper();
case N_SDEC:
return QString("%1").arg((dsint)val);
case N_UDEC:
return QString("%1").arg(val);
case N_BIN:
{
QString binary = QString("%1").arg(val, 8 * sizeof(duint), 2, QChar('0')).toUpper();
QString ans = "";
for(int i = 0; i < sizeof(duint) * 8; i++)
{
if((i % 4 == 0) && (i != 0))
ans += " ";
ans += binary[i];
}
return ans;
}
case N_OCT:
return QString("%1").arg(val, 1, 8, QChar('0')).toUpper();
case N_ASCII:
return QString("%1").arg(QChar((ushort)val));
}
}
void CalculatorDialog::on_btnGoto_clicked()
{
DbgCmdExecDirect(QString("disasm " + ui->txtExpression->text()));
}
void CalculatorDialog::on_txtHex_textEdited(const QString & arg1)
{
bool ok = false;
@ -208,11 +233,26 @@ void CalculatorDialog::on_txtOct_textEdited(const QString & arg1)
ui->txtExpression->setText(QString("%1").arg(val, 1, 16, QChar('0')).toUpper());
}
void CalculatorDialog::on_txtBytes_textEdited(const QString & arg1)
{
bool ok = false;
QString text = arg1;
text = text.leftJustified(sizeof(duint) * 2, '0', true);
ULONGLONG val = text.toULongLong(&ok, 16);
if(!ok)
{
ui->txtBytes->setStyleSheet("border: 2px solid red");
return;
}
ui->txtBytes->setStyleSheet("");
ui->txtExpression->setText(QString("%1").arg(bswap(val), 1, 16, QChar('0')).toUpper());
}
void CalculatorDialog::on_txtBin_textEdited(const QString & arg1)
{
bool ok = false;
QString text = arg1;
text = text.replace(" ", "");
text = text.replace(" ", "").leftJustified(sizeof(duint) * 8, '0', true);
ULONGLONG val = text.toULongLong(&ok, 2);
if(!ok)
{
@ -241,7 +281,18 @@ void CalculatorDialog::on_txtUnicode_textEdited(const QString & arg1)
ui->txtUnicode->selectAll();
}
void CalculatorDialog::on_btnGoto_clicked()
{
DbgCmdExecDirect(QString("disasm " + ui->txtHex->text()));
}
void CalculatorDialog::on_btnGotoDump_clicked()
{
DbgCmdExecDirect(QString("dump " + ui->txtExpression->text()));
DbgCmdExecDirect(QString("dump " + ui->txtHex->text()));
}
void CalculatorDialog::on_btnGotoMemoryMap_clicked()
{
DbgCmdExecDirect(QString("memmapdump " + ui->txtHex->text()));
}

View File

@ -14,17 +14,20 @@ class CalculatorDialog : public QDialog
{
Q_OBJECT
enum NUMBERFORMAT
enum class Format
{
N_HEX = 16,
N_SDEC = 10,
N_UDEC = 11,
N_BIN = 2,
N_OCT = 8,
N_ASCII = 0,
N_UNKNOWN = -1
Unknown = 0,
Hex,
SignedDec,
UnsignedDec,
Octal,
Bytes,
Binary,
};
QString inFormat(const duint val, CalculatorDialog::Format format) const;
public:
explicit CalculatorDialog(QWidget* parent = 0);
~CalculatorDialog();
@ -38,20 +41,20 @@ signals:
private slots:
void expressionChanged(bool validExpression, bool validPointer, dsint value);
void on_btnGoto_clicked();
void on_txtExpression_textChanged(const QString & arg1);
void on_txtHex_textEdited(const QString & arg1);
void on_txtSignedDec_textEdited(const QString & arg1);
void on_txtUnsignedDec_textEdited(const QString & arg1);
void on_txtOct_textEdited(const QString & arg1);
void on_txtBytes_textEdited(const QString & arg1);
void on_txtBin_textEdited(const QString & arg1);
void on_txtAscii_textEdited(const QString & arg1);
void on_txtUnicode_textEdited(const QString & arg1);
void on_txtExpression_textChanged(const QString & arg1);
void on_btnGoto_clicked();
void on_btnGotoDump_clicked();
void on_btnGotoMemoryMap_clicked();
private:
ValidateExpressionThread* mValidateThread;
Ui::CalculatorDialog* ui;
QString inFormat(const duint val, CalculatorDialog::NUMBERFORMAT NF) const;
};

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>502</width>
<height>270</height>
<height>279</height>
</rect>
</property>
<property name="sizePolicy">
@ -26,7 +26,150 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="8" column="1">
<item row="2" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Signed:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Unsigned:</string>
</property>
</widget>
</item>
<item row="10" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Unicode:</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Octal:</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Expression:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="txtSignedDec">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="txtUnsignedDec">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Binary:</string>
</property>
</widget>
</item>
<item row="9" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>ASCII:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="txtHex">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLineEdit" name="txtBin">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Lucida Console</family>
<pointsize>7</pointsize>
</font>
</property>
<property name="text">
<string>0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000</string>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLineEdit" name="txtOct">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item row="10" column="1">
<widget class="QLineEdit" name="txtUnicode">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
@ -50,140 +193,7 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Expression:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="txtExpression">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Octal:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Unsigned:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="txtUnsignedDec">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="txtHex">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Hexadecimal:</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>ASCII:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLineEdit" name="txtOct">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLineEdit" name="txtBin">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Lucida Console</family>
<pointsize>7</pointsize>
</font>
</property>
<property name="text">
<string>0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 0010</string>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Binary:</string>
</property>
</widget>
</item>
<item row="7" column="1">
<item row="9" column="1">
<widget class="QLineEdit" name="txtAscii">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
@ -207,22 +217,15 @@
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QLabel" name="label_7">
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Unicode:</string>
<string>Hexadecimal:</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Signed:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="txtSignedDec">
<item row="0" column="1">
<widget class="QLineEdit" name="txtExpression">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
@ -234,8 +237,28 @@
<family>Courier New</family>
</font>
</property>
<property name="readOnly">
<bool>false</bool>
</widget>
</item>
<item row="6" column="1">
<widget class="QLineEdit" name="txtBytes">
<property name="font">
<font>
<family>Courier New</family>
<pointsize>8</pointsize>
</font>
</property>
<property name="text">
<string>0000000000000000</string>
</property>
<property name="cursorPosition">
<number>0</number>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Bytes:</string>
</property>
</widget>
</item>
@ -257,6 +280,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnGotoMemoryMap">
<property name="text">
<string>Follow in Memory Map</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
@ -271,7 +301,7 @@
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<widget class="QPushButton" name="btnClose">
<property name="text">
<string>&amp;Close</string>
</property>
@ -287,24 +317,28 @@
<tabstop>txtSignedDec</tabstop>
<tabstop>txtUnsignedDec</tabstop>
<tabstop>txtOct</tabstop>
<tabstop>txtBytes</tabstop>
<tabstop>txtBin</tabstop>
<tabstop>txtAscii</tabstop>
<tabstop>txtUnicode</tabstop>
<tabstop>btnGoto</tabstop>
<tabstop>btnGotoDump</tabstop>
<tabstop>btnGotoMemoryMap</tabstop>
<tabstop>btnClose</tabstop>
</tabstops>
<resources>
<include location="../../resource.qrc"/>
</resources>
<connections>
<connection>
<sender>pushButton</sender>
<sender>btnClose</sender>
<signal>clicked()</signal>
<receiver>CalculatorDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>347</x>
<y>216</y>
<x>491</x>
<y>268</y>
</hint>
<hint type="destinationlabel">
<x>284</x>