From 2ef482e925a3983377ffc0d0f5fe689764ccb5f9 Mon Sep 17 00:00:00 2001 From: Nikita Zimin Date: Wed, 3 Jan 2024 15:10:43 +0300 Subject: [PATCH] Octal dialog now shows octal/hex values --- emulator/!astyle.bat | 5 +++ emulator/Common.cpp | 51 ++++++++++++++++++++++++---- emulator/Common.h | 5 +-- emulator/qdialogs.cpp | 72 ++++++++++++++++++++++++++++++++-------- emulator/qdialogs.h | 12 ++++--- emulator/qmemoryview.cpp | 2 +- 6 files changed, 118 insertions(+), 29 deletions(-) create mode 100644 emulator/!astyle.bat diff --git a/emulator/!astyle.bat b/emulator/!astyle.bat new file mode 100644 index 0000000..fd091b2 --- /dev/null +++ b/emulator/!astyle.bat @@ -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 diff --git a/emulator/Common.cpp b/emulator/Common.cpp index 3a97317..763ee2a 100644 --- a/emulator/Common.cpp +++ b/emulator/Common.cpp @@ -7,6 +7,8 @@ #include #include #include +#include "main.h" +#include "mainwindow.h" ////////////////////////////////////////////////////////////////////// @@ -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, ...) @@ -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); @@ -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); @@ -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; @@ -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; @@ -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(); diff --git a/emulator/Common.h b/emulator/Common.h index 0856fa9..b05d824 100644 --- a/emulator/Common.h +++ b/emulator/Common.h @@ -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 @@ -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); diff --git a/emulator/qdialogs.cpp b/emulator/qdialogs.cpp index 1673e66..fa99bd5 100644 --- a/emulator/qdialogs.cpp +++ b/emulator/qdialogs.cpp @@ -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; @@ -63,7 +107,7 @@ QAboutDialog::QAboutDialog(QWidget * parent) "Special thanks to:\nArseny Gordin")); QLabel * linkLabel = new QLabel( - "https://github.com/nzeemin/ukncbtl-qt"); + "https://github.com/nzeemin/ukncbtl-qt"); linkLabel->setOpenExternalLinks(true); QLabel * disclamerLabel = new QLabel(tr( diff --git a/emulator/qdialogs.h b/emulator/qdialogs.h index f37fa92..51e6245 100644 --- a/emulator/qdialogs.h +++ b/emulator/qdialogs.h @@ -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; }; diff --git a/emulator/qmemoryview.cpp b/emulator/qmemoryview.cpp index 572582b..2a8dfc2 100644 --- a/emulator/qmemoryview.cpp +++ b/emulator/qmemoryview.cpp @@ -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