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"
13 const int DIAGRAM_WIDTH = 400;
14 const int DIAGRAM_HEIGHT = 300;
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;
22 const QPoint diagramStemStart(70, 330);
23 const QPoint diagramStemEnd(70, 30);
25 const QPoint diagramHorizontalStart(70, 330);
26 const QPoint diagramHorizontalEnd(470, 330);
28 const int diagramGap = 30;
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};
34 // Test point array for the diagram.
38 * Constructor of this class.
39 * @param QWidget pointer to parent object. By default the value is NULL.
41 ResultDialog::ResultDialog(QWidget *parent) :
43 ui(new Ui::ResultDialog)
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";
53 * Destructor of this class. Should be used to release all allocated resources.
55 ResultDialog::~ResultDialog()
60 void ResultDialog::changeEvent(QEvent *e)
62 QDialog::changeEvent(e);
64 case QEvent::LanguageChange:
65 ui->retranslateUi(this);
73 * Draws speed diagram to the UI
76 void ResultDialog::paintEvent(QPaintEvent *)
78 QPainter painter(this);
80 painter.setRenderHint(QPainter::Antialiasing, true);
81 painter.setPen(QPen((Qt::red),2));
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);
90 int currentY = diagramStemStart.y();
92 painter.setPen(QPen((Qt::blue),1));
93 // Draws diagram's X-axel
95 while (currentX <= DIAGRAM_WIDTH)
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;
103 currentX = diagramStemStart.x();
107 // Draws diagram's Y-axel
108 while (currentY >= -(DIAGRAM_HEIGHT))
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;
116 painter.setPen(QPen((Qt::white),2));
118 // Draws result line to the diagram
119 if (this->diagramGapStem == DIAGRAMGAP100KMH)
121 painter.drawPolyline(points, 11);
124 else if (this->diagramGapStem == DIAGRAMGAP80KMH)
126 painter.drawPolyline(points, 9);
129 else if (this->diagramGapStem == DIAGRAMGAP60KMH)
131 painter.drawPolyline(points, 7);
134 else if (this->diagramGapStem == DIAGRAMGAP50KMH)
136 painter.drawPolyline(points, 6);
141 painter.drawPolyline(points, 5);
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.
150 QPoint ResultDialog::changeMeasuresToDiagramPoint(int aSpeed, qreal aTime)
157 // Calculate speed and time to the point which can be drawn to the diagram
158 if (this->diagramGapStem == DIAGRAMGAP100KMH)
160 speedAsPixels = DIAGRAM_HEIGHT*aSpeed/100;
161 timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
164 else if (this->diagramGapStem == DIAGRAMGAP80KMH)
166 speedAsPixels = DIAGRAM_HEIGHT*aSpeed/80;
167 timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
170 else if (this->diagramGapStem == DIAGRAMGAP60KMH)
172 speedAsPixels = DIAGRAM_HEIGHT*aSpeed/60;
173 timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
176 else if (this->diagramGapStem == DIAGRAMGAP50KMH)
178 speedAsPixels = DIAGRAM_HEIGHT*aSpeed/50;
179 timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
184 speedAsPixels = DIAGRAM_HEIGHT*aSpeed/40;
185 timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
187 point.setY(diagramStemStart.y()-speedAsPixels);
188 point.setX(diagramStemStart.x()+timeAsPixels);
194 * Saves the given measures to array.
195 * @param pMeasures has information about acceleration.
197 void ResultDialog::saveMeasuresToArray(Measures *pMeasures)
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();
213 for (int i = 0; i < 11; i++)
215 points[i] = changeMeasuresToDiagramPoint(speedArray[i], timeArray[i]);
218 setTimesIntoLabels();
221 for (int i = 0; i < 11; i++)
227 void ResultDialog::on_pushButtonSend_clicked()
233 * Saves the given diagram gap to the member variable.
234 * @param pDiagramGapStem has information about the right gap for diagram stem axel.
236 void ResultDialog::setDiagramGapStem(double pDiagramGapStem)
238 this->diagramGapStem = pDiagramGapStem;
242 * Saves the given diagram gap to the member variable.
243 * @param pDiagramGapHorizontal has information about the right gap for diagram horizontal axel.
245 void ResultDialog::setDiagramGapHorizontal(double pDiagramGapHorizontal)
247 this->diagramGapHorizontal = pDiagramGapHorizontal;
251 * Sets result times in to the labels and shows only wanted results and hides
254 void ResultDialog::setTimesIntoLabels()
256 QString time, timeInteger;
257 timeInteger.setNum(timeArray[4]);
258 time = "0 - 40 km/h: ";
259 time.append(timeInteger);
260 ui->labelResult40kmh->setText(time);
262 timeInteger.setNum(timeArray[3]);
263 time = "0 - 30 km/h: ";
264 time.append(timeInteger);
265 ui->labelResult30kmh->setText(time);
267 timeInteger.setNum(timeArray[2]);
268 time = "0 - 20 km/h: ";
269 time.append(timeInteger);
270 ui->labelResult20kmh->setText(time);
272 timeInteger.setNum(timeArray[1]);
273 time = "0 - 10 km/h: ";
274 time.append(timeInteger);
275 ui->labelResult10kmh->setText(time);
277 timeInteger.setNum(timeArray[6]);
278 time = "0 - 60 km/h: ";
279 time.append(timeInteger);
280 ui->labelResult60kmh->setText(time);
282 timeInteger.setNum(timeArray[5]);
283 time = "0 - 50 km/h: ";
284 time.append(timeInteger);
285 ui->labelResult50kmh->setText(time);
287 timeInteger.setNum(timeArray[7]);
288 time = "0 - 70 km/h: ";
289 time.append(timeInteger);
290 ui->labelResult70kmh->setText(time);
292 timeInteger.setNum(timeArray[8]);
293 time = "0 - 80 km/h: ";
294 time.append(timeInteger);
295 ui->labelResult80kmh->setText(time);
297 timeInteger.setNum(timeArray[9]);
298 time = "0 - 90 km/h: ";
299 time.append(timeInteger);
300 ui->labelResult90kmh->setText(time);
302 timeInteger.setNum(timeArray[10]);
303 time = "0 - 100 km/h: ";
304 time.append(timeInteger);
305 ui->labelResult100kmh->setText(time);
307 if (this->diagramGapStem == DIAGRAMGAP40KMH)
309 ui->labelResult50kmh->hide();
310 ui->labelResult60kmh->hide();
311 ui->labelResult70kmh->hide();
312 ui->labelResult80kmh->hide();
313 ui->labelResult90kmh->hide();
314 ui->labelResult100kmh->hide();
317 else if (this->diagramGapStem == DIAGRAMGAP80KMH)
319 ui->labelResult50kmh->show();
320 ui->labelResult60kmh->show();
321 ui->labelResult70kmh->show();
322 ui->labelResult80kmh->show();
323 ui->labelResult90kmh->hide();
324 ui->labelResult100kmh->hide();
329 ui->labelResult50kmh->show();
330 ui->labelResult60kmh->show();
331 ui->labelResult70kmh->show();
332 ui->labelResult80kmh->show();
333 ui->labelResult90kmh->show();
334 ui->labelResult100kmh->show();
339 * Sets right timeAxelLength value depending the time which
340 * has spent to reach target speed.
342 void ResultDialog::setTimeAxelLength()
344 if (this->diagramGapStem == DIAGRAMGAP40KMH)
346 if (timeArray[4] <= 5)
351 else if (timeArray[4] <= 10)
356 else if (timeArray[4] <= 15)
367 else if (this->diagramGapStem == DIAGRAMGAP80KMH)
369 if (timeArray[8] <= 5)
374 else if (timeArray[8] <= 10)
379 else if (timeArray[8] <= 15)
392 if (timeArray[10] <= 5)
397 else if (timeArray[10] <= 10)
402 else if (timeArray[10] <= 15)