1212#include " qhexvalidator.h"
1313#include " quintvalidator.h"
1414#include " qint64validator.h"
15+ #include " qintvalidatorex.h"
16+ #include " qdoublevalidatorex.h"
1517#include " numericlineedit.h"
1618
1719namespace
@@ -72,11 +74,10 @@ NumericLineEdit::NumericLineEdit(QWidget* parent)
7274 ,_hexButtonVisible(false )
7375 ,_allowEmptyValue(false )
7476{
75- connect (_hexButton, &QToolButton::toggled, this , &NumericLineEdit::on_hexViewToggled);
76-
7777 setInputMode (Int32Mode);
7878 setValue (0 );
7979
80+ connect (_hexButton, &QToolButton::toggled, this , &NumericLineEdit::on_hexViewToggled);
8081 connect (this , &QLineEdit::editingFinished, this , &NumericLineEdit::on_editingFinished);
8182 connect (this , &QLineEdit::textChanged, this , &NumericLineEdit::on_textChanged);
8283 connect (this , &NumericLineEdit::rangeChanged, this , &NumericLineEdit::on_rangeChanged);
@@ -96,11 +97,10 @@ NumericLineEdit::NumericLineEdit(NumericLineEdit::InputMode mode, QWidget *paren
9697 ,_hexButtonVisible(false )
9798 ,_allowEmptyValue(false )
9899{
99- connect (_hexButton, &QToolButton::toggled, this , &NumericLineEdit::on_hexViewToggled);
100-
101100 setInputMode (mode);
102101 setValue (0 );
103102
103+ connect (_hexButton, &QToolButton::toggled, this , &NumericLineEdit::on_hexViewToggled);
104104 connect (this , &QLineEdit::editingFinished, this , &NumericLineEdit::on_editingFinished);
105105 connect (this , &QLineEdit::textChanged, this , &NumericLineEdit::on_textChanged);
106106 connect (this , &NumericLineEdit::rangeChanged, this , &NumericLineEdit::on_rangeChanged);
@@ -260,8 +260,7 @@ void NumericLineEdit::updateButtons()
260260 const bool hexVisible = _hexButtonVisible && isHexViewApplicable ();
261261 _hexButton->setVisible (hexVisible);
262262
263- if (hexVisible)
264- {
263+ if (hexVisible) {
265264 _hexButton->setGeometry (width () - h - 4 , 4 , h, h);
266265 _hexButton->setIconSize (QSize (h, h));
267266 }
@@ -304,8 +303,7 @@ void NumericLineEdit::setAllowEmptyValue(bool allow)
304303{
305304 _allowEmptyValue = allow;
306305 setClearButtonEnabled (allow);
307- if (!allow && !_value.isValid ())
308- internalSetValue (_minValue);
306+ clear ();
309307}
310308
311309// /
@@ -317,12 +315,16 @@ bool NumericLineEdit::isEmpty() const
317315}
318316
319317// /
320- // / \brief NumericLineEdit::clearValue
318+ // / \brief NumericLineEdit::clear
321319// /
322- void NumericLineEdit::clearValue ()
320+ void NumericLineEdit::clear ()
323321{
324- if (_allowEmptyValue)
322+ if (_allowEmptyValue) {
325323 internalSetValue (QVariant ());
324+ return ;
325+ }
326+
327+ internalSetValue (0 );
326328}
327329
328330// /
@@ -335,8 +337,10 @@ void NumericLineEdit::internalSetValue(QVariant value)
335337 {
336338 const auto oldValue = _value;
337339 _value = QVariant ();
340+
338341 if (!QLineEdit::text ().isEmpty ())
339342 QLineEdit::setText (QString ());
343+
340344 if (oldValue.isValid ())
341345 {
342346 emit valueChanged (_value);
@@ -617,11 +621,16 @@ void NumericLineEdit::updateValue()
617621// /
618622void NumericLineEdit::focusInEvent (QFocusEvent* e)
619623{
620- internalSetValue (_value); // sets text without "0x" prefix first
621- // Reduce max length after text is updated to avoid truncation via setMaxLength
622- if (_inputMode == HexMode || _hexView)
624+ QSignalBlocker blocker (this );
625+
626+ // Reduce max length before text is updated to avoid truncation via setMaxLength
627+ if (_inputMode == HexMode || _hexView) {
623628 setMaxLength (_leadingZeroWidth);
629+ }
630+ internalSetValue (_value); // sets text without "0x" prefix first
631+
624632 QLineEdit::focusInEvent (e);
633+ QLineEdit::selectAll ();
625634}
626635
627636// /
@@ -631,8 +640,10 @@ void NumericLineEdit::focusInEvent(QFocusEvent* e)
631640void NumericLineEdit::focusOutEvent (QFocusEvent* e)
632641{
633642 // Restore max length before updateValue so setText("0x...") is not truncated
634- if (_inputMode == HexMode || _hexView)
643+ if (_inputMode == HexMode || _hexView) {
635644 setMaxLength (_leadingZeroWidth + 2 );
645+ }
646+
636647 updateValue ();
637648 QLineEdit::focusOutEvent (e);
638649}
@@ -643,6 +654,8 @@ void NumericLineEdit::focusOutEvent(QFocusEvent* e)
643654// /
644655void NumericLineEdit::keyPressEvent (QKeyEvent* e)
645656{
657+ if (e->key () == Qt::Key_Return || e->key () == Qt::Key_Enter)
658+ updateValue ();
646659 QLineEdit::keyPressEvent (e);
647660}
648661
@@ -821,13 +834,13 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to
821834 {
822835 _leadingZeroWidth = qMax (1 , (int )QString::number (static_cast <uint>(top.toInt ()), 16 ).length ());
823836 setMaxLength (_leadingZeroWidth + 2 ); // +2 for "0x"
824- setValidator (new QHexValidator (this ));
837+ setValidator (new QHexValidator (this , _allowEmptyValue ));
825838 }
826839 else
827840 {
828841 const int nums = QString::number (top.toInt ()).length ();
829842 setMaxLength (qMax (2 , nums + 1 ));
830- setValidator (new QIntValidator (bottom.toInt (), top.toInt (), this ));
843+ setValidator (new QIntValidatorEx (bottom.toInt (), top.toInt (), _allowEmptyValue , this ));
831844 }
832845 }
833846 break ;
@@ -838,14 +851,14 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to
838851 {
839852 _leadingZeroWidth = qMax (1 , (int )QString::number (top.toUInt (), 16 ).length ());
840853 setMaxLength (_leadingZeroWidth + 2 ); // +2 for "0x"
841- setValidator (new QHexValidator (this ));
854+ setValidator (new QHexValidator (this , _allowEmptyValue ));
842855 }
843856 else
844857 {
845858 const int nums = QString::number (top.toUInt ()).length ();
846859 _leadingZeroWidth = qMax (1 , nums);
847860 setMaxLength (qMax (1 , nums));
848- setValidator (new QUIntValidator (bottom.toUInt (), top.toUInt (), this ));
861+ setValidator (new QUIntValidator (bottom.toUInt (), top.toUInt (), _allowEmptyValue, this ));
849862 }
850863 }
851864 break ;
@@ -855,7 +868,7 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to
855868 const int nums = QString::number (top.toUInt (), 16 ).length ();
856869 _leadingZeroWidth = qMax (1 , nums);
857870 setMaxLength (qMax (1 , nums + 2 ));
858- setValidator (new QHexValidator (bottom.toUInt (), top.toUInt (), this ));
871+ setValidator (new QHexValidator (bottom.toUInt (), top.toUInt (), this , _allowEmptyValue ));
859872 }
860873 break ;
861874
@@ -866,7 +879,7 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to
866879 case FloatMode:
867880 case DoubleMode:
868881 setMaxLength (INT16_MAX);
869- setValidator (new QDoubleValidator (bottom.toDouble (), top.toDouble (), 6 , this ));
882+ setValidator (new QDoubleValidatorEx (bottom.toDouble (), top.toDouble (), 6 , _allowEmptyValue , this ));
870883 break ;
871884
872885 case Int64Mode:
@@ -880,7 +893,7 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to
880893 {
881894 const int nums = QString::number (top.toLongLong ()).length ();
882895 setMaxLength (qMax (2 , nums + 1 ));
883- setValidator (new QInt64Validator (bottom.toLongLong (), top.toLongLong (), this ));
896+ setValidator (new QInt64Validator (bottom.toLongLong (), top.toLongLong (), _allowEmptyValue, this ));
884897 }
885898 }
886899 break ;
@@ -897,7 +910,7 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to
897910 const int nums = QString::number (top.toULongLong ()).length ();
898911 _leadingZeroWidth = qMax (1 , nums);
899912 setMaxLength (qMax (1 , nums));
900- setValidator (new QUIntValidator (bottom.toULongLong (), top.toULongLong (), this ));
913+ setValidator (new QUIntValidator (bottom.toULongLong (), top.toULongLong (), _allowEmptyValue, this ));
901914 }
902915 }
903916 break ;
0 commit comments