2 * CarMainWindow main class
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
14 #include "carmainwindow.h"
17 #define kAccelerometerSampleRate 40
18 #define kFilteringFactor 0.1
19 #define kSecondsInHour 3600
22 *Constructor of this class.
23 *@param QWidget pointer to parent object. By default the value is NULL.
25 CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow)
28 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
30 //Disable start buttons before calibration
31 ui->autoStartButton->setEnabled(false);
32 ui->manualStartButton->setEnabled(false);
34 result = new ResultDialog();
35 //measure = new MeasureDialog();
36 welcomeDialog = new WelcomeDialog();
37 welcomeDialog->show();
39 initComboBoxStartTabUnits();
40 initListViewStartTabAccelerationCategories();
42 myLogin = new LoginWindow(this);
43 myCategorylist = new CategoryList();
44 myHttpClient = new HttpClient(this);
45 myRegistration = new Registration(this);
46 connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(regUserToServer()));
47 connect(myLogin,SIGNAL(userNameChanged()),this,SLOT(userLogin()));
48 connect(myHttpClient->myXmlreader, SIGNAL(receivedCategoryList()), this, SLOT(setCategoryCompoBox()));
49 connect(myHttpClient->myXmlreader, SIGNAL(receivedTop10List()), this, SLOT(showTop10()));
50 myRoute = new RouteDialog( this);
53 location = new Maemo5Location(this);
54 gpsData = new GPSData(location);
55 connect(location,SIGNAL(agnss()),this,SLOT(gpsStatus()));
56 gpsTime = new QDateTime();
57 gpsTimer = new QTimer();
58 connect(gpsTimer, SIGNAL(timeout()),this, SLOT(gpsTimerTimeout()));
60 gpsSpeedPrevious = 0.0;
61 gpsAcceleration = 0.0;
62 timeFromGps = 0.0; //Measure-tab view.
67 counterForSaveResults = 0;
71 accelerometer = new Accelerometer();
73 reverseAccelerationFlag = false;
74 vehicleStartedMoving = false;
78 accelerationStartThreshold = 0.1;
80 accelerometerTimer = new QTimer(this);
81 connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
82 //accelerometerTimer->start(kAccelerometerSampleRate);
85 calculate = new Calculate();
86 connect(calculate, SIGNAL(checkPointReached()), this, SLOT(handleCheckPoint()));
88 resetAccelerometerMeasurements();
90 measures = new Measures();
91 this->initializeMeasures();
93 this->timer->setInterval(100);
95 connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
96 connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
98 ui->labelMeasureTabResult->hide();
99 ui->pushButtonShowResultDialog->setEnabled(false);
100 ui->pushButtonShowResultDialog->setEnabled(false);
102 this->setWindowTitle("Speed Freak");
106 *Destructor of this class. Deletes all dynamic objects and sets them to NULL.
108 CarMainWindow::~CarMainWindow()
114 delete myCategorylist;
115 myCategorylist = NULL;
116 delete welcomeDialog;
117 welcomeDialog = NULL;
127 gpsSpeedPrevious = 0.0;
128 gpsAcceleration = 0.0;
135 *This function is used to .
138 void CarMainWindow::changeEvent(QEvent *e)
140 QMainWindow::changeEvent(e);
142 case QEvent::LanguageChange:
143 ui->retranslateUi(this);
151 *This slot function is called when ever list view is update. Start-tab view.
153 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
155 QString str = index.data().toString();
156 QStringList list = str.split("-");
157 QStringList list2 = list[1].split(" ");
159 ui->lineEditStartTabMin->setText(list[0]);
160 ui->lineEditStartTabMax->setText(list2[0]);
161 updateComboBoxStartTabUnits(list2[1]);
165 *This slot function is called when ever auto start button clicked. Start-tab view.
166 *@todo Check setDiagramGapStem(100) <- (choiceInt == 2)
168 void CarMainWindow::on_autoStartButton_clicked()
170 initializeMeasures();
171 resetAccelerometerMeasurements();
172 ui->pushButtonSendResult->setEnabled(false);
173 ui->pushButtonShowResultDialog->setEnabled(false);
174 choice = ui->listViewStartTabAccelerationCategories->currentIndex();
175 choiceInt = choice.row();
176 //qDebug() << "choiceInt" << choiceInt << " " << catList.at(choiceInt);
179 ui->labelMeasureTabHeader->setText("Accelerate to 40 km/h");
180 result->setDiagramGapStem(75);
182 else if (choiceInt == 1)
184 ui->labelMeasureTabHeader->setText("Accelerate to 100 km/h");
185 result->setDiagramGapStem(30);
187 else if (choiceInt == 2)
189 ui->labelMeasureTabHeader->setText("Accelerate to 10 km/h");
190 result->setDiagramGapStem(100);
194 ui->labelMeasureTabHeader->setText("Accelerate to 80 km/h");
195 result->setDiagramGapStem(37.5);
197 ui->labelMeasureTabResult->setText("");
199 this->accelerometerTimer->start(kAccelerometerSampleRate);
200 this->timer->start();
203 ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
207 *This slot function is called when ever list view is update. Start-tab view.
208 *@param QString unit.
210 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
212 ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
216 *This function is used to init unit combobox. Start-tab view.
218 void CarMainWindow::initComboBoxStartTabUnits()
220 units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
221 ui->comboBoxStartTabUnits->addItems(units);
225 *This function is used to set items to unit combobox. Start-tab view.
226 *@param QStringlist units
228 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
230 ui->comboBoxStartTabUnits->addItems(units);
234 *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
235 *@todo During development categories index values that are used for measuring are hardcoded
236 *@todo and accelerationCategoriesStartTab and catList are used instead of using
237 *@todo CategoryList::categoryList and CategoryList::cats.
239 void CarMainWindow::initListViewStartTabAccelerationCategories()
241 //Connect the user`s choice fron GUI to a correct variable name
242 catList.insert(0,"acceleration-0-40");
243 catList.insert(1,"acceleration-0-100");
244 catList.insert(2,"acceleration-0-10");
246 accelerationCategoriesStartTab << "0-40 km/h" << "0-100 km/h" << "0-10 km/h";
247 //<< "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";
248 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
249 ui->listViewStartTabAccelerationCategories->setModel(model);
253 *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
254 *@param QStringlist accelerationCategoriesStartTab
256 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
258 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
259 ui->listViewStartTabAccelerationCategories->setModel(model);
262 void CarMainWindow::setLabelInfoToUser(QString infoText)
264 ui->labelInfoToUser->setText(infoText);
268 *This function is used to set items to category combobox. Top-tab view.
271 void CarMainWindow::setCategoryCompoBox()
273 qDebug() << "_setCategoryCompoBox";
274 ui->comboBoxTopCategory->addItems(myHttpClient->myXmlreader->myCategoryList->getCategoryList());
278 *This function prcesses UI updating after a new top10List has been received.
279 *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
281 void CarMainWindow::showTop10()
284 setListViewTopList(recentCategory, limitNr);
288 *This function is used to set items to labelTopList. Top-tab view.
290 *@param Size, number of results.
292 void CarMainWindow::setListViewTopList(QString category, int size)
294 qDebug() << "_setListViewTopList";
296 topList.append(myHttpClient->myXmlreader->myCategoryList->getTopList(category, size));
297 ui->labelTopList->setText(topList);
301 *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
303 void CarMainWindow::openResultView()
309 *This slot function is called when registrate button is clicked.
311 void CarMainWindow::on_registratePushButton_clicked()
313 myRegistration->show();
317 *This slot function is called when ever refresh button clicked. Top-tab view.
319 void CarMainWindow::on_buttonTopRefresh_clicked()
321 myHttpClient->requestCategories();
325 *This slot function is called when ever category combobox current index changed. Top-tab view.
326 *@param QString category.
327 *@todo Check where limitNr is taken, fixed or user input, see showTop10.
329 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
331 qDebug() << "_on_comboBoxTopCategory_currentIndexChanged: " << category;
332 recentCategory = category; //for showTop10()
334 QString limit = QString::number(limitNr);
335 myHttpClient->requestTopList(category, limit);
339 *This slot function is called when set/change user button is clicked.
341 void CarMainWindow::on_setUserPushButton_clicked()
347 *@brief Just for development, for the real button is not shown until
348 *measurin started and there are results.
349 *@todo Implement with real code and yet leave sendXml in the bottom in use.
351 void CarMainWindow::on_manualStartButton_clicked()
357 * This slot function is called when timer gives timeout signal. Checks current speed
358 * and stores times in measure class.
360 void CarMainWindow::after_timeout()
362 if ( gpsSpeed > 1.0 )
369 * Initializes measures class's member variables.
371 void CarMainWindow::initializeMeasures()
373 measures->setTime10kmh(0);
374 measures->setTime20kmh(0);
375 measures->setTime30kmh(0);
376 measures->setTime40kmh(0);
377 measures->setTime50kmh(0);
378 measures->setTime60kmh(0);
379 measures->setTime70kmh(0);
380 measures->setTime80kmh(0);
381 measures->setTime90kmh(0);
382 measures->setTime100kmh(0);
386 * This slot function is called when Abort button is clicked.
388 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
390 ui->pushButtonSendResult->setEnabled(false);
391 ui->pushButtonShowResultDialog->setEnabled(false);
392 ui->labelMeasureTabResult->hide();
393 ui->labelMeasureTabTime->setText("");
394 ui->labelMeasureTabSpeed->setText("");
395 measures->setTime10kmh(0);
396 measures->setTime20kmh(0);
397 measures->setTime30kmh(0);
398 measures->setTime40kmh(0);
399 measures->setTime50kmh(0);
400 measures->setTime60kmh(0);
401 measures->setTime70kmh(0);
402 measures->setTime80kmh(0);
403 measures->setTime90kmh(0);
404 measures->setTime100kmh(0);
405 this->accelerometerTimer->stop();
409 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
418 *This slot function is called when pushButtonSendResult is clicked.
419 *@todo Use real category value.
421 void CarMainWindow::on_pushButtonSendResult_clicked()
423 //Pick up relevant category name and pass it to the server
424 myHttpClient->sendResultXml(catList.at(choiceInt));
425 ui->pushButtonSendResult->setEnabled(false);
428 void CarMainWindow::updateUserName()
432 newUserName = myLogin->getUserName();
433 ui->userNameLabel->setText( "User: " + newUserName);
435 if (newUserName.length())
437 ui->setUserPushButton->setText( "Change User");
438 this->setWindowTitle("Speed Freak - " + newUserName);
442 ui->setUserPushButton->setText( "Set User");
443 this->setWindowTitle("Speed Freak");
447 void CarMainWindow::regUserToServer()
449 myHttpClient->requestRegistration();
453 void CarMainWindow::on_drawRoutePushButton_clicked()
455 QString routeFile = QString("route.txt");
456 if (myRoute->readRouteFromFile( routeFile) == true)
463 * Opens result dialog when show result button is clicked.
464 * Sends measures as parameter to the resultdialogs saveMeasuresToArray-function.
466 void CarMainWindow::on_pushButtonShowResultDialog_clicked()
468 result->saveMeasuresToArray(measures);
469 this->result->show();
472 void CarMainWindow::userLogin()
474 myHttpClient->checkLogin();
478 * Resets Accelerometer measurement variables
480 void CarMainWindow::resetAccelerometerMeasurements()
482 currentAcceleration = 0;
483 currentAccelerationString = "";
486 distanceTraveled = "";
487 //firstAcceleration = 0;
490 //lastScreenUpdateInSeconds = 0;
492 reverseAccelerationFlag = false;
494 //accelerometer->stop();
496 vehicleStartedMoving = false;
501 * This function is called to handle checkpoints
502 *@param totalTime total time elapsed since starting measurements
503 *@param currentSpeed current speed of the device
505 void CarMainWindow::handleCheckPoint(double totalTime, double currentSpeed)
507 switch (counterForSaveResults)
510 measures->setTime10kmh(totalTime);
514 measures->setTime20kmh(totalTime);
518 measures->setTime30kmh(totalTime);
522 measures->setTime40kmh(totalTime);
526 measures->setTime50kmh(totalTime);
530 measures->setTime60kmh(totalTime);
534 measures->setTime70kmh(totalTime);
538 measures->setTime80kmh(totalTime);
542 measures->setTime90kmh(totalTime);
546 measures->setTime100kmh(totalTime);
552 counterForSaveResults++;
554 if (choiceInt == 0 && measures->getTime40kmh() != 0)
556 setTimeAxisGapAndShowResult(measures->getTime40kmh());
558 this->accelerometerTimer->stop();
561 counterForSaveResults = 0;
563 else if (choiceInt == 1 && measures->getTime100kmh() != 0)
565 setTimeAxisGapAndShowResult(measures->getTime100kmh());
567 this->accelerometerTimer->stop();
570 counterForSaveResults = 0;
572 else if (choiceInt == 2 && measures->getTime10kmh() != 0)
574 setTimeAxisGapAndShowResult(measures->getTime10kmh());
576 this->accelerometerTimer->stop();
579 counterForSaveResults = 0;
581 else if (choiceInt != 1 && choiceInt != 0 && measures->getTime80kmh() != 0)
583 setTimeAxisGapAndShowResult(measures->getTime80kmh());
585 this->accelerometerTimer->stop();
588 counterForSaveResults = 0;
592 qDebug() << "something wrong in handleCheckPoint()";
597 *This function is called to read (and process) data from the accelerometer
599 void CarMainWindow::readAccelerometerData()
602 double changeInAcceleration = 0;
605 accelerometer->getAcceleration(x, y, z);
607 // keep the following line as close to the SetKinematicsProperties method as possible
608 currentTime = stopWatch.elapsed();
610 //accelerometer->smoothData(x, y, z);
613 x -= accelerometer->getCalibrationX();
614 y -= accelerometer->getCalibrationY();
615 z -= accelerometer->getCalibrationZ();
617 QString str = QString("acc x: " + QString::number(x) + "\n" +
618 "acc y: " + QString::number(y) + "\n" +
619 "acc z: " + QString::number(z) + "\n");
621 currentAcceleration = z;//sqrt(x*x + y*y + z*z);
622 changeInAcceleration = currentAcceleration;
624 if (((fabs(changeInAcceleration) <= accelerationStartThreshold)
625 && !vehicleStartedMoving))
629 else if(!vehicleStartedMoving)
631 vehicleStartedMoving = true;
637 calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
638 previousTime = currentTime;
640 s.sprintf("%.2f", changeInAcceleration);
641 currentAccelerationString = s;
644 speed = calculate->getCurrentSpeed();
645 speed = speed*3.6;//((speed*1000)/kSecondsInHour);
646 s.sprintf("%.1f", speed);
649 s.sprintf("%.2f", calculate->getDistanceTraveled());
650 distanceTraveled = s;
656 time = calculate->getTotalTime();
658 s.sprintf("%.2f", time);
661 str.append("ca: " + currentAccelerationString + " G\n" );
662 str.append("cspeed: " + currentSpeed + " km/h \n" );
663 str.append("dist: " + distanceTraveled + " m \n" );
664 str.append("time: " + totalTime + " s \n" );
666 if ((stopTime > 0) && (previousTime >= stopTime))
668 // we want to end at a stopping point that the user chose
670 resetAccelerometerMeasurements();
675 *This function is used to calibrate accelerometer
677 void CarMainWindow::calibrateAccelerometer()
679 resetAccelerometerMeasurements();
680 accelerometer->calibrate();
684 *This slot function is called when GPS on checkbox state changed. Route-tab view.
687 void CarMainWindow::on_gpsOnCheckBox_stateChanged(int GPSState)
689 //Stop polling GPS. Route-tab view.
692 ui->labelRouteTabGPSStatus->setText("GPS status: GPS off");
693 location->stopPollingGPS();
697 //Start polling GPS. Route-tab view.
700 ui->labelRouteTabGPSStatus->setText("GPS status: GPS on");
701 location->startPollingGPS();
706 *This slot function is called when GPS status changed. Route- and measure-tab view.
708 void CarMainWindow::gpsStatus()
710 //IF GPS checkbox is ON
711 if (ui->gpsOnCheckBox->isChecked())
713 //If GPS find 4 satellite.
714 if (location->getSatellitesInUse() >= 4)
716 //Set gps status. Route-tab view.
717 ui->labelRouteTabGPSStatus->setText("GPS ready");
719 //Set time. Route-tab view.
720 gpsTime->setTime_t(location->getTime());
721 QString gpsDateNow = gpsTime->toString("dd.MM.yyyy hh:mm:ss");
722 ui->labelRouteTabGPSTime->setText("GPS time: " + gpsDateNow);
724 //Set latitude & longitude. Route-tab view.
725 ui->labelRouteTabLatitude->setText("Latitude: " + QString::number(location->getLatitude()));
726 ui->labelRouteTabLongitude->setText("Longitude: " + QString::number(location->getLongitude()));
728 //Set rec status. Route-tab view.
729 if (ui->startRecPushButton->text() == "Stop recording")
731 ui->labelRouteTabRecStatus->setText("Recorded " + QString::number(gpsData->roundCounter) + " point");
734 //Get speed. Route- and Measure-tab view.
735 gpsSpeed = location->getSpeed();
737 //Set speed. Route-tab view.
738 ui->labelRouteTabSpeed->setText("Speed:" + QString::number(gpsSpeed) + "km/h +/-" + QString::number(location->getEps()) + "km/h");
743 ui->labelMeasureTabSpeed->setText(QString::number(gpsSpeed)); //Set speed. //Measure-tab view.
744 ui->labelMeasureTabTime->setText(QString::number(timeFromGps)); //Set time. //Measure-tab view.
749 timer->stop(); //Measure timer
750 ui->labelMeasureTabResult->setText(QString::number(timeFromGps));
751 ui->labelMeasureTabResult->show();
752 ui->pushButtonShowResultDialog->setEnabled(true);
753 ui->pushButtonShowResultDialog->setEnabled(true);
757 //If GPS find less than 4 satellite.
760 ui->labelRouteTabGPSStatus->setText("GPS status: Waiting for GPS");
770 *This slot function is called when gps timer timeout(10ms).
772 void CarMainWindow::gpsTimerTimeout()
777 //IF time is 1 second
778 if (time1000ms == 1000)
780 //Calculate acceleration 1/s
781 gpsSpeedPrevious = gpsSpeedNow; //Previous speed
782 gpsSpeedNow = (location->getSpeed())/3.6; //Speed now (m/s)
783 gpsAcceleration = (gpsSpeedNow - gpsSpeedPrevious)/1; //Calculate acceleration: speed now - previous speed / 1s.
784 //Set acceleration. Route-tab view.
785 ui->labelRouteTabAcceleration->setText("Acceleration: " + QString::number( gpsAcceleration ) + " m/s2");
789 //Set GPS update time. Route-tab view.
790 ui->labelRouteTabGPSUpdateTime->setText("GPS update time: " + QString::number(gpsUpdateTime) + " ms");
794 *This slot function is called when start rec push button clicked. Route-tab view.
796 void CarMainWindow::on_startRecPushButton_clicked()
798 //Start route recording.
799 if (ui->startRecPushButton->text() == "Start recording")
801 ui->startRecPushButton->setText("Stop recording");
802 ui->labelRouteTabRecStatus->setText("Recording started");
803 gpsData->startRouteRecording(ui->labelRouteTabGPSTime->text());
806 //Stop route recording.
807 else if (ui->startRecPushButton->text() == "Stop recording")
809 ui->startRecPushButton->setText("Start recording");
810 ui->labelRouteTabRecStatus->setText("Recording stopped");
811 gpsData->stopRouteRecording(ui->labelRouteTabGPSTime->text());
816 *Sets time axis right way in result dialog and shows target speed result.
817 *@param double pTime is the target speed result time which is shown to the user.
819 void CarMainWindow::setTimeAxisGapAndShowResult(double pTime)
821 ui->pushButtonShowResultDialog->setEnabled(true);
822 ui->pushButtonSendResult->setEnabled(true);
824 timeInteger.setNum(pTime);
825 ui->labelMeasureTabResult->show();
826 ui->labelMeasureTabResult->setText(timeInteger);
828 if (floor(pTime) <= 5)
830 result->setDiagramGapHorizontal(80);
833 else if (floor(pTime) <= 10)
835 result->setDiagramGapHorizontal(40);
840 result->setDiagramGapHorizontal(20);
844 void CarMainWindow::on_calibrateButton_clicked()
846 ui->autoStartButton->setEnabled(true);
847 ui->manualStartButton->setEnabled(true);
849 this->accelerometer->calibrate();