Added button for sending route to server.
[speedfreak] / Client / resultdialog.cpp
1 /*
2  * CarMainWindow main class
3  *
4  * @author     Janne Änäkkälä <janne.anakkala@fudeco.com>
5  * @copyright  (c) 2010 Speed Freak team
6  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "resultdialog.h"
10 #include "ui_resultdialog.h"
11 #include <QPainter>
12
13 const int DIAGRAM_WIDTH = 400;
14 const int DIAGRAM_HEIGHT = 300;
15
16 const int DIAGRAMGAP100KMH = 30;
17 const int DIAGRAMGAP40KMH = 75;
18 const int DIAGRAMGAP60KMH = 50;
19 const int DIAGRAMGAP50KMH = 60;
20 const double DIAGRAMGAP80KMH = 37.5;
21
22 const QPoint diagramStemStart(70, 330);
23 const QPoint diagramStemEnd(70, 30);
24
25 const QPoint diagramHorizontalStart(70, 330);
26 const QPoint diagramHorizontalEnd(470, 330);
27
28 const int diagramGap = 30;
29
30 // Test arrays for changing speeds and times to the points in diagram
31 static const int speedArray[11] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
32 //static const int timeArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 12};
33
34 // Test point array for the diagram.
35 QPoint points[11];
36
37 /**
38   * Constructor of this class.
39   * @param QWidget pointer to parent object. By default the value is NULL.
40   */
41 ResultDialog::ResultDialog(QWidget *parent) :
42     QDialog(parent),
43     ui(new Ui::ResultDialog)
44 {
45     ui->setupUi(this);
46     timeAxelLength = 10;
47     speedList << "0" << "10" << "20" << "30" << "40" << "50" << "60" << "70" << "80" << "90" << "100" ;
48     timeList << "0" << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10" << "11"
49             << "12" << "13" << "14" << "15" << "16" << "17" << "18" << "19" << "20";
50 }
51
52 /**
53   * Destructor of this class.  Should be used to release all allocated resources.
54   */
55 ResultDialog::~ResultDialog()
56 {
57     delete ui;
58 }
59
60 void ResultDialog::changeEvent(QEvent *e)
61 {
62     QDialog::changeEvent(e);
63     switch (e->type()) {
64     case QEvent::LanguageChange:
65         ui->retranslateUi(this);
66         break;
67     default:
68         break;
69     }
70 }
71
72 /**
73   * Draws speed diagram to the UI
74   * @param QPaintEvent
75  */
76 void ResultDialog::paintEvent(QPaintEvent *)
77 {
78     QPainter painter(this);
79
80     painter.setRenderHint(QPainter::Antialiasing, true);
81     painter.setPen(QPen((Qt::red),2));
82     QFont font;
83     font.setPixelSize(12);
84     painter.setFont(font);
85     painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
86     painter.drawLine(diagramStemStart, diagramStemEnd);
87     painter.drawLine(diagramHorizontalStart, diagramHorizontalEnd);
88
89     int currentX = 0;
90     int currentY = diagramStemStart.y();
91
92     painter.setPen(QPen((Qt::blue),1));
93     // Draws diagram's X-axel
94     int i = 0;
95     while (currentX <= DIAGRAM_WIDTH)
96     {
97         painter.drawLine(currentX + diagramStemStart.x(), currentY, currentX + diagramStemStart.x(), currentY - 300);
98         painter.drawText(currentX + diagramStemStart.x(), currentY + 20, timeList[i]);
99         currentX += this->diagramGapHorizontal;
100         i++;
101     }
102
103     currentX = diagramStemStart.x();
104     currentY = 0;
105
106     i = 0;
107     // Draws diagram's Y-axel
108     while (currentY >= -(DIAGRAM_HEIGHT))
109     {
110         painter.drawLine(currentX, diagramStemStart.y() + currentY, currentX+400, diagramStemStart.y() + currentY);
111         painter.drawText(currentX - 25, diagramStemStart.y() + currentY, speedList[i]);
112         currentY -= this->diagramGapStem;
113         i++;
114     }
115
116     painter.setPen(QPen((Qt::white),2));
117
118     // Draws result line to the diagram
119     if (this->diagramGapStem == DIAGRAMGAP100KMH)
120     {
121         painter.drawPolyline(points, 11);
122     }
123
124     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
125     {
126         painter.drawPolyline(points, 9);
127     }
128
129     else if (this->diagramGapStem == DIAGRAMGAP60KMH)
130     {
131         painter.drawPolyline(points, 7);
132     }
133
134     else if (this->diagramGapStem == DIAGRAMGAP50KMH)
135     {
136         painter.drawPolyline(points, 6);
137     }
138
139     else
140     {
141         painter.drawPolyline(points, 5);
142     }
143 }
144
145 /**
146   * Change the given speed and time to the point for the diagram.
147   * @param aSpeed is speed which need to change, aTime is time in seconds which need to change.
148   * @return point is calculated from aSpeed and aTime.
149   */
150 QPoint ResultDialog::changeMeasuresToDiagramPoint(int aSpeed, qreal aTime)
151 {
152     QPoint point;
153
154     int speedAsPixels;
155     int timeAsPixels;
156
157     // Calculate speed and time to the point which can be drawn to the diagram
158     if (this->diagramGapStem == DIAGRAMGAP100KMH)
159     {
160         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/100;
161         timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
162     }
163
164     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
165     {
166         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/80;
167         timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
168     }
169
170     else if (this->diagramGapStem == DIAGRAMGAP60KMH)
171     {
172         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/60;
173         timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
174     }
175
176     else if (this->diagramGapStem == DIAGRAMGAP50KMH)
177     {
178         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/50;
179         timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
180     }
181
182     else
183     {
184         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/40;
185         timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
186     }
187     point.setY(diagramStemStart.y()-speedAsPixels);
188     point.setX(diagramStemStart.x()+timeAsPixels);
189
190     return point;
191 }
192
193 /**
194   * Saves the given measures to array.
195   * @param pMeasures has information about acceleration.
196   */
197 void ResultDialog::saveMeasuresToArray(Measures *pMeasures)
198 {
199     timeArray[0] = 0;
200     timeArray[1] = pMeasures->getTime10kmh();
201     timeArray[2] = pMeasures->getTime20kmh();
202     timeArray[3] = pMeasures->getTime30kmh();
203     timeArray[4] = pMeasures->getTime40kmh();
204     timeArray[5] = pMeasures->getTime50kmh();
205     timeArray[6] = pMeasures->getTime60kmh();
206     timeArray[7] = pMeasures->getTime70kmh();
207     timeArray[8] = pMeasures->getTime80kmh();
208     timeArray[9] = pMeasures->getTime90kmh();
209     timeArray[10] = pMeasures->getTime100kmh();
210
211     setTimeAxelLength();
212
213     for (int i = 0; i < 11; i++)
214     {
215         points[i] = changeMeasuresToDiagramPoint(speedArray[i], timeArray[i]);
216     }
217
218     setTimesIntoLabels();
219     this->repaint();
220
221     for (int i = 0; i < 11; i++)
222     {
223         timeArray[i] = 0;
224     }
225 }
226
227 void ResultDialog::on_pushButtonSend_clicked()
228 {
229     emit sendresult();
230 }
231
232 /**
233   * Saves the given diagram gap to the member variable.
234   * @param pDiagramGapStem has information about the right gap for diagram stem axel.
235   */
236 void ResultDialog::setDiagramGapStem(double pDiagramGapStem)
237 {
238     this->diagramGapStem = pDiagramGapStem;
239 }
240
241 /**
242   * Saves the given diagram gap to the member variable.
243   * @param pDiagramGapHorizontal has information about the right gap for diagram horizontal axel.
244   */
245 void ResultDialog::setDiagramGapHorizontal(double pDiagramGapHorizontal)
246 {
247     this->diagramGapHorizontal = pDiagramGapHorizontal;
248 }
249
250 /**
251   * Sets result times in to the labels and shows only wanted results and hides
252   * unwanted.
253   */
254 void ResultDialog::setTimesIntoLabels()
255 {
256     QString time, timeInteger;
257     timeInteger.setNum(timeArray[4]);
258     time = "0 - 40 km/h: ";
259     time.append(timeInteger);
260     ui->labelResult40kmh->setText(time);
261
262     timeInteger.setNum(timeArray[3]);
263     time = "0 - 30 km/h: ";
264     time.append(timeInteger);
265     ui->labelResult30kmh->setText(time);
266
267     timeInteger.setNum(timeArray[2]);
268     time = "0 - 20 km/h: ";
269     time.append(timeInteger);
270     ui->labelResult20kmh->setText(time);
271
272     timeInteger.setNum(timeArray[1]);
273     time = "0 - 10 km/h: ";
274     time.append(timeInteger);
275     ui->labelResult10kmh->setText(time);
276
277     timeInteger.setNum(timeArray[6]);
278     time = "0 - 60 km/h: ";
279     time.append(timeInteger);
280     ui->labelResult60kmh->setText(time);
281
282     timeInteger.setNum(timeArray[5]);
283     time = "0 - 50 km/h: ";
284     time.append(timeInteger);
285     ui->labelResult50kmh->setText(time);
286
287     timeInteger.setNum(timeArray[7]);
288     time = "0 - 70 km/h: ";
289     time.append(timeInteger);
290     ui->labelResult70kmh->setText(time);
291
292     timeInteger.setNum(timeArray[8]);
293     time = "0 - 80 km/h: ";
294     time.append(timeInteger);
295     ui->labelResult80kmh->setText(time);
296
297     timeInteger.setNum(timeArray[9]);
298     time = "0 - 90 km/h: ";
299     time.append(timeInteger);
300     ui->labelResult90kmh->setText(time);
301
302     timeInteger.setNum(timeArray[10]);
303     time = "0 - 100 km/h: ";
304     time.append(timeInteger);
305     ui->labelResult100kmh->setText(time);
306
307     if (this->diagramGapStem == DIAGRAMGAP40KMH)
308     {
309         ui->labelResult50kmh->hide();
310         ui->labelResult60kmh->hide();
311         ui->labelResult70kmh->hide();
312         ui->labelResult80kmh->hide();
313         ui->labelResult90kmh->hide();
314         ui->labelResult100kmh->hide();
315     }
316
317     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
318     {
319         ui->labelResult50kmh->show();
320         ui->labelResult60kmh->show();
321         ui->labelResult70kmh->show();
322         ui->labelResult80kmh->show();
323         ui->labelResult90kmh->hide();
324         ui->labelResult100kmh->hide();
325     }
326
327     else
328     {
329         ui->labelResult50kmh->show();
330         ui->labelResult60kmh->show();
331         ui->labelResult70kmh->show();
332         ui->labelResult80kmh->show();
333         ui->labelResult90kmh->show();
334         ui->labelResult100kmh->show();
335     }
336 }
337
338 /**
339   * Sets right timeAxelLength value depending the time which
340   * has spent to reach target speed.
341   */
342 void ResultDialog::setTimeAxelLength()
343 {
344     if (this->diagramGapStem == DIAGRAMGAP40KMH)
345     {
346         if (timeArray[4] <= 5)
347         {
348             timeAxelLength = 5;
349         }
350
351         else if (timeArray[4] <= 10)
352         {
353             timeAxelLength = 10;
354         }
355
356         else if (timeArray[4] <= 15)
357         {
358             timeAxelLength = 15;
359         }
360
361         else
362         {
363             timeAxelLength = 20;
364         }
365     }
366
367     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
368     {
369         if (timeArray[8] <= 5)
370         {
371             timeAxelLength = 5;
372         }
373
374         else if (timeArray[8] <= 10)
375         {
376             timeAxelLength = 10;
377         }
378
379         else if (timeArray[8] <= 15)
380         {
381             timeAxelLength = 15;
382         }
383
384         else
385         {
386             timeAxelLength = 20;
387         }
388     }
389
390     else
391     {
392         if (timeArray[10] <= 5)
393         {
394             timeAxelLength = 5;
395         }
396
397         else if (timeArray[10] <= 10)
398         {
399             timeAxelLength = 10;
400         }
401
402         else if (timeArray[10] <= 15)
403         {
404             timeAxelLength = 15;
405         }
406
407         else
408         {
409             timeAxelLength = 20;
410         }
411     }
412 }