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()));
51 ui->labelMeasureTabResult->hide();
56 *Destructor of this class. Should be used to release all allocated resources.
58 CarMainWindow::~CarMainWindow()
69 *This function is used to .
72 void CarMainWindow::changeEvent(QEvent *e)
74 QMainWindow::changeEvent(e);
76 case QEvent::LanguageChange:
77 ui->retranslateUi(this);
85 *This slot function is called when ever list view is update. Start-tab view.
87 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
89 QString str = index.data().toString();
90 QStringList list = str.split("-");
91 QStringList list2 = list[1].split(" ");
93 ui->lineEditStartTabMin->setText(list[0]);
94 ui->lineEditStartTabMax->setText(list2[0]);
95 updateComboBoxStartTabUnits(list2[1]);
99 *This slot function is called when ever auto start button clicked. Start-tab view.
101 void CarMainWindow::on_autoStartButton_clicked()
106 //measure = new MeasureDialog();
107 // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
108 accelerometer->start();
110 // Show measure dialog.
112 ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
116 *This slot function is called when ever list view is update. Start-tab view.
117 *@param QString unit.
119 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
121 ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
125 *This function is used to init unit combobox. Start-tab view.
127 void CarMainWindow::initComboBoxStartTabUnits()
129 units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
130 ui->comboBoxStartTabUnits->addItems(units);
134 *This function is used to set items to unit combobox. Start-tab view.
135 *@param QStringlist units
137 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
139 ui->comboBoxStartTabUnits->addItems(units);
143 *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
145 void CarMainWindow::initListViewStartTabAccelerationCategories()
147 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";
148 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
149 ui->listViewStartTabAccelerationCategories->setModel(model);
153 *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
154 *@param QStringlist accelerationCategoriesStartTab
156 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
158 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
159 ui->listViewStartTabAccelerationCategories->setModel(model);
163 *This function is used to set items to category combobox. Top-tab view.
166 void CarMainWindow::setCategoryCompoBox()
168 ui->comboBoxTopCategory->addItems(xmlreader->getTop10List());
172 *This function is used to set items to labelTopList. Top-tab view.
173 *@param QString category
175 void CarMainWindow::setListViewTopList(QString category)
179 if (category == "acceleration-0-100")
181 topList.append(xmlreader->getTop10AccelerationList());
184 else if (category == "Speed")
186 topList.append(xmlreader->getTop10SpeedList());
189 else if (category == "G-force")
191 topList.append(xmlreader->getTop10GforceList());
193 ui->labelTopList->setText(topList);
197 *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
199 void CarMainWindow::openResultView()
201 //result->saveMeasuresToArray(measure->measures);
202 // Show result dialog.
205 timeInteger.setNum(this->measures->getTime40kmh());
206 //time = "0 - 40 km/h: ";
207 //time.append(timeInteger);
208 //ui->labelResult40kmh->setText(time);
209 ui->labelMeasureTabResult->show();
210 ui->labelMeasureTabResult->setText(timeInteger);
211 //ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
215 *This slot function is called when the server has finished guery.
217 void CarMainWindow::networkResponse(QNetworkReply *reply)
223 *This slot function is called when the user will to send data to server.
224 *@todo Where is this callback connected?
226 void CarMainWindow::on_pushButton_clicked()
232 *This slot function is called when login/logout button is clicked.
234 void CarMainWindow::on_loginLogoutButton_clicked()
236 //LoginWindow myLogin;
239 //ui->loginLogoutButton->setText("logout");
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();
259 *This slot function is called when ever category combobox current index changed. Top-tab view.
260 *@param QString category
262 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
264 setListViewTopList(category);
268 *This slot function is called when ever category combobox activated. Top-tab view.
269 *@param QString category
271 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
273 setListViewTopList(category);
277 *This slot function is called when set/change user button is clicked.
279 void CarMainWindow::on_setUserPushButton_clicked()
283 ui->userNameLabel->setText( "User: " + myLogin->getUserName());
284 ui->setUserPushButton->setText( "Change User");
288 *@brief Sends registration information to the server in xml format.
289 *Reads user name, password and emaol address from resuldialogs internal variables.
290 *@todo Replace msg box with better reaction to server`s responce.
291 *@todo Write error handling.
293 void CarMainWindow::registrate()
295 qDebug() << "_registrate" ;
296 qDebug() << this->myRegistration->getUserName() << "+" << this->myRegistration->getPassword() << "+" << this->myRegistration->getEmail();
298 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()));
323 *@brief Sends result(s) to the server in xml format with authentication information in the header.
324 *@todo Write error handling.
326 void CarMainWindow::sendXml()
328 qDebug() << "_sendXml";
330 QBuffer *xmlbuffer = new QBuffer();
331 QNetworkReply *currentDownload;
333 QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
334 credentials = "Basic " + credentials.toAscii().toBase64();
336 QUrl qurl("http//:api.speedfreak-app.com/update/acceleration-0-40");
337 QNetworkRequest request(qurl);
339 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
341 xmlwriter->writeResult(xmlbuffer);
343 currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
344 //QString data("abcdefg"); //testing
345 //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data)); //testing
348 //ackFromServer function gets called when HTTP request is completed
349 connect(currentDownload, SIGNAL(finished()),SLOT(ackOfResult()));
354 *@brief React to servers responce after result has been sent.
355 *@todo Implement function and write error handling.
357 void CarMainWindow::ackOfResult()
359 qDebug() << "Server acknowledged posting of result";
363 *@brief React to servers responce after registration has been sent.
364 *@todo Implement function and write error handling.
367 void CarMainWindow::ackOfRegistration()
369 qDebug() << "Server acknowledged registration";
374 *@brief Just for development, for the real button is not shown until
375 *measurin started and there are results.
376 *@todo Implement with real code and yet leave sendXml in the bottom in use.
379 void CarMainWindow::on_manualStartButton_clicked()
385 * This slot function is called when timer gives timeout signal. Checks current speed
386 * and stores times in measure class.
388 void CarMainWindow::after_timeout()
390 QString timeString, speedString;
392 time = accelerometer->getTotalTime();
393 speed = accelerometer->getCurrentSpeed();
396 if (floor(speed) == 10)
398 measures->setTime10kmh(time);
401 else if (floor(speed) == 20)
403 measures->setTime20kmh(time);
406 else if (floor(speed) == 30)
408 measures->setTime30kmh(time);
411 else if (floor(speed) == 40)
413 measures->setTime40kmh(time);
416 else if (floor(speed) == 50)
418 measures->setTime50kmh(time);
421 else if (floor(speed) == 60)
423 measures->setTime60kmh(time);
426 else if (floor(speed) == 70)
428 measures->setTime70kmh(time);
431 else if (floor(speed) == 80)
433 measures->setTime80kmh(time);
436 else if (floor(speed) == 90)
438 measures->setTime90kmh(time);
441 else if (floor(speed) == 100)
443 measures->setTime100kmh(time);
451 // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
455 accelerometer->stop();
458 //emit this->speedAchieved();
459 this->openResultView();
464 // Updates speed and time.
467 timeString.setNum(time);
468 speedString.setNum(speed);
469 ui->labelMeasureTabTime->setText(timeString);
470 ui->labelMeasureTabSpeed->setText(speedString);
478 * Initializes measures class's member variables.
480 void CarMainWindow::initializeMeasures()
482 measures->setTime10kmh(0);
483 measures->setTime20kmh(0);
484 measures->setTime30kmh(0);
485 measures->setTime40kmh(0);
486 measures->setTime50kmh(0);
487 measures->setTime60kmh(0);
488 measures->setTime70kmh(0);
489 measures->setTime80kmh(0);
490 measures->setTime90kmh(0);
491 measures->setTime100kmh(0);
495 * This slot function is called when Abort button is clicked.
497 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
499 measures->setTime10kmh(0);
500 measures->setTime20kmh(0);
501 measures->setTime30kmh(0);
502 measures->setTime40kmh(0);
503 measures->setTime50kmh(0);
504 measures->setTime60kmh(0);
505 measures->setTime70kmh(0);
506 measures->setTime80kmh(0);
507 measures->setTime90kmh(0);
508 measures->setTime100kmh(0);
510 accelerometer->stop();
513 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
517 void CarMainWindow::on_pushButtonSendResult_clicked()