mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
Can debugger
Add hex spinbox
This commit is contained in:
@@ -21,6 +21,8 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Svg WebSockets)
|
|||||||
|
|
||||||
add_subdirectory(qwt)
|
add_subdirectory(qwt)
|
||||||
|
|
||||||
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
|
||||||
set(PROJECT_SOURCES
|
set(PROJECT_SOURCES
|
||||||
simulators/sim-cvs_i10.h
|
simulators/sim-cvs_i10.h
|
||||||
simulators/sim-cvs_i10.cpp
|
simulators/sim-cvs_i10.cpp
|
||||||
@@ -43,6 +45,8 @@ set(PROJECT_SOURCES
|
|||||||
helpers.h
|
helpers.h
|
||||||
helpers.cpp
|
helpers.cpp
|
||||||
IterableEnum.hpp
|
IterableEnum.hpp
|
||||||
|
hex_spinbox.h
|
||||||
|
hex_spinbox.cpp
|
||||||
better_radiobutton.h
|
better_radiobutton.h
|
||||||
better_radiobutton.cpp
|
better_radiobutton.cpp
|
||||||
led.h
|
led.h
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef _HEXSPINBOX_H_
|
||||||
|
#define _HEXSPINBOX_H_
|
||||||
|
|
||||||
|
#include <QSpinBox>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QRegularExpressionValidator>
|
||||||
|
|
||||||
|
// 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_
|
||||||
Reference in New Issue
Block a user