-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathCheckAbove.cpp
95 lines (84 loc) · 2.43 KB
/
CheckAbove.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// CheckAbove.cpp : implementation file
//
#include "stdafx.h"
#include "CheckAbove.h"
#include "ui/ui_CheckAbove.h"
namespace DSS
{
CheckAbove::CheckAbove(QWidget* parent /*=nullptr*/) :
QDialog(parent),
ui(new Ui::CheckAbove)
{
ui->setupUi(this);
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
ui->lineEdit->setValidator(new CheckAboveValidator(this));
}
CheckAbove::~CheckAbove()
{
delete ui;
}
void CheckAbove::accept()
{
ZASSERT(ui->lineEdit->validator() != nullptr);
QString strValue = ui->lineEdit->text();
QLocale const& locale = ui->lineEdit->validator()->locale();
if (strValue.endsWith(locale.percent()))
{
m_bPercent = true;
strValue = strValue.left(strValue.length() - locale.percent().length());
}
m_fThreshold = locale.toDouble(strValue);
Inherited::accept();
}
CheckAboveValidator::CheckAboveValidator(QObject* parent /*= nullptr*/) :
QValidator(parent),
m_doubleValueValidator{ new QDoubleValidator{this} },
m_percentValueValidator{ new QDoubleValidator{0., 99.99, 2, this} }
{
m_doubleValueValidator->setNotation(QDoubleValidator::StandardNotation);
//TODO: define top/bottom if known
// Note: the only downside of using a QDoubleValidator and not creating a custom one is
// it will accept "weird" expressions like: '.%' or '+1.%'
// But then they're valid expressions and they would be hard to avoid anyway.
m_percentValueValidator->setNotation(QDoubleValidator::StandardNotation);
}
QValidator::State CheckAboveValidator::validate(QString& input, int&) const
{
QValidator::State state;
auto tmpPos = 0;
const auto percent = locale().percent();
if (input.endsWith(percent))
{
auto tmpInput = input.left(input.length() - percent.length());
if (!tmpInput.isEmpty())
{
state = m_percentValueValidator->validate(tmpInput, tmpPos);
input = tmpInput + percent;
}
else
{
state = QValidator::State::Invalid;
}
}
else
{
state = m_doubleValueValidator->validate(input, tmpPos);
}
return state;
}
void CheckAboveValidator::fixup(QString& input) const
{
const auto percent = locale().percent();
if (input.endsWith(percent))
{
auto tmpInput = input.left(input.length() - percent.length());
m_percentValueValidator->fixup(input);
input = tmpInput + percent;
}
else
{
m_doubleValueValidator->fixup(input);
}
}
}