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()
230 //setCategoryCompoBox();
235 *This slot function is called when ever category combobox current index changed. Top-tab view.
236 *@param QString category
238 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
240 setListViewTopList(category);
244 *This slot function is called when ever category combobox activated. Top-tab view.
245 *@param QString category
247 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
249 setListViewTopList(category);
253 *This slot function is called when set/change user button is clicked.
255 void CarMainWindow::on_setUserPushButton_clicked()
261 *@brief Sends registration information to the server in xml format.
262 *Reads user name, password and emaol address from resuldialogs internal variables.
263 *@todo Replace msg box with better reaction to server`s responce.
264 *@todo Write error handling.
266 void CarMainWindow::registrate()
268 qDebug() << "_registrate" ;
269 qDebug() << this->myRegistration->getUserName() << "+" << this->myRegistration->getPassword() << "+" << this->myRegistration->getEmail();
271 QBuffer *regbuffer = new QBuffer();
272 QUrl qurl("http://api.speedfreak-app.com/api/register");
273 QNetworkRequest request(qurl);
274 qDebug() << qurl.toString();
276 regbuffer->open(QBuffer::ReadWrite);
277 xmlwriter->writeRegistering(regbuffer,
278 this->myRegistration->getUserName(),
279 this->myRegistration->getPassword(),
280 this->myRegistration->getEmail());
281 //Tmp msgbox - later server responce
282 QMessageBox::about(this,"Registrate",this->myRegistration->getUserName() + this->myRegistration->getPassword() + this->myRegistration->getEmail());
284 manager->post(request, ("data=" + regbuffer->data()));
285 qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
287 //ackOfRegistration function gets called when HTTP request is completed
288 connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfRegistration(QNetworkReply*)));
289 //connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
294 *@brief Sends result(s) to the server in xml format with authentication information in the header.
295 *@todo Write error handling.
297 void CarMainWindow::sendXml()
299 qDebug() << "_sendXml";
301 QBuffer *xmlbuffer = new QBuffer();
302 QString category_name = "acceleration-0-100"; //replace with real value from category list
304 QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
305 credentials = "Basic " + credentials.toAscii().toBase64();
307 QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name);
308 qDebug() << qurl.toString();
309 QNetworkRequest request(qurl);
310 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
312 xmlbuffer->open(QBuffer::ReadWrite);
313 xmlwriter->writeResult(xmlbuffer);
314 qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
316 manager->post(request, ("data=" + xmlbuffer->data()));
317 //connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfResult(QNetworkReply*)));
318 //connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
320 //QNetworkReply *currentDownload;
321 //QString data("abcdefg");
322 //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data)); //testing
323 //currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
324 //ackOfResult function gets called when HTTP request is completed
325 //connect(currentDownload, SIGNAL(finished()), SLOT(ackOfResult()));
331 *@brief Sends request to the server for a top list with authentication information in the header.
332 *@todo Write error handling.
333 *@todo Replace with real value from category list and limitNr
335 void CarMainWindow::requestTopList()
337 qDebug() << "_requestTopList" ;
339 QString urlBase = "http://api.speedfreak-app.com/api/results/";
340 QString category_name = "acceleration-0-100"; //replace with real value from category list/top window
342 QString limit = QString::number(limitNr);
344 QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
345 credentials = "Basic " + credentials.toAscii().toBase64();
347 QUrl qurl(urlBase + category_name + "/" + limit);
348 qDebug() << qurl.toString();
349 QNetworkRequest request(qurl);
351 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
352 manager->post(request, ("data=" ));
353 //connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*)));
354 //connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
356 //QNetworkReply *currentDownload;
357 //currentDownload = manager->post(request, ("data=" ));
358 //ackOfResult function gets called when HTTP request is completed
359 //connect(currentDownload, SIGNAL(error()),SLOT(errorFromServer()));
363 *@brief React to servers responce after result has been sent.
364 *@todo Implement function and write error handling.
366 void CarMainWindow::ackOfResult(QNetworkReply* reply)
368 qDebug() << "_ackOfResult";
369 qDebug() << reply->readAll();
370 QNetworkReply::NetworkError errorcode;
371 errorcode = reply->error();
373 qDebug() << errorcode << reply->errorString();
376 qDebug() << "errorcode=0";
381 *@brief React to servers responce after registration has been sent.
382 *@todo Implement function and write error handling.
384 void CarMainWindow::ackOfRegistration(QNetworkReply* reply)
386 qDebug() << "_ackOfRegistration";
387 qDebug() << reply->readAll();
388 QNetworkReply::NetworkError errorcode;
389 errorcode = reply->error();
391 qDebug() << "errorcode:" << errorcode << reply->errorString();
394 qDebug() << "errorcode=0";
398 void CarMainWindow::errorFromServer(QNetworkReply* reply)
400 qDebug() << "_errorFromServer";
401 QNetworkReply::NetworkError errorcode;
403 errorcode = reply->error();
405 qDebug() << errorcode;
408 qDebug() << errorcode;
413 *This slot function is called when the server has finished guery.
415 void CarMainWindow::networkResponse(QNetworkReply *reply)
417 qDebug() << "_networkResponse";
418 xmlreader->xmlRead(reply);
419 qDebug() << reply->readAll();
420 QNetworkReply::NetworkError errorcode;
421 errorcode = reply->error();
423 qDebug() << errorcode << reply->errorString();
426 qDebug() << "errorcode=0";
431 *@brief Just for development, for the real button is not shown until
432 *measurin started and there are results.
433 *@todo Implement with real code and yet leave sendXml in the bottom in use.
435 void CarMainWindow::on_manualStartButton_clicked()
441 * This slot function is called when timer gives timeout signal. Checks current speed
442 * and stores times in measure class.
444 void CarMainWindow::after_timeout()
446 QString timeString, speedString;
448 time = accelerometer->getTotalTime();
449 speed = accelerometer->getCurrentSpeed();
452 if (floor(speed) == 10)
454 measures->setTime10kmh(time);
457 else if (floor(speed) == 20)
459 measures->setTime20kmh(time);
462 else if (floor(speed) == 30)
464 measures->setTime30kmh(time);
467 else if (floor(speed) == 40)
469 measures->setTime40kmh(time);
472 else if (floor(speed) == 50)
474 measures->setTime50kmh(time);
477 else if (floor(speed) == 60)
479 measures->setTime60kmh(time);
482 else if (floor(speed) == 70)
484 measures->setTime70kmh(time);
487 else if (floor(speed) == 80)
489 measures->setTime80kmh(time);
492 else if (floor(speed) == 90)
494 measures->setTime90kmh(time);
497 else if (floor(speed) == 100)
499 measures->setTime100kmh(time);
507 // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
511 accelerometer->stop();
514 //emit this->speedAchieved();
515 this->openResultView();
520 // Updates speed and time.
523 timeString.setNum(time);
524 speedString.setNum(speed);
525 ui->labelMeasureTabTime->setText(timeString);
526 ui->labelMeasureTabSpeed->setText(speedString);
534 * Initializes measures class's member variables.
536 void CarMainWindow::initializeMeasures()
538 measures->setTime10kmh(0);
539 measures->setTime20kmh(0);
540 measures->setTime30kmh(0);
541 measures->setTime40kmh(0);
542 measures->setTime50kmh(0);
543 measures->setTime60kmh(0);
544 measures->setTime70kmh(0);
545 measures->setTime80kmh(0);
546 measures->setTime90kmh(0);
547 measures->setTime100kmh(0);
551 * This slot function is called when Abort button is clicked.
553 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
555 measures->setTime10kmh(0);
556 measures->setTime20kmh(0);
557 measures->setTime30kmh(0);
558 measures->setTime40kmh(0);
559 measures->setTime50kmh(0);
560 measures->setTime60kmh(0);
561 measures->setTime70kmh(0);
562 measures->setTime80kmh(0);
563 measures->setTime90kmh(0);
564 measures->setTime100kmh(0);
566 accelerometer->stop();
569 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
573 void CarMainWindow::on_pushButtonSendResult_clicked()
576 ui->pushButtonSendResult->setEnabled(false);
579 void CarMainWindow::updateUserName()
583 newUserName = myLogin->getUserName();
584 ui->userNameLabel->setText( "User: " + newUserName);
586 if (newUserName.length())
588 ui->setUserPushButton->setText( "Change User");
589 this->setWindowTitle("Speed freak - " + newUserName);
593 ui->setUserPushButton->setText( "Set User");
594 this->setWindowTitle("Speed freak");