Added feature which scales result diagrams speed axel right way concerning the choice...
[speedfreak] / Client / carmainwindow.cpp
1 /*
2  * CarMainWindow main class
3  *
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
10  */
11
12 #include "carmainwindow.h"
13 #include "math.h"
14
15 /**
16   *Constructor of this class.
17   *@param QWidget pointer to parent object. By default the value is NULL.
18   */
19 CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow)
20 {
21     ui->setupUi(this);
22     ui->tabWidget->setCurrentWidget(this->ui->StartTab);
23     result = new ResultDialog();
24     //measure = new MeasureDialog();
25     welcomeDialog = new WelcomeDialog();
26     welcomeDialog->show();
27
28     initComboBoxStartTabUnits();
29     initListViewStartTabAccelerationCategories();
30
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);
38
39     time = 0;
40     speed = 0;
41     timer = new QTimer();
42
43     accelerometer = new Accelerometer();
44     accelerometer->setSampleRate(100);
45
46     measures = new Measures();
47     this->initializeMeasures();
48
49     timer->setInterval(300);
50
51     connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
52     connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
53
54     ui->labelMeasureTabResult->hide();
55
56     this->setWindowTitle("Speed Freak");
57
58 }
59
60 /**
61   *Destructor of this class. Should be used to release all allocated resources.
62   */
63 CarMainWindow::~CarMainWindow()
64 {
65     delete ui;
66     //delete result;
67     //delete measure;
68     delete categorylist;
69     delete welcomeDialog;
70     delete myRoute;
71 }
72
73 /**
74   *This function is used to .
75   *@param
76   */
77 void CarMainWindow::changeEvent(QEvent *e)
78 {
79     QMainWindow::changeEvent(e);
80     switch (e->type()) {
81     case QEvent::LanguageChange:
82         ui->retranslateUi(this);
83         break;
84     default:
85         break;
86     }
87 }
88
89 /**
90   *This slot function is called when ever list view is update. Start-tab view.
91   */
92 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
93 {
94     QString str = index.data().toString();
95     QStringList list = str.split("-");
96     QStringList list2 = list[1].split(" ");
97
98     ui->lineEditStartTabMin->setText(list[0]);
99     ui->lineEditStartTabMax->setText(list2[0]);
100     updateComboBoxStartTabUnits(list2[1]);
101 }
102
103 /**
104   *This slot function is called when ever auto start button clicked. Start-tab view.
105   */
106 void CarMainWindow::on_autoStartButton_clicked()
107 {
108     choice = ui->listViewStartTabAccelerationCategories->currentIndex();
109     choiceInt = choice.row();
110     qDebug() << choiceInt;
111     if (choiceInt == 0)
112     {
113         ui->labelMeasureTabHeader->setText("Accelerate to 40 km/h");
114         result->setDiagramGapStem(75);
115     }
116
117     else if (choiceInt == 1)
118     {
119         ui->labelMeasureTabHeader->setText("Accelerate to 100 km/h");
120         result->setDiagramGapStem(30);
121     }
122
123     else
124     {
125         ui->labelMeasureTabHeader->setText("Accelerate to 80 km/h");
126         result->setDiagramGapStem(37.5);
127     }
128     ui->labelMeasureTabResult->setText("");
129     //delete measure;
130     //measure = NULL;
131     //measure = new MeasureDialog();
132    // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
133     accelerometer->start();
134     timer->start();
135     // Show measure dialog.
136     //measure->show();
137
138     // TODO: Move next if else to the function which is called when target speed
139     // has reached.
140     if (choiceInt == 0)
141     {
142         if (floor(this->measures->getTime40kmh()) <= 5)
143         {
144             result->setDiagramGapHorizontal(80);
145         }
146
147         else if (floor(this->measures->getTime40kmh()) <= 10)
148         {
149             result->setDiagramGapHorizontal(40);
150         }
151
152         else
153         {
154             result->setDiagramGapHorizontal(20);
155         }
156     }
157
158     else
159     {
160         if (floor(this->measures->getTime40kmh()) <= 5)
161         {
162             result->setDiagramGapHorizontal(80);
163         }
164
165         else if (floor(this->measures->getTime40kmh()) <= 10)
166         {
167             result->setDiagramGapHorizontal(40);
168         }
169
170         else
171         {
172             result->setDiagramGapHorizontal(20);
173         }
174     }
175
176     ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
177 }
178
179 /**
180   *This slot function is called when ever list view is update. Start-tab view.
181   *@param QString unit.
182   */
183 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
184 {
185     ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
186 }
187
188 /**
189   *This function is used to init unit combobox. Start-tab view.
190   */
191 void CarMainWindow::initComboBoxStartTabUnits()
192 {
193     units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
194     ui->comboBoxStartTabUnits->addItems(units);
195 }
196
197 /**
198   *This function is used to set items to unit combobox. Start-tab view.
199   *@param QStringlist units
200   */
201 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
202 {
203     ui->comboBoxStartTabUnits->addItems(units);
204 }
205
206 /**
207   *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
208   */
209 void CarMainWindow::initListViewStartTabAccelerationCategories()
210 {
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);
214 }
215
216 /**
217   *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
218   *@param QStringlist accelerationCategoriesStartTab
219   */
220 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
221 {
222     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
223     ui->listViewStartTabAccelerationCategories->setModel(model);
224 }
225
226 /**
227   *This function is used to set items to category combobox. Top-tab view.
228   *@param
229   */
230 void CarMainWindow::setCategoryCompoBox()
231 {
232     ui->comboBoxTopCategory->addItems(categorylist->getCategoryList());
233 }
234
235 /**
236   *This function is used to set items to labelTopList. Top-tab view.
237   *@param QString category
238   */
239 void CarMainWindow::setListViewTopList(QString category, int size)
240 {
241     QString topList;
242     topList.append( categorylist->getTopList(category, size));
243     ui->labelTopList->setText(topList);
244 }
245
246 /**
247   *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
248   */
249 void CarMainWindow::openResultView()
250 {
251     //result->saveMeasuresToArray(measure->measures);
252     // Show result dialog.
253     //result->show();
254     ui->pushButtonSendResult->setEnabled(true);
255     QString timeInteger;
256     if (choiceInt == 0)
257     {
258         if (floor(this->measures->getTime40kmh()) <= 5)
259         {
260             result->setDiagramGapHorizontal(80);
261         }
262
263         else if (floor(this->measures->getTime40kmh()) <= 10)
264         {
265             result->setDiagramGapHorizontal(40);
266         }
267
268         else
269         {
270             result->setDiagramGapHorizontal(20);
271         }
272     }
273
274     else
275     {
276         if (floor(this->measures->getTime40kmh()) <= 5)
277         {
278             result->setDiagramGapHorizontal(80);
279         }
280
281         else if (floor(this->measures->getTime40kmh()) <= 10)
282         {
283             result->setDiagramGapHorizontal(40);
284         }
285
286         else
287         {
288             result->setDiagramGapHorizontal(20);
289         }
290     }
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);
298 }
299
300 /**
301   *This slot function is called when registrate button is clicked.
302   */
303 void CarMainWindow::on_registratePushButton_clicked()
304 {
305     myRegistration->show();
306 }
307
308 /**
309   *This slot function is called when ever refresh button clicked. Top-tab view.
310   */
311 void CarMainWindow::on_buttonTopRefresh_clicked()
312 {
313     myHttpClient->requestCategories();
314     setCategoryCompoBox();
315 }
316
317 /**
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.
321   */
322 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
323 {
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);
329 }
330
331 /**
332   *This slot function is called when ever category combobox activated. Top-tab view.
333   *@param QString category
334   */
335 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
336 {
337     setListViewTopList(category,10);
338 }
339
340 /**
341   *This slot function is called when set/change user button is clicked.
342   */
343 void CarMainWindow::on_setUserPushButton_clicked()
344 {
345     myLogin->show();
346 }
347
348 /**
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.
352   */
353 void CarMainWindow::on_manualStartButton_clicked()
354 {
355
356 }
357
358 /**
359   * This slot function is called when timer gives timeout signal. Checks current speed
360   * and stores times in measure class.
361   */
362 void CarMainWindow::after_timeout()
363 {
364     QString timeString, speedString;
365     //time++;
366     time = accelerometer->getTotalTime();
367     speed = accelerometer->getCurrentSpeed();
368     //speed = speed +10;
369
370     if (floor(speed) == 10)
371     {
372         measures->setTime10kmh(time);
373     }
374
375     else if (floor(speed) == 20)
376     {
377         measures->setTime20kmh(time);
378     }
379
380     else if (floor(speed) == 30)
381     {
382         measures->setTime30kmh(time);
383     }
384
385     else if (floor(speed) == 40)
386     {
387         measures->setTime40kmh(time);
388     }
389
390     else if (floor(speed) == 50)
391     {
392         measures->setTime50kmh(time);
393     }
394
395     else if (floor(speed) == 60)
396     {
397         measures->setTime60kmh(time);
398     }
399
400     else if (floor(speed) == 70)
401     {
402         measures->setTime70kmh(time);
403     }
404
405     else if (floor(speed) == 80)
406     {
407         measures->setTime80kmh(time);
408     }
409
410     else if (floor(speed) == 90)
411     {
412         measures->setTime90kmh(time);
413     }
414
415     else if (floor(speed) == 100)
416     {
417         measures->setTime100kmh(time);
418     }
419
420     else
421     {
422
423     }
424
425     // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
426     if (speed >= 40.0)
427     {
428         timer->stop();
429         accelerometer->stop();
430         time = 0;
431         speed = 0;
432         //emit this->speedAchieved();
433         this->openResultView();
434         //this->close();
435
436     }
437
438     // Updates speed and time.
439     else
440     {
441         timeString.setNum(time);
442         speedString.setNum(speed);
443         ui->labelMeasureTabTime->setText(timeString);
444         ui->labelMeasureTabSpeed->setText(speedString);
445
446         timer->start();
447     }
448
449 }
450
451 /**
452   * Initializes measures class's member variables.
453   */
454 void CarMainWindow::initializeMeasures()
455 {
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);
466 }
467
468 /**
469   * This slot function is called when Abort button is clicked.
470   */
471 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
472 {
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);
483     timer->stop();
484     accelerometer->stop();
485     time = 0;
486     speed = 0;
487     ui->tabWidget->setCurrentWidget(this->ui->StartTab);
488     //this->close();
489 }
490
491 void CarMainWindow::on_pushButtonSendResult_clicked()
492 {
493     myHttpClient->sendResultXml();
494     ui->pushButtonSendResult->setEnabled(false);
495 }
496
497 void CarMainWindow::updateUserName()
498 {
499     QString newUserName;
500
501     newUserName = myLogin->getUserName();
502     ui->userNameLabel->setText( "User: " + newUserName);
503
504     if (newUserName.length())
505     {
506        ui->setUserPushButton->setText( "Change User");
507        this->setWindowTitle("Speed freak - " + newUserName);
508     }
509     else
510     {
511         ui->setUserPushButton->setText( "Set User");
512         this->setWindowTitle("Speed freak");
513     }
514 }
515
516 void CarMainWindow::regUserToServer()
517 {
518     myHttpClient->requestRegistration();
519 }
520
521
522 void CarMainWindow::on_drawRoutePushButton_clicked()
523 {
524     myRoute->show();
525 }
526
527 /**
528   * Opens result dialog when show result button is clicked.
529   * Sends measures as parameter to the resultdialogs saveMeasuresToArray-function.
530   */
531 void CarMainWindow::on_pushButtonShowResultDialog_clicked()
532 {
533     Measures meas;
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();
547 }
548
549 void CarMainWindow::userLogin()
550 {
551     myHttpClient->checkLogin();
552 }