Merge branch 'changes/UI'
[speedfreak] / Client / carmainwindow.cpp
index 25cdebc..807159f 100644 (file)
@@ -10,6 +10,7 @@
  */
 
 #include "carmainwindow.h"
+#include "math.h"
 
 /**
   *Constructor of this class.
@@ -18,8 +19,9 @@
 CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow)
 {
     ui->setupUi(this);
-    result = new ResultDialog();
-    measure = new MeasureDialog();
+    ui->tabWidget->setCurrentWidget(this->ui->StartTab);
+    //result = new ResultDialog();
+    //measure = new MeasureDialog();
     xmlreader = new XmlReader();
 
     initComboBoxStartTabUnits();
@@ -29,9 +31,26 @@ CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::Ca
     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(result,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 freak");
 
 }
 
@@ -41,8 +60,8 @@ 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;
@@ -84,13 +103,15 @@ void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelInde
 void CarMainWindow::on_autoStartButton_clicked()
 {
 
-    delete measure;
-    measure = NULL;
-    measure = new MeasureDialog();
-
-    connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
+    //delete measure;
+    //measure = NULL;
+    //measure = new MeasureDialog();
+   // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
+    accelerometer->start();
+    timer->start();
     // Show measure dialog.
-    measure->show();
+    //measure->show();
+    ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
 }
 
 /**
@@ -179,37 +200,18 @@ void CarMainWindow::setListViewTopList(QString category)
   */
 void CarMainWindow::openResultView()
 {
-    result->saveMeasuresToArray(measure->measures);
+    //result->saveMeasuresToArray(measure->measures);
     // Show result dialog.
-    result->show();
-}
-
-/**
-  *This slot function is called when the server has finished guery.
-  */
-void CarMainWindow::networkResponse(QNetworkReply *reply)
-{
-
-}
-
-/**
-  *This slot function is called when the user will to send data to server.
-  *@todo Where is this callback connected?
-  */
-void CarMainWindow::on_pushButton_clicked()
-{
-     sendXml();
-}
-
-/**
-  *This slot function is called when login/logout button is clicked.
-  */
-void CarMainWindow::on_loginLogoutButton_clicked()
-{
-    //LoginWindow myLogin;
-
-    myLogin->show();
-    //ui->loginLogoutButton->setText("logout");
+    //result->show();
+    ui->pushButtonSendResult->setEnabled(true);
+    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);
 }
 
 /**
@@ -225,7 +227,8 @@ void CarMainWindow::on_registratePushButton_clicked()
   */
 void CarMainWindow::on_buttonTopRefresh_clicked()
 {
-    setCategoryCompoBox();
+    //setCategoryCompoBox();
+    requestTopList();
 }
 
 /**
@@ -252,9 +255,6 @@ void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
 void CarMainWindow::on_setUserPushButton_clicked()
 {
     myLogin->show();
-
-    ui->userNameLabel->setText( "User: " + myLogin->getUserName());
-    ui->setUserPushButton->setText( "Change User");
 }
 
 /**
@@ -269,16 +269,11 @@ void CarMainWindow::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");
+    QUrl qurl("http://api.speedfreak-app.com/api/register");
     QNetworkRequest request(qurl);
+    qDebug() << qurl.toString();
 
-    //write also to a file during development, :
-    xmlwriter->writeXml(this->myRegistration->getUserName(),
-                      this->myRegistration->getPassword(),
-                      this->myRegistration->getEmail());
+    regbuffer->open(QBuffer::ReadWrite);
     xmlwriter->writeRegistering(regbuffer,
                       this->myRegistration->getUserName(),
                       this->myRegistration->getPassword(),
@@ -286,10 +281,13 @@ void CarMainWindow::registrate()
     //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()));
+    manager->post(request, ("data=" + regbuffer->data()));
+    qDebug() << "carmainwindow: regbuffer->data(): " << regbuffer->data();
 
-    //ackFromServer function gets called when HTTP request is completed
-    connect(currentDownload, SIGNAL(finished()),SLOT(ackOfRegistration()));
+    //ackOfRegistration function gets called when HTTP request is completed
+    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfRegistration(QNetworkReply*)));
+    //connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
+    regbuffer->close();
 }
 
 /**
@@ -301,55 +299,298 @@ 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/acceleration-0-40");
+    QUrl qurl("http://api.speedfreak-app.com/api/update/" + category_name);
+    qDebug() << qurl.toString();
     QNetworkRequest request(qurl);
-
     request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
 
+    xmlbuffer->open(QBuffer::ReadWrite);
     xmlwriter->writeResult(xmlbuffer);
+    qDebug() << "carmainwindow: xmlbuffer->data(): " << xmlbuffer->data();
 
-    currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
-    //QString data("abcdefg");    //testing
+    manager->post(request, ("data=" + xmlbuffer->data()));
+    //connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(ackOfResult(QNetworkReply*)));
+    //connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
+
+    //QNetworkReply *currentDownload;
+    //QString data("abcdefg");
     //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data));   //testing
+    //currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
+    //ackOfResult function gets called when HTTP request is completed
+    //connect(currentDownload, SIGNAL(finished()), SLOT(ackOfResult()));
 
+    xmlbuffer->close();
+}
 
-    //ackFromServer function gets called when HTTP request is completed
-    connect(currentDownload, SIGNAL(finished()),SLOT(ackOfResult()));
+/**
+  *@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 limitNr
+  */
+void CarMainWindow::requestTopList()
+{
+    qDebug() << "_requestTopList" ;
+
+    QString urlBase = "http://api.speedfreak-app.com/api/results/";
+    QString category_name = "acceleration-0-100";    //replace with real value from category list/top window
+    int limitNr = 5;
+    QString limit = QString::number(limitNr);
 
+    QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
+    credentials = "Basic " + credentials.toAscii().toBase64();
+
+    QUrl qurl(urlBase + category_name + "/" + limit);
+    qDebug() << qurl.toString();
+    QNetworkRequest request(qurl);
+
+    request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+    manager->post(request, ("data=" ));
+    //connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*)));
+    //connect(manager,SIGNAL(sslErrors(QNetworkReply*)),this,SLOT(errorFromServer(QNetworkReply*)));
+
+    //QNetworkReply *currentDownload;
+    //currentDownload = manager->post(request, ("data=" ));
+    //ackOfResult function gets called when HTTP request is completed
+    //connect(currentDownload, SIGNAL(error()),SLOT(errorFromServer()));
 }
 
 /**
   *@brief React to servers responce after result has been sent.
   *@todo Implement function and write error handling.
   */
