Added result dialog.
[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 "math.h"
12 #include <QPainter>
13 #include <QPicture>
14
15 const int DIAGRAM_WIDTH = 400;
16 const int DIAGRAM_HEIGHT = 300;
17
18 const int DIAGRAMGAP10KMH = 300;
19 const int DIAGRAMGAP20KMH = 150;
20 const int DIAGRAMGAP30KMH = 100;
21 const int DIAGRAMGAP40KMH = 75;
22 const int DIAGRAMGAP50KMH = 60;
23 const int DIAGRAMGAP60KMH = 50;
24 const double DIAGRAMGAP70KMH = 42.86;
25 const double DIAGRAMGAP80KMH = 37.5;
26 const double DIAGRAMGAP90KMH = 33.33;
27 const int DIAGRAMGAP100KMH = 30;
28
29 const int DIAGRAMGAP5S = 80;
30 const int DIAGRAMGAP10S = 40;
31 const int DIAGRAMGAP20S = 20;
32
33 const QPoint diagramStemStart(70, 330);
34 const QPoint diagramStemEnd(70, 30);
35
36 const QPoint diagramHorizontalStart(70, 330);
37 const QPoint diagramHorizontalEnd(470, 330);
38
39 const int diagramGap = 30;
40
41 // Test arrays for changing speeds and times to the points in diagram
42 static const int speedArray[11] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
43 //static const int timeArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 12};
44
45 // Test point array for the diagram.
46 QPoint points[11];
47
48 /**
49   * Constructor of this class.
50   * @param QWidget pointer to parent object. By default the value is NULL.
51   */
52 ResultDialog::ResultDialog(QWidget *parent) :
53     QDialog(parent),
54     ui(new Ui::ResultDialog)
55 {
56     ui->setupUi(this);
57     timeAxelLength = 10;
58     speedList << "0" << "10" << "20" << "30" << "40" << "50" << "60" << "70" << "80" << "90" << "100" ;
59     timeList << "0" << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10" << "11"
60             << "12" << "13" << "14" << "15" << "16" << "17" << "18" << "19" << "20";
61     for (int i = 0; i < 11; i++)
62     {
63         timeArray[i] = 0;
64     }
65     /*
66     // TODO check that logging is ok.
67     if (isloggingNotOk)
68     {
69         ui->pushButtonSend->setEnabled(false);
70     }
71     else
72     {
73         ui->pushButtonSend->setEnabled(true);
74     }
75     */
76 }
77
78 /**
79   * Destructor of this class.  Should be used to release all allocated resources.
80   */
81 ResultDialog::~ResultDialog()
82 {
83     delete ui;
84 }
85
86 void ResultDialog::changeEvent(QEvent *e)
87 {
88     QDialog::changeEvent(e);
89     switch (e->type()) {
90     case QEvent::LanguageChange:
91         ui->retranslateUi(this);
92         break;
93     default:
94         break;
95     }
96 }
97
98 /**
99   * Draws speed diagram to the UI
100   * @param QPaintEvent
101  */
102 void ResultDialog::paintEvent(QPaintEvent *)
103 {
104     QString resultString;
105     resultString.append("Time was ");
106
107     if (this->diagramGapStem == DIAGRAMGAP100KMH)
108     {
109         resultString.append(QString::number(timeArray[10]));
110         this->setWindowTitle("Result for accelerating 100 km/h");
111     }
112
113     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
114     {
115         resultString.append(QString::number(timeArray[8]));
116         this->setWindowTitle("Result for accelerating 80 km/h");
117     }
118
119     else
120     {
121         resultString.append(QString::number(timeArray[4]));
122         this->setWindowTitle("Result for accelerating 40 km/h");
123     }
124
125     QPainter painter(this);
126
127     painter.setRenderHint(QPainter::Antialiasing, true);
128     painter.setPen(QPen((Qt::gray),2));
129     QFont font;
130     QFont fontForResult;
131     font.setPixelSize(12);
132     painter.setFont(font);
133     painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
134
135     fontForResult.setPixelSize(50);
136     painter.setFont(fontForResult);
137     painter.drawText(diagramStemStart.x() + 50, diagramStemStart.y() - 150, resultString);
138     painter.setFont(font);
139
140     painter.setPen(QPen((Qt::darkCyan),2));
141     painter.drawLine(diagramStemStart, diagramStemEnd);
142     painter.drawLine(diagramHorizontalStart, diagramHorizontalEnd);
143
144     int currentX = 0;
145     int currentY = diagramStemStart.y();
146
147     painter.setPen(QPen((Qt::darkCyan),1));
148
149     int i = 0;
150     // Draws diagram's X-axel
151     while (currentX <= DIAGRAM_WIDTH)
152     {
153         painter.drawLine(currentX + diagramStemStart.x(), currentY, currentX + diagramStemStart.x(), currentY - 300);
154         painter.drawText(currentX + diagramStemStart.x() - 3, currentY + 20, timeList[i]);
155         currentX += this->diagramGapHorizontal;
156         i++;
157     }
158
159     currentX = diagramStemStart.x();
160     currentY = 0;
161
162     i = 0;
163     // Draws diagram's Y-axel
164     while (currentY >= -(DIAGRAM_HEIGHT))
165     {
166         painter.drawLine(currentX, diagramStemStart.y() + currentY, currentX+400, diagramStemStart.y() + currentY);
167         painter.drawText(currentX - 25, diagramStemStart.y() + currentY + 3, speedList[i]);
168         currentY -= this->diagramGapStem;
169         i++;
170     }
171
172     painter.setPen(QPen((Qt::red),2));
173
174     int pointsToShow = 0;
175     bool pointsUnderDiagramWidth = true;
176
177     for (i = 0; i < 11 ; i++)
178     {
179         if (points[i].x() > diagramHorizontalEnd.x())
180         {
181             pointsToShow = i;
182             pointsUnderDiagramWidth = false;
183             i = 10;
184         }
185     }
186
187     // Draws result line to the diagram
188     if (this->diagramGapStem == DIAGRAMGAP100KMH)
189     {
190         if (pointsUnderDiagramWidth)
191         {
192             painter.drawPolyline(points, 11);
193         }
194
195         else
196         {
197             painter.drawPolyline(points, pointsToShow);
198         }
199     }
200
201     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
202     {
203         if (pointsUnderDiagramWidth)
204         {
205             painter.drawPolyline(points, 9);
206         }
207
208         else
209         {
210             painter.drawPolyline(points, pointsToShow);
211         }
212     }
213
214     else if (this->diagramGapStem == DIAGRAMGAP60KMH)
215     {
216         if (pointsUnderDiagramWidth)
217         {
218             painter.drawPolyline(points, 7);
219         }
220
221         else
222         {
223             painter.drawPolyline(points, pointsToShow);
224         }
225     }
226
227     else if (this->diagramGapStem == DIAGRAMGAP50KMH)
228     {
229         if (pointsUnderDiagramWidth)
230         {
231             painter.drawPolyline(points, 6);
232         }
233
234         else
235         {
236             painter.drawPolyline(points, pointsToShow);
237         }
238     }
239
240     else
241     {
242         if (pointsUnderDiagramWidth)
243         {
244             painter.drawPolyline(points, 5);
245         }
246
247         else
248         {
249             painter.drawPolyline(points, pointsToShow);
250         }
251     }
252 }
253
254 /**
255   * Change the given speed and time to the point for the diagram.
256   * @param aSpeed is speed which need to change, aTime is time in seconds which need to change.
257   * @return point is calculated from aSpeed and aTime.
258   */
259 QPoint ResultDialog::changeMeasuresToDiagramPoint(int aSpeed, qreal aTime)
260 {
261     QPoint point;
262
263     int speedAsPixels;
264     int timeAsPixels;
265
266     // Calculate speed and time to the point which can be drawn to the diagram
267     if (this->diagramGapStem == DIAGRAMGAP100KMH)
268     {
269         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/100;
270         timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
271     }
272
273     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
274     {
275         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/80;
276         timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
277     }
278
279     else if (this->diagramGapStem == DIAGRAMGAP60KMH)
280     {
281         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/60;
282         timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
283     }
284
285     else if (this->diagramGapStem == DIAGRAMGAP50KMH)
286     {
287         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/50;
288         timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
289     }
290
291     else
292     {
293         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/40;
294         timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
295     }
296     point.setY(diagramStemStart.y()-speedAsPixels);
297     point.setX(diagramStemStart.x()+timeAsPixels);
298
299     return point;
300 }
301
302 /**
303   * Saves the given measures to array.
304   * @param pMeasures has information about acceleration.
305   */
306 /*
307 void ResultDialog::saveMeasuresToArray(Measures *pMeasures)
308 {
309     timeArray[0] = 0;
310     timeArray[1] = pMeasures->getTime10kmh();
311     timeArray[2] = pMeasures->getTime20kmh();
312     timeArray[3] = pMeasures->getTime30kmh();
313     timeArray[4] = pMeasures->getTime40kmh();
314     timeArray[5] = pMeasures->getTime50kmh();
315     timeArray[6] = pMeasures->getTime60kmh();
316     timeArray[7] = pMeasures->getTime70kmh();
317     timeArray[8] = pMeasures->getTime80kmh();
318     timeArray[9] = pMeasures->getTime90kmh();
319     timeArray[10] = pMeasures->getTime100kmh();
320
321     setTimeAxelLength();
322
323     for (int i = 0; i < 11; i++)
324     {
325         points[i] = changeMeasuresToDiagramPoint(speedArray[i], timeArray[i]);
326     }
327
328     setTimesIntoLabels();
329     this->repaint();
330 }*/
331
332 /**
333   * Saves the given diagram gap to the member variable.
334   * @param pDiagramGapStem has information about the right gap for diagram stem axel.
335   */
336 void ResultDialog::setDiagramGapStem(double pDiagramGapStem)
337 {
338     this->diagramGapStem = pDiagramGapStem;
339 }
340
341 /**
342   * Saves the given diagram gap to the member variable.
343   * @param pDiagramGapHorizontal has information about the right gap for diagram horizontal axel.
344   */
345 void ResultDialog::setDiagramGapHorizontal(double pDiagramGapHorizontal)
346 {
347     this->diagramGapHorizontal = pDiagramGapHorizontal;
348 }
349
350 /**
351   * Sets result times in to the labels and shows only wanted results and hides
352   * unwanted.
353   */
354 void ResultDialog::setTimesIntoLabels()
355 {
356     QString time, timeInteger;
357     timeInteger.setNum(timeArray[4]);
358     time = "0 - 40 km/h: ";
359     time.append(timeInteger);
360     ui->labelResult40kmh->setText(time);
361
362     timeInteger.setNum(timeArray[3]);
363     time = "0 - 30 km/h: ";
364     time.append(timeInteger);
365     ui->labelResult30kmh->setText(time);
366
367     timeInteger.setNum(timeArray[2]);
368     time = "0 - 20 km/h: ";
369     time.append(timeInteger);
370     ui->labelResult20kmh->setText(time);
371
372     timeInteger.setNum(timeArray[1]);
373     time = "0 - 10 km/h: ";
374     time.append(timeInteger);
375     ui->labelResult10kmh->setText(time);
376
377     timeInteger.setNum(timeArray[6]);
378     time = "0 - 60 km/h: ";
379     time.append(timeInteger);
380     ui->labelResult60kmh->setText(time);
381
382     timeInteger.setNum(timeArray[5]);
383     time = "0 - 50 km/h: ";
384     time.append(timeInteger);
385     ui->labelResult50kmh->setText(time);
386
387     timeInteger.setNum(timeArray[7]);
388     time = "0 - 70 km/h: ";
389     time.append(timeInteger);
390     ui->labelResult70kmh->setText(time);
391
392     timeInteger.setNum(timeArray[8]);
393     time = "0 - 80 km/h: ";
394     time.append(timeInteger);
395     ui->labelResult80kmh->setText(time);
396
397     timeInteger.setNum(timeArray[9]);
398     time = "0 - 90 km/h: ";
399     time.append(timeInteger);
400     ui->labelResult90kmh->setText(time);
401
402     timeInteger.setNum(timeArray[10]);
403     time = "0 - 100 km/h: ";
404     time.append(timeInteger);
405     ui->labelResult100kmh->setText(time);
406
407     if (this->diagramGapStem == DIAGRAMGAP40KMH)
408     {
409         ui->labelResult50kmh->hide();
410         ui->labelResult60kmh->hide();
411         ui->labelResult70kmh->hide();
412         ui->labelResult80kmh->hide();
413         ui->labelResult90kmh->hide();
414         ui->labelResult100kmh->hide();
415     }
416
417     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
418     {
419         ui->labelResult50kmh->show();
420         ui->labelResult60kmh->show();
421         ui->labelResult70kmh->show();
422         ui->labelResult80kmh->show();
423         ui->labelResult90kmh->hide();
424         ui->labelResult100kmh->hide();
425     }
426
427     else
428     {
429         ui->labelResult50kmh->show();
430         ui->labelResult60kmh->show();
431         ui->labelResult70kmh->show();
432         ui->labelResult80kmh->show();
433         ui->labelResult90kmh->show();
434         ui->labelResult100kmh->show();
435     }
436 }
437
438 /**
439   * Sets right timeAxelLength value depending the time which
440   * has spent to reach target speed.
441   */
442 void ResultDialog::setTimeAxelLength()
443 {
444     if (this->diagramGapStem == DIAGRAMGAP40KMH)
445     {
446         if (timeArray[4] <= 5)
447         {
448             timeAxelLength = 5;
449         }
450
451         else if (timeArray[4] <= 10)
452         {
453             timeAxelLength = 10;
454         }
455
456         else if (timeArray[4] <= 15)
457         {
458             timeAxelLength = 15;
459         }
460
461         else
462         {
463             timeAxelLength = 20;
464         }
465     }
466
467     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
468     {
469         if (timeArray[8] <= 5)
470         {
471             timeAxelLength = 5;
472         }
473
474         else if (timeArray[8] <= 10)
475         {
476             timeAxelLength = 10;
477         }
478
479         else if (timeArray[8] <= 15)
480         {
481             timeAxelLength = 15;
482         }
483
484         else
485         {
486             timeAxelLength = 20;
487         }
488     }
489
490     else
491     {
492         if (timeArray[10] <= 5)
493         {
494             timeAxelLength = 5;
495         }
496
497         else if (timeArray[10] <= 10)
498         {
499             timeAxelLength = 10;
500         }
501
502         else if (timeArray[10] <= 15)
503         {
504             timeAxelLength = 15;
505         }
506
507         else
508         {
509             timeAxelLength = 20;
510         }
511     }
512 }
513
514 /**
515   * This slot function close result dialog when new run -button has been clicked.
516   */
517 void ResultDialog::on_pushButtonNew_clicked()
518 {
519     for (int i = 0; i < 11; i++)
520     {
521         timeArray[i] = 0;
522     }
523     this->close();
524 }
525
526 /**
527   * This slot function emits sendresult signal for sending results to server when
528   * send results -button has been clicked.
529   */
530 void ResultDialog::on_pushButtonSend_clicked()
531 {
532     if (this->diagramGapStem == DIAGRAMGAP100KMH)
533     {
534         emit sendresult(timeArray[10]);
535     }
536
537     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
538     {
539         emit sendresult(timeArray[8]);
540     }
541
542     else
543     {
544         emit sendresult(timeArray[4]);
545     }
546 }
547
548 /**
549   * This public function sets diagram's stem gap
550   * @param pValue is the speed value which determines diagram gap's value
551   */
552 void ResultDialog::setEnd(int pValue)
553 {
554     switch (pValue)
555     {
556     case 10:
557         this->diagramGapStem = DIAGRAMGAP10KMH;
558         break;
559
560     case 20:
561         this->diagramGapStem = DIAGRAMGAP20KMH;
562         break;
563
564     case 30:
565         this->diagramGapStem = DIAGRAMGAP30KMH;
566         break;
567
568     case 40:
569         this->diagramGapStem = DIAGRAMGAP40KMH;
570         break;
571
572     case 50:
573         this->diagramGapStem = DIAGRAMGAP50KMH;
574         break;
575
576     case 60:
577         this->diagramGapStem = DIAGRAMGAP60KMH;
578         break;
579
580     case 70:
581         this->diagramGapStem = DIAGRAMGAP70KMH;
582         break;
583
584     case 80:
585         this->diagramGapStem = DIAGRAMGAP80KMH;
586         break;
587
588     case 90:
589         this->diagramGapStem = DIAGRAMGAP90KMH;
590         break;
591
592     case 100:
593         this->diagramGapStem = DIAGRAMGAP100KMH;
594         break;
595
596     default:
597         this->diagramGapStem = DIAGRAMGAP100KMH;
598         break;
599     }
600
601 }
602
603 /**
604   * This public function stores times in timeArray
605   * @param pSpeed is the speed value at the time so we know where store time
606   * @param pTime is the result which needs to be store in timeArray
607   */
608 void ResultDialog::setValue(int pSpeed, double pTime)
609 {
610     timeArray[0] = 0;
611     if (floor(pTime) <= 5)
612     {
613         this->diagramGapHorizontal = DIAGRAMGAP5S;
614     }
615
616     else if (floor(pTime) <= 10)
617     {
618        this->diagramGapHorizontal = DIAGRAMGAP10S;
619     }
620
621     else
622     {
623         this->diagramGapHorizontal = DIAGRAMGAP20S;
624     }
625
626     switch (pSpeed)
627     {
628     case 10:
629         timeArray[1] = pTime;
630         break;
631     case 20:
632         timeArray[2] = pTime;
633         break;
634     case 30:
635         timeArray[3] = pTime;
636         break;
637     case 40:
638         timeArray[4] = pTime;
639         break;
640     case 50:
641         timeArray[5] = pTime;
642         break;
643     case 60:
644         timeArray[6] = pTime;
645         break;
646     case 70:
647         timeArray[7] = pTime;
648         break;
649     case 80:
650         timeArray[8] = pTime;
651         break;
652     case 90:
653         timeArray[9] = pTime;
654         break;
655     case 100:
656         timeArray[10] = pTime;
657         break;
658     }
659     setTimeAxelLength();
660
661     for (int i = 0; i < 11; i++)
662     {
663         points[i] = changeMeasuresToDiagramPoint(speedArray[i], timeArray[i]);
664     }
665
666     setTimesIntoLabels();
667     this->repaint();
668 }