Added feature: GPS on/off and checks if at least 4 satellites is in use.
[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     //GPS
40     location = new Maemo5Location(this);
41     gpsData = new GPSData(location);
42     connect(location,SIGNAL(agnss()),this,SLOT(gpsStatus()));
43
44     time = 0;
45     speed = 0;
46     timer = new QTimer();
47
48     accelerometer = new Accelerometer();
49     accelerometer->setSampleRate(100);
50
51     measures = new Measures();
52     this->initializeMeasures();
53
54     timer->setInterval(300);
55
56     connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
57     connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
58
59     ui->labelMeasureTabResult->hide();
60
61     this->setWindowTitle("Speed Freak");
62
63 }
64
65 /**
66   *Destructor of this class. Deletes all dynamic objects and sets them to NULL.
67   */
68 CarMainWindow::~CarMainWindow()
69 {
70     delete ui;
71     ui = NULL;
72     //delete result;
73     //delete measure;
74     delete categorylist;
75     categorylist = NULL;
76     delete welcomeDialog;
77     welcomeDialog = NULL;
78     delete myRoute;
79     myRoute = NULL;
80     delete gpsData;
81     gpsData = NULL;
82 }
83
84 /**
85   *This function is used to .
86   *@param
87   */
88 void CarMainWindow::changeEvent(QEvent *e)
89 {
90     QMainWindow::changeEvent(e);
91     switch (e->type()) {
92     case QEvent::LanguageChange:
93         ui->retranslateUi(this);
94         break;
95     default:
96         break;
97     }
98 }
99
100 /**
101   *This slot function is called when ever list view is update. Start-tab view.
102   */
103 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
104 {
105     QString str = index.data().toString();
106     QStringList list = str.split("-");
107     QStringList list2 = list[1].split(" ");
108
109     ui->lineEditStartTabMin->setText(list[0]);
110     ui->lineEditStartTabMax->setText(list2[0]);
111     updateComboBoxStartTabUnits(list2[1]);
112 }
113
114 /**
115   *This slot function is called when ever auto start button clicked. Start-tab view.
116   */
117 void CarMainWindow::on_autoStartButton_clicked()
118 {
119
120     //delete measure;
121     //measure = NULL;
122     //measure = new MeasureDialog();
123    // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
124     accelerometer->start();
125     timer->start();
126     // Show measure dialog.
127     //measure->show();
128     ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
129 }
130
131 /**
132   *This slot function is called when ever list view is update. Start-tab view.
133   *@param QString unit.
134   */
135 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
136 {
137     ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
138 }
139
140 /**
141   *This function is used to init unit combobox. Start-tab view.
142   */
143 void CarMainWindow::initComboBoxStartTabUnits()
144 {
145     units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
146     ui->comboBoxStartTabUnits->addItems(units);
147 }
148
149 /**
150   *This function is used to set items to unit combobox. Start-tab view.
151   *@param QStringlist units
152   */
153 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
154 {
155     ui->comboBoxStartTabUnits->addItems(units);
156 }
157
158 /**
159   *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
160   */
161 void CarMainWindow::initListViewStartTabAccelerationCategories()
162 {
163     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";
164     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
165     ui->listViewStartTabAccelerationCategories->setModel(model);
166 }
167
168 /**
169   *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
170   *@param QStringlist accelerationCategoriesStartTab
171   */
172 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
173 {
174     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
175     ui->listViewStartTabAccelerationCategories->setModel(model);
176 }
177
178 /**
179   *This function is used to set items to category combobox. Top-tab view.
180   *@param
181   */
182 void CarMainWindow::setCategoryCompoBox()
183 {
184     ui->comboBoxTopCategory->addItems(categorylist->getCategoryList());
185 }
186
187 /**
188   *This function is used to set items to labelTopList. Top-tab view.
189   *@param QString category
190   */
191 void CarMainWindow::setListViewTopList(QString category, int size)
192 {
193     QString topList;
194     topList.append( categorylist->getTopList(category, size));
195     ui->labelTopList->setText(topList);
196 }
197
198 /**
199   *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
200   */
201 void CarMainWindow::openResultView()
202 {
203     //result->saveMeasuresToArray(measure->measures);
204     // Show result dialog.
205     //result->show();
206     ui->pushButtonSendResult->setEnabled(true);
207     QString timeInteger;
208     timeInteger.setNum(this->measures->getTime40kmh());
209     //time = "0 - 40 km/h: ";
210     //time.append(timeInteger);
211     //ui->labelResult40kmh->setText(time);
212     ui->labelMeasureTabResult->show();
213     ui->labelMeasureTabResult->setText(timeInteger);
214     //ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
215 }
216
217 /**
218   *This slot function is called when registrate button is clicked.
219   */
220 void CarMainWindow::on_registratePushButton_clicked()
221 {
222     myRegistration->show();
223 }
224
225 /**
226   *This slot function is called when ever refresh button clicked. Top-tab view.
227   */
228 void CarMainWindow::on_buttonTopRefresh_clicked()
229 {
230     myHttpClient->requestCategories();
231     setCategoryCompoBox();
232 }
233
234 /**
235   *This slot function is called when ever category combobox current index changed. Top-tab view.
236   *@param QString category
237   *@todo Check where limitNr is taken.
238   */
239 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
240 {
241     int limitNr = 5;                    //replace with real value?
242     QString limit = QString::number(limitNr);
243     category = "acceleration-0-100";    //replace with real value from category list/top window
244     myHttpClient->requestTopList(category, limit);
245     setListViewTopList(category,10);
246 }
247
248 /**
249   *This slot function is called when ever category combobox activated. Top-tab view.
250   *@param QString category
251   */
252 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
253 {
254     setListViewTopList(category,10);
255 }
256
257 /**
258   *This slot function is called when set/change user button is clicked.
259   */
260 void CarMainWindow::on_setUserPushButton_clicked()
261 {
262     myLogin->show();
263 }
264
265 /**
266   *@brief Just for development, for the real button is not shown until
267   *measurin started and there are results.
268   *@todo Implement with real code and yet leave sendXml in the bottom in use.
269   */
270 void CarMainWindow::on_manualStartButton_clicked()
271 {
272
273 }
274
275 /**
276   * This slot function is called when timer gives timeout signal. Checks current speed
277   * and stores times in measure class.
278   */
279 void CarMainWindow::after_timeout()
280 {
281     QString timeString, speedString;
282     //time++;
283     time = accelerometer->getTotalTime();
284     speed = accelerometer->getCurrentSpeed();
285     //speed = speed +10;
286
287     if (floor(speed) == 10)
288     {
289         measures->setTime10kmh(time);
290     }
291
292     else if (floor(speed) == 20)
293     {
294         measures->setTime20kmh(time);
295     }
296
297     else if (floor(speed) == 30)
298     {
299         measures->setTime30kmh(time);
300     }
301
302     else if (floor(speed) == 40)
303     {
304         measures->setTime40kmh(time);
305     }
306
307     else if (floor(speed) == 50)
308     {
309         measures->setTime50kmh(time);
310     }
311
312     else if (floor(speed) == 60)
313     {
314         measures->setTime60kmh(time);
315     }
316
317     else if (floor(speed) == 70)
318     {
319         measures->setTime70kmh(time);
320     }
321
322     else if (floor(speed) == 80)
323     {
324         measures->setTime80kmh(time);
325     }
326
327     else if (floor(speed) == 90)
328     {
329         measures->setTime90kmh(time);
330     }
331
332     else if (floor(speed) == 100)
333     {
334         measures->setTime100kmh(time);
335     }
336
337     else
338     {
339
340     }
341
342     // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
343     if (speed >= 40.0)
344     {
345         timer->stop();
346         accelerometer->stop();
347         time = 0;
348         speed = 0;
349         //emit this->speedAchieved();
350         this->openResultView();
351         //this->close();
352
353     }
354
355     // Updates speed and time.
356     else
357     {
358         timeString.setNum(time);
359         speedString.setNum(speed);
360         ui->labelMeasureTabTime->setText(timeString);
361         ui->labelMeasureTabSpeed->setText(speedString);
362
363         timer->start();
364     }
365
366 }
367
368 /**
369   * Initializes measures class's member variables.
370   */
371 void CarMainWindow::initializeMeasures()
372 {
373     measures->setTime10kmh(0);
374     measures->setTime20kmh(0);
375     measures->setTime30kmh(0);
376     measures->setTime40kmh(0);
377     measures->setTime50kmh(0);
378     measures->setTime60kmh(0);
379     measures->setTime70kmh(0);
380     measures->setTime80kmh(0);
381     measures->setTime90kmh(0);
382     measures->setTime100kmh(0);
383 }
384
385 /**
386   * This slot function is called when Abort button is clicked.
387   */
388 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
389 {
390     measures->setTime10kmh(0);
391     measures->setTime20kmh(0);
392     measures->setTime30kmh(0);
393     measures->setTime40kmh(0);
394     measures->setTime50kmh(0);
395     measures->setTime60kmh(0);
396     measures->setTime70kmh(0);
397     measures->setTime80kmh(0);
398     measures->setTime90kmh(0);
399     measures->setTime100kmh(0);
400     timer->stop();
401     accelerometer->stop();
402     time = 0;
403     speed = 0;
404     ui->tabWidget->setCurrentWidget(this->ui->StartTab);
405     //this->close();
406 }
407
408 void CarMainWindow::on_pushButtonSendResult_clicked()
409 {
410     myHttpClient->sendResultXml();
411     ui->pushButtonSendResult->setEnabled(false);
412 }
413
414 void CarMainWindow::updateUserName()
415 {
416     QString newUserName;
417
418     newUserName = myLogin->getUserName();
419     ui->userNameLabel->setText( "User: " + newUserName);
420
421     if (newUserName.length())
422     {
423        ui->setUserPushButton->setText( "Change User");
424        this->setWindowTitle("Speed freak - " + newUserName);
425     }
426     else
427     {
428         ui->setUserPushButton->setText( "Set User");
429         this->setWindowTitle("Speed freak");
430     }
431 }
432
433 void CarMainWindow::regUserToServer()
434 {
435     myHttpClient->requestRegistration();
436 }
437
438
439 void CarMainWindow::on_drawRoutePushButton_clicked()
440 {
441     myRoute->show();
442 }
443
444 void CarMainWindow::userLogin()
445 {
446     myHttpClient->checkLogin();
447 }
448
449 /**
450   *This slot function is called when GPS on checkbox state changed.  Route-tab view.
451   *@param int GPSState
452   */
453 void CarMainWindow::on_gpsOnCheckBox_stateChanged(int GPSState)
454 {
455     if (GPSState == 0)
456     {
457         ui->labelRouteTabGPSStatus->setText("GPS off");//testing
458         location->stopPollingGPS();
459     }
460     else
461     {
462         ui->labelRouteTabGPSStatus->setText("GPS on");//testing
463         location->startPollingGPS();
464     }
465 }
466
467 /**
468   *This slot function is called when GPS status changed.  Route-tab view.
469   */
470 void CarMainWindow::gpsStatus()
471 {
472     if (ui->gpsOnCheckBox->isChecked())
473     {
474         if (location->getSatellitesInUse() >= 4)
475         {
476             ui->labelRouteTabGPSStatus->setText("GPS ready");
477         }
478
479         else
480         {
481             ui->labelRouteTabGPSStatus->setText("Waiting for GPS");
482         }
483     }
484 }