-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMainWindow.cpp
More file actions
240 lines (181 loc) · 7.42 KB
/
Copy pathCMainWindow.cpp
File metadata and controls
240 lines (181 loc) · 7.42 KB
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
#include <QtDebug>
#include <QKeyEvent>
#include <iostream>
#include <json.h>
#include "CMainWindow.h"
#include "CResultTestDialog.h"
#include "CJSonDialog.h"
#include "CVerdict.h"
CMainWindow::CMainWindow(QWidget *parent) : QMainWindow(parent) {
setupUi(this);
testVoiture = 0;
testVoiturePilote = 0;
testPiloteTimer = 0;
spVainqueur->setMaximum(TAILLE_POPULATION);
spVainqueur->setValue(TAILLE_POPULATION / 4);
cbCircuit->addItem("Circuit normal", QVariant(":/circuits/circuitFinal.png"));
cbCircuit->addItem("Circuit fun", QVariant(":/circuits/circuitFinalFun.png"));
}
CMainWindow::~CMainWindow(void) {
}
bool CMainWindow::eventFilter(QObject *obj, QEvent *event) {
if(testVoiturePilote != 0) {
if(event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch(keyEvent->key()) {
case Qt::Key_Plus:
testVoiturePilote->incVitesse(1.0);
break;
case Qt::Key_D:
testVoiturePilote->incAngle(-PI/8);
break;
case Qt::Key_Minus:
testVoiturePilote->incVitesse(-1.0);
break;
case Qt::Key_G:
testVoiturePilote->incAngle(PI/8);
break;
default:
break;
}
}
}
return QObject::eventFilter(obj, event);
}
void CMainWindow::onGeneticCalculOk(void) {
int i;
QString result = "[\r\n";
for(i=0;i<genetic->getVainqueurs().size();i++) {
if(i != 0) {
result += ",\r\n";
}
result += genetic->getVainqueurs().at(i)->serialize();
}
result += "\r\n]\r\n";
CResultTestDialog resultDialog(this, result);
resultDialog.exec();
}
void CMainWindow::onGeneticCircuitChange(CCircuit *circuit) {
wCircuit->setCircuit(circuit);
}
void CMainWindow::onGeneticRepaintRequested(const QPointF& posMeilleur) {
int elapsed = mainTime.elapsed() / 1000;
QString time = "%1:%2:%3";
time = time.arg((elapsed / 3600) % 60, 2, 10, QChar('0')).arg((elapsed / 60) % 60, 2, 10, QChar('0')).arg(elapsed % 60, 2, 10, QChar('0'));
wCircuit->setElapsedTime(time);
wCircuit->setPositionRef(posMeilleur.toPoint());
if(setup.getCreateImages()) {
wCircuit->createImage("images/img_"+QString("%1").arg(imgIdx, 6, 10, QChar('0'))+".jpg");
}
imgIdx++;
}
void CMainWindow::onGeneticTerminated(void) {
disconnect(cbFastMode, SIGNAL(toggled(bool)), genetic, SLOT(setFastMode(bool)));
wCircuit->setCircuit(nullptr);
delete genetic;
genetic = nullptr;
pbTest->setEnabled(true);
}
void CMainWindow::on_pbTest_clicked(bool) {
pbTest->setEnabled(false);
imgIdx = 1;
setup.setNbCircuit(sbCircuit->value());
setup.setCreateImages(cbCreateImage->isChecked());
setup.setNbVainqueur(spVainqueur->value());
setup.setFastMode(cbFastMode->isChecked());
genetic = new CGenetic(wCircuit, setup);
connect(genetic, SIGNAL(calculOk()), this, SLOT(onGeneticCalculOk()));
connect(genetic, SIGNAL(circuitChange(CCircuit*)), this, SLOT(onGeneticCircuitChange(CCircuit*)));
connect(genetic, SIGNAL(repaintRequested(const QPointF&)), this, SLOT(onGeneticRepaintRequested(const QPointF&)));
connect(genetic, SIGNAL(finished()), this, SLOT(onGeneticTerminated()));
connect(cbFastMode, SIGNAL(toggled(bool)), genetic, SLOT(setFastMode(bool)), Qt::DirectConnection);
mainTime.start();
genetic->start();
}
void CMainWindow::on_pbTestVoiture_clicked(bool) {
testVoiture = new CTestVoitureAngle(QPoint(400, 300), leAngle->text().toDouble());
connect(wCircuit, SIGNAL(drawVoitures(QPainter *, double , double)), this, SLOT(onTVdrawVoitures(QPainter *, double , double)));
for(int i=0;i<50;i++) {
bool b;
testVoiture->move(0, b);
wCircuit->repaint();
}
disconnect(wCircuit, SIGNAL(drawVoitures(QPainter *, double , double)), this, SLOT(onTVdrawVoitures(QPainter *, double , double)));
delete testVoiture;
testVoiture = 0;
}
void CMainWindow::CMainWindow::onTVdrawVoitures(QPainter *painter, double dx, double dy) {
if(testVoiture != 0) {
testVoiture->draw(painter, dx, dy);
}
}
void CMainWindow::on_pbCalculMarkers_clicked(bool) {
QStringList l = leDepart->text().split(",");
QPoint depart(QPoint(l.at(0).toInt(), l.at(1).toInt()));
CCircuit circuit(depart, 0, ":circuits/circuit"+QString::number(sbNumCircuit->value())+".png");
wCircuit->setCircuit(&circuit);
wCircuit->calculMarkers(depart, leDistance->text().toDouble(), leAngleDepart->text().toDouble(), sbNumCircuit->value(), cbShowDroite->isChecked());
}
void CMainWindow::on_pbTestPilote_clicked(bool) {
if(testVoiturePilote == 0) {
QStringList l = leDepartPilote->text().split(",");
QPoint depart(QPoint(l.at(0).toInt(), l.at(1).toInt()));
circuitPilote = new CCircuit(depart, 0, ":circuits/circuit"+QString::number(sbNumCircuitPilote->value())+".png");
wCircuit->setCircuit(circuitPilote);
testVoiturePilote = new CTestVoiturePilote(circuitPilote, depart, leAngleDepartPilote->text().toDouble(), cbCapteurs->isChecked());
connect(wCircuit, SIGNAL(drawVoitures(QPainter *, double , double)), this, SLOT(onTVPdrawVoitures(QPainter *, double , double)));
installEventFilter(this);
testPiloteTimer = new QTimer(this);
connect(testPiloteTimer, SIGNAL(timeout()), this, SLOT(onTestTimerPiloteTimeout()));
testPiloteTimer->setInterval(1000 / 24);
testPiloteTimer->start();
} else {
disconnect(wCircuit, SIGNAL(drawVoitures(QPainter *, double , double)), this, SLOT(onTVPdrawVoitures(QPainter *, double , double)));
removeEventFilter(this);
delete testVoiturePilote;
delete testPiloteTimer;
delete circuitPilote;
testVoiturePilote = 0;
}
}
void CMainWindow::onTVPdrawVoitures(QPainter *painter, double dx, double dy) {
if(testVoiturePilote != 0) {
testVoiturePilote->draw(painter, dx, dy);
}
}
void CMainWindow::onTestTimerPiloteTimeout(void) {
if(testVoiturePilote != 0) {
bool b;
testVoiturePilote->move(0, b);
wCircuit->update();
}
}
void CMainWindow::on_pbVerdict_clicked(bool) {
CJSonDialog dialog(this);
if(dialog.exec() == QDialog::Accepted) {
QString jSon = dialog.getJSon();
if(!jSon.isEmpty()) {
CVerdict *verdict = new CVerdict(wCircuit, cbCircuit->itemData(cbCircuit->currentIndex()).toString(), jSon);
connect(verdict, SIGNAL(repaintRequested(const QPointF&)), this, SLOT(onVerdictRepaintRequested(const QPointF&)));
connect(verdict, SIGNAL(finished()), this, SLOT(onVerdictFinished()));
connect(verdict, SIGNAL(finished()), verdict, SLOT(deleteLater()));
imgIdx = 1;
mainTime.start();
verdict->start();
}
}
}
void CMainWindow::onVerdictFinished(void) {
wCircuit->setCircuit(nullptr);
}
void CMainWindow::onVerdictRepaintRequested(const QPointF& posMeilleur) {
int elapsed = mainTime.elapsed() / 1000;
QString time = "%1:%2:%3";
time = time.arg((elapsed / 3600) % 60, 2, 10, QChar('0')).arg((elapsed / 60) % 60, 2, 10, QChar('0')).arg(elapsed % 60, 2, 10, QChar('0'));
wCircuit->setElapsedTime(time);
wCircuit->setPositionRef(posMeilleur.toPoint());
if(cbCreateImageVerdict->isChecked()) {
wCircuit->createImage("images/img_"+QString("%1").arg(imgIdx, 6, 10, QChar('0'))+".jpg");
}
imgIdx++;
}