-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
241 lines (189 loc) · 6.29 KB
/
mainwindow.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
231
232
233
234
235
236
237
238
239
240
241
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dependencies/json.hpp"
#include "OpenGL/widgets/A2_2Dwidget.h"
#include "OpenGL/layers/A2_2dlayer.h"
#include "solution/a2solution.h"
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
extern bool global_reguralizer;
using json = nlohmann::json;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mode_group = new QActionGroup(this);
mode_group->addAction(ui->actionSelect_and_Edit_Links);
mode_group->addAction(ui->actionSelect_and_Edit_Joints);
mode_group->addAction(ui->actionSelect_and_Edit_Obstacles);
mode_group->addAction(ui->actionShowtime);
mode_group->setExclusive(true);
}
MainWindow::~MainWindow()
{
delete ui;
delete mode_group;
}
void MainWindow::on_actionLoad_triggered()
{
// Logic for file opening, putting into JSON format
QString filename = QFileDialog::getOpenFileName(this, tr("Load scene."), "", tr("JSON File (*.json)"));
if (filename.isEmpty())
return;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QTextStream in(&file);
QString json_data = in.readAll();
json j = json::parse(json_data.toStdString());
A2_2DWidget* w = ui->OpenGL;
A2_2DLayer* l = dynamic_cast<A2_2DLayer*>(w->m_layers[0]);
// Deletes existing structures
for(unsigned int i =0; i<l->m_joints.size(); i++){
// Note, deleting a joint also deletes any associated links
delete l->m_joints[i];
l->m_joints[i] = nullptr;
}
l->m_joints.clear();
l->m_links.clear();
for(unsigned int i =0; i<l->m_obstacles.size(); i++){
// Note, deleting a joint also deletes any associated links
delete l->m_obstacles[i];
l->m_obstacles[i] = nullptr;
}
l->m_obstacles.clear();
// Logic for deserialization
// Map for efficiently creating links
std::map<std::string, Joint2D*> id_map;
for (auto itr = j["joints"].begin(); itr!= j["joints"].end(); itr++){
Joint2D* joint = new Joint2D(l->m_parent);
joint->m_id = (*itr)["id"].get<std::string>();
id_map[joint->m_id] = joint;
joint->set_locked((*itr)["locked"]);
QVector2D pos((*itr)["pos_x"], (*itr)["pos_y"]);
joint->set_position(pos);
l->m_joints.push_back(joint);
}
for (auto itr = j["links"].begin(); itr!= j["links"].end(); itr++){
Link2D* link = new Link2D(l->m_parent);
link->m_id = (*itr)["id"].get<std::string>();
link->set_first_joint(id_map[(*itr)["first_joint_id"]]);
link->set_second_joint(id_map[(*itr)["second_joint_id"]]);
link->compute(QVector2D(0,0));
l->m_links.push_back(link);
}
for (auto itr = j["obstacles"].begin(); itr!= j["obstacles"].end(); itr++){
Obstacle2D* obstacle = new Obstacle2D(l->m_parent);
obstacle->m_id = (*itr)["id"].get<std::string>();
QVector2D pos((*itr)["pos_x"], (*itr)["pos_y"]);
obstacle->compute(pos, 20, 100);
obstacle->m_color = QVector3D(1,0,0);
l->m_obstacles.push_back(obstacle);
}
if(w->m_layers.size()!=0){
}
w->update();
}
void MainWindow::on_actionSave_triggered()
{
// Logic for file opening
QString filename = QFileDialog::getSaveFileName(this, tr("Save scene."), "", tr("JSON File (*.json)"));
if (filename.isEmpty())
return;
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
// Logic for serialization
json j;
A2_2DWidget* w = ui->OpenGL;
A2_2DLayer* l = dynamic_cast<A2_2DLayer*>(w->m_layers[0]);
j["joints"] = json::array();
j["links"] = json::array();
j["obstacles"] = json::array();
for(unsigned int i =0; i<l->m_joints.size(); i++){
Joint2D* joint = l->m_joints[i];
json saved_joint = json::object();
QVector2D pos = joint->get_position();
saved_joint["pos_x"] = pos.x();
saved_joint["pos_y"] = pos.y();
saved_joint["id"] = joint->m_id;
saved_joint["locked"] = joint->is_locked();
j["joints"].push_back(saved_joint);
}
for(unsigned int i =0; i<l->m_links.size(); i++){
Link2D* link = l->m_links[i];
json saved_link = json::object();
saved_link["id"] = link->m_id;
saved_link["first_joint_id"] = link->get_first_joint()->m_id;
saved_link["second_joint_id"] = link->get_second_joint()->m_id;
j["links"].push_back(saved_link);
}
for(unsigned int i =0; i<l->m_obstacles.size(); i++){
Obstacle2D* obstacle = l->m_obstacles[i];
json saved_obstacle = json::object();
QVector2D pos = obstacle->m_center;
saved_obstacle["pos_x"] = pos.x();
saved_obstacle["pos_y"] = pos.y();
saved_obstacle["id"] = obstacle->m_id;
j["obstacles"].push_back(saved_obstacle);
}
// Completes the saving process
QString out_string = QString::fromUtf8(j.dump(4).c_str());
QTextStream out(&file);
out << out_string;
}
void MainWindow::on_actionReset_View_triggered()
{
A2_2DWidget* w = ui->OpenGL;
if(w->m_layers.size()!=0){
w->m_layers[0]->reset_view();
}
w->update();
}
void MainWindow::on_actionSelect_and_Edit_Joints_triggered()
{
A2_2DWidget* w = ui->OpenGL;
if(w->m_layers.size()!=0){
w->m_layers[0]->setUIMode(1);
}
w->update();
}
void MainWindow::on_actionSelect_and_Edit_Links_triggered()
{
A2_2DWidget* w = ui->OpenGL;
if(w->m_layers.size()!=0){
w->m_layers[0]->setUIMode(2);
}
w->update();
}
void MainWindow::on_actionSelect_and_Edit_Obstacles_triggered()
{
A2_2DWidget* w = ui->OpenGL;
if(w->m_layers.size()!=0){
w->m_layers[0]->setUIMode(3);
}
w->update();
}
void MainWindow::on_actionShowtime_triggered()
{
A2_2DWidget* w = ui->OpenGL;
if(w->m_layers.size()!=0){
w->m_layers[0]->setUIMode(4);
}
w->update();
}
void MainWindow::on_actionTest_Eigen_triggered()
{
A2Solution::test_eigen_library();
}