Skip to content

Commit

Permalink
Octal dialog now shows octal/hex values
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeemin committed Jan 2, 2024
1 parent 4496b7b commit 8a9f74e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 24 deletions.
47 changes: 43 additions & 4 deletions emulator/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <QFont>
#include <QPainter>
#include <QCoreApplication>
#include "main.h"
#include "mainwindow.h"


//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -59,9 +61,11 @@ bool AlertOkCancel(const QString &sMessage)

#if !defined(PRODUCT)

void DebugPrint(const char* /*message*/)
void DebugPrint(const char* message)
{
//TODO: Implement in this environment
MainWindow* mainWindow = Global_getMainWindow();
if (mainWindow != nullptr)
mainWindow->consolePrint(message);
}

void DebugPrintFormat(const char* pszFormat, ...)
Expand Down Expand Up @@ -205,7 +209,7 @@ void DrawBinaryValue(QPainter &painter, int x, int y, quint16 value)
painter.drawText(x, y, buffer);
}

// Parse octal value from text
// Parse 16-bit octal value from text
bool ParseOctalValue(const char* text, quint16* pValue)
{
quint16 value = 0;
Expand All @@ -224,7 +228,7 @@ bool ParseOctalValue(const char* text, quint16* pValue)
return true;
}

// Parse octal value from text
// Parse 16-bit octal value from text
bool ParseOctalValue(const QString &text, quint16* pValue)
{
quint16 value = 0;
Expand All @@ -242,6 +246,41 @@ bool ParseOctalValue(const QString &text, quint16* pValue)
return true;
}

// Parse 16-bit hex value from text
bool ParseHexValue(const char* text, quint16* pValue)
{
quint16 value = 0;
char* pChar = (char*) text;
for (int p = 0; ; p++)
{
if (p > 4) return false;
char ch = *pChar; pChar++;
if (ch == 0) break;
if (ch >= '0' && ch <= '9')
{
value = (value << 4);
int digit = ch - '0';
value += digit;
}
else if (ch >= 'a' && ch <= 'f')
{
value = (value << 4);
int digit = ch - 'a' + 10;
value += digit;
}
else if (ch >= 'A' && ch <= 'F')
{
value = (value << 4);
int digit = ch - 'A' + 10;
value += digit;
}
else
return false;
}
*pValue = value;
return true;
}

void CopyTextToClipboard(const char* text)
{
QClipboard *clipboard = QGuiApplication::clipboard();
Expand Down
1 change: 1 addition & 0 deletions emulator/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ void DrawHexValue(QPainter &painter, int x, int y, quint16 value);
void DrawBinaryValue(QPainter &painter, int x, int y, quint16 value);
bool ParseOctalValue(const char* text, quint16* pValue);
bool ParseOctalValue(const QString &text, quint16* pValue);
bool ParseHexValue(const char* text, quint16* pValue);

void CopyTextToClipboard(const char* text);
void CopyWordOctalToClipboard(uint16_t value);
Expand Down
68 changes: 54 additions & 14 deletions emulator/qdialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,73 @@
//////////////////////////////////////////////////////////////////////


QInputOctalDialog::QInputOctalDialog(QWidget *parent, const QString & title, const QString & prompt, quint16 * value)
QInputOctalDialog::QInputOctalDialog(QWidget *parent, const QString & title, quint16 * value)
: QDialog(parent, nullptr)
{
m_result = value;

char buffer[8];
PrintOctalValue(buffer, *value);

setWindowTitle(title);
resize(340, 120);
m_label.setText(prompt);
m_layout.addWidget(&m_label);
m_edit.setText(buffer);
m_edit.selectAll();
m_layout.addWidget(&m_edit);
m_layout.addWidget(&m_spacer);
resize(260, 120);
m_labelOctal.setText(tr("Octal"));
m_labelHex.setText(tr("Hex"));
m_buttons.setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

m_layout.addWidget(&m_labelOctal, 0, 0);
m_layout.addWidget(&m_labelHex, 0, 1);
m_layout.addWidget(&m_editOctal, 1, 0);
m_layout.addWidget(&m_editHex, 1, 1);
m_layout.addWidget(&m_spacer, 2, 0);
m_layout.addWidget(&m_buttons, 3, 0, 1, -1);
setLayout(&m_layout);

QObject::connect(&m_editOctal, SIGNAL(textEdited(QString)), this, SLOT(octalEdited(QString)));
QObject::connect(&m_editHex, SIGNAL(textEdited(QString)), this, SLOT(hexEdited(QString)));
QObject::connect(&m_buttons, SIGNAL(rejected()), this, SLOT(reject()));
QObject::connect(&m_buttons, SIGNAL(accepted()), this, SLOT(accept()));
m_layout.addWidget(&m_buttons);
setLayout(&m_layout);

char buffer[8];
PrintOctalValue(buffer, *value);
m_editOctal.setText(buffer);
PrintHexValue(buffer, *value);
m_editHex.setText(buffer);
m_editOctal.selectAll();
}

void QInputOctalDialog::octalEdited(const QString &text)
{
quint16 value;
if (! ParseOctalValue(text.toLatin1().data(), &value))
{
m_editHex.setText(nullptr);
}
else
{
char buffer[8];
PrintHexValue(buffer, value);
m_editHex.setText(buffer);
}
}

void QInputOctalDialog::hexEdited(const QString &text)
{
quint16 value;
if (! ParseHexValue(text.toLatin1().data(), &value))
{
m_editOctal.setText(nullptr);
}
else
{
char buffer[8];
PrintOctalValue(buffer, value);
m_editOctal.setText(buffer);
}
}

void QInputOctalDialog::accept()
{
QString text = m_editOctal.text();
quint16 value;
if (! ParseOctalValue(m_edit.text().toLatin1().data(), &value))
if (text.isEmpty() || !ParseOctalValue(text.toLatin1().data(), &value))
{
QMessageBox::warning(this, nullptr, tr("Please enter correct octal value."));
return;
Expand Down
14 changes: 9 additions & 5 deletions emulator/qdialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <QDialog>
#include <QVBoxLayout>
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QLabel>
#include <QLineEdit>


//////////////////////////////////////////////////////////////////////
Expand All @@ -17,16 +17,20 @@ class QInputOctalDialog : public QDialog
Q_OBJECT

public:
QInputOctalDialog(QWidget * parent, const QString & title, const QString & prompt, quint16 * value);
QInputOctalDialog(QWidget * parent, const QString & title, quint16 * value);

public slots:
void octalEdited(const QString &text);
void hexEdited(const QString &text);
virtual void accept();

private:
quint16 * m_result;
QVBoxLayout m_layout;
QLabel m_label;
QLineEdit m_edit;
QGridLayout m_layout;
QLabel m_labelOctal;
QLabel m_labelHex;
QLineEdit m_editOctal;
QLineEdit m_editHex;
QWidget m_spacer;
QDialogButtonBox m_buttons;
};
Expand Down
2 changes: 1 addition & 1 deletion emulator/qmemoryview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void QMemoryView::scrollBy(qint16 delta)
void QMemoryView::gotoAddress()
{
quint16 value = m_wBaseAddress;
QInputOctalDialog dialog(this, tr("Go To Address"), tr("Address (octal):"), &value);
QInputOctalDialog dialog(this, tr("Go To Address"), &value);
if (dialog.exec() == QDialog::Rejected) return;

// Scroll to the address
Expand Down

0 comments on commit 8a9f74e

Please # to comment.