From 5938645654664bba80b46e78bd3e511847b33f96 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sat, 27 Jul 2024 14:42:18 +0200 Subject: [PATCH] Can debugger Add hex spinbox --- Qt/CMakeLists.txt | 4 ++++ Qt/hex_spinbox.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ Qt/hex_spinbox.h | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 Qt/hex_spinbox.cpp create mode 100644 Qt/hex_spinbox.h diff --git a/Qt/CMakeLists.txt b/Qt/CMakeLists.txt index b90545b..b785bea 100644 --- a/Qt/CMakeLists.txt +++ b/Qt/CMakeLists.txt @@ -21,6 +21,8 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Svg WebSockets) add_subdirectory(qwt) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + set(PROJECT_SOURCES simulators/sim-cvs_i10.h simulators/sim-cvs_i10.cpp @@ -43,6 +45,8 @@ set(PROJECT_SOURCES helpers.h helpers.cpp IterableEnum.hpp + hex_spinbox.h + hex_spinbox.cpp better_radiobutton.h better_radiobutton.cpp led.h diff --git a/Qt/hex_spinbox.cpp b/Qt/hex_spinbox.cpp new file mode 100644 index 0000000..2982f1e --- /dev/null +++ b/Qt/hex_spinbox.cpp @@ -0,0 +1,44 @@ +#include "hex_spinbox.h" + +HexSpinBox::HexSpinBox(QWidget *parent, + bool showPrefix, + const QString &format) : + QSpinBox(parent), + format(format) +{ + // Validates hex strings up to 8 chars with or w/out leading "0x" prefix. + // For arbitrary prefix/suffix, the regex could be built dynamically + // in validate(), or override setPrefix()/setSuffix() methods. + const QRegularExpression rx("(?:0[xX])?[0-9A-Fa-f]{1,8}"); + validator = new QRegularExpressionValidator(rx, this); + setShowPrefix(showPrefix); +} + +void HexSpinBox::setShowPrefix(bool show) +{ + if (show) + setPrefix(QStringLiteral("0x")); + else + setPrefix(QString()); +} + +void HexSpinBox::setFormat(const QString &text) +{ + format = text; + this->lineEdit()->setText(textFromValue(value())); +} + +QValidator::State HexSpinBox::validate(QString &text, int &pos) const +{ + return validator->validate(text, pos); +} + +int HexSpinBox::valueFromText(const QString &text) const +{ + return text.toInt(0, 16); +} + +QString HexSpinBox::textFromValue(int value) const +{ + return QString().asprintf(qPrintable(format), value); +} diff --git a/Qt/hex_spinbox.h b/Qt/hex_spinbox.h new file mode 100644 index 0000000..aeb1d4d --- /dev/null +++ b/Qt/hex_spinbox.h @@ -0,0 +1,34 @@ +#ifndef _HEXSPINBOX_H_ +#define _HEXSPINBOX_H_ + +#include +#include +#include + +// NOTE: Since QSpinBox uses int as the storage type, the effective editing range +// is +/- 0x7FFF FFFF, so it can't handle a full unsigned int. +// QDoubleSpinBox would be a more suitable base class if a wider range is needed. +class HexSpinBox : public QSpinBox +{ + Q_OBJECT + + public: + HexSpinBox(QWidget *parent = nullptr, + bool showPrefix = false, + const QString &format = QStringLiteral("%X")); + + public slots: + void setShowPrefix(bool show); + void setFormat(const QString &text); + + protected: + QValidator::State validate(QString &text, int &pos) const override; + int valueFromText(const QString &text) const override; + QString textFromValue(int value) const override; + + private: + QRegularExpressionValidator *validator; + QString format; +}; + +#endif // _HEXSPINBOX_H_