Skip to content

Commit dbaa95a

Browse files
saintmatthieuigorkorsukov
authored andcommitted
Fix doubleinputvalidator for large values (10⁴ and more)
1 parent 2f840d8 commit dbaa95a

1 file changed

Lines changed: 29 additions & 6 deletions

File tree

src/framework/uicomponents/qml/Muse/UiComponents/validators/doubleinputvalidator.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,27 @@
2222
#include "doubleinputvalidator.h"
2323
#include "global/realfn.h"
2424

25+
#include <cmath>
26+
2527
using namespace muse::uicomponents;
2628

29+
namespace {
30+
int maxIntegerDigits(qreal top, qreal bottom)
31+
{
32+
const qreal maxAbs = std::max(std::abs(top), std::abs(bottom));
33+
if (maxAbs < 1.0) {
34+
return 1;
35+
}
36+
int digits = 1;
37+
qreal v = std::floor(maxAbs);
38+
while (v >= 10.0) {
39+
v /= 10.0;
40+
++digits;
41+
}
42+
return digits;
43+
}
44+
}
45+
2746
DoubleInputValidator::DoubleInputValidator(QObject* parent)
2847
: QValidator(parent)
2948
{
@@ -72,9 +91,10 @@ void DoubleInputValidator::fixup(QString& string) const
7291
QString intPart = strList.at(0);
7392
QString floatPart = strList.size() > 1 ? strList.at(1) : 0;
7493

75-
if (intPart.contains(QRegularExpression("^0{1,3}$"))) {
94+
const int maxIntDigits = maxIntegerDigits(m_top, m_bottom);
95+
if (intPart.contains(QRegularExpression(QString("^0{1,%1}$").arg(maxIntDigits)))) {
7696
intPart = QString("0");
77-
} else if (intPart.contains(QRegularExpression("^\\-0{0,3}$"))) {
97+
} else if (intPart.contains(QRegularExpression(QString("^\\-0{0,%1}$").arg(maxIntDigits)))) {
7898
intPart = QString("-0");
7999
}
80100

@@ -111,10 +131,12 @@ QValidator::State DoubleInputValidator::validate(QString& inputStr, int& cursorP
111131
QValidator::State state = Invalid;
112132

113133
QString decimalSep = QRegularExpression::escape(locale.decimalPoint());
114-
QRegularExpression validRegex(QString("^\\-?\\d{1,3}(" + decimalSep + "\\d{1,%1})?$").arg(m_decimal));
134+
const int maxIntDigits = maxIntegerDigits(m_top, m_bottom);
135+
QRegularExpression validRegex(QString("^\\-?\\d{1,%1}(" + decimalSep + "\\d{1,%2})?$")
136+
.arg(maxIntDigits).arg(m_decimal));
115137

116138
if (inputStr.contains(validRegex)) {
117-
QRegularExpression invalidZeroRegex("^\\-?0{2,3}" + decimalSep); // for '-000,' or '-000.'
139+
QRegularExpression invalidZeroRegex(QString("^\\-?0{2,%1}").arg(std::max(maxIntDigits, 2)) + decimalSep); // e.g. '-000,' or '-000.'
118140
QRegularExpression invalidTrailingZeroRegex("^\\-?\\d+" + decimalSep + "0{1,}$"); // for '1,00' or '1.00'
119141
QRegularExpression invalidTrailingDotRegex("^\\-?\\d+" + decimalSep + "$"); // for '1,' or '1.'
120142

@@ -128,8 +150,9 @@ QValidator::State DoubleInputValidator::validate(QString& inputStr, int& cursorP
128150
} else {
129151
state = Acceptable;
130152
}
131-
} else if (inputStr.contains(QRegularExpression("^\\-?\\d{0,3}" + decimalSep + "?$"))
132-
|| inputStr.contains(QRegularExpression(QString("^\\-?\\d{0,3}" + decimalSep + "\\d{0,%1}$").arg(m_decimal)))) {
153+
} else if (inputStr.contains(QRegularExpression(QString("^\\-?\\d{0,%1}" + decimalSep + "?$").arg(maxIntDigits)))
154+
|| inputStr.contains(QRegularExpression(QString("^\\-?\\d{0,%1}" + decimalSep
155+
+ "\\d{0,%2}$").arg(maxIntDigits).arg(m_decimal)))) {
133156
state = Intermediate;
134157
} else {
135158
cursorPos = 0;

0 commit comments

Comments
 (0)