Skip to content

Commit

Permalink
Shortened decimal check code in QuantitySpinBox
Browse files Browse the repository at this point in the history
  • Loading branch information
AjinkyaDahale authored and wwmayer committed Oct 16, 2016
1 parent 001ebc0 commit 7535d7a
Showing 1 changed file with 12 additions and 29 deletions.
41 changes: 12 additions & 29 deletions src/Gui/QuantitySpinBox.cpp
Expand Up @@ -140,36 +140,19 @@ class QuantitySpinBoxPrivate
goto end;
}
else if (len > 1) {
int dec = copy.indexOf(locale.decimalPoint());
if (dec != -1) {
// fix two directly consecutive decimal points immediately
if (dec + 1 < copy.size() && copy.at(dec + 1) == locale.decimalPoint() && pos == dec + 1) {
copy.remove(dec + 1, 1);
}
else {
// search for a second decimal point
int dec2 = copy.indexOf(locale.decimalPoint(), dec + 1);
while (dec2 != -1) {
// if the string between the two decimal points builds one number then
// the input is invalid
bool digits = true;
for (int i = dec + 1; i < dec2; i++) {
QChar c = copy.at(i);
if (!(c.isDigit() || c == locale.groupSeparator())) {
digits = false;
break;
}
}

if (digits) {
// trying to add a second decimal point inside the same number is not allowed
state = QValidator::Invalid;
goto end;
}

dec = dec2;
dec2 = copy.indexOf(locale.decimalPoint(), dec + 1);
bool decOccurred = false;
for (int i = 0; i<copy.size(); i++) {
if (copy.at(i) == locale.decimalPoint()) {
// Disallow multiple decimal points within the same numeric substring
if (decOccurred) {
state = QValidator::Invalid;
goto end;
}
decOccurred = true;
}
// Reset decOcurred if non-numeric character found
else if (!(copy.at(i) == locale.groupSeparator() || copy.at(i).isDigit())) {
decOccurred = false;
}
}
}
Expand Down

0 comments on commit 7535d7a

Please sign in to comment.