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 * @copyright (c) 2010 Speed Freak team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 #include "carmainwindow.h"
16 *Constructor of this class.
17 *@param QWidget pointer to parent object. By default the value is NULL.
19 CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow)
22 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
23 result = new ResultDialog();
24 //measure = new MeasureDialog();
25 welcomeDialog = new WelcomeDialog();
26 welcomeDialog->show();
28 initComboBoxStartTabUnits();
29 initListViewStartTabAccelerationCategories();
31 myLogin = new LoginWindow(this);
32 categorylist = new CategoryList();
33 myHttpClient = new HttpClient(this);
34 myRegistration = new Registration(this);
35 connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(regUserToServer()));
36 connect(myLogin,SIGNAL(userNameChanged()),this,SLOT(userLogin()));
37 myRoute = new RouteDialog( this);
43 accelerometer = new Accelerometer();
44 accelerometer->setSampleRate(100);
46 measures = new Measures();
47 this->initializeMeasures();
49 timer->setInterval(300);
51 connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
52 connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
54 ui->labelMeasureTabResult->hide();
56 this->setWindowTitle("Speed Freak");
61 *Destructor of this class. Should be used to release all allocated resources.
63 CarMainWindow::~CarMainWindow()
74 *This function is used to .
77 void CarMainWindow::changeEvent(QEvent *e)
79 QMainWindow::changeEvent(e);
81 case QEvent::LanguageChange:
82 ui->retranslateUi(this);
90 *This slot function is called when ever list view is update. Start-tab view.
92 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
94 QString str = index.data().toString();
95 QStringList list = str.split("-");
96 QStringList list2 = list[1].split(" ");
98 ui->lineEditStartTabMin->setText(list[0]);
99 ui->lineEditStartTabMax->setText(list2[0]);
100 updateComboBoxStartTabUnits(list2[1]);
104 *This slot function is called when ever auto start button clicked. Start-tab view.
106 void CarMainWindow::on_autoStartButton_clicked()
108 choice = ui->listViewStartTabAccelerationCategories->currentIndex();
109 choiceInt = choice.row();
110 qDebug() << choiceInt;
113 ui->labelMeasureTabHeader->setText("Accelerate to 40 km/h");
114 result->setDiagramGapStem(75);
117 else if (choiceInt == 1)
119 ui->labelMeasureTabHeader->setText("Accelerate to 100 km/h");
120 result->setDiagramGapStem(30);
125 ui->labelMeasureTabHeader->setText("Accelerate to 80 km/h");
126 result->setDiagramGapStem(37.5);
128 ui->labelMeasureTabResult->setText("");
131 //measure = new MeasureDialog();
132 // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
133 accelerometer->start();
135 // Show measure dialog.
138 // TODO: Move next if else to the function which is called when target speed
142 if (floor(this->measures->getTime40kmh()) <= 5)
144 result->setDiagramGapHorizontal(80);
147 else if (floor(this->measures->getTime40kmh()) <= 10)
149 result->setDiagramGapHorizontal(40);
154 result->setDiagramGapHorizontal(20);
160 if (floor(this->measures->getTime40kmh()) <= 5)
162 result->setDiagramGapHorizontal(80);
165 else if (floor(this->measures->getTime40kmh()) <= 10)
167 result->setDiagramGapHorizontal(40);
172 result->setDiagramGapHorizontal(20);
176 ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
180 *This slot function is called when ever list view is update. Start-tab view.
181 *@param QString unit.
183 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
185 ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
189 *This function is used to init unit combobox. Start-tab view.
191 void CarMainWindow::initComboBoxStartTabUnits()
193 units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
194 ui->comboBoxStartTabUnits->addItems(units);
198 *This function is used to set items to unit combobox. Start-tab view.
199 *@param QStringlist units
201 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
203 ui->comboBoxStartTabUnits->addItems(units);
207 *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
209 void CarMainWindow::initListViewStartTabAccelerationCategories()
211 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";
212 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
213 ui->listViewStartTabAccelerationCategories->setModel(model);
217 *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
218 *@param QStringlist accelerationCategoriesStartTab
220 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
222 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
223 ui->listViewStartTabAccelerationCategories->setModel(model);
227 *This function is used to set items to category combobox. Top-tab view.
230 void CarMainWindow::setCategoryCompoBox()
232 ui->comboBoxTopCategory->addItems(categorylist->getCategoryList());
236 *This function is used to set items to labelTopList. Top-tab view.
237 *@param QString category
239 void CarMainWindow::setListViewTopList(QString category, int size)
242 topList.append( categorylist->getTopList(category, size));
243 ui->labelTopList->setText(topList);
247 *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
249 void CarMainWindow::openResultView()
251 //result->saveMeasuresToArray(measure->measures);
252 // Show result dialog.
254 ui->pushButtonSendResult->setEnabled(true);
258 if (floor(this->measures->getTime40kmh()) <= 5)
260 result->setDiagramGapHorizontal(80);
263 else if (floor(this->measures->getTime40kmh()) <= 10)
265 result->setDiagramGapHorizontal(40);
270 result->setDiagramGapHorizontal(20);
276 if (floor(this->measures->getTime40kmh()) <= 5)
278 result->setDiagramGapHorizontal(80);
281 else if (floor(this->measures->getTime40kmh()) <= 10)
283 result->setDiagramGapHorizontal(40);
288 result->setDiagramGapHorizontal(20);
291 timeInteger.setNum(this->measures->getTime40kmh());
292 //time = "0 - 40 km/h: ";
293 //time.append(timeInteger);
294 //ui->labelResult40kmh->setText(time);
295 ui->labelMeasureTabResult->show();
296 ui->labelMeasureTabResult->setText(timeInteger);
297 //ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
301 *This slot function is called when registrate button is clicked.
303 void CarMainWindow::on_registratePushButton_clicked()
305 myRegistration->show();
309 *This slot function is called when ever refresh button clicked. Top-tab view.
311 void CarMainWindow::on_buttonTopRefresh_clicked()
313 myHttpClient->requestCategories();
314 setCategoryCompoBox();
318 *This slot function is called when ever category combobox current index changed. Top-tab view.
319 *@param QString category
320 *@todo Check where limitNr is taken.
322 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
324 int limitNr = 5; //replace with real value?
325 QString limit = QString::number(limitNr);
326 category = "acceleration-0-100"; //replace with real value from category list/top window
327 myHttpClient->requestTopList(category, limit);
328 setListViewTopList(category,10);
332 *This slot function is called when ever category combobox activated. Top-tab view.
333 *@param QString category
335 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
337 setListViewTopList(category,10);
341 *This slot function is called when set/change user button is clicked.
343 void CarMainWindow::on_setUserPushButton_clicked()
349 *@brief Just for development, for the real button is not shown until
350 *measurin started and there are results.
351 *@todo Implement with real code and yet leave sendXml in the bottom in use.
353 void CarMainWindow::on_manualStartButton_clicked()
359 * This slot function is called when timer gives timeout signal. Checks current speed
360 * and stores times in measure class.
362 void CarMainWindow::after_timeout()
364 QString timeString, speedString;
366 time = accelerometer->getTotalTime();
367 speed = accelerometer->getCurrentSpeed();
370 if (floor(speed) == 10)
372 measures->setTime10kmh(time);
375 else if (floor(speed) == 20)
377 measures->setTime20kmh(time);
380 else if (floor(speed) == 30)
382 measures->setTime30kmh(time);
385 else if (floor(speed) == 40)
387 measures->setTime40kmh(time);
390 else if (floor(speed) == 50)
392 measures->setTime50kmh(time);
395 else if (floor(speed) == 60)
397 measures->setTime60kmh(time);
400 else if (floor(speed) == 70)
402 measures->setTime70kmh(time);
405 else if (floor(speed) == 80)
407 measures->setTime80kmh(time);
410 else if (floor(speed) == 90)
412 measures->setTime90kmh(time);
415 else if (floor(speed) == 100)
417 measures->setTime100kmh(time);
425 // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
429 accelerometer->stop();
432 //emit this->speedAchieved();
433 this->openResultView();
438 // Updates speed and time.
441 timeString.setNum(time);
442 speedString.setNum(speed);
443 ui->labelMeasureTabTime->setText(timeString);
444 ui->labelMeasureTabSpeed->setText(speedString);
452 * Initializes measures class's member variables.
454 void CarMainWindow::initializeMeasures()
456 measures->setTime10kmh(0);
457 measures->setTime20kmh(0);
458 measures->setTime30kmh(0);
459 measures->setTime40kmh(0);
460 measures->setTime50kmh(0);
461 measures->setTime60kmh(0);
462 measures->setTime70kmh(0);
463 measures->setTime80kmh(0);
464 measures->setTime90kmh(0);
465 measures->setTime100kmh(0);
469 * This slot function is called when Abort button is clicked.
471 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
473 measures->setTime10kmh(0);
474 measures->setTime20kmh(0);
475 measures->setTime30kmh(0);
476 measures->setTime40kmh(0);
477 measures->setTime50kmh(0);
478 measures->setTime60kmh(0);
479 measures->setTime70kmh(0);
480 measures->setTime80kmh(0);
481 measures->setTime90kmh(0);
482 measures->setTime100kmh(0);
484 accelerometer->stop();
487 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
491 void CarMainWindow::on_pushButtonSendResult_clicked()
493 myHttpClient->sendResultXml();
494 ui->pushButtonSendResult->setEnabled(false);
497 void CarMainWindow::updateUserName()
501 newUserName = myLogin->getUserName();
502 ui->userNameLabel->setText( "User: " + newUserName);
504 if (newUserName.length())
506 ui->setUserPushButton->setText( "Change User");
507 this->setWindowTitle("Speed freak - " + newUserName);
511 ui->setUserPushButton->setText( "Set User");
512 this->setWindowTitle("Speed freak");
516 void CarMainWindow::regUserToServer()
518 myHttpClient->requestRegistration();
522 void CarMainWindow::on_drawRoutePushButton_clicked()
528 * Opens result dialog when show result button is clicked.
529 * Sends measures as parameter to the resultdialogs saveMeasuresToArray-function.
531 void CarMainWindow::on_pushButtonShowResultDialog_clicked()
534 meas.setTime10kmh(1.3);
535 meas.setTime20kmh(2.5);
536 meas.setTime30kmh(3.6);
537 meas.setTime40kmh(6.7);
538 meas.setTime50kmh(7.3);
539 meas.setTime60kmh(7.5);
540 meas.setTime70kmh(8.6);
541 meas.setTime80kmh(8.7);
542 meas.setTime90kmh(9.6);
543 meas.setTime100kmh(9.9);
544 result->setDiagramGapHorizontal(40);
545 result->saveMeasuresToArray(&meas);
546 this->result->show();
549 void CarMainWindow::userLogin()
551 myHttpClient->checkLogin();