forked from andreagen0r/ColorPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextfielddoublevalidator.cpp
More file actions
39 lines (29 loc) · 1.05 KB
/
textfielddoublevalidator.cpp
File metadata and controls
39 lines (29 loc) · 1.05 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
#include "textfielddoublevalidator.h"
TextFieldDoubleValidator::TextFieldDoubleValidator( QObject* parent )
: QDoubleValidator( parent ) { }
QValidator::State TextFieldDoubleValidator::validate( QString& s, int& /*pos*/ ) const {
if ( s.isEmpty() || ( s.startsWith( u"-"_qs ) && s.length() == 1 ) ) {
return QValidator::Intermediate;
}
QChar point = locale().decimalPoint().back();
if ( s.indexOf( point ) != -1 ) {
const auto lengthDecimals = s.length() - s.indexOf( point ) - 1;
if ( lengthDecimals > decimals() ) {
return QValidator::Invalid;
}
}
bool ok = false;
const double value = s.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 );
}