-void CarMainWindow::ackOfResult()
+void CarMainWindow::ackOfResult(QNetworkReply* reply)
 {
-    qDebug() << "Server acknowledged posting of result";
+    qDebug() << "_ackOfResult";
+    qDebug() << reply->readAll();
+    QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() << errorcode << reply->errorString();
+    }
+    else {
+        qDebug() << "errorcode=0";
+    }
 }
 
 /**
   *@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:" << errorcode << reply->errorString();
+    }
+    else {
+        qDebug() << "errorcode=0";
+    }
+}
 
-void CarMainWindow::ackOfRegistration()
+void CarMainWindow::errorFromServer(QNetworkReply* reply)
 {
-    qDebug() << "Server acknowledged registration";
+    qDebug() << "_errorFromServer";
+    QNetworkReply::NetworkError errorcode;
+
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() << errorcode;
+    }
+    else {
+        qDebug() << errorcode;
+    }
 }
 
+/**
+  *This slot function is called when the server has finished guery.
+  */
+void CarMainWindow::networkResponse(QNetworkReply *reply)
+{
+    qDebug() << "_networkResponse";
+    xmlreader->xmlRead(reply);
+    qDebug() << reply->readAll();
+    QNetworkReply::NetworkError errorcode;
+    errorcode = reply->error();
+    if(errorcode != 0) {
+        qDebug() << errorcode << reply->errorString();
+    }
+    else {
+        qDebug() << "errorcode=0";
+    }
+}
 
 /**
   *@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()
+{
+    sendXml();
+    ui->pushButtonSendResult->setEnabled(false);
+}
+
+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");
+    }
+}