-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmatrixwidget.cpp
127 lines (115 loc) · 3.67 KB
/
kmatrixwidget.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
#include "kmatrixwidget.h"
#include "sciencedoubledelegate.h"
KMatrixWidget::KMatrixWidget(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->view = new QTableView;
this->view->setItemDelegate(new ScienceDoubleDelegate(this));
this->view->horizontalHeader()->setStretchLastSection(true);
layout->addLayout(bLay);
layout->addWidget(this->view);
QGroupBox *groupBox = new QGroupBox(tr("KMatrix"));
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()));
}
void KMatrixWidget::setModel(KMatrixModel *model)
{
this->model = model;
this->view->setModel(model);
}
QTableView *KMatrixWidget::getView()
{
return this->view;
}
void KMatrixWidget::saveFile()
{
QSettings settings;
QFileDialog dialog(this, tr("Save Matrix K"), settings.value("default_dir").toString());
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setFileMode(QFileDialog::AnyFile);
while (dialog.exec() == QDialog::Accepted)
if (saveKMatrix(dialog.selectedFiles())) break;
if (!dialog.selectedFiles().isEmpty())
settings.setValue("default_dir", dialog.selectedFiles().first());
}
void KMatrixWidget::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 (loadKMatrix(dialog.selectedFiles())) break;
if (!dialog.selectedFiles().isEmpty())
settings.setValue("default_dir", dialog.selectedFiles().first());
}
void KMatrixWidget::clear()
{
this->model->clear();
}
bool KMatrixWidget::saveKMatrix(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);
KValue value = this->model->core()->getValue();
// writing:
st.setRealNumberPrecision(16);
st<<"fx: \n";
st<<value.fx<<'\n';
st<<"fy: \n";
st<<value.fy<<'\n';
st<<"x0: \n";
st<<value.x0<<'\n';
st<<"y0: \n";
st<<value.y0<<'\n';
st<<"s: \n";
st<<value.s<<'\n';
if (file.commit()) return true;
else return false;
}
bool KMatrixWidget::loadKMatrix(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);
KValue value;
// reading
st.setRealNumberPrecision(16);
st.readLine();// "fx: \n"
st>>value.fx;
st.readLine();// n"
st.readLine();// "fy: \n"
st>>value.fy;
st.readLine();// n"
st.readLine();// "x0: \n"
st>>value.x0;
st.readLine();// n"
st.readLine();// "y0: \n"
st>>value.y0;
st.readLine();// n"
st.readLine();// "s: \n"
st>>value.s;
if (st.status() != QTextStream::Ok) return false;
// end reading
this->model->core()->setValue(value);
return true;
}