-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistortionwidget.cpp
133 lines (120 loc) · 4.14 KB
/
distortionwidget.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "distortionwidget.h"
#include "distortionmodel.h"
#include "sciencedoubledelegate.h"
#include <QtWidgets>
DistortionWidget::DistortionWidget(QWidget *parent) : QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
QPushButton *loadButton = new QPushButton(tr("Load"));
QPushButton *saveButton = new QPushButton(tr("Save"));
QPushButton *clearButton = new QPushButton(tr("Clear"));
QHBoxLayout *bLay = new QHBoxLayout;
bLay->addWidget(loadButton);
bLay->addWidget(saveButton);
bLay->addWidget(clearButton);
this->tableView = new QTableView;
this->tableView->horizontalHeader()->setStretchLastSection(true);
layout->addLayout(bLay);
layout->addWidget(this->tableView);
QGroupBox *groupBox = new QGroupBox(tr("Distortion Polynomial"));
groupBox->setLayout(layout);
QHBoxLayout *boxLayout = new QHBoxLayout;
boxLayout->addWidget(groupBox);
setLayout(boxLayout);
connect(loadButton, SIGNAL(clicked(bool)), this, SLOT(loadFile()));
connect(saveButton, SIGNAL(clicked(bool)), this, SLOT(saveFile()));
connect(clearButton, SIGNAL(clicked(bool)), this, SLOT(clear()));
this->tableView->setItemDelegate(new ScienceDoubleDelegate(this));
}
void DistortionWidget::setModel(DistortionModel *model)
{
this->tableView->setModel(model);
this->model = model;
}
QTableView *DistortionWidget::getView()
{
return this->tableView;
}
void DistortionWidget::saveFile()
{
QSettings settings;
QFileDialog dialog(this, tr("Save Distortion"), settings.value("default_dir").toString());
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setFileMode(QFileDialog::AnyFile);
while (dialog.exec() == QDialog::Accepted)
if (saveDistortion(dialog.selectedFiles())) break;
if(!dialog.selectedFiles().isEmpty()){
settings.setValue("default_dir",dialog.selectedFiles().first());
}
}
void DistortionWidget::loadFile()
{
QSettings settings;
QFileDialog dialog(this, tr("Load Distortion"), settings.value("default_dir").toString());
dialog.setAcceptMode(QFileDialog::AcceptOpen);
dialog.setFileMode(QFileDialog::ExistingFile);
while (dialog.exec() == QDialog::Accepted)
if (loadDistortion(dialog.selectedFiles())) break;
if(!dialog.selectedFiles().isEmpty()){
settings.setValue("default_dir",dialog.selectedFiles().first());
}
}
void DistortionWidget::clear()
{
this->model->clear();
}
bool DistortionWidget::saveDistortion(const QStringList &list)
{
if (list.size() != 1) return false;
QString name = list.first();
QSaveFile file(name);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
QTextStream st(&file);
DistortionValue value = this->model->core()->getValue();
int size = value._size;
// writing:
st.setRealNumberPrecision(16);
st<<"maxOrder: \n";
st<<value._maxOrder<<'\n';
st<<"Polynomial for X: \n";
for (int i = 0; i < size; ++i)
st<<value._XYData[i].first<<'\n';
st<<"Polynomial for Y: \n";
for (int i = 0; i < size; ++i)
st<<value._XYData[i].second<<'\n';
// end writing
if (file.commit()) return true;
else return false;
}
bool DistortionWidget::loadDistortion(const QStringList &list)
{
if (list.size() != 1) return false;
QString name = list.first();
QFile file(name);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
QTextStream st(&file);
DistortionValue value;
int maxOrder, size;
// reading
st.setRealNumberPrecision(16);
st.readLine();// "maxOrder: \n"
st>>maxOrder;
if (st.status() != QTextStream::Ok) return false;
if (maxOrder < 0) return false;
value.setMaxOrder(maxOrder);
size = value._size;
st.readLine();
st.readLine(); // "\nPolynomial for X: \n"
for (int i = 0; i < size; ++i)
st>>value._XYData[i].first;
st.readLine();
st.readLine(); // "\nPolynomial for Y: \n"
for (int i = 0; i < size; ++i)
st>>value._XYData[i].second;
if (st.status() != QTextStream::Ok) return false;
// end reading
this->model->core()->setValue(value);
return true;
}