0deeddcba40f7c266326870e95caf3f6c076f0a3
[speedfreak] / Client / carmainwindow.cpp
1 /*
2  * CarMainWindow main class
3  *
4  * @author     Toni Jussila <toni.jussila@fudeco.com>
5  * @author     Janne Änäkkälä <janne.anakkala@fudeco.com>
6  * @author     Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
7  * @author     Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
8  * @author     Rikhard Kuutti <rikhard.kuutti@fudeco.com>
9  * @author     Kai Rasilainen <kai.rasilainen@fudeco.com>
10  * @copyright  (c) 2010 Speed Freak team
11  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
12  */
13
14 #include "carmainwindow.h"
15 #include "math.h"
16
17 #define kAccelerometerSampleRate    50
18 #define kFilteringFactor            0.2
19 #define kSecondsInHour              3600
20
21 /**
22   *Constructor of this class.
23   *@param QWidget pointer to parent object. By default the value is NULL.
24   */
25 CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow)
26 {
27     ui->setupUi(this);
28     ui->tabWidget->setCurrentWidget(this->ui->StartTab);
29     result = new ResultDialog();
30     //measure = new MeasureDialog();
31     welcomeDialog = new WelcomeDialog();
32     welcomeDialog->show();
33
34     initComboBoxStartTabUnits();
35     initListViewStartTabAccelerationCategories();
36
37     myLogin = new LoginWindow(this);
38     myCategorylist = new CategoryList();
39     myHttpClient = new HttpClient(this);
40     myRegistration = new Registration(this);
41     connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(regUserToServer()));
42     connect(myLogin,SIGNAL(userNameChanged()),this,SLOT(userLogin()));
43     connect(myHttpClient->myXmlreader, SIGNAL(receivedCategoryList()), this, SLOT(setCategoryCompoBox()));
44     connect(myHttpClient->myXmlreader, SIGNAL(receivedTop10List()), this, SLOT(showTop10()));
45     myRoute = new RouteDialog( this);
46
47     //GPS
48     location = new Maemo5Location(this);
49     gpsData = new GPSData(location);
50     connect(location,SIGNAL(agnss()),this,SLOT(gpsStatus()));
51     gpsTime = new QDateTime();
52     gpsTimer = new QTimer();
53     connect(gpsTimer, SIGNAL(timeout()),this, SLOT(gpsTimerTimeout()));
54     gpsSpeedNow = 0.0;
55     gpsSpeedPrevious = 0.0;
56     gpsAcceleration = 0.0;
57     timeFromGps = 0.0; //Measure-tab view.
58     gpsSpeed = 0.0;
59
60     this->time = 0;
61     this->speed = 0;
62     counterForSaveResults = 0;
63     timer = new QTimer();
64
65     // Accelerometer
66     accelerometer = new Accelerometer();
67
68     reverseAccelerationFlag = false;
69     vehicleStartedMoving = false;
70     isNewRun = true;
71     isSetup = false;
72     stopTime = 0;
73     accelerationStartThreshold = 0.02;
74
75     accelerometerTimer = new QTimer(this);
76     connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
77     //accelerometerTimer->start(kAccelerometerSampleRate);
78
79     // Calculate
80     calculate = new Calculate();
81     connect(calculate, SIGNAL(checkPointReached()), this, SLOT(handleCheckPoint()));
82
83     resetAccelerometerMeasurements();
84
85     measures = new Measures();
86     this->initializeMeasures();
87
88     this->timer->setInterval(100);
89
90     connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
91     connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
92
93     ui->labelMeasureTabResult->hide();
94     ui->pushButtonShowResultDialog->setEnabled(false);
95     ui->pushButtonShowResultDialog->setEnabled(false);
96
97     this->setWindowTitle("Speed Freak");
98 }
99
100 /**
101   *Destructor of this class. Deletes all dynamic objects and sets them to NULL.
102   */
103 CarMainWindow::~CarMainWindow()
104 {
105     delete ui;
106     ui = NULL;
107     //delete result;
108     //delete measure;
109     delete myCategorylist;
110     myCategorylist = NULL;
111     delete welcomeDialog;
112     welcomeDialog = NULL;
113     delete myRoute;
114     myRoute = NULL;
115     delete gpsData;
116     gpsData = NULL;
117     delete gpsTime;
118     gpsTime = NULL;
119
120     //Route-tab view
121     gpsSpeedNow = 0.0;
122     gpsSpeedPrevious = 0.0;
123     gpsAcceleration = 0.0;
124     timeFromGps = 0.0;
125     gpsSpeed = 0.0;
126     gpsUpdateTime = 0;
127 }
128
129 /**
130   *This function is used to .
131   *@param
132   */
133 void CarMainWindow::changeEvent(QEvent *e)
134 {
135     QMainWindow::changeEvent(e);
136     switch (e->type()) {
137     case QEvent::LanguageChange:
138         ui->retranslateUi(this);
139         break;
140     default:
141         break;
142     }
143 }
144
145 /**
146   *This slot function is called when ever list view is update. Start-tab view.
147   */
148 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
149 {
150     QString str = index.data().toString();
151     QStringList list = str.split("-");
152     QStringList list2 = list[1].split(" ");
153
154     ui->lineEditStartTabMin->setText(list[0]);
155     ui->lineEditStartTabMax->setText(list2[0]);
156     updateComboBoxStartTabUnits(list2[1]);
157 }
158
159 /**
160   *This slot function is called when ever auto start button clicked. Start-tab view.
161   */
162 void CarMainWindow::on_autoStartButton_clicked()
163 {
164     initializeMeasures();
165     resetAccelerometerMeasurements();
166     ui->pushButtonSendResult->setEnabled(false);
167     ui->pushButtonShowResultDialog->setEnabled(false);
168     choice = ui->listViewStartTabAccelerationCategories->currentIndex();
169     choiceInt = choice.row();
170     qDebug() << choiceInt;
171     if (choiceInt == 0)
172     {
173         ui->labelMeasureTabHeader->setText("Accelerate to 40 km/h");
174         result->setDiagramGapStem(75);
175     }
176
177     else if (choiceInt == 1)
178     {
179         ui->labelMeasureTabHeader->setText("Accelerate to 100 km/h");
180         result->setDiagramGapStem(30);
181     }
182
183     else
184     {
185         ui->labelMeasureTabHeader->setText("Accelerate to 80 km/h");
186         result->setDiagramGapStem(37.5);
187     }
188     ui->labelMeasureTabResult->setText("");
189
190     this->accelerometerTimer->start(kAccelerometerSampleRate);
191     this->timer->start();
192     this->time = 0;
193     this->speed = 0;
194     ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
195 }
196
197 /**
198   *This slot function is called when ever list view is update. Start-tab view.
199   *@param QString unit.
200   */
201 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
202 {
203     ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
204 }
205
206 /**
207   *This function is used to init unit combobox. Start-tab view.
208   */
209 void CarMainWindow::initComboBoxStartTabUnits()
210 {
211     units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
212     ui->comboBoxStartTabUnits->addItems(units);
213 }
214
215 /**
216   *This function is used to set items to unit combobox. Start-tab view.
217   *@param QStringlist units
218   */
219 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
220 {
221     ui->comboBoxStartTabUnits->addItems(units);
222 }
223
224 /**
225   *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
226   */
227 void CarMainWindow::initListViewStartTabAccelerationCategories()
228 {
229     accelerationCategoriesStartTab << "0-40 km/h" << "0-100 km/h"; //<< "0-1/4 Mile" << "0-1/8 Mile" << "0-50 km" << "50-100 Mile" << "0-60 Mph" << "0-100 m" << "0-50 ft" << "0-50 yrd" << "0-500 in";
230     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
231     ui->listViewStartTabAccelerationCategories->setModel(model);
232 }
233
234 /**
235   *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
236   *@param QStringlist accelerationCategoriesStartTab
237   */
238 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
239 {
240     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
241     ui->listViewStartTabAccelerationCategories->setModel(model);
242 }
243
244 /**
245   *This function is used to set items to category combobox. Top-tab view.
246   *@param
247   */
248 void CarMainWindow::setCategoryCompoBox()
249 {
250     qDebug() << "_setCategoryCompoBox";
251     ui->comboBoxTopCategory->addItems(myHttpClient->myXmlreader->myCategoryList->getCategoryList());
252 }
253
254 /**
255   *This function prcesses UI updating after a new top10List has been received.
256   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
257   */
258 void CarMainWindow::showTop10()
259 {
260     int limitNr = 5;
261     setListViewTopList(recentCategory, limitNr);
262 }
263
264 /**
265   *This function is used to set items to labelTopList. Top-tab view.
266   *@param Category
267   *@param Size, number of results.
268   */
269 void CarMainWindow::setListViewTopList(QString category, int size)
270 {
271     qDebug() << "_setListViewTopList";
272     QString topList;
273     topList.append(myHttpClient->myXmlreader->myCategoryList->getTopList(category, size));
274     ui->labelTopList->setText(topList);
275 }
276
277 /**
278   *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
279   */
280 void CarMainWindow::openResultView()
281 {
282
283 }
284
285 /**
286   *This slot function is called when registrate button is clicked.
287   */
288 void CarMainWindow::on_registratePushButton_clicked()
289 {
290     myRegistration->show();
291 }
292
293 /**
294   *This slot function is called when ever refresh button clicked. Top-tab view.
295   */
296 void CarMainWindow::on_buttonTopRefresh_clicked()
297 {
298     myHttpClient->requestCategories();
299 }
300
301 /**
302   *This slot function is called when ever category combobox current index changed. Top-tab view.
303   *@param QString category.
304   *@todo Check where limitNr is taken, fixed or user input, see showTop10.
305   */
306 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
307 {
308     qDebug() << "_on_comboBoxTopCategory_currentIndexChanged: " << category;
309     recentCategory = category;      //for showTop10()
310     int limitNr = 5;
311     QString limit = QString::number(limitNr);
312     myHttpClient->requestTopList(category, limit);
313 }
314
315 /**
316   *This slot function is called when set/change user button is clicked.
317   */
318 void CarMainWindow::on_setUserPushButton_clicked()
319 {
320     myLogin->show();
321 }
322
323 /**
324   *@brief Just for development, for the real button is not shown until
325   *measurin started and there are results.
326   *@todo Implement with real code and yet leave sendXml in the bottom in use.
327   */
328 void CarMainWindow::on_manualStartButton_clicked()
329 {
330
331 }
332
333 /**
334   * This slot function is called when timer gives timeout signal. Checks current speed
335   * and stores times in measure class.
336   */
337 void CarMainWindow::after_timeout()
338 {
339     if ( gpsSpeed > 1.0 )
340     {
341         timeFromGps += 0.1;
342     }
343 }
344
345 /**
346   * Initializes measures class's member variables.
347   */
348 void CarMainWindow::initializeMeasures()
349 {
350     measures->setTime10kmh(0);
351     measures->setTime20kmh(0);
352     measures->setTime30kmh(0);
353     measures->setTime40kmh(0);
354     measures->setTime50kmh(0);
355     measures->setTime60kmh(0);
356     measures->setTime70kmh(0);
357     measures->setTime80kmh(0);
358     measures->setTime90kmh(0);
359     measures->setTime100kmh(0);
360 }
361
362 /**
363   * This slot function is called when Abort button is clicked.
364   */
365 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
366 {
367     ui->pushButtonSendResult->setEnabled(false);
368     ui->pushButtonShowResultDialog->setEnabled(false);
369     ui->labelMeasureTabResult->hide();
370     ui->labelMeasureTabTime->setText("");
371     ui->labelMeasureTabSpeed->setText("");
372     measures->setTime10kmh(0);
373     measures->setTime20kmh(0);
374     measures->setTime30kmh(0);
375     measures->setTime40kmh(0);
376     measures->setTime50kmh(0);
377     measures->setTime60kmh(0);
378     measures->setTime70kmh(0);
379     measures->setTime80kmh(0);
380     measures->setTime90kmh(0);
381     measures->setTime100kmh(0);
382     this->accelerometerTimer->stop();
383     this->timer->stop();
384     this->time = 0;
385     this->speed = 0;
386     ui->tabWidget->setCurrentWidget(this->ui->StartTab);
387     //this->close();
388
389     //GPS
390     gpsSpeed = 0.0;
391     timeFromGps = 0.0;
392 }
393
394 /**
395   *This slot function is called when pushButtonSendResult is clicked.
396   *@todo Use real category value.
397   */
398 void CarMainWindow::on_pushButtonSendResult_clicked()
399 {
400     myHttpClient->sendResultXml("acceleration-0-100");
401     ui->pushButtonSendResult->setEnabled(false);
402 }
403
404 void CarMainWindow::updateUserName()
405 {
406     QString newUserName;
407
408     newUserName = myLogin->getUserName();
409     ui->userNameLabel->setText( "User: " + newUserName);
410
411     if (newUserName.length())
412     {
413        ui->setUserPushButton->setText( "Change User");
414        this->setWindowTitle("Speed Freak - " + newUserName);
415     }
416     else
417     {
418         ui->setUserPushButton->setText( "Set User");
419         this->setWindowTitle("Speed Freak");
420     }
421 }
422
423 void CarMainWindow::regUserToServer()
424 {
425     myHttpClient->requestRegistration();
426 }
427
428
429 void CarMainWindow::on_drawRoutePushButton_clicked()
430 {
431     myRoute->show();
432 }
433
434 /**
435   * Opens result dialog when show result button is clicked.
436   * Sends measures as parameter to the resultdialogs saveMeasuresToArray-function.
437   */
438 void CarMainWindow::on_pushButtonShowResultDialog_clicked()
439 {
440     result->saveMeasuresToArray(measures);
441     this->result->show();
442 }
443
444 void CarMainWindow::userLogin()
445 {
446     myHttpClient->checkLogin();
447 }
448
449 /**
450   * Resets Accelerometer measurement variables
451   */
452 void CarMainWindow::resetAccelerometerMeasurements()
453 {
454     currentAcceleration = 0;
455     currentAccelerationString = "";
456     currentSpeed = "";
457     currentTime = 0;
458     distanceTraveled = "";
459     firstAcceleration = 0;
460     //horsepower = null;
461     isNewRun = true;
462     //lastScreenUpdateInSeconds = 0;
463     previousTime = 0;
464     reverseAccelerationFlag = false;
465     stopWatch.setHMS(0, 0, 0, 0);
466     //accelerometer->stop();
467     totalTime = "";
468     vehicleStartedMoving = false;
469     calculate->reset();
470 }
471
472 /**
473   * This function is called to handle checkpoints
474   *@param totalTime total time elapsed since starting measurements
475   *@param currentSpeed current speed of the device
476   */
477 void CarMainWindow::handleCheckPoint(double totalTime, double currentSpeed)
478 {
479     switch (counterForSaveResults)
480     {
481     case 0:
482         measures->setTime10kmh(totalTime);
483         break;
484
485     case 1:
486         measures->setTime20kmh(totalTime);
487         break;
488
489     case 2:
490         measures->setTime30kmh(totalTime);
491         break;
492
493     case 3:
494         measures->setTime40kmh(totalTime);
495         break;
496
497     case 4:
498         measures->setTime50kmh(totalTime);
499         break;
500
501     case 5:
502         measures->setTime60kmh(totalTime);
503         break;
504
505     case 6:
506         measures->setTime70kmh(totalTime);
507         break;
508
509     case 7:
510         measures->setTime80kmh(totalTime);
511         break;
512
513     case 8:
514         measures->setTime90kmh(totalTime);
515         break;
516
517     case 9:
518         measures->setTime100kmh(totalTime);
519         break;
520
521     default:
522         break;
523     }
524     counterForSaveResults++;
525
526     if (choiceInt == 0 && measures->getTime40kmh() != 0)
527     {
528         setTimeAxisGapAndShowResult(measures->getTime40kmh());
529         this->timer->stop();
530         this->accelerometerTimer->stop();
531         this->time = 0;
532         this->speed = 0;
533         counterForSaveResults = 0;
534     }
535
536     else if (choiceInt == 1 && measures->getTime100kmh() != 0)
537     {
538         setTimeAxisGapAndShowResult(measures->getTime100kmh());
539         this->timer->stop();
540         this->accelerometerTimer->stop();
541         this->time = 0;
542         this->speed = 0;
543         counterForSaveResults = 0;
544
545     }
546
547     else if (choiceInt != 1 && choiceInt != 0 && measures->getTime80kmh() != 0)
548     {
549         setTimeAxisGapAndShowResult(measures->getTime80kmh());
550         this->timer->stop();
551         this->accelerometerTimer->stop();
552         this->time = 0;
553         this->speed = 0;
554         counterForSaveResults = 0;
555     }
556
557     else
558     {
559
560     }
561 }
562
563 /**
564   *This function is called to read (and process) data from the accelerometer
565   */
566 void CarMainWindow::readAccelerometerData()
567 {
568     QString s;
569     double changeInAcceleration = 0;
570     qreal x, y, z;
571
572     accelerometer->getAcceleration(x, y, z);
573     accelerometer->smoothData(x, y, z);
574
575     // Apply calibration
576     x -= accelerometer->getCalibrationX();
577     y -= accelerometer->getCalibrationY();
578     z -= accelerometer->getCalibrationZ();
579
580     QString str = QString("acc x: " + QString::number(x) + "\n" +
581                           "acc y: " + QString::number(y) + "\n" +
582                           "acc z: " + QString::number(z) + "\n");
583
584     if (!vehicleStartedMoving)
585     {
586         if (isNewRun)
587         {
588             firstAcceleration = sqrt(x*x + y*y + z*z);
589             //firstAcceleration = y; // first read
590             isNewRun = false;
591         }
592     }
593
594     currentAcceleration = sqrt(x*x + y*y + z*z);
595     changeInAcceleration = (currentAcceleration - firstAcceleration); // firstAcceleration only gets set once
596
597     if (((fabs(changeInAcceleration) <= accelerationStartThreshold)
598                 && !vehicleStartedMoving))
599     {
600         return;
601     }
602
603     if (reverseAccelerationFlag)
604     {
605         // will be false until after 1st calculation
606         if ((changeInAcceleration <= 0))
607         {
608             // actually increasing here...
609             changeInAcceleration = fabs(changeInAcceleration);
610         }
611         else
612         {
613             // actually decreasing here...
614             changeInAcceleration = (changeInAcceleration * -1);
615         }
616     }
617     if (!vehicleStartedMoving)
618     {
619         if ((changeInAcceleration < 0))
620         {
621             // we started to move backwards first time through
622             reverseAccelerationFlag = true;
623             changeInAcceleration = fabs(changeInAcceleration);
624         }
625         vehicleStartedMoving = true;
626
627         stopWatch.setHMS(0, 0, 0, 0);
628         stopWatch.start();
629     }
630     //  keep the following line as close to the SetKinematicsProperties method as possible
631     currentTime = stopWatch.elapsed();
632     calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
633     previousTime = currentTime;
634
635     s.sprintf("%.2f", changeInAcceleration);
636     currentAccelerationString = s;
637
638     speed = 0.0;
639     speed = calculate->getCurrentSpeed();
640     speed = ((speed*1000)/kSecondsInHour);
641     s.sprintf("%.2f", speed);
642     currentSpeed = s;
643
644     s.sprintf("%.2f", calculate->getDistanceTraveled());
645     distanceTraveled = s;
646
647     // TODO
648     //distanceTraveled;
649     //horsepower;
650
651     time = calculate->getTotalTime();
652
653     s.sprintf("%.2f", time);
654     totalTime = s;
655
656     str.append("ca: " + currentAccelerationString + " G\n" );
657     str.append("cspeed: " + currentSpeed + " km/h \n" );
658     str.append("dist: " + distanceTraveled + " m \n" );
659     str.append("time: " + totalTime + " s \n" );
660
661     if ((stopTime > 0) && (previousTime >= stopTime))
662     {
663         // we want to end at a stopping point that the user chose
664         // output results
665         resetAccelerometerMeasurements();
666     }
667 }
668
669 /**
670   *This function is used to calibrate accelerometer
671   */
672 void CarMainWindow::calibrateAccelerometer()
673 {
674     resetAccelerometerMeasurements();
675     accelerometer->calibrate();
676 }
677
678 /**
679   *This slot function is called when GPS on checkbox state changed. Route-tab view.
680   *@param int GPSState
681   */
682 void CarMainWindow::on_gpsOnCheckBox_stateChanged(int GPSState)
683 {
684     //Stop polling GPS. Route-tab view.
685     if (GPSState == 0)
686     {
687         ui->labelRouteTabGPSStatus->setText("GPS status: GPS off");
688         location->stopPollingGPS();
689         gpsUpdateTime = 0;
690         gpsTimer->stop();
691     }
692     //Start polling GPS. Route-tab view.
693     else
694     {
695         ui->labelRouteTabGPSStatus->setText("GPS status: GPS on");
696         location->startPollingGPS();
697     }
698 }
699
700 /**
701   *This slot function is called when GPS status changed. Route- and measure-tab view.
702   */
703 void CarMainWindow::gpsStatus()
704 {
705     //IF GPS checkbox is ON
706     if (ui->gpsOnCheckBox->isChecked())
707     {
708         //If GPS find 4 satellite.
709         if (location->getSatellitesInUse() >= 4)
710         {
711             //Set gps status. Route-tab view.
712             ui->labelRouteTabGPSStatus->setText("GPS ready");
713
714             //Set time. Route-tab view.
715             gpsTime->setTime_t(location->getTime());
716             QString gpsDateNow = gpsTime->toString("dd.MM.yyyy hh:mm:ss");
717             ui->labelRouteTabGPSTime->setText("GPS time: " + gpsDateNow);
718
719             //Set latitude & longitude. Route-tab view.
720             ui->labelRouteTabLatitude->setText("Latitude: " + QString::number(location->getLatitude()));
721             ui->labelRouteTabLongitude->setText("Longitude: " + QString::number(location->getLongitude()));
722
723             //Set rec status. Route-tab view.
724             if (ui->startRecPushButton->text() == "Stop recording")
725             {
726                 ui->labelRouteTabRecStatus->setText("Recorded " + QString::number(gpsData->roundCounter) + " point");
727             }
728
729             //Get speed. Route- and Measure-tab view.
730             gpsSpeed = location->getSpeed();
731
732             //Set speed. Route-tab view.
733             ui->labelRouteTabSpeed->setText("Speed:" + QString::number(gpsSpeed) + "km/h +/-" + QString::number(location->getEps()) + "km/h");
734
735             //Measure-tab view.
736             if (gpsSpeed < 40.0)
737             {              
738                 ui->labelMeasureTabSpeed->setText(QString::number(gpsSpeed)); //Set speed. //Measure-tab view.
739                 ui->labelMeasureTabTime->setText(QString::number(timeFromGps)); //Set time. //Measure-tab view.
740             }
741             //Measure-tab view.
742             else
743             {
744                 timer->stop(); //Measure timer
745                 ui->labelMeasureTabResult->setText(QString::number(timeFromGps));
746                 ui->labelMeasureTabResult->show();
747                 ui->pushButtonShowResultDialog->setEnabled(true);
748                 ui->pushButtonShowResultDialog->setEnabled(true);
749             }
750         }
751
752         //If GPS find less than 4 satellite.
753         else
754         {
755             ui->labelRouteTabGPSStatus->setText("GPS status: Waiting for GPS");
756             gpsTimer->stop();
757         }
758
759         gpsUpdateTime = 0;
760         gpsTimer->start(10);
761     }
762 }
763
764 /**
765   *This slot function is called when gps timer timeout(10ms).
766   */
767 void CarMainWindow::gpsTimerTimeout()
768 {
769     int time1000ms;
770     time1000ms += 10;
771
772     //IF time is 1 second
773     if (time1000ms == 1000)
774     {
775         //Calculate acceleration 1/s
776         gpsSpeedPrevious = gpsSpeedNow; //Previous speed
777         gpsSpeedNow = (location->getSpeed())/3.6; //Speed now (m/s)
778         gpsAcceleration = (gpsSpeedNow - gpsSpeedPrevious)/1; //Calculate acceleration: speed now - previous speed / 1s.
779         //Set acceleration. Route-tab view.
780         ui->labelRouteTabAcceleration->setText("Acceleration: " + QString::number( gpsAcceleration ) + " m/s2");
781     }
782
783     gpsUpdateTime++;
784     //Set GPS update time. Route-tab view.
785     ui->labelRouteTabGPSUpdateTime->setText("GPS update time: " + QString::number(gpsUpdateTime) + " ms");
786 }
787
788 /**
789   *This slot function is called when start rec push button clicked. Route-tab view.
790   */
791 void CarMainWindow::on_startRecPushButton_clicked()
792 {
793     //Start route recording.
794     if (ui->startRecPushButton->text() == "Start recording")
795     {
796         ui->startRecPushButton->setText("Stop recording");
797         ui->labelRouteTabRecStatus->setText("Recording started");
798         gpsData->startRouteRecording(ui->labelRouteTabGPSTime->text());
799     }
800
801     //Stop route recording.
802     else if (ui->startRecPushButton->text() == "Stop recording")
803     {
804         ui->startRecPushButton->setText("Start recording");
805         ui->labelRouteTabRecStatus->setText("Recording stopped");
806         gpsData->stopRouteRecording(ui->labelRouteTabGPSTime->text());
807     }
808 }
809
810 /**
811   *Sets time axis right way in result dialog and shows target speed result.
812   *@param double pTime is the target speed result time which is shown to the user.
813   */
814 void CarMainWindow::setTimeAxisGapAndShowResult(double pTime)
815 {
816     ui->pushButtonShowResultDialog->setEnabled(true);
817     ui->pushButtonSendResult->setEnabled(true);
818     QString timeInteger;
819     timeInteger.setNum(pTime);
820     ui->labelMeasureTabResult->show();
821     ui->labelMeasureTabResult->setText(timeInteger);
822
823     if (floor(pTime) <= 5)
824     {
825         result->setDiagramGapHorizontal(80);
826     }
827
828     else if (floor(pTime) <= 10)
829     {
830         result->setDiagramGapHorizontal(40);
831     }
832
833     else
834     {
835         result->setDiagramGapHorizontal(20);
836     }
837 }