+/**
+ * CarMainWindow main class
+ *
+ * @author Toni Jussila <toni.jussila@fudeco.com>
+ * @author Janne Änäkkälä <janne.anakkala@fudeco.com>
+ * @author Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
+ * @author Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
+ * @copyright (c) 2010 Speed Freak team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
#include "carmainwindow.h"
+#include "math.h"
/**
*Constructor of this class.
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();
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);
+ accelerometer->start();
+
+ measures = new Measures();
+ this->initializeMeasures();
+
+ timer->setInterval(1000);
+
+ connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
+
+ ui->labelMeasureTabResult->hide();
+
}
/**
CarMainWindow::~CarMainWindow()
{
delete ui;
- delete result;
- delete measure;
+ //delete result;
+ //delete measure;
delete xmlreader;
+ delete xmlwriter;
+ delete manager;
}
/**
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()));
+ timer->start();
// Show measure dialog.
- measure->show();
+ //measure->show();
+ ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
}
/**
*/
void CarMainWindow::openResultView()
{
- result->saveMeasuresToArray(measure->measures);
+ //result->saveMeasuresToArray(measure->measures);
// Show result dialog.
- result->show();
+ //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);
}
/**
*/
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()
{
- QNetworkRequest postData;
- postData.setUrl(QString("http://weather.yahooapis.com/forecastrss?p=FIXX0013&u=c"));
- manager->get(postData);
-
+ sendXml();
}
/**
{
setListViewTopList(category);
}
+
+/**
+ *This slot function is called when set/change user button is clicked.
+ */
+void CarMainWindow::on_setUserPushButton_clicked()
+{
+ myLogin->show();
+
+ ui->userNameLabel->setText( "User: " + myLogin->getUserName());
+ ui->setUserPushButton->setText( "Change User");
+}
+
+/**
+ *@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::registrate()
+{
+ 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()));
+}
+
+/**
+ *@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 credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
+ credentials = "Basic " + credentials.toAscii().toBase64();
+
+ QUrl qurl("http//:api.speedfreak-app.com/update/acceleration-0-40");
+ QNetworkRequest request(qurl);
+
+ request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
+
+ xmlwriter->writeResult(xmlbuffer);
+
+ currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
+ //QString data("abcdefg"); //testing
+ //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data)); //testing
+
+
+ //ackFromServer function gets called when HTTP request is completed
+ connect(currentDownload, SIGNAL(finished()),SLOT(ackOfResult()));
+
+}
+
+/**
+ *@brief React to servers responce after result has been sent.
+ *@todo Implement function and write error handling.
+ */
+void CarMainWindow::ackOfResult()
+{
+ qDebug() << "Server acknowledged posting of result";
+}
+
+/**
+ *@brief React to servers responce after registration has been sent.
+ *@todo Implement function and write error handling.
+ */
+
+void CarMainWindow::ackOfRegistration()
+{
+ qDebug() << "Server acknowledged registration";
+}
+
+
+/**
+ *@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();
+}