ca0cb8ea43863e31629bbf9b3659f05317d0f212
[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   *@todo Check setDiagramGapStem(100) <- (choiceInt == 2)
162   */
163 void CarMainWindow::on_autoStartButton_clicked()
164 {
165     initializeMeasures();
166     resetAccelerometerMeasurements();
167     ui->pushButtonSendResult->setEnabled(false);
168     ui->pushButtonShowResultDialog->setEnabled(false);
169     choice = ui->listViewStartTabAccelerationCategories->currentIndex();
170     choiceInt = choice.row();
171     //qDebug() << "choiceInt" << choiceInt << " " << catList.at(choiceInt);
172     if (choiceInt == 0)
173     {
174         ui->labelMeasureTabHeader->setText("Accelerate to 40 km/h");
175         result->setDiagramGapStem(75);
176     }
177     else if (choiceInt == 1)
178     {
179         ui->labelMeasureTabHeader->setText("Accelerate to 100 km/h");
180         result->setDiagramGapStem(30);
181     }   
182     else if (choiceInt == 2)
183     {
184         ui->labelMeasureTabHeader->setText("Accelerate to 10 km/h");
185         result->setDiagramGapStem(100);
186     }
187     else
188     {
189         ui->labelMeasureTabHeader->setText("Accelerate to 80 km/h");
190         result->setDiagramGapStem(37.5);
191     }
192     ui->labelMeasureTabResult->setText("");
193
194     this->accelerometerTimer->start(kAccelerometerSampleRate);
195     this->timer->start();
196     this->time = 0;
197     this->speed = 0;
198     ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
199 }
200
201 /**
202   *This slot function is called when ever list view is update. Start-tab view.
203   *@param QString unit.
204   */
205 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
206 {
207     ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
208 }
209
210 /**
211   *This function is used to init unit combobox. Start-tab view.
212   */
213 void CarMainWindow::initComboBoxStartTabUnits()
214 {
215     units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
216     ui->comboBoxStartTabUnits->addItems(units);
217 }
218
219 /**
220   *This function is used to set items to unit combobox. Start-tab view.
221   *@param QStringlist units
222   */
223 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
224 {
225     ui->comboBoxStartTabUnits->addItems(units);
226 }
227
228 /**
229   *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
230   *@todo During development categories index values that are used for measuring are hardcoded
231   *@todo and accelerationCategoriesStartTab and catList are used instead of using
232   *@todo CategoryList::categoryList and CategoryList::cats.
233   */
234 void CarMainWindow::initListViewStartTabAccelerationCategories()
235 {
236     //Connect the user`s choice fron GUI to a correct variable name
237     catList.insert(0,"acceleration-0-40");
238     catList.insert(1,"acceleration-0-100");
239     catList.insert(2,"acceleration-0-10");
240
241     accelerationCategoriesStartTab << "0-40 km/h" << "0-100 km/h" << "0-10 km/h";
242     //<< "0-1/4 Mile" << "0-1/8 Mile" << "50-100 Mile" << "0-60 Mph" << "0-100 m" << "0-50 ft" << "0-50 yrd" << "0-500 in";
243     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
244     ui->listViewStartTabAccelerationCategories->setModel(model);
245 }
246
247 /**
248   *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
249   *@param QStringlist accelerationCategoriesStartTab
250   */
251 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
252 {
253     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
254     ui->listViewStartTabAccelerationCategories->setModel(model);
255 }
256
257 void CarMainWindow::setLabelInfoToUser(QString infoText)
258 {
259     ui->labelInfoToUser->setText(infoText);
260 }
261
262 /**
263   *This function is used to set items to category combobox. Top-tab view.
264   *@param
265   */
266 void CarMainWindow::setCategoryCompoBox()
267 {
268     qDebug() << "_setCategoryCompoBox";
269     ui->comboBoxTopCategory->addItems(myHttpClient->myXmlreader->myCategoryList->getCategoryList());
270 }
271
272 /**
273   *This function prcesses UI updating after a new top10List has been received.
274   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
275   */
276 void CarMainWindow::showTop10()
277 {
278     int limitNr = 5;
279     setListViewTopList(recentCategory, limitNr);
280 }
281
282 /**
283   *This function is used to set items to labelTopList. Top-tab view.
284   *@param Category
285   *@param Size, number of results.
286   */
287 void CarMainWindow::setListViewTopList(QString category, int size)
288 {
289     qDebug() << "_setListViewTopList";
290     QString topList;
291     topList.append(myHttpClient->myXmlreader->myCategoryList->getTopList(category, size));
292     ui->labelTopList->setText(topList);
293 }
294
295 /**
296   *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
297   */
298 void CarMainWindow::openResultView()
299 {
300
301 }
302
303 /**
304   *This slot function is called when registrate button is clicked.
305   */
306 void CarMainWindow::on_registratePushButton_clicked()
307 {
308     myRegistration->show();
309 }
310
311 /**
312   *This slot function is called when ever refresh button clicked. Top-tab view.
313   */
314 void CarMainWindow::on_buttonTopRefresh_clicked()
315 {
316     myHttpClient->requestCategories();
317 }
318
319 /**
320   *This slot function is called when ever category combobox current index changed. Top-tab view.
321   *@param QString category.
322   *@todo Check where limitNr is taken, fixed or user input, see showTop10.
323   */
324 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
325 {
326     qDebug() << "_on_comboBoxTopCategory_currentIndexChanged: " << category;
327     recentCategory = category;      //for showTop10()
328     int limitNr = 5;
329     QString limit = QString::number(limitNr);
330     myHttpClient->requestTopList(category, limit);
331 }
332
333 /**
334   *This slot function is called when set/change user button is clicked.
335   */
336 void CarMainWindow::on_setUserPushButton_clicked()
337 {
338     myLogin->show();
339 }
340
341 /**
342   *@brief Just for development, for the real button is not shown until
343   *measurin started and there are results.
344   *@todo Implement with real code and yet leave sendXml in the bottom in use.
345   */
346 void CarMainWindow::on_manualStartButton_clicked()
347 {
348
349 }
350
351 /**
352   * This slot function is called when timer gives timeout signal. Checks current speed
353   * and stores times in measure class.
354   */
355 void CarMainWindow::after_timeout()
356 {
357     if ( gpsSpeed > 1.0 )
358     {
359         timeFromGps += 0.1;
360     }
361 }
362
363 /**
364   * Initializes measures class's member variables.
365   */
366 void CarMainWindow::initializeMeasures()
367 {
368     measures->setTime10kmh(0);
369     measures->setTime20kmh(0);
370     measures->setTime30kmh(0);
371     measures->setTime40kmh(0);
372     measures->setTime50kmh(0);
373     measures->setTime60kmh(0);
374     measures->setTime70kmh(0);
375     measures->setTime80kmh(0);
376     measures->setTime90kmh(0);
377     measures->setTime100kmh(0);
378 }
379
380 /**
381   * This slot function is called when Abort button is clicked.
382   */
383 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
384 {
385     ui->pushButtonSendResult->setEnabled(false);
386     ui->pushButtonShowResultDialog->setEnabled(false);
387     ui->labelMeasureTabResult->hide();
388     ui->labelMeasureTabTime->setText("");
389     ui->labelMeasureTabSpeed->setText("");
390     measures->setTime10kmh(0);
391     measures->setTime20kmh(0);
392     measures->setTime30kmh(0);
393     measures->setTime40kmh(0);
394     measures->setTime50kmh(0);
395     measures->setTime60kmh(0);
396     measures->setTime70kmh(0);
397     measures->setTime80kmh(0);
398     measures->setTime90kmh(0);
399     measures->setTime100kmh(0);
400     this->accelerometerTimer->stop();
401     this->timer->stop();
402     this->time = 0;
403     this->speed = 0;
404     ui->tabWidget->setCurrentWidget(this->ui->StartTab);
405     //this->close();
406
407     //GPS
408     gpsSpeed = 0.0;
409     timeFromGps = 0.0;
410 }
411
412 /**
413   *This slot function is called when pushButtonSendResult is clicked.
414   *@todo Use real category value.
415   */
416 void CarMainWindow::on_pushButtonSendResult_clicked()
417 {
418     //Pick up relevant category name and pass it to the server
419     myHttpClient->sendResultXml(catList.at(choiceInt));
420     ui->pushButtonSendResult->setEnabled(false);
421 }
422
423 void CarMainWindow::updateUserName()
424 {
425     QString newUserName;
426
427     newUserName = myLogin->getUserName();
428     ui->userNameLabel->setText( "User: " + newUserName);
429
430     if (newUserName.length())
431     {
432        ui->setUserPushButton->setText( "Change User");
433        this->setWindowTitle("Speed Freak - " + newUserName);
434     }
435     else
436     {
437         ui->setUserPushButton->setText( "Set User");
438         this->setWindowTitle("Speed Freak");
439     }
440 }
441
442 void CarMainWindow::regUserToServer()
443 {
444     myHttpClient->requestRegistration();
445 }
446
447
448 void CarMainWindow::on_drawRoutePushButton_clicked()
449 {
450     QString routeFile = QString("route.txt");
451     if (myRoute->readRouteFromFile( routeFile) == true)
452     {
453         myRoute->show();
454     }
455 }
456
457 /**
458   * Opens result dialog when show result button is clicked.
459   * Sends measures as parameter to the resultdialogs saveMeasuresToArray-function.
460   */
461 void CarMainWindow::on_pushButtonShowResultDialog_clicked()
462 {
463     result->saveMeasuresToArray(measures);
464     this->result->show();
465 }
466
467 void CarMainWindow::userLogin()
468 {
469     myHttpClient->checkLogin();
470 }
471
472 /**
473   * Resets Accelerometer measurement variables
474   */
475 void CarMainWindow::resetAccelerometerMeasurements()
476 {
477     currentAcceleration = 0;
478     currentAccelerationString = "";
479     currentSpeed = "";
480     currentTime = 0;
481     distanceTraveled = "";
482     firstAcceleration = 0;
483     //horsepower = null;
484     isNewRun = true;
485     //lastScreenUpdateInSeconds = 0;
486     previousTime = 0;
487     reverseAccelerationFlag = false;
488     stopWatch.setHMS(0, 0, 0, 0);
489     //accelerometer->stop();
490     totalTime = "";
491     vehicleStartedMoving = false;
492     calculate->reset();
493 }
494
495 /**
496   * This function is called to handle checkpoints
497   *@param totalTime total time elapsed since starting measurements
498   *@param currentSpeed current speed of the device
499   */
500 void CarMainWindow::handleCheckPoint(double totalTime, double currentSpeed)
501 {
502     switch (counterForSaveResults)
503     {
504     case 0:
505         measures->setTime10kmh(totalTime);
506         break;
507
508     case 1:
509         measures->setTime20kmh(totalTime);
510         break;
511
512     case 2:
513         measures->setTime30kmh(totalTime);
514         break;
515
516     case 3:
517         measures->setTime40kmh(totalTime);
518         break;
519
520     case 4:
521         measures->setTime50kmh(totalTime);
522         break;
523
524     case 5:
525         measures->setTime60kmh(totalTime);
526         break;
527
528     case 6:
529         measures->setTime70kmh(totalTime);
530         break;
531
532     case 7:
533         measures->setTime80kmh(totalTime);
534         break;
535
536     case 8:
537         measures->setTime90kmh(totalTime);
538         break;
539
540     case 9:
541         measures->setTime100kmh(totalTime);
542         break;
543
544     default:
545         break;
546     }
547     counterForSaveResults++;
548
549     if (choiceInt == 0 && measures->getTime40kmh() != 0)
550     {
551         setTimeAxisGapAndShowResult(measures->getTime40kmh());
552         this->timer->stop();
553         this->accelerometerTimer->stop();
554         this->time = 0;
555         this->speed = 0;
556         counterForSaveResults = 0;
557     }
558     else if (choiceInt == 1 && measures->getTime100kmh() != 0)
559     {
560         setTimeAxisGapAndShowResult(measures->getTime100kmh());
561         this->timer->stop();
562         this->accelerometerTimer->stop();
563         this->time = 0;
564         this->speed = 0;
565         counterForSaveResults = 0;
566     }
567     else if (choiceInt == 2 && measures->getTime10kmh() != 0)
568     {
569         setTimeAxisGapAndShowResult(measures->getTime10kmh());
570         this->timer->stop();
571         this->accelerometerTimer->stop();
572         this->time = 0;
573         this->speed = 0;
574         counterForSaveResults = 0;
575     }
576     else if (choiceInt != 1 && choiceInt != 0 && measures->getTime80kmh() != 0)
577     {
578         setTimeAxisGapAndShowResult(measures->getTime80kmh());
579         this->timer->stop();
580         this->accelerometerTimer->stop();
581         this->time = 0;
582         this->speed = 0;
583         counterForSaveResults = 0;
584     }
585     else
586     {
587         qDebug() << "something wrong in handleCheckPoint()";
588     }
589 }
590
591 /**
592   *This function is called to read (and process) data from the accelerometer
593   */
594 void CarMainWindow::readAccelerometerData()
595 {
596     QString s;
597     double changeInAcceleration = 0;
598     qreal x, y, z;
599
600     accelerometer->getAcceleration(x, y, z);
601     accelerometer->smoothData(x, y, z);
602
603     // Apply calibration
604     x -= accelerometer->getCalibrationX();
605     y -= accelerometer->getCalibrationY();
606     z -= accelerometer->getCalibrationZ();
607
608     QString str = QString("acc x: " + QString::number(x) + "\n" +
609                           "acc y: " + QString::number(y) + "\n" +
610                           "acc z: " + QString::number(z) + "\n");
611
612     if (!vehicleStartedMoving)
613     {
614         if (isNewRun)
615         {
616             firstAcceleration = sqrt(x*x + y*y + z*z);
617             //firstAcceleration = y; // first read
618             isNewRun = false;
619         }
620     }
621
622     currentAcceleration = sqrt(x*x + y*y + z*z);
623     changeInAcceleration = (currentAcceleration - firstAcceleration); // firstAcceleration only gets set once
624
625     if (((fabs(changeInAcceleration) <= accelerationStartThreshold)
626                 && !vehicleStartedMoving))
627     {
628         return;
629     }
630
631     if (reverseAccelerationFlag)
632     {
633         // will be false until after 1st calculation
634         if ((changeInAcceleration <= 0))
635         {
636             // actually increasing here...
637             changeInAcceleration = fabs(changeInAcceleration);
638         }
639         else
640         {
641             // actually decreasing here...
642             changeInAcceleration = (changeInAcceleration * -1);
643         }
644     }
645     if (!vehicleStartedMoving)
646     {
647         if ((changeInAcceleration < 0))
648         {
649             // we started to move backwards first time through
650             reverseAccelerationFlag = true;
651             changeInAcceleration = fabs(changeInAcceleration);
652         }
653         vehicleStartedMoving = true;
654
655         stopWatch.setHMS(0, 0, 0, 0);
656         stopWatch.start();
657     }
658     //  keep the following line as close to the SetKinematicsProperties method as possible
659     currentTime = stopWatch.elapsed();
660     calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
661     previousTime = currentTime;
662
663     s.sprintf("%.2f", changeInAcceleration);
664     currentAccelerationString = s;
665
666     speed = 0.0;
667     speed = calculate->getCurrentSpeed();
668     speed = ((speed*1000)/kSecondsInHour);
669     s.sprintf("%.2f", speed);
670     currentSpeed = s;
671
672     s.sprintf("%.2f", calculate->getDistanceTraveled());
673     distanceTraveled = s;
674
675     // TODO
676     //distanceTraveled;
677     //horsepower;
678
679     time = calculate->getTotalTime();
680
681     s.sprintf("%.2f", time);
682     totalTime = s;
683
684     str.append("ca: " + currentAccelerationString + " G\n" );
685     str.append("cspeed: " + currentSpeed + " km/h \n" );
686     str.append("dist: " + distanceTraveled + " m \n" );
687     str.append("time: " + totalTime + " s \n" );
688
689     if ((stopTime > 0) && (previousTime >= stopTime))
690     {
691         // we want to end at a stopping point that the user chose
692         // output results
693         resetAccelerometerMeasurements();
694     }
695 }
696
697 /**
698   *This function is used to calibrate accelerometer
699   */
700 void CarMainWindow::calibrateAccelerometer()
701 {
702     resetAccelerometerMeasurements();
703     accelerometer->calibrate();
704 }
705
706 /**
707   *This slot function is called when GPS on checkbox state changed. Route-tab view.
708   *@param int GPSState
709   */
710 void CarMainWindow::on_gpsOnCheckBox_stateChanged(int GPSState)
711 {
712     //Stop polling GPS. Route-tab view.
713     if (GPSState == 0)
714     {
715         ui->labelRouteTabGPSStatus->setText("GPS status: GPS off");
716         location->stopPollingGPS();
717         gpsUpdateTime = 0;
718         gpsTimer->stop();
719     }
720     //Start polling GPS. Route-tab view.
721     else
722     {
723         ui->labelRouteTabGPSStatus->setText("GPS status: GPS on");
724         location->startPollingGPS();
725     }
726 }
727
728 /**
729   *This slot function is called when GPS status changed. Route- and measure-tab view.
730   */
731 void CarMainWindow::gpsStatus()
732 {
733     //IF GPS checkbox is ON
734     if (ui->gpsOnCheckBox->isChecked())
735     {
736         //If GPS find 4 satellite.
737         if (location->getSatellitesInUse() >= 4)
738         {
739             //Set gps status. Route-tab view.
740             ui->labelRouteTabGPSStatus->setText("GPS ready");
741
742             //Set time. Route-tab view.
743             gpsTime->setTime_t(location->getTime());
744             QString gpsDateNow = gpsTime->toString("dd.MM.yyyy hh:mm:ss");
745             ui->labelRouteTabGPSTime->setText("GPS time: " + gpsDateNow);
746
747             //Set latitude & longitude. Route-tab view.
748             ui->labelRouteTabLatitude->setText("Latitude: " + QString::number(location->getLatitude()));
749             ui->labelRouteTabLongitude->setText("Longitude: " + QString::number(location->getLongitude()));
750
751             //Set rec status. Route-tab view.
752             if (ui->startRecPushButton->text() == "Stop recording")
753             {
754                 ui->labelRouteTabRecStatus->setText("Recorded " + QString::number(gpsData->roundCounter) + " point");
755             }
756
757             //Get speed. Route- and Measure-tab view.
758             gpsSpeed = location->getSpeed();
759
760             //Set speed. Route-tab view.
761             ui->labelRouteTabSpeed->setText("Speed:" + QString::number(gpsSpeed) + "km/h +/-" + QString::number(location->getEps()) + "km/h");
762
763             //Measure-tab view.
764             if (gpsSpeed < 40.0)
765             {              
766                 ui->labelMeasureTabSpeed->setText(QString::number(gpsSpeed)); //Set speed. //Measure-tab view.
767                 ui->labelMeasureTabTime->setText(QString::number(timeFromGps)); //Set time. //Measure-tab view.
768             }
769             //Measure-tab view.
770             else
771             {
772                 timer->stop(); //Measure timer
773                 ui->labelMeasureTabResult->setText(QString::number(timeFromGps));
774                 ui->labelMeasureTabResult->show();
775                 ui->pushButtonShowResultDialog->setEnabled(true);
776                 ui->pushButtonShowResultDialog->setEnabled(true);
777             }
778         }
779
780         //If GPS find less than 4 satellite.
781         else
782         {
783             ui->labelRouteTabGPSStatus->setText("GPS status: Waiting for GPS");
784             gpsTimer->stop();
785         }
786
787         gpsUpdateTime = 0;
788         gpsTimer->start(10);
789     }
790 }
791
792 /**
793   *This slot function is called when gps timer timeout(10ms).
794   */
795 void CarMainWindow::gpsTimerTimeout()
796 {
797     int time1000ms;
798     time1000ms += 10;
799
800     //IF time is 1 second
801     if (time1000ms == 1000)
802     {
803         //Calculate acceleration 1/s
804         gpsSpeedPrevious = gpsSpeedNow; //Previous speed
805         gpsSpeedNow = (location->getSpeed())/3.6; //Speed now (m/s)
806         gpsAcceleration = (gpsSpeedNow - gpsSpeedPrevious)/1; //Calculate acceleration: speed now - previous speed / 1s.
807         //Set acceleration. Route-tab view.
808         ui->labelRouteTabAcceleration->setText("Acceleration: " + QString::number( gpsAcceleration ) + " m/s2");
809     }
810
811     gpsUpdateTime++;
812     //Set GPS update time. Route-tab view.
813     ui->labelRouteTabGPSUpdateTime->setText("GPS update time: " + QString::number(gpsUpdateTime) + " ms");
814 }
815
816 /**
817   *This slot function is called when start rec push button clicked. Route-tab view.
818   */
819 void CarMainWindow::on_startRecPushButton_clicked()
820 {
821     //Start route recording.
822     if (ui->startRecPushButton->text() == "Start recording")
823     {
824         ui->startRecPushButton->setText("Stop recording");
825         ui->labelRouteTabRecStatus->setText("Recording started");
826         gpsData->startRouteRecording(ui->labelRouteTabGPSTime->text());
827     }
828
829     //Stop route recording.
830     else if (ui->startRecPushButton->text() == "Stop recording")
831     {
832         ui->startRecPushButton->setText("Start recording");
833         ui->labelRouteTabRecStatus->setText("Recording stopped");
834         gpsData->stopRouteRecording(ui->labelRouteTabGPSTime->text());
835     }
836 }
837
838 /**
839   *Sets time axis right way in result dialog and shows target speed result.
840   *@param double pTime is the target speed result time which is shown to the user.
841   */
842 void CarMainWindow::setTimeAxisGapAndShowResult(double pTime)
843 {
844     ui->pushButtonShowResultDialog->setEnabled(true);
845     ui->pushButtonSendResult->setEnabled(true);
846     QString timeInteger;
847     timeInteger.setNum(pTime);
848     ui->labelMeasureTabResult->show();
849     ui->labelMeasureTabResult->setText(timeInteger);
850
851     if (floor(pTime) <= 5)
852     {
853         result->setDiagramGapHorizontal(80);
854     }
855
856     else if (floor(pTime) <= 10)
857     {
858         result->setDiagramGapHorizontal(40);
859     }
860
861     else
862     {
863         result->setDiagramGapHorizontal(20);
864     }
865 }