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