From 8a9f74e5e119208333d3487089087c85dc9519ae Mon Sep 17 00:00:00 2001 From: Nikita Zimin Date: Tue, 2 Jan 2024 19:13:02 +0300 Subject: [PATCH] Octal dialog now shows octal/hex values --- emulator/Common.cpp | 47 ++++++++++++++++++++++++--- emulator/Common.h | 1 + emulator/qdialogs.cpp | 68 +++++++++++++++++++++++++++++++--------- emulator/qdialogs.h | 14 ++++++--- emulator/qmemoryview.cpp | 2 +- 5 files changed, 108 insertions(+), 24 deletions(-) diff --git a/emulator/Common.cpp b/emulator/Common.cpp index 131b93e..17bbbe0 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, ...) @@ -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; @@ -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; @@ -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(); diff --git a/emulator/Common.h b/emulator/Common.h index c5c9ac2..528e7c4 100644 --- a/emulator/Common.h +++ b/emulator/Common.h @@ -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); diff --git a/emulator/qdialogs.cpp b/emulator/qdialogs.cpp index 576b591..de08246 100644 --- a/emulator/qdialogs.cpp +++ b/emulator/qdialogs.cpp @@ -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; diff --git a/emulator/qdialogs.h b/emulator/qdialogs.h index 92714d4..b251cdc 100644 --- a/emulator/qdialogs.h +++ b/emulator/qdialogs.h @@ -5,8 +5,8 @@ #include #include #include -#include #include +#include ////////////////////////////////////////////////////////////////////// @@ -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; }; diff --git a/emulator/qmemoryview.cpp b/emulator/qmemoryview.cpp index d247c0d..76d7a9a 100644 --- a/emulator/qmemoryview.cpp +++ b/emulator/qmemoryview.cpp @@ -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