a
[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 QPoint diagramStemStart(50, 350);
14 const QPoint diagramStemEnd(50, 30);
15
16 const QPoint diagramHorizontalStart(50, 350);
17 const QPoint diagramHorizontalEnd(450, 350);
18
19 const int diagramGap = 30;
20
21 // Test arrays for changing speeds and times to the points in diagram
22 static const int speedArray[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
23 //static const int timeArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 12};
24
25 // Test point array for the diagram.
26 QPoint points[10];
27
28 /**
29   * Constructor of this class.
30   * @param QWidget pointer to parent object. By default the value is NULL.
31   */
32 ResultDialog::ResultDialog(QWidget *parent) :
33     QDialog(parent),
34     ui(new Ui::ResultDialog)
35 {
36     ui->setupUi(this);
37
38
39
40    // ui->labelXLine->setText(ui->labelXLine->text().append(": time/ s"));
41    // ui->labelYLine->setText(ui->labelYLine->text().append(": speed/ km/h"));
42 }
43
44 /**
45   * Destructor of this class.  Should be used to release all allocated resources.
46   */
47 ResultDialog::~ResultDialog()
48 {
49     delete ui;
50 }
51
52 void ResultDialog::changeEvent(QEvent *e)
53 {
54     QDialog::changeEvent(e);
55     switch (e->type()) {
56     case QEvent::LanguageChange:
57         ui->retranslateUi(this);
58         break;
59     default:
60         break;
61     }
62 }
63
64 /**
65   * Draws speed diagram to the UI
66   * @param QPaintEvent
67  */
68 void ResultDialog::paintEvent(QPaintEvent *)
69 {
70         QPainter painter(this);
71
72         painter.setRenderHint(QPainter::Antialiasing, true);
73         painter.setPen(QPen((Qt::white),2));
74         painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
75         painter.drawLine(diagramStemStart, diagramStemEnd);
76         painter.drawLine(diagramHorizontalStart, diagramHorizontalEnd);
77
78         int currentX = diagramStemStart.x();
79         int currentY = diagramStemStart.y();
80
81         // Draws diagram's X-axel
82         for (int i = 0; i < 13; i++)
83         {
84             currentX += diagramGap;
85             painter.drawLine(currentX, currentY, currentX, currentY - 10);
86         }
87
88         currentX = diagramStemStart.x();
89         currentY = diagramStemStart.y();
90
91         // Draws diagram's Y-axel
92         for (int i = 0; i < 10; i++)
93         {
94             currentY -= diagramGap;
95             painter.drawLine(currentX, currentY, currentX+10, currentY);
96         }
97
98         painter.drawPolyline(points, 4);
99
100 }
101
102 /**
103   * Change the given speed and time to the point for the diagram.
104   * @param aSpeed is speed which need to change, aTime is time in seconds which need to change.
105   * @return point is calculated from aSpeed and aTime.
106   */
107 QPoint ResultDialog::changeMeasuresToDiagramPoint(int aSpeed, qreal aTime)
108 {
109     QPoint point;
110
111     int speedAsPixels;
112     int timeAsPixels;
113     speedAsPixels = 300*aSpeed/100;
114     timeAsPixels = 300*aTime/10;
115     point.setY(diagramStemStart.y()-speedAsPixels);
116     point.setX(diagramStemStart.x()+timeAsPixels);
117
118     return point;
119 }
120
121 /**
122   * Saves the given measures to array.
123   * @param pMeasures has information about acceleration.
124   */
125 void ResultDialog::saveMeasuresToArray(Measures *pMeasures)
126 {
127     timeArray[0] = pMeasures->getTime10kmh();
128     timeArray[1] = pMeasures->getTime20kmh();
129     timeArray[2] = pMeasures->getTime30kmh();
130     timeArray[3] = pMeasures->getTime40kmh();
131     timeArray[4] = pMeasures->getTime50kmh();
132     timeArray[5] = pMeasures->getTime60kmh();
133     timeArray[6] = pMeasures->getTime70kmh();
134     timeArray[7] = pMeasures->getTime80kmh();
135     timeArray[8] = pMeasures->getTime90kmh();
136     timeArray[9] = pMeasures->getTime100kmh();
137
138     for (int i = 0; i < 4; i++)
139     {
140         points[i] = changeMeasuresToDiagramPoint(speedArray[i], timeArray[i]);
141     }
142
143     QString time, timeInteger;
144     timeInteger.setNum(timeArray[3]);
145     time = "0 - 40 km/h: ";
146     time.append(timeInteger);
147     ui->labelResult40kmh->setText(time);
148
149     timeInteger.setNum(timeArray[2]);
150     time = "0 - 30 km/h: ";
151     time.append(timeInteger);
152     ui->labelResult30kmh->setText(time);
153
154     timeInteger.setNum(timeArray[1]);
155     time = "0 - 20 km/h: ";
156     time.append(timeInteger);
157     ui->labelResult20kmh->setText(time);
158
159     timeInteger.setNum(timeArray[0]);
160     time = "0 - 10 km/h: ";
161     time.append(timeInteger);
162     ui->labelResult10kmh->setText(time);
163
164    /* timeInteger.setNum(timeArray[5]);
165     time = "0 - 60 km/h: ";
166     time.append(timeInteger);
167     ui->labelResult_5->setText(time);
168
169     timeInteger.setNum(timeArray[4]);
170     time = "0 - 50 km/h: ";
171     time.append(timeInteger);
172     ui->labelResult_6->setText(time);
173
174     timeInteger.setNum(timeArray[3]);
175     time = "0 - 40 km/h: ";
176     time.append(timeInteger);
177     ui->labelResult_7->setText(time);*/
178
179     this->repaint();
180
181     for (int i = 0; i < 10; i++)
182     {
183         timeArray[i] = 0;
184     }
185 }
186
187 void ResultDialog::on_pushButtonSend_clicked()
188 {
189     emit sendresult();
190 }