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 //result = new ResultDialog();
23 //measure = new MeasureDialog();
24 xmlreader = new XmlReader();
26 initComboBoxStartTabUnits();
27 initListViewStartTabAccelerationCategories();
29 myLogin = new LoginWindow(this);
30 myRegistration = new Registration(this);
31 xmlwriter = new XmlWriter();
32 manager = new QNetworkAccessManager(this);
33 connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*)));
34 connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(registrate()));
35 connect(this,SIGNAL(sendresult()),this,SLOT(sendXml()));
41 accelerometer = new Accelerometer();
42 accelerometer->setSampleRate(100);
44 measures = new Measures();
45 this->initializeMeasures();
47 timer->setInterval(300);
49 connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
50 connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
52 ui->labelMeasureTabResult->hide();
54 this->setWindowTitle("Speed bfreak");
59 *Destructor of this class. Should be used to release all allocated resources.
61 CarMainWindow::~CarMainWindow()
72 *This function is used to .
75 void CarMainWindow::changeEvent(QEvent *e)
77 QMainWindow::changeEvent(e);
79 case QEvent::LanguageChange:
80 ui->retranslateUi(this);
88 *This slot function is called when ever list view is update. Start-tab view.
90 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
92 QString str = index.data().toString();
93 QStringList list = str.split("-");
94 QStringList list2 = list[1].split(" ");
96 ui->lineEditStartTabMin->setText(list[0]);
97 ui->lineEditStartTabMax->setText(list2[0]);
98 updateComboBoxStartTabUnits(list2[1]);
102 *This slot function is called when ever auto start button clicked. Start-tab view.
104 void CarMainWindow::on_autoStartButton_clicked()
109 //measure = new MeasureDialog();
110 // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
111 accelerometer->start();
113 // Show measure dialog.
115 ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
119 *This slot function is called when ever list view is update. Start-tab view.
120 *@param QString unit.
122 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
124 ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
128 *This function is used to init unit combobox. Start-tab view.
130 void CarMainWindow::initComboBoxStartTabUnits()
132 units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
133 ui->comboBoxStartTabUnits->addItems(units);
137 *This function is used to set items to unit combobox. Start-tab view.
138 *@param QStringlist units
140 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
142 ui->comboBoxStartTabUnits->addItems(units);
146 *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
148 void CarMainWindow::initListViewStartTabAccelerationCategories()
150 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";
151 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
152 ui->listViewStartTabAccelerationCategories->setModel(model);
156 *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
157 *@param QStringlist accelerationCategoriesStartTab
159 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
161 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
162 ui->listViewStartTabAccelerationCategories->setModel(model);
166 *This function is used to set items to category combobox. Top-tab view.
169 void CarMainWindow::setCategoryCompoBox()
171 ui->comboBoxTopCategory->addItems(xmlreader->getTop10List());
175 *This function is used to set items to labelTopList. Top-tab view.
176 *@param QString category
178 void CarMainWindow::setListViewTopList(QString category)
182 if (category == "acceleration-0-100")
184 topList.append(xmlreader->getTop10AccelerationList());
187 else if (category == "Speed")
189 topList.append(xmlreader->getTop10SpeedList());
192 else if (category == "G-force")
194 topList.append(xmlreader->getTop10GforceList());
196 ui->labelTopList->setText(topList);
200 *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
202 void CarMainWindow::openResultView()
204 //result->saveMeasuresToArray(measure->measures);
205 // Show result dialog.
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);
218 *This slot function is called when the server has finished guery.
220 void CarMainWindow::networkResponse(QNetworkReply *reply)
226 *This slot function is called when the user will to send data to server.
227 *@todo Where is this callback connected?
229 void CarMainWindow::on_pushButton_clicked()
235 *This slot function is called when login/logout button is clicked.
237 void CarMainWindow::on_loginLogoutButton_clicked()
243 *This slot function is called when registrate button is clicked.
245 void CarMainWindow::on_registratePushButton_clicked()
247 myRegistration->show();
251 *This slot function is called when ever refresh button clicked. Top-tab view.
253 void CarMainWindow::on_buttonTopRefresh_clicked()
255 //setCategoryCompoBox();
260 *This slot function is called when ever category combobox current index changed. Top-tab view.
261 *@param QString category
263 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
265 setListViewTopList(category);
269 *This slot function is called when ever category combobox activated. Top-tab view.
270 *@param QString category
272 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
274 setListViewTopList(category);
278 *This slot function is called when set/change user button is clicked.
280 void CarMainWindow::on_setUserPushButton_clicked()
284 ui->userNameLabel->setText( "User: " + myLogin->getUserName());
285 ui->setUserPushButton->setText( "Change User");
289 *@brief Sends registration information to the server in xml format.
290 *Reads user name, password and emaol address from resuldialogs internal variables.
291 *@todo Replace msg box with better reaction to server`s responce.
292 *@todo Write error handling.
294 void CarMainWindow::registrate()
296 qDebug() << "_registrate" ;
297 qDebug() << this->myRegistration->getUserName() << "+" << this->myRegistration->getPassword() << "+" << this->myRegistration->getEmail();
299 QBuffer *regbuffer = new QBuffer();
300 QNetworkReply *currentDownload;
302 QUrl qurl("http://api.speedfreak-app.com/register");
303 QNetworkRequest request(qurl);
305 //write also to a file during development, :
306 xmlwriter->writeXml(this->myRegistration->getUserName(),
307 this->myRegistration->getPassword(),
308 this->myRegistration->getEmail());
309 xmlwriter->writeRegistering(regbuffer,
310 this->myRegistration->getUserName(),
311 this->myRegistration->getPassword(),
312 this->myRegistration->getEmail());
313 //Tmp msgbox - later server responce
314 QMessageBox::about(this,"Registrate",this->myRegistration->getUserName() + this->myRegistration->getPassword() + this->myRegistration->getEmail());
316 currentDownload = manager->post(request, ("data=" + regbuffer->data()));
318 //ackFromServer function gets called when HTTP request is completed
319 connect(currentDownload, SIGNAL(finished()),SLOT(ackOfRegistration()));
320 manager->post(request, ("data=" + regbuffer->data()));
322 //ackOfRegistration function gets called when HTTP request is completed
323 //connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfRegistration()));
324 connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfRegistration(QNetworkReply*)));
325 connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
329 *@brief Sends result(s) to the server in xml format with authentication information in the header.
330 *@todo Write error handling.
332 void CarMainWindow::sendXml()
334 qDebug() << "_sendXml";
336 QBuffer *xmlbuffer = new QBuffer();
337 QNetworkReply *currentDownload;
339 QString category_name = "acceleration-0-100"; //replace with real value from category list
341 QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
342 credentials = "Basic " + credentials.toAscii().toBase64();
344 QUrl qurl("http://api.speedfreak-app.com/update/category_name");
345 QNetworkRequest request(qurl);
347 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
349 xmlwriter->writeResult(xmlbuffer);
351 //currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
352 manager->post(request, ("data=" + xmlbuffer->data()));
353 //QString data("abcdefg"); //testing
354 //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data)); //testing
356 //ackOfResult function gets called when HTTP request is completed
357 //connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfResult()));
358 connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfResult(QNetworkReply*)));
359 connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
364 *@brief Sends request to the server for a top list with authentication information in the header.
365 *@todo Write error handling.
366 *@todo Replace with real value from category list and limit
368 void CarMainWindow::requestTopList()
370 qDebug() << "_registrate" ;
372 QString category_name = "acceleration-0-100"; //replace with real value from category list/top window
374 //QNetworkReply *currentDownload;
376 QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
377 credentials = "Basic " + credentials.toAscii().toBase64();
379 QUrl qurl("http://api.speedfreak-app.com/results/category_name/limit");
380 QNetworkRequest request(qurl);
382 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
384 //currentDownload = manager->post(request, ("data=" ));
385 manager->post(request, ("data=" ));
387 //ackOfResult function gets called when HTTP request is completed
388 //connect(currentDownload, SIGNAL(error()),SLOT(errorFromServer()));
389 connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*)));
390 connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
394 *@brief React to servers responce after result has been sent.
395 *@todo Implement function and write error handling.
397 void CarMainWindow::ackOfResult(QNetworkReply* reply)
399 qDebug() << "_ackOfResult";
400 QNetworkReply::NetworkError errorcode;
401 errorcode = reply->error();
403 qDebug() << errorcode << reply->errorString();
406 qDebug() << errorcode;
411 *@brief React to servers responce after registration has been sent.
412 *@todo Implement function and write error handling.
414 void CarMainWindow::ackOfRegistration(QNetworkReply* reply)
416 qDebug() << "_ackOfRegistration";
417 qDebug() << reply->readAll();
418 QNetworkReply::NetworkError errorcode;
419 errorcode = reply->error();
421 qDebug() << errorcode << reply->errorString();
424 qDebug() << errorcode;
430 void CarMainWindow::errorFromServer(QNetworkReply* reply)
432 qDebug() << "_errorFromServer";
433 QNetworkReply::NetworkError errorcode;
435 errorcode = reply->error();
437 qDebug() << errorcode;
440 qDebug() << errorcode;
446 *@brief Just for development, for the real button is not shown until
447 *measurin started and there are results.
448 *@todo Implement with real code and yet leave sendXml in the bottom in use.
450 void CarMainWindow::on_manualStartButton_clicked()
456 * This slot function is called when timer gives timeout signal. Checks current speed
457 * and stores times in measure class.
459 void CarMainWindow::after_timeout()
461 QString timeString, speedString;
463 time = accelerometer->getTotalTime();
464 speed = accelerometer->getCurrentSpeed();
467 if (floor(speed) == 10)
469 measures->setTime10kmh(time);
472 else if (floor(speed) == 20)
474 measures->setTime20kmh(time);
477 else if (floor(speed) == 30)
479 measures->setTime30kmh(time);
482 else if (floor(speed) == 40)
484 measures->setTime40kmh(time);
487 else if (floor(speed) == 50)
489 measures->setTime50kmh(time);
492 else if (floor(speed) == 60)
494 measures->setTime60kmh(time);
497 else if (floor(speed) == 70)
499 measures->setTime70kmh(time);
502 else if (floor(speed) == 80)
504 measures->setTime80kmh(time);
507 else if (floor(speed) == 90)
509 measures->setTime90kmh(time);
512 else if (floor(speed) == 100)
514 measures->setTime100kmh(time);
522 // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
526 accelerometer->stop();
529 //emit this->speedAchieved();
530 this->openResultView();
535 // Updates speed and time.
538 timeString.setNum(time);
539 speedString.setNum(speed);
540 ui->labelMeasureTabTime->setText(timeString);
541 ui->labelMeasureTabSpeed->setText(speedString);
549 * Initializes measures class's member variables.
551 void CarMainWindow::initializeMeasures()
553 measures->setTime10kmh(0);
554 measures->setTime20kmh(0);
555 measures->setTime30kmh(0);
556 measures->setTime40kmh(0);
557 measures->setTime50kmh(0);
558 measures->setTime60kmh(0);
559 measures->setTime70kmh(0);
560 measures->setTime80kmh(0);
561 measures->setTime90kmh(0);
562 measures->setTime100kmh(0);
566 * This slot function is called when Abort button is clicked.
568 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
570 measures->setTime10kmh(0);
571 measures->setTime20kmh(0);
572 measures->setTime30kmh(0);
573 measures->setTime40kmh(0);
574 measures->setTime50kmh(0);
575 measures->setTime60kmh(0);
576 measures->setTime70kmh(0);
577 measures->setTime80kmh(0);
578 measures->setTime90kmh(0);
579 measures->setTime100kmh(0);
581 accelerometer->stop();
584 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
588 void CarMainWindow::on_pushButtonSendResult_clicked()
593 void CarMainWindow::updateUserName()
597 newUserName = myLogin->getUserName();
598 ui->userNameLabel->setText( "User: " + newUserName);
600 if (newUserName.length())
602 ui->setUserPushButton->setText( "Change User");
603 this->setWindowTitle("Speed freak - " + newUserName);
607 ui->setUserPushButton->setText( "Set User");
608 this->setWindowTitle("Speed freak");