Bug fixed in resultdialog.cpp
[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     speedList << "0" << "10" << "20" << "30" << "40" << "50" << "60" << "70" << "80" << "90" << "100" ;
47     timeList << "0" << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10" << "11"
48             << "12" << "13" << "14" << "15" << "16" << "17" << "18" << "19" << "20";
49 }
50
51 /**
52   * Destructor of this class.  Should be used to release all allocated resources.
53   */
54 ResultDialog::~ResultDialog()
55 {
56     delete ui;
57 }
58
59 void ResultDialog::changeEvent(QEvent *e)
60 {
61     QDialog::changeEvent(e);
62     switch (e->type()) {
63     case QEvent::LanguageChange:
64         ui->retranslateUi(this);
65         break;
66     default:
67         break;
68     }
69 }
70
71 /**
72   * Draws speed diagram to the UI
73   * @param QPaintEvent
74  */
75 void ResultDialog::paintEvent(QPaintEvent *)
76 {
77         QPainter painter(this);
78
79     painter.setRenderHint(QPainter::Antialiasing, true);
80     painter.setPen(QPen((Qt::red),2));
81     QFont font;
82     font.setPixelSize(12);
83     painter.setFont(font);
84     painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
85     painter.drawLine(diagramStemStart, diagramStemEnd);
86     painter.drawLine(diagramHorizontalStart, diagramHorizontalEnd);
87
88     int currentX = 0;
89     int currentY = diagramStemStart.y();
90
91     painter.setPen(QPen((Qt::blue),1));
92     // Draws diagram's X-axel
93     int i = 0;
94     while (currentX <= DIAGRAM_WIDTH)
95     {
96         painter.drawLine(currentX + diagramStemStart.x(), currentY, currentX + diagramStemStart.x(), currentY - 300);
97         painter.drawText(currentX + diagramStemStart.x(), currentY + 20, timeList[i]);
98         currentX += this->diagramGapHorizontal;
99         i++;
100     }
101
102     currentX = diagramStemStart.x();
103     currentY = 0;
104
105     i = 0;
106     // Draws diagram's Y-axel
107     while (currentY >= -(DIAGRAM_HEIGHT))
108     {
109         painter.drawLine(currentX, diagramStemStart.y() + currentY, currentX+400, diagramStemStart.y() + currentY);
110         painter.drawText(currentX - 25, diagramStemStart.y() + currentY, speedList[i]);
111         currentY -= this->diagramGapStem;
112         i++;
113     }
114
115     painter.setPen(QPen((Qt::white),2));
116
117     // Draws result line to the diagram
118     if (this->diagramGapStem == DIAGRAMGAP100KMH)
119     {
120         painter.drawPolyline(points, 11);
121     }
122
123     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
124     {
125         painter.drawPolyline(points, 9);
126     }
127
128     else if (this->diagramGapStem == DIAGRAMGAP60KMH)
129     {
130         painter.drawPolyline(points, 7);
131     }
132
133     else if (this->diagramGapStem == DIAGRAMGAP50KMH)
134     {
135         painter.drawPolyline(points, 6);
136     }
137
138     else
139     {
140         painter.drawPolyline(points, 5);
141     }
142 }
143
144 /**
145   * Change the given speed and time to the point for the diagram.
146   * @param aSpeed is speed which need to change, aTime is time in seconds which need to change.
147   * @return point is calculated from aSpeed and aTime.
148   */
149 QPoint ResultDialog::changeMeasuresToDiagramPoint(int aSpeed, qreal aTime)
150 {
151     QPoint point;
152
153     int speedAsPixels;
154     int timeAsPixels;
155
156     // Calculate speed and time to the point which can be drawn to the diagram
157     if (this->diagramGapStem == DIAGRAMGAP100KMH)
158     {
159         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/100;
160         timeAsPixels = DIAGRAM_WIDTH*aTime/10;
161     }
162
163     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
164     {
165         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/80;
166         timeAsPixels = DIAGRAM_WIDTH*aTime/10;
167     }
168
169     else if (this->diagramGapStem == DIAGRAMGAP60KMH)
170     {
171         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/60;
172         timeAsPixels = DIAGRAM_WIDTH*aTime/10;
173     }
174
175     else if (this->diagramGapStem == DIAGRAMGAP50KMH)
176     {
177         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/50;
178         timeAsPixels = DIAGRAM_WIDTH*aTime/10;
179     }
180
181     else
182     {
183         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/40;
184         timeAsPixels = DIAGRAM_WIDTH*aTime/10;
185     }
186     point.setY(diagramStemStart.y()-speedAsPixels);
187     point.setX(diagramStemStart.x()+timeAsPixels);
188
189     return point;
190 }
191
192 /**
193   * Saves the given measures to array.
194   * @param pMeasures has information about acceleration.
195   */
196 void ResultDialog::saveMeasuresToArray(Measures *pMeasures)
197 {
198     timeArray[0] = 0;
199     timeArray[1] = pMeasures->getTime10kmh();
200     timeArray[2] = pMeasures->getTime20kmh();
201     timeArray[3] = pMeasures->getTime30kmh();
202     timeArray[4] = pMeasures->getTime40kmh();
203     timeArray[5] = pMeasures->getTime50kmh();
204     timeArray[6] = pMeasures->getTime60kmh();
205     timeArray[7] = pMeasures->getTime70kmh();
206     timeArray[8] = pMeasures->getTime80kmh();
207     timeArray[9] = pMeasures->getTime90kmh();
208     timeArray[10] = pMeasures->getTime100kmh();
209
210     for (int i = 0; i < 11; i++)
211     {
212         points[i] = changeMeasuresToDiagramPoint(speedArray[i], timeArray[i]);
213     }
214
215     setTimesIntoLabels();
216     this->repaint();
217
218     for (int i = 0; i < 11; i++)
219     {
220         timeArray[i] = 0;
221     }
222 }
223
224 void ResultDialog::on_pushButtonSend_clicked()
225 {
226     emit sendresult();
227 }
228
229 /**
230   * Saves the given diagram gap to the member variable.
231   * @param pDiagramGapStem has information about the right gap for diagram stem axel.
232   */
233 void ResultDialog::setDiagramGapStem(double pDiagramGapStem)
234 {
235     this->diagramGapStem = pDiagramGapStem;
236 }
237
238 /**
239   * Saves the given diagram gap to the member variable.
240   * @param pDiagramGapHorizontal has information about the right gap for diagram horizontal axel.
241   */
242 void ResultDialog::setDiagramGapHorizontal(double pDiagramGapHorizontal)
243 {
244     this->diagramGapHorizontal = pDiagramGapHorizontal;
245 }
246
247 /**
248   * Sets result times in to the labels and shows only wanted results and hides
249   * unwanted.
250   */
251 void ResultDialog::setTimesIntoLabels()
252 {
253     QString time, timeInteger;
254     timeInteger.setNum(timeArray[4]);
255     time = "0 - 40 km/h: ";
256     time.append(timeInteger);
257     ui->labelResult40kmh->setText(time);
258
259     timeInteger.setNum(timeArray[3]);
260     time = "0 - 30 km/h: ";
261     time.append(timeInteger);
262     ui->labelResult30kmh->setText(time);
263
264     timeInteger.setNum(timeArray[2]);
265     time = "0 - 20 km/h: ";
266     time.append(timeInteger);
267     ui->labelResult20kmh->setText(time);
268
269     timeInteger.setNum(timeArray[1]);
270     time = "0 - 10 km/h: ";
271     time.append(timeInteger);
272     ui->labelResult10kmh->setText(time);
273
274     timeInteger.setNum(timeArray[6]);
275     time = "0 - 60 km/h: ";
276     time.append(timeInteger);
277     ui->labelResult60kmh->setText(time);
278
279     timeInteger.setNum(timeArray[5]);
280     time = "0 - 50 km/h: ";
281     time.append(timeInteger);
282     ui->labelResult50kmh->setText(time);
283
284     timeInteger.setNum(timeArray[7]);
285     time = "0 - 70 km/h: ";
286     time.append(timeInteger);
287     ui->labelResult70kmh->setText(time);
288
289     timeInteger.setNum(timeArray[8]);
290     time = "0 - 80 km/h: ";
291     time.append(timeInteger);
292     ui->labelResult80kmh->setText(time);
293
294     timeInteger.setNum(timeArray[9]);
295     time = "0 - 90 km/h: ";
296     time.append(timeInteger);
297     ui->labelResult90kmh->setText(time);
298
299     timeInteger.setNum(timeArray[10]);
300     time = "0 - 100 km/h: ";
301     time.append(timeInteger);
302     ui->labelResult100kmh->setText(time);
303
304     if (this->diagramGapStem == DIAGRAMGAP40KMH)
305     {
306         ui->labelResult50kmh->hide();
307         ui->labelResult60kmh->hide();
308         ui->labelResult70kmh->hide();
309         ui->labelResult80kmh->hide();
310         ui->labelResult90kmh->hide();
311         ui->labelResult100kmh->hide();
312     }
313
314     else
315     {
316         ui->labelResult50kmh->show();
317         ui->labelResult60kmh->show();
318         ui->labelResult70kmh->show();
319         ui->labelResult80kmh->show();
320         ui->labelResult90kmh->show();
321         ui->labelResult100kmh->show();
322     }
323 }