-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialogpiecewiselinear.cpp
104 lines (93 loc) · 2.54 KB
/
dialogpiecewiselinear.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
96
97
98
99
100
101
102
103
104
#include "dialogpiecewiselinear.h"
#include "ui_dialogpiecewiselinear.h"
#include <QtMath>
#include <vector>
#include <list>
#include <QDebug>
DialogPiecewiseLinear::DialogPiecewiseLinear(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogPiecewiseLinear)
{
ui->setupUi(this);
}
DialogPiecewiseLinear::~DialogPiecewiseLinear()
{
delete ui;
}
const std::vector<double>& DialogPiecewiseLinear::getNums()
{
return nums;
}
QString DialogPiecewiseLinear::getString()
{
return ui->textEdit->toPlainText();
}
void DialogPiecewiseLinear::setText(QString text)
{
ui->textEdit->setPlainText(text);
}
bool DialogPiecewiseLinear::isNormalize()
{
return ui->checkBox->isChecked();
}
bool DialogPiecewiseLinear::calcRange()
{
std::list< std::pair<int, int> > range;
for (unsigned int i = 2; i < nums.size(); i += 4)
{
int left = qFloor(nums[i]);
int right = qFloor(nums[i+1]);
if (left > right || left < 0 || right < 0 || left > 255 || right > 255)
return false;
for (std::list< std::pair<int, int> >::iterator it = range.begin(); it != range.end(); ++it)
{
int curLeft = it->first;
int curRight = it->second;
if ( (left <= curLeft && right >= curLeft) ||
(left <= curRight && right >= curRight) ||
(left >= curLeft && right <= curRight) ||
(left <= curLeft && right >= curRight) )
return false;
}
range.push_back( std::make_pair(left, right) );
}
return true;
}
bool DialogPiecewiseLinear::isValidFormat()
{
nums.clear();
QString plainText = ui->textEdit->toPlainText();
QStringList lines = plainText.split("\n");
for (QStringList::Iterator it = lines.begin(); it != lines.end(); ++it)
{
QString curLine = *it;
QStringList elems = curLine.split(";");
if (elems.size() != 4)
return false;
for (QStringList::Iterator it = elems.begin(); it != elems.end(); ++it)
{
QString curElem = *it;
bool ok;
double num = curElem.toDouble(&ok);
if ( ! ok )
return false;
nums.push_back(num);
}
}
if ( ! calcRange() )
return false;
return true;
}
void DialogPiecewiseLinear::on_textEdit_textChanged()
{
if (isValidFormat())
{
ui->buttonBox->setEnabled(true);
ui->label->setVisible(false);
}
else
{
ui->buttonBox->setEnabled(false);
ui->label->setVisible(true);
}
}