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 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
23 //result = new ResultDialog();
24 //measure = new MeasureDialog();
25 xmlreader = new XmlReader();
27 initComboBoxStartTabUnits();
28 initListViewStartTabAccelerationCategories();
30 myLogin = new LoginWindow(this);
31 myRegistration = new Registration(this);
32 xmlwriter = new XmlWriter();
33 manager = new QNetworkAccessManager(this);
34 connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(registrate()));
40 accelerometer = new Accelerometer();
41 accelerometer->setSampleRate(100);
43 measures = new Measures();
44 this->initializeMeasures();
46 timer->setInterval(300);
48 connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
49 connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
51 ui->labelMeasureTabResult->hide();
53 this->setWindowTitle("Speed Freak");
58 *Destructor of this class. Should be used to release all allocated resources.
60 CarMainWindow::~CarMainWindow()
71 *This function is used to .
74 void CarMainWindow::changeEvent(QEvent *e)
76 QMainWindow::changeEvent(e);
78 case QEvent::LanguageChange:
79 ui->retranslateUi(this);
87 *This slot function is called when ever list view is update. Start-tab view.
89 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
91 QString str = index.data().toString();
92 QStringList list = str.split("-");
93 QStringList list2 = list[1].split(" ");
95 ui->lineEditStartTabMin->setText(list[0]);
96 ui->lineEditStartTabMax->setText(list2[0]);
97 updateComboBoxStartTabUnits(list2[1]);
101 *This slot function is called when ever auto start button clicked. Start-tab view.
103 void CarMainWindow::on_autoStartButton_clicked()
108 //measure = new MeasureDialog();
109 // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
110 accelerometer->start();
112 // Show measure dialog.
114 ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
118 *This slot function is called when ever list view is update. Start-tab view.
119 *@param QString unit.
121 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
123 ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
127 *This function is used to init unit combobox. Start-tab view.
129 void CarMainWindow::initComboBoxStartTabUnits()
131 units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
132 ui->comboBoxStartTabUnits->addItems(units);
136 *This function is used to set items to unit combobox. Start-tab view.
137 *@param QStringlist units
139 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
141 ui->comboBoxStartTabUnits->addItems(units);
145 *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
147 void CarMainWindow::initListViewStartTabAccelerationCategories()
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);
155 *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
156 *@param QStringlist accelerationCategoriesStartTab
158 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
160 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
161 ui->listViewStartTabAccelerationCategories->setModel(model);
165 *This function is used to set items to category combobox. Top-tab view.
168 void CarMainWindow::setCategoryCompoBox()
170 ui->comboBoxTopCategory->addItems(xmlreader->getTop10List());
174 *This function is used to set items to labelTopList. Top-tab view.
175 *@param QString category
177 void CarMainWindow::setListViewTopList(QString category)
181 if (category == "acceleration-0-100")
183 topList.append(xmlreader->getTop10AccelerationList());
186 else if (category == "Speed")
188 topList.append(xmlreader->getTop10SpeedList());
191 else if (category == "G-force")
193 topList.append(xmlreader->getTop10GforceList());
195 ui->labelTopList->setText(topList);
199 *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
201 void CarMainWindow::openResultView()
203 //result->saveMeasuresToArray(measure->measures);
204 // Show result dialog.
206 ui->pushButtonSendResult->setEnabled(true);
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 registrate button is clicked.
220 void CarMainWindow::on_registratePushButton_clicked()
222 myRegistration->show();
226 *This slot function is called when ever refresh button clicked. Top-tab view.
228 void CarMainWindow::on_buttonTopRefresh_clicked()
231 setCategoryCompoBox();
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.
239 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
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 requestTopList(category, limit);
245 setListViewTopList(category);
249 *This slot function is called when ever category combobox activated. Top-tab view.
250 *@param QString category
252 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
254 setListViewTopList(category);
258 *This slot function is called when set/change user button is clicked.
260 void CarMainWindow::on_setUserPushButton_clicked()
266 *@brief Sends registration information to the server in xml format.
267 *Reads user name, password and emaol address from resuldialogs internal variables.
268 *@todo Replace msg box with better reaction to server`s responce.
270 void CarMainWindow::registrate()
272 qDebug() << "_registrate" ;
273 qDebug() << this->myRegistration->getUserName() << "+" << this->myRegistration->getPassword() << "+" << this->myRegistration->getEmail();
275 QBuffer *regbuffer = new QBuffer();
276 QUrl qurl("http://api.speedfreak-app.com/api/register");
277 QNetworkRequest request(qurl);
278 qDebug() << qurl.toString();
279 QNetworkReply *currentDownload;
281 regbuffer->open(QBuffer::ReadWrite);
282 xmlwriter->writeRegistering(regbuffer,
283 this->myRegistration->getUserName(),
284 this->myRegistration->getPassword(),
285 this->myRegistration->getEmail());
286 //Tmp msgbox - later server responce
287 QMessageBox::about(this,"Registrate",this->myRegistration->getUserName() + this->myRegistration->getPassword() + this->myRegistration->getEmail());
289 currentDownload = manager->post(request, ("xml=" + regbuffer->data()));
290 qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
292 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfRegistration()));
293 connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
300 *@brief Sends result(s) to the server in xml format.
301 *Send authentication information in the header.
302 *@todo Read category elsewhere.
304 void CarMainWindow::sendResultXml()
306 qDebug() << "_sendResultXml";
308 QBuffer *xmlbuffer = new QBuffer();
309 QString category_name = "acceleration-0-100"; //replace with real value from category list
311 QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name);
312 qDebug() << qurl.toString();
313 QNetworkRequest request(qurl);
314 QNetworkReply *currentDownload;
316 xmlbuffer->open(QBuffer::ReadWrite);
317 xmlwriter->writeResult(xmlbuffer);
318 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
320 QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
321 credentials = "Basic " + credentials.toAscii().toBase64();
322 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
324 currentDownload = manager->post(request, ("xml=" + xmlbuffer->data()));
325 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfResult()));
326 connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
332 *@brief Request the Top10List of certain category from the server.
333 *Send authentication information in the header.
334 *@param Category of results.
335 *@param Limit, the number of results.
337 void CarMainWindow::requestTopList(QString category, QString limit)
339 qDebug() << "_requestTopList" ;
341 QString urlBase = "http://api.speedfreak-app.com/api/results/";
342 QUrl qurl(urlBase + category + "/" + limit);
343 qDebug() << qurl.toString();
344 QNetworkRequest request(qurl);
345 QNetworkReply *currentDownload;
347 QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
348 credentials = "Basic " + credentials.toAscii().toBase64();
349 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
351 currentDownload = manager->post(request, ("data=" ));
352 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfToplist()));
353 connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
358 *@brief Request categories list from the server.
359 *Send authentication information in the header.
361 void CarMainWindow::requestCategories()
363 qDebug() << "_requestCategories" ;
365 QUrl qurl("http://api.speedfreak-app.com/api/categories/");
366 qDebug() << qurl.toString();
367 QNetworkRequest request(qurl);
368 QNetworkReply *currentDownload;
370 QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
371 credentials = "Basic " + credentials.toAscii().toBase64();
372 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
374 currentDownload = manager->post(request, ("data=" ));
375 connect(currentDownload,SIGNAL(finished()),this,SLOT(ackOfCategories()));
376 connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(errorFromServer(QNetworkReply::NetworkError)));
381 *@brief React to servers responce after result has been sent.
382 *@todo Implement consequencies of reply.
384 void CarMainWindow::ackOfResult()
386 qDebug() << "_ackOfResult";
388 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
389 qDebug() << reply->readAll();
390 QNetworkReply::NetworkError errorcode;
391 errorcode = reply->error();
393 qDebug() << errorcode << reply->errorString();
396 qDebug() << "errorcode=0";
402 *@brief React to servers responce after registration has been sent.
403 *@todo Implement consequencies of reply.
405 void CarMainWindow::ackOfRegistration()
407 qDebug() << "_ackOfRegistration";
409 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
410 qDebug() << reply->readAll();
411 QNetworkReply::NetworkError errorcode;
412 errorcode = reply->error();
414 qDebug() << "errorcode:" << errorcode << reply->errorString();
417 qDebug() << "errorcode=0";
423 *@brief React to servers responce after request for categories has been sent.
424 *@todo Implement reply`s feeding to categories list.
426 void CarMainWindow::ackOfCategories()
428 qDebug() << "_ackOfCategories";
430 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
431 qDebug() << reply->readAll();
432 QNetworkReply::NetworkError errorcode;
433 errorcode = reply->error();
435 qDebug() << "errorcode:" << errorcode << reply->errorString();
438 qDebug() << "errorcode=0";
443 *@brief Reports errors, when server has sent error signal.
445 void CarMainWindow::errorFromServer(QNetworkReply::NetworkError errorcode)
447 qDebug() << "_errorFromServer";
450 qDebug() << errorcode;
453 qDebug() << errorcode;
459 *@brief React to servers responce after request of TopList in certain category has been sent.
460 *@todo Implement routing reply`s contents to UI.
462 void CarMainWindow::ackOfToplist()
464 qDebug() << "_ackOfToplist";
466 QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
467 xmlreader->xmlRead(reply);
468 qDebug() << reply->readAll();
469 QNetworkReply::NetworkError errorcode;
470 errorcode = reply->error();
472 qDebug() << errorcode << reply->errorString();
475 qDebug() << "errorcode=0";
481 *@brief Just for development, for the real button is not shown until
482 *measurin started and there are results.
483 *@todo Implement with real code and yet leave sendXml in the bottom in use.
485 void CarMainWindow::on_manualStartButton_clicked()
491 * This slot function is called when timer gives timeout signal. Checks current speed
492 * and stores times in measure class.
494 void CarMainWindow::after_timeout()
496 QString timeString, speedString;
498 time = accelerometer->getTotalTime();
499 speed = accelerometer->getCurrentSpeed();
502 if (floor(speed) == 10)
504 measures->setTime10kmh(time);
507 else if (floor(speed) == 20)
509 measures->setTime20kmh(time);
512 else if (floor(speed) == 30)
514 measures->setTime30kmh(time);
517 else if (floor(speed) == 40)
519 measures->setTime40kmh(time);
522 else if (floor(speed) == 50)
524 measures->setTime50kmh(time);
527 else if (floor(speed) == 60)
529 measures->setTime60kmh(time);
532 else if (floor(speed) == 70)
534 measures->setTime70kmh(time);
537 else if (floor(speed) == 80)
539 measures->setTime80kmh(time);
542 else if (floor(speed) == 90)
544 measures->setTime90kmh(time);
547 else if (floor(speed) == 100)
549 measures->setTime100kmh(time);
557 // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
561 accelerometer->stop();
564 //emit this->speedAchieved();
565 this->openResultView();
570 // Updates speed and time.
573 timeString.setNum(time);
574 speedString.setNum(speed);
575 ui->labelMeasureTabTime->setText(timeString);
576 ui->labelMeasureTabSpeed->setText(speedString);
584 * Initializes measures class's member variables.
586 void CarMainWindow::initializeMeasures()
588 measures->setTime10kmh(0);
589 measures->setTime20kmh(0);
590 measures->setTime30kmh(0);
591 measures->setTime40kmh(0);
592 measures->setTime50kmh(0);
593 measures->setTime60kmh(0);
594 measures->setTime70kmh(0);
595 measures->setTime80kmh(0);
596 measures->setTime90kmh(0);
597 measures->setTime100kmh(0);
601 * This slot function is called when Abort button is clicked.
603 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
605 measures->setTime10kmh(0);
606 measures->setTime20kmh(0);
607 measures->setTime30kmh(0);
608 measures->setTime40kmh(0);
609 measures->setTime50kmh(0);
610 measures->setTime60kmh(0);
611 measures->setTime70kmh(0);
612 measures->setTime80kmh(0);
613 measures->setTime90kmh(0);
614 measures->setTime100kmh(0);
616 accelerometer->stop();
619 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
623 void CarMainWindow::on_pushButtonSendResult_clicked()
626 ui->pushButtonSendResult->setEnabled(false);
629 void CarMainWindow::updateUserName()
633 newUserName = myLogin->getUserName();
634 ui->userNameLabel->setText( "User: " + newUserName);
636 if (newUserName.length())
638 ui->setUserPushButton->setText( "Change User");
639 this->setWindowTitle("Speed freak - " + newUserName);
643 ui->setUserPushButton->setText( "Set User");
644 this->setWindowTitle("Speed freak");