-
Notifications
You must be signed in to change notification settings - Fork 1
/
point2dwidget.cpp
230 lines (209 loc) · 7.68 KB
/
point2dwidget.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "point2dwidget.h"
#include "point2dview.h"
#include "point2dmodel.h"
#include "sciencedoubledelegate.h"
#include <QtWidgets>
Point2DWidget::Point2DWidget(QWidget *parent) : QWidget(parent)
{
view = new Point2DView(this);
ScienceDoubleDelegate *delegate = new ScienceDoubleDelegate(this);
delegate->setPrecision(6);
this->view->setItemDelegate(delegate);
QPushButton *addPointButton = new QPushButton(tr("+"));
QPushButton *removePointButton = new QPushButton(tr("-"));
QPushButton *moveUpButton = new QPushButton(tr("^"));
QPushButton *moveDownButton = new QPushButton(tr("v"));
QPushButton *loadButton = new QPushButton(tr("Load"));
QPushButton *saveButton = new QPushButton(tr("Save"));
QPushButton *clearButton = new QPushButton(tr("Clear"));
addPointButton->setMaximumWidth(20);
removePointButton->setMaximumWidth(20);
moveUpButton->setMaximumWidth(20);
moveDownButton->setMaximumWidth(20);
loadButton->setMaximumWidth(40);
saveButton->setMaximumWidth(40);
clearButton->setMaximumWidth(40);
addPointButton->setMaximumHeight(20);
removePointButton->setMaximumHeight(20);
moveUpButton->setMaximumHeight(20);
moveDownButton->setMaximumHeight(20);
loadButton->setMaximumHeight(20);
saveButton->setMaximumHeight(20);
clearButton->setMaximumHeight(20);
QHBoxLayout *bLay1 = new QHBoxLayout;
QHBoxLayout *bLay2 = new QHBoxLayout;
bLay1->addWidget(addPointButton);
bLay1->addWidget(removePointButton);
bLay1->addWidget(moveUpButton);
bLay1->addWidget(moveDownButton);
bLay2->addWidget(loadButton);
bLay2->addWidget(saveButton);
bLay2->addWidget(clearButton);
QVBoxLayout *bLay = new QVBoxLayout;
bLay->addLayout(bLay1);
bLay->addLayout(bLay2);
connect(addPointButton, SIGNAL(clicked(bool)), this, SLOT(addPoint()));
connect(removePointButton, SIGNAL(clicked(bool)), this, SLOT(removePoint()));
connect(loadButton, SIGNAL(clicked(bool)), this, SLOT(loadFile()));
connect(saveButton, SIGNAL(clicked(bool)), this, SLOT(saveFile()));
connect(clearButton, SIGNAL(clicked(bool)), this, SLOT(clear()));
connect(moveUpButton, SIGNAL(clicked(bool)), this, SLOT(movePointUp()));
connect(moveDownButton, SIGNAL(clicked(bool)), this, SLOT(movePointDown()));
QVBoxLayout *hLay = new QVBoxLayout;
hLay->addLayout(bLay);
hLay->addWidget(view);
QGroupBox *groupBox = new QGroupBox(tr("Point 2D"));
groupBox->setLayout(hLay);
QHBoxLayout *lay = new QHBoxLayout;
lay->addWidget(groupBox);
setLayout(lay);
}
void Point2DWidget::setModel(Point2DModel *model)
{
this->model = model;
view->setPoint2DModel(model);
}
Point2DView *Point2DWidget::getPoint2DView()
{
return this->view;
}
void Point2DWidget::addPoint()
{
this->model->appendPoint();
}
void Point2DWidget::removePoint()
{
QModelIndex sId = this->getFirstSelectedItem();
this->model->removePoint(sId);
}
void Point2DWidget::movePointUp()
{
QModelIndex index = this->getFirstSelectedItem();
if (this->model->indexMeansPoint(index)) {
this->model->movePointUp(index);
if (index.row() > 0) {
QModelIndex id = this->model->index(index.row()-1, index.column(), index.parent());
this->view->selectionModel()->select(id,
QItemSelectionModel::ClearAndSelect
|QItemSelectionModel::Rows);
} else {
QModelIndex id = this->model->index(index.row(), index.column(), index.parent());
this->view->selectionModel()->select(id,
QItemSelectionModel::ClearAndSelect
|QItemSelectionModel::Rows);
}
}
}
void Point2DWidget::movePointDown()
{
QModelIndex index = this->getFirstSelectedItem();
if (this->model->indexMeansPoint(index)) {
this->model->movePointDown(index);
if (index.row() < this->model->pointCount()-1) {
QModelIndex id = this->model->index(index.row()+1, index.column(), index.parent());
this->view->selectionModel()->select(id,
QItemSelectionModel::ClearAndSelect
|QItemSelectionModel::Rows);
} else {
QModelIndex id = this->model->index(index.row(), index.column(), index.parent());
this->view->selectionModel()->select(id,
QItemSelectionModel::ClearAndSelect
|QItemSelectionModel::Rows);
}
}
}
void Point2DWidget::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 (savePoint2D(dialog.selectedFiles())) break;
if (!dialog.selectedFiles().isEmpty())
settings.setValue("default_dir", dialog.selectedFiles().first());
}
void Point2DWidget::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 (loadPoint2D(dialog.selectedFiles())) break;
if (!dialog.selectedFiles().isEmpty())
settings.setValue("default_dir", dialog.selectedFiles().first());
}
void Point2DWidget::clear()
{
this->model->clear();
}
bool Point2DWidget::savePoint2D(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);
QList<QList<QPointF> > plist;
this->model->core()->getAllPoints(plist);
int size = plist.size();
if (size == 0) return false;
int pointcount = plist[0].size();
// writing:
st.setRealNumberPrecision(16);
st<<"ImageCount:\n"<<size<<'\n';
st<<"PointCount:\n"<<pointcount<<'\n';
for (int i = 0; i < size; i++) {
st<<tr("Image_%1").arg(i+1)<<'\n';
for (int j = 0; j < pointcount; ++j) {
st<<plist[i][j].x()<<'\t';
st<<plist[i][j].y()<<'\n';
}
}
// end writing
if (file.commit()) return true;
else return false;
}
bool Point2DWidget::loadPoint2D(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);
QList<QList<QPointF> > plist;
int size, pointcount;
// reading
st.setRealNumberPrecision(16);
st.readLine();// ImageCount
st>>size;
st.skipWhiteSpace();
st.readLine();// PointCount
st>>pointcount;
st.skipWhiteSpace();
if (st.status() != QTextStream::Ok) return false;
for (int i = 0; i < size; i++) {
plist.append(QList<QPointF>());
st.readLine();// Image_i
for (int j = 0; j < pointcount; ++j) {
double x, y;
st>>x>>y;
st.skipWhiteSpace();
plist[i].append(QPointF(x, y));
}
}
if (st.status() != QTextStream::Ok) return false;
// end reading
this->model->core()->setAllPoints(plist);
return true;
}
QModelIndex Point2DWidget::getFirstSelectedItem()
{
QItemSelection s = view->selectionModel()->selection();
if (!s.isEmpty())
return s.first().indexes().first();
return QModelIndex();
}