-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmydoublevalidator.cpp
77 lines (64 loc) · 1.54 KB
/
mydoublevalidator.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "mydoublevalidator.h"
#include <iostream>
MyDoubleValidator::MyDoubleValidator(QObject *parent)
: QDoubleValidator(parent)
{
}
MyDoubleValidator::MyDoubleValidator(double bottom, double top, int decimals, QObject *parent):QDoubleValidator(parent)
{
setRange(bottom,top,decimals);
}
MyDoubleValidator::~MyDoubleValidator()
{
}
QValidator::State MyDoubleValidator::validate(QString & input, int & pos) const
{
if(input == "+"){
return QValidator::Invalid;
}
if(bottom() < 0 && input == "-"){
return QValidator::Intermediate;
}
if (input.isEmpty())
{
return QValidator::Intermediate;
}
bool sign = false;
QString input2 = input;
if(input.indexOf("-") == 0){
sign = true;
input2 = input.mid(1, input.length() - 1);
}
bool OK = false;
double val = input2.toDouble(&OK);
if (!OK)
{
return QValidator::Invalid;
}
int dotPos = input2.indexOf(".");
if (dotPos > 0)
{
if (input2.right(input2.length() - dotPos - 1).length() > decimals())
{
return QValidator::Invalid;
}
}
if(sign){
val = 0 - val;
}
if(val <= top() && val >= bottom())
return QValidator::Acceptable;
if(val<bottom()){
if(!sign){
return QValidator::Intermediate;
}else{
return QValidator::Invalid;
}
}
if(val>top())
return QValidator::Invalid;
}
void MyDoubleValidator::fixup(QString &s) const
{
s = QString("%1").arg(bottom());
}