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 3, 2024
1 parent 07fbe34 commit 2ef482e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 29 deletions.
5 changes: 5 additions & 0 deletions emulator/!astyle.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off
set ASTYLEEXE=c:\bin\astyle.exe
set ASTYLEOPT=-n -Q --options=..\astyle-cpp-options
%ASTYLEEXE% %ASTYLEOPT% Util\*.h Util\*.cpp
%ASTYLEEXE% %ASTYLEOPT% *.h *.cpp --exclude=ui_mainwindow.h
51 changes: 45 additions & 6 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 All @@ -70,7 +74,7 @@ void DebugPrintFormat(const char* pszFormat, ...)

va_list ptr;
va_start(ptr, pszFormat);
_snprintf(buffer, 512, pszFormat, ptr);
vsnprintf(buffer, 512, pszFormat, ptr);
va_end(ptr);

DebugPrint(buffer);
Expand Down Expand Up @@ -101,7 +105,7 @@ void DebugLogFormat(const char* pszFormat, ...)

va_list ptr;
va_start(ptr, pszFormat);
_snprintf(buffer, 512, pszFormat, ptr);
vsnprintf(buffer, 512, pszFormat, ptr);
va_end(ptr);

DebugLog(buffer);
Expand Down Expand Up @@ -208,7 +212,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 @@ -227,7 +231,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 @@ -245,6 +249,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
5 changes: 1 addition & 4 deletions emulator/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ typedef const char * LPCTSTR;

#define MAKELONG(a, b) ((qint16)(((quint16)(((quint32)(a)) & 0xffff)) | ((quint32)((quint16)(((quint32)(b)) & 0xffff))) << 16))
#define MAKEWORD(a, b) ((quint16)(((quint8)(((quint32)(a)) & 0xff)) | ((quint16)((quint8)(((quint32)(b)) & 0xff))) << 8))
//#define LOWORD(l) ((quint16)(((quint32)(l)) & 0xffff))
//#define HIWORD(l) ((quint16)((((quint32)(l)) >> 16) & 0xffff))
//#define LOBYTE(w) ((quint8)(((quint32)(w)) & 0xff))
//#define HIBYTE(w) ((quint8)((((quint32)(w)) >> 8) & 0xff))

#ifdef __GNUC__
#define CALLBACK
Expand Down Expand Up @@ -127,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
72 changes: 58 additions & 14 deletions emulator/qdialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,77 @@
//////////////////////////////////////////////////////////////////////


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);
QFont font = Common_GetMonospacedFont();
m_editOctal.setFont(font);
m_editHex.setFont(font);

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 Expand Up @@ -63,7 +107,7 @@ QAboutDialog::QAboutDialog(QWidget * parent)
"Special thanks to:\nArseny Gordin"));

QLabel * linkLabel = new QLabel(
"<a href=\"https://github.com/nzeemin/ukncbtl-qt\">https://github.com/nzeemin/ukncbtl-qt</a>");
"<a href=\"https://github.com/nzeemin/ukncbtl-qt\">https://github.com/nzeemin/ukncbtl-qt</a>");
linkLabel->setOpenExternalLinks(true);

QLabel * disclamerLabel = new QLabel(tr(
Expand Down
12 changes: 8 additions & 4 deletions emulator/qdialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,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 @@ -168,7 +168,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 2ef482e

Please sign in to comment.