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