-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtextfielddoublevalidator.cpp
More file actions
42 lines (30 loc) · 1.14 KB
/
textfielddoublevalidator.cpp
File metadata and controls
42 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "textfielddoublevalidator.h"
using namespace Qt::StringLiterals;
TextFieldDoubleValidator::TextFieldDoubleValidator( QObject* parent )
: QDoubleValidator( parent ) { }
QValidator::State TextFieldDoubleValidator::validate( QString& strValue, int& /*pos*/ ) const {
if ( strValue.isEmpty() || ( strValue.startsWith( "-"_L1 ) && strValue.length() == 1 ) ) {
return QValidator::Intermediate;
}
const QChar point = locale().decimalPoint().back();
if ( strValue.indexOf( point ) != -1 ) {
const auto lengthDecimals = strValue.length() - strValue.indexOf( point ) - 1;
if ( lengthDecimals > decimals() ) {
return QValidator::Invalid;
}
}
bool ok = false;
const double value = strValue.toDouble( &ok );
if ( ok && bottom() <= value && value <= top() ) {
return QValidator::Acceptable;
}
return QValidator::Invalid;
}
void TextFieldDoubleValidator::fixup( QString& input ) const {
bool ok { false };
double tmp = input.toDouble( &ok );
if ( ok ) {
tmp = std::clamp( tmp, bottom(), top() );
}
input = QString::number( tmp, 'g', 3 );
}