-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
174 lines (138 loc) · 4.47 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->addCar = false;
this->addCone = false;
this->removeCone = false;
this->carpoint = QPoint(0, 0);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
if (this->carpoint.x() > 0 && this->carpoint.y() > 0) {
QPen penCar(Qt::black);
penCar.setWidth(10);
painter.setPen(penCar);
painter.drawPoint(carpoint);
}
if (!this->cones.empty()) {
QPen conePen;
conePen.setWidth(10);
for (auto icone : this->cones) {
QPoint cone(icone.coords.x, icone.coords.y);
if (icone.color.compare("blue") == 0) {
conePen.setColor(Qt::blue);
}
else if (icone.color.compare("yellow") == 0) {
conePen.setColor(Qt::yellow);
}
else if (icone.color.compare("orange") == 0) {
conePen.setColor("orange");
}
else {
std::cout << "Cone without color!" << std::endl;
}
painter.setPen(conePen);
painter.drawPoint(cone);
}
}
}
void MainWindow::on_buttonLoad_clicked()
{
fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), "../PathSim/resources", tr("XML Files (*.xml *.sdf)"));
if (fileName.size() > 0) {
this->cones.clear();
this->cones = XMLMap(fileName.toLocal8Bit().data()).getCones();
double min_x = 0;
double min_y = 0;
double max_x = 0;
double max_y = 0;
for (auto cone : this->cones) {
if (min_x > cone.coords.x) {
min_x = cone.coords.x;
}
if (min_y > cone.coords.y) {
min_y = cone.coords.y;
}
if (max_x < cone.coords.x) {
max_x = cone.coords.x;
}
if (max_y < cone.coords.y) {
max_y = cone.coords.y;
}
}
for (unsigned long long i = 0; i < this->cones.size(); ++i) {
this->cones[i].coords.x = (((this->cones[i].coords.x - min_x)
* (this->size().width() - 100)) / (max_x - min_x)) + 50;
this->cones[i].coords.y = (((this->cones[i].coords.y - min_y)
* (this->size().height() - 100)) / (max_y - min_y)) + 50;
std::cout << this->cones[i].coords.x << ' ' << this->cones[i].coords.y << ' '
<< this->cones[i].color << std::endl;
}
}
this->update();
}
void MainWindow::on_buttonCar_clicked()
{
this->removeCone = false;
this->addCone = false;
this->addCar = true;
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(e->buttons() == Qt::LeftButton) {
if (addCar) {
this->carpoint.setX(e->x());
this->carpoint.setY(e->y());
this->addCar = false;
}
if (addCone) {
cone cone;
cone.coords.x = e->x();
cone.coords.y = e->y();
if (ui->colorBox->currentText() == "Yellow") {
cone.color = "yellow";
}
else if (ui->colorBox->currentText() == "Blue") {
cone.color = "blue";
}
else if (ui->colorBox->currentText() == "Orange") {
cone.color = "orange";
}
else {
std::cout << "No color chosen";
}
this->cones.push_back(cone);
}
if (removeCone) {
if (!this->cones.empty()) {
for (auto icone = this->cones.begin(); icone != this->cones.end(); ++icone) {
if ((sqrt(pow(e->x() - icone->coords.x, 2) + (pow(e->y() - icone->coords.y, 2)))) < 5) {
this->cones.erase(icone);
break;
}
}
}
}
}
this->update();
}
void MainWindow::on_buttonCone_clicked()
{
this->removeCone = false;
this->addCone = true;
}
void MainWindow::on_buttonRemove_clicked()
{
this->addCone = false;
this->removeCone = true;
}