From 7f419ed6bc4476b439eb77be3dd1666fb059cd0f Mon Sep 17 00:00:00 2001 From: sanny Date: Tue, 5 Sep 2023 14:45:41 +0300 Subject: [PATCH] Correct numeric edit to write unsigned values --- omodscan/controls/numericlineedit.cpp | 45 +++++++++++++++++++ omodscan/controls/numericlineedit.h | 1 + .../dialogs/dialogwriteholdingregister.cpp | 1 + 3 files changed, 47 insertions(+) diff --git a/omodscan/controls/numericlineedit.cpp b/omodscan/controls/numericlineedit.cpp index ba3a069..d0be607 100644 --- a/omodscan/controls/numericlineedit.cpp +++ b/omodscan/controls/numericlineedit.cpp @@ -83,6 +83,11 @@ void NumericLineEdit::setInputMode(InputMode mode) _maxValue = INT_MAX; break; + case UnsignedMode: + _minValue = 0U; + _maxValue = UINT_MAX; + break; + case FloatMode: _minValue = -FLT_MAX; _maxValue = FLT_MAX; @@ -129,6 +134,20 @@ void NumericLineEdit::internalSetValue(QVariant value) } break; + case UnsignedMode: + value = qBound(_minValue.toUInt(), value.toUInt(), _maxValue.toUInt()); + if(_paddingZeroes) + { + const auto text = QStringLiteral("%1").arg(value.toUInt(), _paddingZeroWidth, 10, QLatin1Char('0')); + QLineEdit::setText(text); + } + else + { + const auto text = QString::number(value.toUInt()); + QLineEdit::setText(text); + } + break; + case HexMode: value = qBound(_minValue.toInt() > 0 ? _minValue.toUInt() : 0, value.toUInt(), _maxValue.toUInt()); if(_paddingZeroes) @@ -177,6 +196,15 @@ void NumericLineEdit::updateValue() } break; + case UnsignedMode: + { + bool ok; + const auto value = text().toUInt(&ok); + if(ok) internalSetValue(value); + else internalSetValue(_value); + } + break; + case HexMode: { bool ok; @@ -240,6 +268,14 @@ void NumericLineEdit::on_textChanged(const QString& text) } break; + case UnsignedMode: + { + bool ok; + const auto valueInt = text.toUInt(&ok); + if(ok) value = qBound(_minValue.toUInt(), valueInt, _maxValue.toUInt()); + } + break; + case HexMode: { bool ok; @@ -293,6 +329,15 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to } break; + case UnsignedMode: + { + const int nums = QString::number(top.toUInt()).length(); + _paddingZeroWidth = qMax(1, nums); + setMaxLength(qMax(1, nums)); + setValidator(new QIntValidator(bottom.toUInt(), top.toUInt(), this)); + } + break; + case HexMode: { const int nums = QString::number(top.toUInt(), 16).length(); diff --git a/omodscan/controls/numericlineedit.h b/omodscan/controls/numericlineedit.h index df1439b..cf35aa8 100644 --- a/omodscan/controls/numericlineedit.h +++ b/omodscan/controls/numericlineedit.h @@ -14,6 +14,7 @@ class NumericLineEdit : public QLineEdit enum InputMode { DecMode = 0, + UnsignedMode, HexMode, FloatMode, DoubleMode diff --git a/omodscan/dialogs/dialogwriteholdingregister.cpp b/omodscan/dialogs/dialogwriteholdingregister.cpp index dc5a8d0..9e14c13 100644 --- a/omodscan/dialogs/dialogwriteholdingregister.cpp +++ b/omodscan/dialogs/dialogwriteholdingregister.cpp @@ -69,6 +69,7 @@ DialogWriteHoldingRegister::DialogWriteHoldingRegister(ModbusWriteParams& params case DataDisplayMode::UnsignedLongInteger: case DataDisplayMode::SwappedUnsignedLI: ui->lineEditValue->setInputRange(0U, UINT_MAX); + ui->lineEditValue->setInputMode(NumericLineEdit::UnsignedMode); ui->lineEditValue->setValue(params.Value.toUInt()); break; }