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();
57 reverseAccelerationFlag = false;
58 vehicleStartedMoving = false;
62 accelerationStartThreshold = 0.02;
64 QTimer *accelerometerTimer = new QTimer(this);
65 connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
66 accelerometerTimer->start(kAccelerometerSampleRate);
69 calculate = new Calculate();
70 //connect(calculate, SIGNAL(checkPointReached()), this, SLOT(handleCheckPoint()));
72 resetAccelerometerMeasurements();
74 measures = new Measures();
75 this->initializeMeasures();
77 timer->setInterval(300);
79 connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
80 connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
82 ui->labelMeasureTabResult->hide();
84 this->setWindowTitle("Speed Freak");
89 *Destructor of this class. Deletes all dynamic objects and sets them to NULL.
91 CarMainWindow::~CarMainWindow()
100 welcomeDialog = NULL;
108 *This function is used to .
111 void CarMainWindow::changeEvent(QEvent *e)
113 QMainWindow::changeEvent(e);
115 case QEvent::LanguageChange:
116 ui->retranslateUi(this);
124 *This slot function is called when ever list view is update. Start-tab view.
126 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
128 QString str = index.data().toString();
129 QStringList list = str.split("-");
130 QStringList list2 = list[1].split(" ");
132 ui->lineEditStartTabMin->setText(list[0]);
133 ui->lineEditStartTabMax->setText(list2[0]);
134 updateComboBoxStartTabUnits(list2[1]);
138 *This slot function is called when ever auto start button clicked. Start-tab view.
140 void CarMainWindow::on_autoStartButton_clicked()
142 choice = ui->listViewStartTabAccelerationCategories->currentIndex();
143 choiceInt = choice.row();
144 qDebug() << choiceInt;
147 ui->labelMeasureTabHeader->setText("Accelerate to 40 km/h");
148 result->setDiagramGapStem(75);
151 else if (choiceInt == 1)
153 ui->labelMeasureTabHeader->setText("Accelerate to 100 km/h");
154 result->setDiagramGapStem(30);
159 ui->labelMeasureTabHeader->setText("Accelerate to 80 km/h");
160 result->setDiagramGapStem(37.5);
162 ui->labelMeasureTabResult->setText("");
165 //measure = new MeasureDialog();
166 // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
168 // Show measure dialog.
171 // TODO: Move next if else to the function which is called when target speed
175 if (floor(this->measures->getTime40kmh()) <= 5)
177 result->setDiagramGapHorizontal(80);
180 else if (floor(this->measures->getTime40kmh()) <= 10)
182 result->setDiagramGapHorizontal(40);
187 result->setDiagramGapHorizontal(20);
193 if (floor(this->measures->getTime40kmh()) <= 5)
195 result->setDiagramGapHorizontal(80);
198 else if (floor(this->measures->getTime40kmh()) <= 10)
200 result->setDiagramGapHorizontal(40);
205 result->setDiagramGapHorizontal(20);
209 ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
213 *This slot function is called when ever list view is update. Start-tab view.
214 *@param QString unit.
216 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
218 ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
222 *This function is used to init unit combobox. Start-tab view.
224 void CarMainWindow::initComboBoxStartTabUnits()
226 units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
227 ui->comboBoxStartTabUnits->addItems(units);
231 *This function is used to set items to unit combobox. Start-tab view.
232 *@param QStringlist units
234 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
236 ui->comboBoxStartTabUnits->addItems(units);
240 *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
242 void CarMainWindow::initListViewStartTabAccelerationCategories()
244 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";
245 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
246 ui->listViewStartTabAccelerationCategories->setModel(model);
250 *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
251 *@param QStringlist accelerationCategoriesStartTab
253 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
255 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
256 ui->listViewStartTabAccelerationCategories->setModel(model);
260 *This function is used to set items to category combobox. Top-tab view.
263 void CarMainWindow::setCategoryCompoBox()
265 ui->comboBoxTopCategory->addItems(categorylist->getCategoryList());
269 *This function is used to set items to labelTopList. Top-tab view.
270 *@param QString category
272 void CarMainWindow::setListViewTopList(QString category, int size)
275 topList.append( categorylist->getTopList(category, size));
276 ui->labelTopList->setText(topList);
280 *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
282 void CarMainWindow::openResultView()
284 //result->saveMeasuresToArray(measure->measures);
285 // Show result dialog.
287 ui->pushButtonSendResult->setEnabled(true);
291 if (floor(this->measures->getTime40kmh()) <= 5)
293 result->setDiagramGapHorizontal(80);
296 else if (floor(this->measures->getTime40kmh()) <= 10)
298 result->setDiagramGapHorizontal(40);
303 result->setDiagramGapHorizontal(20);
309 if (floor(this->measures->getTime40kmh()) <= 5)
311 result->setDiagramGapHorizontal(80);
314 else if (floor(this->measures->getTime40kmh()) <= 10)
316 result->setDiagramGapHorizontal(40);
321 result->setDiagramGapHorizontal(20);
324 timeInteger.setNum(this->measures->getTime40kmh());
325 //time = "0 - 40 km/h: ";
326 //time.append(timeInteger);
327 //ui->labelResult40kmh->setText(time);
328 ui->labelMeasureTabResult->show();
329 ui->labelMeasureTabResult->setText(timeInteger);
330 //ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
334 *This slot function is called when registrate button is clicked.
336 void CarMainWindow::on_registratePushButton_clicked()
338 myRegistration->show();
342 *This slot function is called when ever refresh button clicked. Top-tab view.
344 void CarMainWindow::on_buttonTopRefresh_clicked()
346 myHttpClient->requestCategories();
347 setCategoryCompoBox();
351 *This slot function is called when ever category combobox current index changed. Top-tab view.
352 *@param QString category
353 *@todo Check where limitNr is taken.
355 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
357 int limitNr = 5; //replace with real value?
358 QString limit = QString::number(limitNr);
359 category = "acceleration-0-100"; //replace with real value from category list/top window
360 myHttpClient->requestTopList(category, limit);
361 setListViewTopList(category,10);
365 *This slot function is called when ever category combobox activated. Top-tab view.
366 *@param QString category
368 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
370 setListViewTopList(category,10);
374 *This slot function is called when set/change user button is clicked.
376 void CarMainWindow::on_setUserPushButton_clicked()
382 *@brief Just for development, for the real button is not shown until
383 *measurin started and there are results.
384 *@todo Implement with real code and yet leave sendXml in the bottom in use.
386 void CarMainWindow::on_manualStartButton_clicked()
392 * This slot function is called when timer gives timeout signal. Checks current speed
393 * and stores times in measure class.
395 void CarMainWindow::after_timeout()
397 QString timeString, speedString;
401 if (floor(speed) == 10)
403 measures->setTime10kmh(time);
406 else if (floor(speed) == 20)
408 measures->setTime20kmh(time);
411 else if (floor(speed) == 30)
413 measures->setTime30kmh(time);
416 else if (floor(speed) == 40)
418 measures->setTime40kmh(time);
421 else if (floor(speed) == 50)
423 measures->setTime50kmh(time);
426 else if (floor(speed) == 60)
428 measures->setTime60kmh(time);
431 else if (floor(speed) == 70)
433 measures->setTime70kmh(time);
436 else if (floor(speed) == 80)
438 measures->setTime80kmh(time);
441 else if (floor(speed) == 90)
443 measures->setTime90kmh(time);
446 else if (floor(speed) == 100)
448 measures->setTime100kmh(time);
456 // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
462 //emit this->speedAchieved();
463 this->openResultView();
468 // Updates speed and time.
471 timeString.setNum(time);
472 speedString.setNum(speed);
473 ui->labelMeasureTabTime->setText(timeString);
474 ui->labelMeasureTabSpeed->setText(speedString);
482 * Initializes measures class's member variables.
484 void CarMainWindow::initializeMeasures()
486 measures->setTime10kmh(0);
487 measures->setTime20kmh(0);
488 measures->setTime30kmh(0);
489 measures->setTime40kmh(0);
490 measures->setTime50kmh(0);
491 measures->setTime60kmh(0);
492 measures->setTime70kmh(0);
493 measures->setTime80kmh(0);
494 measures->setTime90kmh(0);
495 measures->setTime100kmh(0);
499 * This slot function is called when Abort button is clicked.
501 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
503 measures->setTime10kmh(0);
504 measures->setTime20kmh(0);
505 measures->setTime30kmh(0);
506 measures->setTime40kmh(0);
507 measures->setTime50kmh(0);
508 measures->setTime60kmh(0);
509 measures->setTime70kmh(0);
510 measures->setTime80kmh(0);
511 measures->setTime90kmh(0);
512 measures->setTime100kmh(0);
516 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
520 void CarMainWindow::on_pushButtonSendResult_clicked()
522 myHttpClient->sendResultXml();
523 ui->pushButtonSendResult->setEnabled(false);
526 void CarMainWindow::updateUserName()
530 newUserName = myLogin->getUserName();
531 ui->userNameLabel->setText( "User: " + newUserName);
533 if (newUserName.length())
535 ui->setUserPushButton->setText( "Change User");
536 this->setWindowTitle("Speed freak - " + newUserName);
540 ui->setUserPushButton->setText( "Set User");
541 this->setWindowTitle("Speed freak");
545 void CarMainWindow::regUserToServer()
547 myHttpClient->requestRegistration();
551 void CarMainWindow::on_drawRoutePushButton_clicked()
557 * Opens result dialog when show result button is clicked.
558 * Sends measures as parameter to the resultdialogs saveMeasuresToArray-function.
560 void CarMainWindow::on_pushButtonShowResultDialog_clicked()
563 meas.setTime10kmh(1.3);
564 meas.setTime20kmh(2.5);
565 meas.setTime30kmh(3.6);
566 meas.setTime40kmh(6.7);
567 meas.setTime50kmh(7.3);
568 meas.setTime60kmh(7.5);
569 meas.setTime70kmh(8.6);
570 meas.setTime80kmh(8.7);
571 meas.setTime90kmh(9.6);
572 meas.setTime100kmh(9.9);
573 result->setDiagramGapHorizontal(40);
574 result->saveMeasuresToArray(&meas);
575 this->result->show();
578 void CarMainWindow::userLogin()
580 myHttpClient->checkLogin();
584 * Resets Accelerometer measurement variables
586 void CarMainWindow::resetAccelerometerMeasurements()
588 currentAcceleration = 0;
589 currentAccelerationString = "";
592 distanceTraveled = "";
593 firstAcceleration = 0;
596 //lastScreenUpdateInSeconds = 0;
598 reverseAccelerationFlag = false;
599 stopWatch.setHMS(0, 0, 0, 0);
600 //accelerometer->stop();
602 vehicleStartedMoving = false;
607 * This function is called to handle checkpoints
608 *@param totalTime total time elapsed since starting measurements
609 *@param currentSpeed current speed of the device
611 void CarMainWindow::handleCheckPoint(double totalTime, double currentSpeed)
620 *This function is called to read (and process) data from the accelerometer
622 void CarMainWindow::readAccelerometerData()
625 double changeInAcceleration = 0;
628 accelerometer->getAcceleration(x, y, z);
629 accelerometer->smoothData(x, y, z);
632 x -= accelerometer->getCalibrationX();
633 y -= accelerometer->getCalibrationY();
634 z -= accelerometer->getCalibrationZ();
636 QString str = QString("acc x: " + QString::number(x) + "\n" +
637 "acc y: " + QString::number(y) + "\n" +
638 "acc z: " + QString::number(z) + "\n");
640 if (!vehicleStartedMoving)
644 firstAcceleration = sqrt(x*x + y*y + z*z);
645 //firstAcceleration = y; // first read
650 currentAcceleration = sqrt(x*x + y*y + z*z);
651 changeInAcceleration = (currentAcceleration - firstAcceleration); // firstAcceleration only gets set once
653 if (((abs(changeInAcceleration) <= accelerationStartThreshold)
654 && !vehicleStartedMoving))
659 if (reverseAccelerationFlag)
661 // will be false until after 1st calculation
662 if ((changeInAcceleration <= 0))
664 // actually increasing here...
665 changeInAcceleration = abs(changeInAcceleration);
669 // actually decreasing here...
670 changeInAcceleration = (changeInAcceleration * -1);
673 if (!vehicleStartedMoving)
675 if ((changeInAcceleration < 0))
677 // we started to move backwards first time through
678 reverseAccelerationFlag = true;
679 changeInAcceleration = abs(changeInAcceleration);
681 vehicleStartedMoving = true;
683 stopWatch.setHMS(0, 0, 0, 0);
686 // keep the following line as close to the SetKinematicsProperties method as possible
687 currentTime = stopWatch.elapsed();
688 calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
689 previousTime = currentTime;
691 s.sprintf("%.2f", changeInAcceleration);
692 currentAccelerationString = s;
695 speed = calculate->getCurrentSpeed();
696 speed = ((speed*1000)/kSecondsInHour);
697 s.sprintf("%.2f", speed);
700 s.sprintf("%.2f", calculate->getDistanceTraveled());
701 distanceTraveled = s;
708 s.sprintf("%.2f", calculate->getTotalTime());
711 str.append("ca: " + currentAccelerationString + " G\n" );
712 str.append("cspeed: " + currentSpeed + " km/h \n" );
713 str.append("dist: " + distanceTraveled + " m \n" );
714 str.append("time: " + totalTime + " s \n" );
716 if ((stopTime > 0) && (previousTime >= stopTime))
718 // we want to end at a stopping point that the user chose
720 resetAccelerometerMeasurements();
725 *This function is used to calibrate accelerometer
727 void CarMainWindow::calibrateAccelerometer()
729 resetAccelerometerMeasurements();
730 accelerometer->calibrate();
734 *This slot function is called when GPS on checkbox state changed. Route-tab view.
737 void CarMainWindow::on_gpsOnCheckBox_stateChanged(int GPSState)
741 ui->labelRouteTabGPSStatus->setText("GPS off");//testing
742 location->stopPollingGPS();
746 ui->labelRouteTabGPSStatus->setText("GPS on");//testing
747 location->startPollingGPS();
752 *This slot function is called when GPS status changed. Route-tab view.
754 void CarMainWindow::gpsStatus()
756 if (ui->gpsOnCheckBox->isChecked())
758 if (location->getSatellitesInUse() >= 4)
760 ui->labelRouteTabGPSStatus->setText("GPS ready");
765 ui->labelRouteTabGPSStatus->setText("Waiting for GPS");