Skip to content

Commit

Permalink
Correct numeric edit to write unsigned values
Browse files Browse the repository at this point in the history
  • Loading branch information
sanny committed Sep 5, 2023
1 parent 0d7d8ad commit 7f419ed
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
45 changes: 45 additions & 0 deletions omodscan/controls/numericlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions omodscan/controls/numericlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class NumericLineEdit : public QLineEdit
enum InputMode
{
DecMode = 0,
UnsignedMode,
HexMode,
FloatMode,
DoubleMode
Expand Down
1 change: 1 addition & 0 deletions omodscan/dialogs/dialogwriteholdingregister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 7f419ed

Please sign in to comment.