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);
50 accelerometer = new Accelerometer();
51 accelerometer->setSampleRate(100);
53 reverseAccelerationFlag = false;
54 vehicleStartedMoving = false;
58 accelerationStartThreshold = 0.02;
60 QTimer *accelerometerTimer = new QTimer(this);
61 connect(accelerometerTimer, SIGNAL(timeout()), this, SLOT(readAccelerometerData()));
62 accelerometerTimer->start(kAccelerometerSampleRate);
65 calculate = new Calculate();
66 //connect(calculate, SIGNAL(checkPointReached()), this, SLOT(handleCheckPoint()));
68 resetAccelerometerMeasurements();
70 measures = new Measures();
71 this->initializeMeasures();
73 timer->setInterval(300);
75 connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
76 connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
78 ui->labelMeasureTabResult->hide();
80 this->setWindowTitle("Speed Freak");
85 *Destructor of this class. Should be used to release all allocated resources.
87 CarMainWindow::~CarMainWindow()
98 *This function is used to .
101 void CarMainWindow::changeEvent(QEvent *e)
103 QMainWindow::changeEvent(e);
105 case QEvent::LanguageChange:
106 ui->retranslateUi(this);
114 *This slot function is called when ever list view is update. Start-tab view.
116 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
118 QString str = index.data().toString();
119 QStringList list = str.split("-");
120 QStringList list2 = list[1].split(" ");
122 ui->lineEditStartTabMin->setText(list[0]);
123 ui->lineEditStartTabMax->setText(list2[0]);
124 updateComboBoxStartTabUnits(list2[1]);
128 *This slot function is called when ever auto start button clicked. Start-tab view.
130 void CarMainWindow::on_autoStartButton_clicked()
132 choice = ui->listViewStartTabAccelerationCategories->currentIndex();
133 choiceInt = choice.row();
134 qDebug() << choiceInt;
137 ui->labelMeasureTabHeader->setText("Accelerate to 40 km/h");
138 result->setDiagramGapStem(75);
141 else if (choiceInt == 1)
143 ui->labelMeasureTabHeader->setText("Accelerate to 100 km/h");
144 result->setDiagramGapStem(30);
149 ui->labelMeasureTabHeader->setText("Accelerate to 80 km/h");
150 result->setDiagramGapStem(37.5);
152 ui->labelMeasureTabResult->setText("");
155 //measure = new MeasureDialog();
156 // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
157 accelerometer->start();
159 // Show measure dialog.
162 // TODO: Move next if else to the function which is called when target speed
166 if (floor(this->measures->getTime40kmh()) <= 5)
168 result->setDiagramGapHorizontal(80);
171 else if (floor(this->measures->getTime40kmh()) <= 10)
173 result->setDiagramGapHorizontal(40);
178 result->setDiagramGapHorizontal(20);
184 if (floor(this->measures->getTime40kmh()) <= 5)
186 result->setDiagramGapHorizontal(80);
189 else if (floor(this->measures->getTime40kmh()) <= 10)
191 result->setDiagramGapHorizontal(40);
196 result->setDiagramGapHorizontal(20);
200 ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
204 *This slot function is called when ever list view is update. Start-tab view.
205 *@param QString unit.
207 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
209 ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
213 *This function is used to init unit combobox. Start-tab view.
215 void CarMainWindow::initComboBoxStartTabUnits()
217 units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
218 ui->comboBoxStartTabUnits->addItems(units);
222 *This function is used to set items to unit combobox. Start-tab view.
223 *@param QStringlist units
225 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
227 ui->comboBoxStartTabUnits->addItems(units);
231 *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
233 void CarMainWindow::initListViewStartTabAccelerationCategories()
235 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";
236 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
237 ui->listViewStartTabAccelerationCategories->setModel(model);
241 *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
242 *@param QStringlist accelerationCategoriesStartTab
244 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
246 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
247 ui->listViewStartTabAccelerationCategories->setModel(model);
251 *This function is used to set items to category combobox. Top-tab view.
254 void CarMainWindow::setCategoryCompoBox()
256 ui->comboBoxTopCategory->addItems(categorylist->getCategoryList());
260 *This function is used to set items to labelTopList. Top-tab view.
261 *@param QString category
263 void CarMainWindow::setListViewTopList(QString category, int size)
266 topList.append( categorylist->getTopList(category, size));
267 ui->labelTopList->setText(topList);
271 *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
273 void CarMainWindow::openResultView()
275 //result->saveMeasuresToArray(measure->measures);
276 // Show result dialog.
278 ui->pushButtonSendResult->setEnabled(true);
282 if (floor(this->measures->getTime40kmh()) <= 5)
284 result->setDiagramGapHorizontal(80);
287 else if (floor(this->measures->getTime40kmh()) <= 10)
289 result->setDiagramGapHorizontal(40);
294 result->setDiagramGapHorizontal(20);
300 if (floor(this->measures->getTime40kmh()) <= 5)
302 result->setDiagramGapHorizontal(80);
305 else if (floor(this->measures->getTime40kmh()) <= 10)
307 result->setDiagramGapHorizontal(40);
312 result->setDiagramGapHorizontal(20);
315 timeInteger.setNum(this->measures->getTime40kmh());
316 //time = "0 - 40 km/h: ";
317 //time.append(timeInteger);
318 //ui->labelResult40kmh->setText(time);
319 ui->labelMeasureTabResult->show();
320 ui->labelMeasureTabResult->setText(timeInteger);
321 //ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
325 *This slot function is called when registrate button is clicked.
327 void CarMainWindow::on_registratePushButton_clicked()
329 myRegistration->show();
333 *This slot function is called when ever refresh button clicked. Top-tab view.
335 void CarMainWindow::on_buttonTopRefresh_clicked()
337 myHttpClient->requestCategories();
338 setCategoryCompoBox();
342 *This slot function is called when ever category combobox current index changed. Top-tab view.
343 *@param QString category
344 *@todo Check where limitNr is taken.
346 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
348 int limitNr = 5; //replace with real value?
349 QString limit = QString::number(limitNr);
350 category = "acceleration-0-100"; //replace with real value from category list/top window
351 myHttpClient->requestTopList(category, limit);
352 setListViewTopList(category,10);
356 *This slot function is called when ever category combobox activated. Top-tab view.
357 *@param QString category
359 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
361 setListViewTopList(category,10);
365 *This slot function is called when set/change user button is clicked.
367 void CarMainWindow::on_setUserPushButton_clicked()
373 *@brief Just for development, for the real button is not shown until
374 *measurin started and there are results.
375 *@todo Implement with real code and yet leave sendXml in the bottom in use.
377 void CarMainWindow::on_manualStartButton_clicked()
383 * This slot function is called when timer gives timeout signal. Checks current speed
384 * and stores times in measure class.
386 void CarMainWindow::after_timeout()
388 QString timeString, speedString;
390 time = accelerometer->getTotalTime();
391 speed = accelerometer->getCurrentSpeed();
394 if (floor(speed) == 10)
396 measures->setTime10kmh(time);
399 else if (floor(speed) == 20)
401 measures->setTime20kmh(time);
404 else if (floor(speed) == 30)
406 measures->setTime30kmh(time);
409 else if (floor(speed) == 40)
411 measures->setTime40kmh(time);
414 else if (floor(speed) == 50)
416 measures->setTime50kmh(time);
419 else if (floor(speed) == 60)
421 measures->setTime60kmh(time);
424 else if (floor(speed) == 70)
426 measures->setTime70kmh(time);
429 else if (floor(speed) == 80)
431 measures->setTime80kmh(time);
434 else if (floor(speed) == 90)
436 measures->setTime90kmh(time);
439 else if (floor(speed) == 100)
441 measures->setTime100kmh(time);
449 // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
453 accelerometer->stop();
456 //emit this->speedAchieved();
457 this->openResultView();
462 // Updates speed and time.
465 timeString.setNum(time);
466 speedString.setNum(speed);
467 ui->labelMeasureTabTime->setText(timeString);
468 ui->labelMeasureTabSpeed->setText(speedString);
476 * Initializes measures class's member variables.
478 void CarMainWindow::initializeMeasures()
480 measures->setTime10kmh(0);
481 measures->setTime20kmh(0);
482 measures->setTime30kmh(0);
483 measures->setTime40kmh(0);
484 measures->setTime50kmh(0);
485 measures->setTime60kmh(0);
486 measures->setTime70kmh(0);
487 measures->setTime80kmh(0);
488 measures->setTime90kmh(0);
489 measures->setTime100kmh(0);
493 * This slot function is called when Abort button is clicked.
495 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
497 measures->setTime10kmh(0);
498 measures->setTime20kmh(0);
499 measures->setTime30kmh(0);
500 measures->setTime40kmh(0);
501 measures->setTime50kmh(0);
502 measures->setTime60kmh(0);
503 measures->setTime70kmh(0);
504 measures->setTime80kmh(0);
505 measures->setTime90kmh(0);
506 measures->setTime100kmh(0);
508 accelerometer->stop();
511 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
515 void CarMainWindow::on_pushButtonSendResult_clicked()
517 myHttpClient->sendResultXml();
518 ui->pushButtonSendResult->setEnabled(false);
521 void CarMainWindow::updateUserName()
525 newUserName = myLogin->getUserName();
526 ui->userNameLabel->setText( "User: " + newUserName);
528 if (newUserName.length())
530 ui->setUserPushButton->setText( "Change User");
531 this->setWindowTitle("Speed freak - " + newUserName);
535 ui->setUserPushButton->setText( "Set User");
536 this->setWindowTitle("Speed freak");
540 void CarMainWindow::regUserToServer()
542 myHttpClient->requestRegistration();
546 void CarMainWindow::on_drawRoutePushButton_clicked()
552 * Opens result dialog when show result button is clicked.
553 * Sends measures as parameter to the resultdialogs saveMeasuresToArray-function.
555 void CarMainWindow::on_pushButtonShowResultDialog_clicked()
558 meas.setTime10kmh(1.3);
559 meas.setTime20kmh(2.5);
560 meas.setTime30kmh(3.6);
561 meas.setTime40kmh(6.7);
562 meas.setTime50kmh(7.3);
563 meas.setTime60kmh(7.5);
564 meas.setTime70kmh(8.6);
565 meas.setTime80kmh(8.7);
566 meas.setTime90kmh(9.6);
567 meas.setTime100kmh(9.9);
568 result->setDiagramGapHorizontal(40);
569 result->saveMeasuresToArray(&meas);
570 this->result->show();
573 void CarMainWindow::userLogin()
575 myHttpClient->checkLogin();
579 * Resets Accelerometer measurement variables
581 void CarMainWindow::resetAccelerometerMeasurements()
583 currentAcceleration = 0;
584 currentAccelerationString = "";
587 distanceTraveled = "";
588 firstAcceleration = 0;
591 //lastScreenUpdateInSeconds = 0;
593 reverseAccelerationFlag = false;
594 stopWatch.setHMS(0, 0, 0, 0);
595 //accelerometer->stop();
597 vehicleStartedMoving = false;
602 * This function is called to handle checkpoints
603 *@param totalTime total time elapsed since starting measurements
604 *@param currentSpeed current speed of the device
606 void CarMainWindow::handleCheckPoint(double totalTime, double currentSpeed)
615 *This function is called to read (and process) data from the accelerometer
617 void CarMainWindow::readAccelerometerData()
620 double changeInAcceleration = 0;
623 accelerometer->getAcceleration(x, y, z);
624 accelerometer->smoothData(x, y, z);
627 x -= accelerometer->getCalibrationX();
628 y -= accelerometer->getCalibrationY();
629 z -= accelerometer->getCalibrationZ();
631 QString str = QString("acc x: " + QString::number(x) + "\n" +
632 "acc y: " + QString::number(y) + "\n" +
633 "acc z: " + QString::number(z) + "\n");
635 if (!vehicleStartedMoving)
639 firstAcceleration = sqrt(x*x + y*y + z*z);
640 //firstAcceleration = y; // first read
645 currentAcceleration = sqrt(x*x + y*y + z*z);
646 changeInAcceleration = (currentAcceleration - firstAcceleration); // firstAcceleration only gets set once
648 if (((abs(changeInAcceleration) <= accelerationStartThreshold)
649 && !vehicleStartedMoving))
654 if (reverseAccelerationFlag)
656 // will be false until after 1st calculation
657 if ((changeInAcceleration <= 0))
659 // actually increasing here...
660 changeInAcceleration = abs(changeInAcceleration);
664 // actually decreasing here...
665 changeInAcceleration = (changeInAcceleration * -1);
668 if (!vehicleStartedMoving)
670 if ((changeInAcceleration < 0))
672 // we started to move backwards first time through
673 reverseAccelerationFlag = true;
674 changeInAcceleration = abs(changeInAcceleration);
676 vehicleStartedMoving = true;
678 stopWatch.setHMS(0, 0, 0, 0);
681 // keep the following line as close to the SetKinematicsProperties method as possible
682 currentTime = stopWatch.elapsed();
683 calculate->calculateParameters(changeInAcceleration, (currentTime - previousTime)/1000);
684 previousTime = currentTime;
686 s.sprintf("%.2f", changeInAcceleration);
687 currentAccelerationString = s;
690 speed = calculate->getCurrentSpeed();
691 speed = ((speed*1000)/kSecondsInHour);
692 s.sprintf("%.2f", speed);
695 s.sprintf("%.2f", calculate->getDistanceTraveled());
696 distanceTraveled = s;
703 s.sprintf("%.2f", calculate->getTotalTime());
706 str.append("ca: " + currentAccelerationString + " G\n" );
707 str.append("cspeed: " + currentSpeed + " km/h \n" );
708 str.append("dist: " + distanceTraveled + " m \n" );
709 str.append("time: " + totalTime + " s \n" );
711 if ((stopTime > 0) && (previousTime >= stopTime))
713 // we want to end at a stopping point that the user chose
715 resetAccelerometerMeasurements();
720 *This function is used to calibrate accelerometer
722 void CarMainWindow::calibrateAccelerometer()
724 resetAccelerometerMeasurements();
725 accelerometer->calibrate();