2 * CarMainWindow main class
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
9 #include "resultdialog.h"
10 #include "ui_resultdialog.h"
15 const int DIAGRAM_WIDTH = 400;
16 const int DIAGRAM_HEIGHT = 300;
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;
29 const int DIAGRAMGAP5S = 80;
30 const int DIAGRAMGAP10S = 40;
31 const int DIAGRAMGAP20S = 20;
33 const QPoint diagramStemStart(70, 330);
34 const QPoint diagramStemEnd(70, 30);
36 const QPoint diagramHorizontalStart(70, 330);
37 const QPoint diagramHorizontalEnd(470, 330);
39 const int diagramGap = 30;
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};
45 // Test point array for the diagram.
49 * Constructor of this class.
50 * @param QWidget pointer to parent object. By default the value is NULL.
52 ResultDialog::ResultDialog(QWidget *parent) :
54 ui(new Ui::ResultDialog)
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++)
66 // TODO check that logging is ok.
69 ui->pushButtonSend->setEnabled(false);
73 ui->pushButtonSend->setEnabled(true);
79 * Destructor of this class. Should be used to release all allocated resources.
81 ResultDialog::~ResultDialog()
86 void ResultDialog::changeEvent(QEvent *e)
88 QDialog::changeEvent(e);
90 case QEvent::LanguageChange:
91 ui->retranslateUi(this);
99 * Draws speed diagram to the UI
102 void ResultDialog::paintEvent(QPaintEvent *)
104 QString resultString;
105 resultString.append("Time was ");
107 if (this->diagramGapStem == DIAGRAMGAP100KMH)
109 resultString.append(QString::number(timeArray[10]));
110 this->setWindowTitle("Result for accelerating 100 km/h");
113 else if (this->diagramGapStem == DIAGRAMGAP80KMH)
115 resultString.append(QString::number(timeArray[8]));
116 this->setWindowTitle("Result for accelerating 80 km/h");
121 resultString.append(QString::number(timeArray[4]));
122 this->setWindowTitle("Result for accelerating 40 km/h");
125 QPainter painter(this);
127 painter.setRenderHint(QPainter::Antialiasing, true);
128 painter.setPen(QPen((Qt::gray),2));
131 font.setPixelSize(12);
132 painter.setFont(font);
133 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
135 fontForResult.setPixelSize(50);
136 painter.setFont(fontForResult);
137 painter.drawText(diagramStemStart.x() + 50, diagramStemStart.y() - 150, resultString);
138 painter.setFont(font);
140 painter.setPen(QPen((Qt::darkCyan),2));
141 painter.drawLine(diagramStemStart, diagramStemEnd);
142 painter.drawLine(diagramHorizontalStart, diagramHorizontalEnd);
145 int currentY = diagramStemStart.y();
147 painter.setPen(QPen((Qt::darkCyan),1));
150 // Draws diagram's X-axel
151 while (currentX <= DIAGRAM_WIDTH)
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;
159 currentX = diagramStemStart.x();
163 // Draws diagram's Y-axel
164 while (currentY >= -(DIAGRAM_HEIGHT))
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;
172 painter.setPen(QPen((Qt::red),2));
174 int pointsToShow = 0;
175 bool pointsUnderDiagramWidth = true;
177 for (i = 0; i < 11 ; i++)
179 if (points[i].x() > diagramHorizontalEnd.x())
182 pointsUnderDiagramWidth = false;
187 // Draws result line to the diagram
188 if (this->diagramGapStem == DIAGRAMGAP100KMH)
190 if (pointsUnderDiagramWidth)
192 painter.drawPolyline(points, 11);
197 painter.drawPolyline(points, pointsToShow);
201 else if (this->diagramGapStem == DIAGRAMGAP80KMH)
203 if (pointsUnderDiagramWidth)
205 painter.drawPolyline(points, 9);
210 painter.drawPolyline(points, pointsToShow);
214 else if (this->diagramGapStem == DIAGRAMGAP60KMH)
216 if (pointsUnderDiagramWidth)
218 painter.drawPolyline(points, 7);
223 painter.drawPolyline(points, pointsToShow);
227 else if (this->diagramGapStem == DIAGRAMGAP50KMH)
229 if (pointsUnderDiagramWidth)
231 painter.drawPolyline(points, 6);
236 painter.drawPolyline(points, pointsToShow);
242 if (pointsUnderDiagramWidth)
244 painter.drawPolyline(points, 5);
249 painter.drawPolyline(points, pointsToShow);
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.
259 QPoint ResultDialog::changeMeasuresToDiagramPoint(int aSpeed, qreal aTime)
266 // Calculate speed and time to the point which can be drawn to the diagram
267 if (this->diagramGapStem == DIAGRAMGAP100KMH)
269 speedAsPixels = DIAGRAM_HEIGHT*aSpeed/100;
270 timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
273 else if (this->diagramGapStem == DIAGRAMGAP80KMH)
275 speedAsPixels = DIAGRAM_HEIGHT*aSpeed/80;
276 timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
279 else if (this->diagramGapStem == DIAGRAMGAP60KMH)
281 speedAsPixels = DIAGRAM_HEIGHT*aSpeed/60;
282 timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
285 else if (this->diagramGapStem == DIAGRAMGAP50KMH)
287 speedAsPixels = DIAGRAM_HEIGHT*aSpeed/50;
288 timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
293 speedAsPixels = DIAGRAM_HEIGHT*aSpeed/40;
294 timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
296 point.setY(diagramStemStart.y()-speedAsPixels);
297 point.setX(diagramStemStart.x()+timeAsPixels);
303 * Saves the given measures to array.
304 * @param pMeasures has information about acceleration.
307 void ResultDialog::saveMeasuresToArray(Measures *pMeasures)
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();
323 for (int i = 0; i < 11; i++)
325 points[i] = changeMeasuresToDiagramPoint(speedArray[i], timeArray[i]);
328 setTimesIntoLabels();
333 * Saves the given diagram gap to the member variable.
334 * @param pDiagramGapStem has information about the right gap for diagram stem axel.
336 void ResultDialog::setDiagramGapStem(double pDiagramGapStem)
338 this->diagramGapStem = pDiagramGapStem;
342 * Saves the given diagram gap to the member variable.
343 * @param pDiagramGapHorizontal has information about the right gap for diagram horizontal axel.
345 void ResultDialog::setDiagramGapHorizontal(double pDiagramGapHorizontal)
347 this->diagramGapHorizontal = pDiagramGapHorizontal;
351 * Sets result times in to the labels and shows only wanted results and hides
354 void ResultDialog::setTimesIntoLabels()
356 QString time, timeInteger;
357 timeInteger.setNum(timeArray[4]);
358 time = "0 - 40 km/h: ";
359 time.append(timeInteger);
360 ui->labelResult40kmh->setText(time);
362 timeInteger.setNum(timeArray[3]);
363 time = "0 - 30 km/h: ";
364 time.append(timeInteger);
365 ui->labelResult30kmh->setText(time);
367 timeInteger.setNum(timeArray[2]);
368 time = "0 - 20 km/h: ";
369 time.append(timeInteger);
370 ui->labelResult20kmh->setText(time);
372 timeInteger.setNum(timeArray[1]);
373 time = "0 - 10 km/h: ";
374 time.append(timeInteger);
375 ui->labelResult10kmh->setText(time);
377 timeInteger.setNum(timeArray[6]);
378 time = "0 - 60 km/h: ";
379 time.append(timeInteger);
380 ui->labelResult60kmh->setText(time);
382 timeInteger.setNum(timeArray[5]);
383 time = "0 - 50 km/h: ";
384 time.append(timeInteger);
385 ui->labelResult50kmh->setText(time);
387 timeInteger.setNum(timeArray[7]);
388 time = "0 - 70 km/h: ";
389 time.append(timeInteger);
390 ui->labelResult70kmh->setText(time);
392 timeInteger.setNum(timeArray[8]);
393 time = "0 - 80 km/h: ";
394 time.append(timeInteger);
395 ui->labelResult80kmh->setText(time);
397 timeInteger.setNum(timeArray[9]);
398 time = "0 - 90 km/h: ";
399 time.append(timeInteger);
400 ui->labelResult90kmh->setText(time);
402 timeInteger.setNum(timeArray[10]);
403 time = "0 - 100 km/h: ";
404 time.append(timeInteger);
405 ui->labelResult100kmh->setText(time);
407 if (this->diagramGapStem == DIAGRAMGAP40KMH)
409 ui->labelResult50kmh->hide();
410 ui->labelResult60kmh->hide();
411 ui->labelResult70kmh->hide();
412 ui->labelResult80kmh->hide();
413 ui->labelResult90kmh->hide();
414 ui->labelResult100kmh->hide();
417 else if (this->diagramGapStem == DIAGRAMGAP80KMH)
419 ui->labelResult50kmh->show();
420 ui->labelResult60kmh->show();
421 ui->labelResult70kmh->show();
422 ui->labelResult80kmh->show();
423 ui->labelResult90kmh->hide();
424 ui->labelResult100kmh->hide();
429 ui->labelResult50kmh->show();
430 ui->labelResult60kmh->show();
431 ui->labelResult70kmh->show();
432 ui->labelResult80kmh->show();
433 ui->labelResult90kmh->show();
434 ui->labelResult100kmh->show();
439 * Sets right timeAxelLength value depending the time which
440 * has spent to reach target speed.
442 void ResultDialog::setTimeAxelLength()
444 if (this->diagramGapStem == DIAGRAMGAP40KMH)
446 if (timeArray[4] <= 5)
451 else if (timeArray[4] <= 10)
456 else if (timeArray[4] <= 15)
467 else if (this->diagramGapStem == DIAGRAMGAP80KMH)
469 if (timeArray[8] <= 5)
474 else if (timeArray[8] <= 10)
479 else if (timeArray[8] <= 15)
492 if (timeArray[10] <= 5)
497 else if (timeArray[10] <= 10)
502 else if (timeArray[10] <= 15)
515 * This slot function close result dialog when new run -button has been clicked.
517 void ResultDialog::on_pushButtonNew_clicked()
519 for (int i = 0; i < 11; i++)
527 * This slot function emits sendresult signal for sending results to server when
528 * send results -button has been clicked.
530 void ResultDialog::on_pushButtonSend_clicked()
532 if (this->diagramGapStem == DIAGRAMGAP100KMH)
534 emit sendresult(timeArray[10]);
537 else if (this->diagramGapStem == DIAGRAMGAP80KMH)
539 emit sendresult(timeArray[8]);
544 emit sendresult(timeArray[4]);
549 * This public function sets diagram's stem gap
550 * @param pValue is the speed value which determines diagram gap's value
552 void ResultDialog::setEnd(int pValue)
557 this->diagramGapStem = DIAGRAMGAP10KMH;
561 this->diagramGapStem = DIAGRAMGAP20KMH;
565 this->diagramGapStem = DIAGRAMGAP30KMH;
569 this->diagramGapStem = DIAGRAMGAP40KMH;
573 this->diagramGapStem = DIAGRAMGAP50KMH;
577 this->diagramGapStem = DIAGRAMGAP60KMH;
581 this->diagramGapStem = DIAGRAMGAP70KMH;
585 this->diagramGapStem = DIAGRAMGAP80KMH;
589 this->diagramGapStem = DIAGRAMGAP90KMH;
593 this->diagramGapStem = DIAGRAMGAP100KMH;
597 this->diagramGapStem = DIAGRAMGAP100KMH;
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
608 void ResultDialog::setValue(int pSpeed, double pTime)
611 if (floor(pTime) <= 5)
613 this->diagramGapHorizontal = DIAGRAMGAP5S;
616 else if (floor(pTime) <= 10)
618 this->diagramGapHorizontal = DIAGRAMGAP10S;
623 this->diagramGapHorizontal = DIAGRAMGAP20S;
629 timeArray[1] = pTime;
632 timeArray[2] = pTime;
635 timeArray[3] = pTime;
638 timeArray[4] = pTime;
641 timeArray[5] = pTime;
644 timeArray[6] = pTime;
647 timeArray[7] = pTime;
650 timeArray[8] = pTime;
653 timeArray[9] = pTime;
656 timeArray[10] = pTime;
661 for (int i = 0; i < 11; i++)
663 points[i] = changeMeasuresToDiagramPoint(speedArray[i], timeArray[i]);
666 setTimesIntoLabels();