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 50
18 #define kFilteringFactor 0.2
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);
29 result = new ResultDialog();
30 //measure = new MeasureDialog();
31 welcomeDialog = new WelcomeDialog();
32 welcomeDialog->show();
34 initComboBoxStartTabUnits();
35 initListViewStartTabAccelerationCategories();
37 myLogin = new LoginWindow(this);
38 categorylist = 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 myRoute = new RouteDialog( this);
46 location = new Maemo5Location(this);
47 gpsData = new GPSData(location);
48 connect(location,SIGNAL(agnss()),this,SLOT(gpsStatus()));
55 accelerometer = new Accelerometer();
56 accelerometer->setSampleRate(100);
58 reverseAccelerationFlag = false;
59 vehicleStartedMoving = false;
63 accelerationStartThreshold = 0.02;
65 QTimer *accelerometerTimer = new QTimer(this);
66 connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
67 accelerometerTimer->start(kAccelerometerSampleRate);
70 calculate = new Calculate();
71 //connect(calculate, SIGNAL(checkPointReached()), this, SLOT(handleCheckPoint()));
73 resetAccelerometerMeasurements();
75 measures = new Measures();
76 this->initializeMeasures();
78 timer->setInterval(300);
80 connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
81 connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
83 ui->labelMeasureTabResult->hide();
85 this->setWindowTitle("Speed Freak");
90 *Destructor of this class. Deletes all dynamic objects and sets them to NULL.
92 CarMainWindow::~CarMainWindow()
100 delete welcomeDialog;
101 welcomeDialog = NULL;
109 *This function is used to .
112 void CarMainWindow::changeEvent(QEvent *e)
114 QMainWindow::changeEvent(e);
116 case QEvent::LanguageChange:
117 ui->retranslateUi(this);
125 *This slot function is called when ever list view is update. Start-tab view.
127 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
129 QString str = index.data().toString();
130 QStringList list = str.split("-");
131 QStringList list2 = list[1].split(" ");
133 ui->lineEditStartTabMin->setText(list[0]);
134 ui->lineEditStartTabMax->setText(list2[0]);
135 updateComboBoxStartTabUnits(list2[1]);
139 *This slot function is called when ever auto start button clicked. Start-tab view.
141 void CarMainWindow::on_autoStartButton_clicked()
143 choice = ui->listViewStartTabAccelerationCategories->currentIndex();
144 choiceInt = choice.row();
145 qDebug() << choiceInt;
148 ui->labelMeasureTabHeader->setText("Accelerate to 40 km/h");
149 result->setDiagramGapStem(75);
152 else if (choiceInt == 1)
154 ui->labelMeasureTabHeader->setText("Accelerate to 100 km/h");
155 result->setDiagramGapStem(30);
160 ui->labelMeasureTabHeader->setText("Accelerate to 80 km/h");
161 result->setDiagramGapStem(37.5);
163 ui->labelMeasureTabResult->setText("");
166 //measure = new MeasureDialog();
167 // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
168 accelerometer->start();
170 // Show measure dialog.
173 // TODO: Move next if else to the function which is called when target speed
177 if (floor(this->measures->getTime40kmh()) <= 5)
179 result->setDiagramGapHorizontal(80);
182 else if (floor(this->measures->getTime40kmh()) <= 10)
184 result->setDiagramGapHorizontal(40);
189 result->setDiagramGapHorizontal(20);
195 if (floor(this->measures->getTime40kmh()) <= 5)
197 result->setDiagramGapHorizontal(80);
200 else if (floor(this->measures->getTime40kmh()) <= 10)
202 result->setDiagramGapHorizontal(40);
207 result->setDiagramGapHorizontal(20);
211 ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
215 *This slot function is called when ever list view is update. Start-tab view.
216 *@param QString unit.
218 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
220 ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
224 *This function is used to init unit combobox. Start-tab view.
226 void CarMainWindow::initComboBoxStartTabUnits()
228 units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
229 ui->comboBoxStartTabUnits->addItems(units);
233 *This function is used to set items to unit combobox. Start-tab view.
234 *@param QStringlist units
236 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
238 ui->comboBoxStartTabUnits->addItems(units);
242 *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
244 void CarMainWindow::initListViewStartTabAccelerationCategories()
246 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";
247 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
248 ui->listViewStartTabAccelerationCategories->setModel(model);
252 *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
253 *@param QStringlist accelerationCategoriesStartTab
255 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
257 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
258 ui->listViewStartTabAccelerationCategories->setModel(model);
262 *This function is used to set items to category combobox. Top-tab view.
265 void CarMainWindow::setCategoryCompoBox()
267 ui->comboBoxTopCategory->addItems(categorylist->getCategoryList());
271 *This function is used to set items to labelTopList. Top-tab view.
272 *@param QString category
274 void CarMainWindow::setListViewTopList(QString category, int size)
277 topList.append( categorylist->getTopList(category, size));
278 ui->labelTopList->setText(topList);
282 *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
284 void CarMainWindow::openResultView()
286 //result->saveMeasuresToArray(measure->measures);
287 // Show result dialog.
289 ui->pushButtonSendResult->setEnabled(true);
293 if (floor(this->measures->getTime40kmh()) <= 5)
295 result->setDiagramGapHorizontal(80);
298 else if (floor(this->measures->getTime40kmh()) <= 10)
300 result->setDiagramGapHorizontal(40);
305 result->setDiagramGapHorizontal(20);
311 if (floor(this->measures->getTime40kmh()) <= 5)
313 result->setDiagramGapHorizontal(80);
316 else if (floor(this->measures->getTime40kmh()) <= 10)
318 result->setDiagramGapHorizontal(40);
323 result->setDiagramGapHorizontal(20);
326 timeInteger.setNum(this->measures->getTime40kmh());
327 //time = "0 - 40 km/h: ";
328 //time.append(timeInteger);
329 //ui->labelResult40kmh->setText(time);
330 ui->labelMeasureTabResult->show();
331 ui->labelMeasureTabResult->setText(timeInteger);
332 //ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
336 *This slot function is called when registrate button is clicked.
338 void CarMainWindow::on_registratePushButton_clicked()
340 myRegistration->show();
344 *This slot function is called when ever refresh button clicked. Top-tab view.
346 void CarMainWindow::on_buttonTopRefresh_clicked()
348 myHttpClient->requestCategories();
349 setCategoryCompoBox();
353 *This slot function is called when ever category combobox current index changed. Top-tab view.
354 *@param QString category
355 *@todo Check where limitNr is taken.
357 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
359 int limitNr = 5; //replace with real value?
360 QString limit = QString::number(limitNr);
361 category = "acceleration-0-100"; //replace with real value from category list/top window
362 myHttpClient->requestTopList(category, limit);
363 setListViewTopList(category,10);
367 *This slot function is called when ever category combobox activated. Top-tab view.
368 *@param QString category
370 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
372 setListViewTopList(category,10);
376 *This slot function is called when set/change user button is clicked.
378 void CarMainWindow::on_setUserPushButton_clicked()
384 *@brief Just for development, for the real button is not shown until
385 *measurin started and there are results.
386 *@todo Implement with real code and yet leave sendXml in the bottom in use.
388 void CarMainWindow::on_manualStartButton_clicked()
394 * This slot function is called when timer gives timeout signal. Checks current speed
395 * and stores times in measure class.
397 void CarMainWindow::after_timeout()
399 QString timeString, speedString;
401 time = accelerometer->getTotalTime();
402 speed = accelerometer->getCurrentSpeed();
405 if (floor(speed) == 10)
407 measures->setTime10kmh(time);
410 else if (floor(speed) == 20)
412 measures->setTime20kmh(time);
415 else if (floor(speed) == 30)
417 measures->setTime30kmh(time);
420 else if (floor(speed) == 40)
422 measures->setTime40kmh(time);
425 else if (floor(speed) == 50)
427 measures->setTime50kmh(time);
430 else if (floor(speed) == 60)
432 measures->setTime60kmh(time);
435 else if (floor(speed) == 70)
437 measures->setTime70kmh(time);
440 else if (floor(speed) == 80)
442 measures->setTime80kmh(time);
445 else if (floor(speed) == 90)
447 measures->setTime90kmh(time);
450 else if (floor(speed) == 100)
452 measures->setTime100kmh(time);
460 // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
464 accelerometer->stop();
467 //emit this->speedAchieved();
468 this->openResultView();
473 // Updates speed and time.
476 timeString.setNum(time);
477 speedString.setNum(speed);
478 ui->labelMeasureTabTime->setText(timeString);
479 ui->labelMeasureTabSpeed->setText(speedString);
487 * Initializes measures class's member variables.
489 void CarMainWindow::initializeMeasures()
491 measures->setTime10kmh(0);
492 measures->setTime20kmh(0);
493 measures->setTime30kmh(0);
494 measures->setTime40kmh(0);
495 measures->setTime50kmh(0);
496 measures->setTime60kmh(0);
497 measures->setTime70kmh(0);
498 measures->setTime80kmh(0);
499 measures->setTime90kmh(0);
500 measures->setTime100kmh(0);
504 * This slot function is called when Abort button is clicked.
506 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
508 measures->setTime10kmh(0);
509 measures->setTime20kmh(0);
510 measures->setTime30kmh(0);
511 measures->setTime40kmh(0);
512 measures->setTime50kmh(0);
513 measures->setTime60kmh(0);
514 measures->setTime70kmh(0);
515 measures->setTime80kmh(0);
516 measures->setTime90kmh(0);
517 measures->setTime100kmh(0);
519 accelerometer->stop();
522 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
526 void CarMainWindow::on_pushButtonSendResult_clicked()
528 myHttpClient->sendResultXml();
529 ui->pushButtonSendResult->setEnabled(false);
532 void CarMainWindow::updateUserName()
536 newUserName = myLogin->getUserName();
537 ui->userNameLabel->setText( "User: " + newUserName);
539 if (newUserName.length())
541 ui->setUserPushButton->setText( "Change User");
542 this->setWindowTitle("Speed freak - " + newUserName);
546 ui->setUserPushButton->setText( "Set User");
547 this->setWindowTitle("Speed freak");
551 void CarMainWindow::regUserToServer()
553 myHttpClient->requestRegistration();
557 void CarMainWindow::on_drawRoutePushButton_clicked()
563 * Opens result dialog when show result button is clicked.
564 * Sends measures as parameter to the resultdialogs saveMeasuresToArray-function.
566 void CarMainWindow::on_pushButtonShowResultDialog_clicked()
569 meas.setTime10kmh(1.3);
570 meas.setTime20kmh(2.5);
571 meas.setTime30kmh(3.6);
572 meas.setTime40kmh(6.7);
573 meas.setTime50kmh(7.3);
574 meas.setTime60kmh(7.5);
575 meas.setTime70kmh(8.6);
576 meas.setTime80kmh(8.7);
577 meas.setTime90kmh(9.6);
578 meas.setTime100kmh(9.9);
579 result->setDiagramGapHorizontal(40);
580 result->saveMeasuresToArray(&meas);
581 this->result->show();
584 void CarMainWindow::userLogin()
586 myHttpClient->checkLogin();
590 * Resets Accelerometer measurement variables
592 void CarMainWindow::resetAccelerometerMeasurements()
594 currentAcceleration = 0;
595 currentAccelerationString = "";
598 distanceTraveled = "";
599 firstAcceleration = 0;
602 //lastScreenUpdateInSeconds = 0;
604 reverseAccelerationFlag = false;
605 stopWatch.setHMS(0, 0, 0, 0);
606 //accelerometer->stop();
608 vehicleStartedMoving = false;
613 * This function is called to handle checkpoints
614 *@param totalTime total time elapsed since starting measurements
615 *@param currentSpeed current speed of the device
617 void CarMainWindow::handleCheckPoint(double totalTime, double currentSpeed)
626 *This function is called to read (and process) data from the accelerometer
628 void CarMainWindow::readAccelerometerData()
631 double changeInAcceleration = 0;
634 accelerometer->getAcceleration(x, y, z);
635 accelerometer->smoothData(x, y, z);
638 x -= accelerometer->getCalibrationX();
639 y -= accelerometer->getCalibrationY();
640 z -= accelerometer->getCalibrationZ();
642 QString str = QString("acc x: " + QString::number(x) + "\n" +
643 "acc y: " + QString::number(y) + "\n" +
644 "acc z: " + QString::number(z) + "\n");
646 if (!vehicleStartedMoving)
650 firstAcceleration = sqrt(x*x + y*y + z*z);
651 //firstAcceleration = y; // first read
656 currentAcceleration = sqrt(x*x + y*y + z*z);
657 changeInAcceleration = (currentAcceleration - firstAcceleration); // firstAcceleration only gets set once
659 if (((abs(changeInAcceleration) <= accelerationStartThreshold)
660 && !vehicleStartedMoving))
665 if (reverseAccelerationFlag)
667 // will be false until after 1st calculation
668 if ((changeInAcceleration <= 0))
670 // actually increasing here...
671 changeInAcceleration = abs(changeInAcceleration);
675 // actually decreasing here...
676 changeInAcceleration = (changeInAcceleration * -1);
679 if (!vehicleStartedMoving)
681 if ((changeInAcceleration < 0))
683 // we started to move backwards first time through
684 reverseAccelerationFlag = true;
685 changeInAcceleration = abs(changeInAcceleration);
687 vehicleStartedMoving = true;
689 stopWatch.setHMS(0, 0, 0, 0);
692 // keep the following line as close to the SetKinematicsProperties method as possible
693 currentTime = stopWatch.elapsed();
694 calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
695 previousTime = currentTime;
697 s.sprintf("%.2f", changeInAcceleration);
698 currentAccelerationString = s;
701 speed = calculate->getCurrentSpeed();
702 speed = ((speed*1000)/kSecondsInHour);
703 s.sprintf("%.2f", speed);
706 s.sprintf("%.2f", calculate->getDistanceTraveled());
707 distanceTraveled = s;
714 s.sprintf("%.2f", calculate->getTotalTime());
717 str.append("ca: " + currentAccelerationString + " G\n" );
718 str.append("cspeed: " + currentSpeed + " km/h \n" );
719 str.append("dist: " + distanceTraveled + " m \n" );
720 str.append("time: " + totalTime + " s \n" );
722 if ((stopTime > 0) && (previousTime >= stopTime))
724 // we want to end at a stopping point that the user chose
726 resetAccelerometerMeasurements();
731 *This function is used to calibrate accelerometer
733 void CarMainWindow::calibrateAccelerometer()
735 resetAccelerometerMeasurements();
736 accelerometer->calibrate();
740 *This slot function is called when GPS on checkbox state changed. Route-tab view.
743 void CarMainWindow::on_gpsOnCheckBox_stateChanged(int GPSState)
747 ui->labelRouteTabGPSStatus->setText("GPS off");//testing
748 location->stopPollingGPS();
752 ui->labelRouteTabGPSStatus->setText("GPS on");//testing
753 location->startPollingGPS();
758 *This slot function is called when GPS status changed. Route-tab view.
760 void CarMainWindow::gpsStatus()
762 if (ui->gpsOnCheckBox->isChecked())
764 if (location->getSatellitesInUse() >= 4)
766 ui->labelRouteTabGPSStatus->setText("GPS ready");
771 ui->labelRouteTabGPSStatus->setText("Waiting for GPS");