X-Git-Url: http://git.maemo.org/git/?p=speedfreak;a=blobdiff_plain;f=Client%2Fcarmainwindow.cpp;h=5ec968850ae44e9b2efaf9fc0d0e003013554b53;hp=b8af9feb8218412e04e48d635edd53e063331c28;hb=f73389530ce2e0cd73cf6dda7b09a5ba86e60f13;hpb=87caacddd53a392c4b747dc586fb91484fc394ae diff --git a/Client/carmainwindow.cpp b/Client/carmainwindow.cpp index b8af9fe..5ec9688 100644 --- a/Client/carmainwindow.cpp +++ b/Client/carmainwindow.cpp @@ -1,9 +1,16 @@ +/* + * CarMainWindow main class + * + * @author Toni Jussila + * @author Janne Änäkkälä + * @author Tiina Kivilinna-Korhola + * @author Olavi Pulkkinen + * @copyright (c) 2010 Speed Freak team + * @license http://opensource.org/licenses/gpl-license.php GNU Public License + */ + #include "carmainwindow.h" -#include "ui_carmainwindow.h" -#include "stringlistmodel.h" -#include -#include -#include +#include "math.h" /** *Constructor of this class. @@ -12,12 +19,40 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow) { ui->setupUi(this); - result = new ResultDialog(); - measure = new MeasureDialog(); + //result = new ResultDialog(); + //measure = new MeasureDialog(); + xmlreader = new XmlReader(); + + initComboBoxStartTabUnits(); + initListViewStartTabAccelerationCategories(); + + myLogin = new LoginWindow(this); + myRegistration = new Registration(this); + xmlwriter = new XmlWriter(); + manager = new QNetworkAccessManager(this); + connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*))); + connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(registrate())); + connect(this,SIGNAL(sendresult()),this,SLOT(sendXml())); + + time = 0; + speed = 0; + timer = new QTimer(); + + accelerometer = new Accelerometer(); + accelerometer->setSampleRate(100); + + measures = new Measures(); + this->initializeMeasures(); + + timer->setInterval(300); + + connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout())); + connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName())); + + ui->labelMeasureTabResult->hide(); + + this->setWindowTitle("Speed bfreak"); - initUnitCompoBox(); - initSpeedListView(); - initCategoryCompoBox(); } /** @@ -26,8 +61,11 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::Ca CarMainWindow::~CarMainWindow() { delete ui; - delete result; - delete measure; + //delete result; + //delete measure; + delete xmlreader; + delete xmlwriter; + delete manager; } /** @@ -47,124 +85,526 @@ void CarMainWindow::changeEvent(QEvent *e) } /** - *This slot function is called when ever list view is update. - *@param QModelIndex index. + *This slot function is called when ever list view is update. Start-tab view. */ -void CarMainWindow::on_listView_clicked(QModelIndex index) +void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index) { QString str = index.data().toString(); QStringList list = str.split("-"); QStringList list2 = list[1].split(" "); - ui->minLineEdit->setText(list[0]); - ui->maxLineEdit->setText(list2[0]); - updateUnitCompoBox(list2[1]); + ui->lineEditStartTabMin->setText(list[0]); + ui->lineEditStartTabMax->setText(list2[0]); + updateComboBoxStartTabUnits(list2[1]); } /** - *This slot function is called when ever auto start button clicked. + *This slot function is called when ever auto start button clicked. Start-tab view. */ void CarMainWindow::on_autoStartButton_clicked() { - if(measure) + + //delete measure; + //measure = NULL; + //measure = new MeasureDialog(); + // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView())); + accelerometer->start(); + timer->start(); + // Show measure dialog. + //measure->show(); + ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult); +} + +/** + *This slot function is called when ever list view is update. Start-tab view. + *@param QString unit. + */ +void CarMainWindow::updateComboBoxStartTabUnits(QString unit) +{ + ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly)); +} + +/** + *This function is used to init unit combobox. Start-tab view. + */ +void CarMainWindow::initComboBoxStartTabUnits() +{ + units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd"; + ui->comboBoxStartTabUnits->addItems(units); +} + +/** + *This function is used to set items to unit combobox. Start-tab view. + *@param QStringlist units + */ +void CarMainWindow::setComboBoxStartTabUnits(QStringList units) +{ + ui->comboBoxStartTabUnits->addItems(units); +} + +/** + *This function is used to init listViewStartTabAccelerationCategories. Start-tab view. + */ +void CarMainWindow::initListViewStartTabAccelerationCategories() +{ + 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"; + QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab); + ui->listViewStartTabAccelerationCategories->setModel(model); +} + +/** + *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view. + *@param QStringlist accelerationCategoriesStartTab + */ +void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab) +{ + QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab); + ui->listViewStartTabAccelerationCategories->setModel(model); +} + +/** + *This function is used to set items to category combobox. Top-tab view. + *@param + */ +void CarMainWindow::setCategoryCompoBox() +{ + ui->comboBoxTopCategory->addItems(xmlreader->getTop10List()); +} + +/** + *This function is used to set items to labelTopList. Top-tab view. + *@param QString category + */ +void CarMainWindow::setListViewTopList(QString category) +{ + QString topList; + + if (category == "acceleration-0-100") { - delete measure; - measure = NULL; - measure = new MeasureDialog(); + topList.append(xmlreader->getTop10AccelerationList()); } - connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView())); + else if (category == "Speed") + { + topList.append(xmlreader->getTop10SpeedList()); + } - // Show measure dialog. - measure->show(); + else if (category == "G-force") + { + topList.append(xmlreader->getTop10GforceList()); + } + ui->labelTopList->setText(topList); } /** - *This slot function is called when ever list view is update. - *@param QString unit. + *This slot function is called when speed is achieved in measure dialog. Opens result dialog. */ -void CarMainWindow::updateUnitCompoBox(QString unit) +void CarMainWindow::openResultView() { - ui->unitComboBox->setCurrentIndex(ui->unitComboBox->findText(unit, Qt::MatchExactly)); + //result->saveMeasuresToArray(measure->measures); + // Show result dialog. + //result->show(); + QString timeInteger; + timeInteger.setNum(this->measures->getTime40kmh()); + //time = "0 - 40 km/h: "; + //time.append(timeInteger); + //ui->labelResult40kmh->setText(time); + ui->labelMeasureTabResult->show(); + ui->labelMeasureTabResult->setText(timeInteger); + //ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult); } /** - *This function is used to init unit combobox. + *This slot function is called when the server has finished guery. */ -void CarMainWindow::initUnitCompoBox() +void CarMainWindow::networkResponse(QNetworkReply *reply) { - units << "km/h" << "km" << "h" << "m" << "min" << "mil" << "in" << "ft" << "yrd"; - ui->unitComboBox->addItems(units); + } /** - *This function is used to set items to unit combobox. - *@param QStringlist numbers + *This slot function is called when the user will to send data to server. + *@todo Where is this callback connected? */ -void CarMainWindow::setUnitCompoBox(QStringList units) +void CarMainWindow::on_pushButton_clicked() { - ui->unitComboBox->addItems(units); + sendXml(); } /** - *This function is used to init speed listview. + *This slot function is called when login/logout button is clicked. */ -void CarMainWindow::initSpeedListView() +void CarMainWindow::on_loginLogoutButton_clicked() { - numbers << "0-100 km/h" << "0-1/4 mil" << "0-50 km" << "50-100 mil" << "0-100 m" << "0-50 ft" << "0-50 yrd" << "0-500 in"; - QAbstractItemModel *model = new StringListModel(numbers); - ui->listView->setModel(model); + myLogin->show(); } /** -<<<<<<< HEAD:Client/carmainwindow.cpp - *This function is used to set items to speed listview. - *@param QStringlist numbers + *This slot function is called when registrate button is clicked. */ -void CarMainWindow::setSpeedListView(QStringList numbers) +void CarMainWindow::on_registratePushButton_clicked() { - QAbstractItemModel *model = new StringListModel(numbers); - ui->listView->setModel(model); + myRegistration->show(); } /** - *This function is used to init category combobox. + *This slot function is called when ever refresh button clicked. Top-tab view. */ -void CarMainWindow::initCategoryCompoBox() +void CarMainWindow::on_buttonTopRefresh_clicked() { - categories << "Top 10 1/4 mile" << "Top 10 0-100 km/h" << "Top 10 car"; - ui->comboBoxTopCategory->addItems(categories); + //setCategoryCompoBox(); + requestTopList(); } /** - *This function is used to set items to category combobox. - *@param QStringlist categories + *This slot function is called when ever category combobox current index changed. Top-tab view. + *@param QString category */ -void CarMainWindow::setCategoryCompoBox(QStringList categories) +void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category) { - ui->comboBoxTopCategory->addItems(categories); + setListViewTopList(category); } /** - *This slot function is called when ever categories combobox is update. + *This slot function is called when ever category combobox activated. Top-tab view. *@param QString category */ void CarMainWindow::on_comboBoxTopCategory_activated(QString category) { - //TODO: get top list + setListViewTopList(category); +} - QStringList topList; - topList << "1. Pertti 7,5s" << "2. Ville 10,2s"; +/** + *This slot function is called when set/change user button is clicked. + */ +void CarMainWindow::on_setUserPushButton_clicked() +{ + myLogin->show(); - QAbstractItemModel *model = new StringListModel(topList); - ui->listViewTopList->setModel(model); + ui->userNameLabel->setText( "User: " + myLogin->getUserName()); + ui->setUserPushButton->setText( "Change User"); } /** - *This slot function is called when speed is achieved in measure dialog. Opens result dialog. + *@brief Sends registration information to the server in xml format. + *Reads user name, password and emaol address from resuldialogs internal variables. + *@todo Replace msg box with better reaction to server`s responce. + *@todo Write error handling. */ -void CarMainWindow::openResultView() +void CarMainWindow::registrate() { - // Show result dialog. - result->show(); + qDebug() << "_registrate" ; + qDebug() << this->myRegistration->getUserName() << "+" << this->myRegistration->getPassword() << "+" << this->myRegistration->getEmail(); + + QBuffer *regbuffer = new QBuffer(); + QNetworkReply *currentDownload; + + QUrl qurl("http://api.speedfreak-app.com/register"); + QNetworkRequest request(qurl); + + //write also to a file during development, : + xmlwriter->writeXml(this->myRegistration->getUserName(), + this->myRegistration->getPassword(), + this->myRegistration->getEmail()); + xmlwriter->writeRegistering(regbuffer, + this->myRegistration->getUserName(), + this->myRegistration->getPassword(), + this->myRegistration->getEmail()); + //Tmp msgbox - later server responce + QMessageBox::about(this,"Registrate",this->myRegistration->getUserName() + this->myRegistration->getPassword() + this->myRegistration->getEmail()); + + currentDownload = manager->post(request, ("data=" + regbuffer->data())); + + //ackFromServer function gets called when HTTP request is completed + connect(currentDownload, SIGNAL(finished()),SLOT(ackOfRegistration())); + manager->post(request, ("data=" + regbuffer->data())); + + //ackOfRegistration function gets called when HTTP request is completed + //connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfRegistration())); + connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfRegistration(QNetworkReply*))); + connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*))); +} + +/** + *@brief Sends result(s) to the server in xml format with authentication information in the header. + *@todo Write error handling. + */ +void CarMainWindow::sendXml() +{ + qDebug() << "_sendXml"; + + QBuffer *xmlbuffer = new QBuffer(); + QNetworkReply *currentDownload; + + QString category_name = "acceleration-0-100"; //replace with real value from category list + + QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword(); + credentials = "Basic " + credentials.toAscii().toBase64(); + + QUrl qurl("http://api.speedfreak-app.com/update/category_name"); + QNetworkRequest request(qurl); + + request.setRawHeader(QByteArray("Authorization"),credentials.toAscii()); + + xmlwriter->writeResult(xmlbuffer); + + //currentDownload = manager->post(request, ("data=" + xmlbuffer->data())); + manager->post(request, ("data=" + xmlbuffer->data())); + //QString data("abcdefg"); //testing + //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data)); //testing + + //ackOfResult function gets called when HTTP request is completed + //connect(currentDownload, SIGNAL(finished()), this, SLOT(ackOfResult())); + connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfResult(QNetworkReply*))); + connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*))); + +} + +/** + *@brief Sends request to the server for a top list with authentication information in the header. + *@todo Write error handling. + *@todo Replace with real value from category list and limit + */ +void CarMainWindow::requestTopList() +{ + qDebug() << "_registrate" ; + + QString category_name = "acceleration-0-100"; //replace with real value from category list/top window + int limit = 5; + //QNetworkReply *currentDownload; + + QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword(); + credentials = "Basic " + credentials.toAscii().toBase64(); + + QUrl qurl("http://api.speedfreak-app.com/results/category_name/limit"); + QNetworkRequest request(qurl); + + request.setRawHeader(QByteArray("Authorization"),credentials.toAscii()); + + //currentDownload = manager->post(request, ("data=" )); + manager->post(request, ("data=" )); + + //ackOfResult function gets called when HTTP request is completed + //connect(currentDownload, SIGNAL(error()),SLOT(errorFromServer())); + connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*))); + connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*))); +} + +/** + *@brief React to servers responce after result has been sent. + *@todo Implement function and write error handling. + */ +void CarMainWindow::ackOfResult(QNetworkReply* reply) +{ + qDebug() << "_ackOfResult"; + QNetworkReply::NetworkError errorcode; + errorcode = reply->error(); + if(errorcode != 0) { + qDebug() << errorcode << reply->errorString(); + } + else { + qDebug() << errorcode; + } +} + +/** + *@brief React to servers responce after registration has been sent. + *@todo Implement function and write error handling. + */ +void CarMainWindow::ackOfRegistration(QNetworkReply* reply) +{ + qDebug() << "_ackOfRegistration"; + qDebug() << reply->readAll(); + QNetworkReply::NetworkError errorcode; + errorcode = reply->error(); + if(errorcode != 0) { + qDebug() << errorcode << reply->errorString(); + } + else { + qDebug() << errorcode; + } +} + + + +void CarMainWindow::errorFromServer(QNetworkReply* reply) +{ + qDebug() << "_errorFromServer"; + QNetworkReply::NetworkError errorcode; + + errorcode = reply->error(); + if(errorcode != 0) { + qDebug() << errorcode; + } + else { + qDebug() << errorcode; + } + +} + +/** + *@brief Just for development, for the real button is not shown until + *measurin started and there are results. + *@todo Implement with real code and yet leave sendXml in the bottom in use. + */ +void CarMainWindow::on_manualStartButton_clicked() +{ + sendXml(); +} + +/** + * This slot function is called when timer gives timeout signal. Checks current speed + * and stores times in measure class. + */ +void CarMainWindow::after_timeout() +{ + QString timeString, speedString; + //time++; + time = accelerometer->getTotalTime(); + speed = accelerometer->getCurrentSpeed(); + //speed = speed +10; + + if (floor(speed) == 10) + { + measures->setTime10kmh(time); + } + + else if (floor(speed) == 20) + { + measures->setTime20kmh(time); + } + + else if (floor(speed) == 30) + { + measures->setTime30kmh(time); + } + + else if (floor(speed) == 40) + { + measures->setTime40kmh(time); + } + + else if (floor(speed) == 50) + { + measures->setTime50kmh(time); + } + + else if (floor(speed) == 60) + { + measures->setTime60kmh(time); + } + + else if (floor(speed) == 70) + { + measures->setTime70kmh(time); + } + + else if (floor(speed) == 80) + { + measures->setTime80kmh(time); + } + + else if (floor(speed) == 90) + { + measures->setTime90kmh(time); + } + + else if (floor(speed) == 100) + { + measures->setTime100kmh(time); + } + + else + { + + } + + // If speed is over 40 km/h emits speedAchieved() signal and close this dialog. + if (speed >= 40.0) + { + timer->stop(); + accelerometer->stop(); + time = 0; + speed = 0; + //emit this->speedAchieved(); + this->openResultView(); + //this->close(); + + } + + // Updates speed and time. + else + { + timeString.setNum(time); + speedString.setNum(speed); + ui->labelMeasureTabTime->setText(timeString); + ui->labelMeasureTabSpeed->setText(speedString); + + timer->start(); + } + +} + +/** + * Initializes measures class's member variables. + */ +void CarMainWindow::initializeMeasures() +{ + measures->setTime10kmh(0); + measures->setTime20kmh(0); + measures->setTime30kmh(0); + measures->setTime40kmh(0); + measures->setTime50kmh(0); + measures->setTime60kmh(0); + measures->setTime70kmh(0); + measures->setTime80kmh(0); + measures->setTime90kmh(0); + measures->setTime100kmh(0); +} + +/** + * This slot function is called when Abort button is clicked. + */ +void CarMainWindow::on_pushButtonMeasureTabAbort_clicked() +{ + measures->setTime10kmh(0); + measures->setTime20kmh(0); + measures->setTime30kmh(0); + measures->setTime40kmh(0); + measures->setTime50kmh(0); + measures->setTime60kmh(0); + measures->setTime70kmh(0); + measures->setTime80kmh(0); + measures->setTime90kmh(0); + measures->setTime100kmh(0); + timer->stop(); + accelerometer->stop(); + time = 0; + speed = 0; + ui->tabWidget->setCurrentWidget(this->ui->StartTab); + //this->close(); +} + +void CarMainWindow::on_pushButtonSendResult_clicked() +{ + emit sendresult(); +} + +void CarMainWindow::updateUserName() +{ + QString newUserName; + + newUserName = myLogin->getUserName(); + ui->userNameLabel->setText( "User: " + newUserName); + + if (newUserName.length()) + { + ui->setUserPushButton->setText( "Change User"); + this->setWindowTitle("Speed freak - " + newUserName); + } + else + { + ui->setUserPushButton->setText( "Set User"); + this->setWindowTitle("Speed freak"); + } }