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"
15 *Constructor of this class.
16 *@param QWidget pointer to parent object. By default the value is NULL.
18 CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow)
21 result = new ResultDialog();
22 measure = new MeasureDialog();
23 xmlreader = new XmlReader();
25 initComboBoxStartTabUnits();
26 initListViewStartTabAccelerationCategories();
28 myLogin = new LoginWindow(this);
29 myRegistration = new Registration(this);
30 xmlwriter = new XmlWriter();
31 manager = new QNetworkAccessManager(this);
32 connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*)));
33 connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(registrate()));
34 connect(result,SIGNAL(sendresult()),this,SLOT(sendXml()));
39 *Destructor of this class. Should be used to release all allocated resources.
41 CarMainWindow::~CarMainWindow()
52 *This function is used to .
55 void CarMainWindow::changeEvent(QEvent *e)
57 QMainWindow::changeEvent(e);
59 case QEvent::LanguageChange:
60 ui->retranslateUi(this);
68 *This slot function is called when ever list view is update. Start-tab view.
70 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
72 QString str = index.data().toString();
73 QStringList list = str.split("-");
74 QStringList list2 = list[1].split(" ");
76 ui->lineEditStartTabMin->setText(list[0]);
77 ui->lineEditStartTabMax->setText(list2[0]);
78 updateComboBoxStartTabUnits(list2[1]);
82 *This slot function is called when ever auto start button clicked. Start-tab view.
84 void CarMainWindow::on_autoStartButton_clicked()
89 measure = new MeasureDialog();
91 connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
92 // Show measure dialog.
97 *This slot function is called when ever list view is update. Start-tab view.
100 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
102 ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
106 *This function is used to init unit combobox. Start-tab view.
108 void CarMainWindow::initComboBoxStartTabUnits()
110 units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
111 ui->comboBoxStartTabUnits->addItems(units);
115 *This function is used to set items to unit combobox. Start-tab view.
116 *@param QStringlist units
118 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
120 ui->comboBoxStartTabUnits->addItems(units);
124 *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
126 void CarMainWindow::initListViewStartTabAccelerationCategories()
128 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";
129 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
130 ui->listViewStartTabAccelerationCategories->setModel(model);
134 *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
135 *@param QStringlist accelerationCategoriesStartTab
137 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
139 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
140 ui->listViewStartTabAccelerationCategories->setModel(model);
144 *This function is used to set items to category combobox. Top-tab view.
147 void CarMainWindow::setCategoryCompoBox()
149 ui->comboBoxTopCategory->addItems(xmlreader->getTop10List());
153 *This function is used to set items to labelTopList. Top-tab view.
154 *@param QString category
156 void CarMainWindow::setListViewTopList(QString category)
160 if (category == "acceleration-0-100")
162 topList.append(xmlreader->getTop10AccelerationList());
165 else if (category == "Speed")
167 topList.append(xmlreader->getTop10SpeedList());
170 else if (category == "G-force")
172 topList.append(xmlreader->getTop10GforceList());
174 ui->labelTopList->setText(topList);
178 *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
180 void CarMainWindow::openResultView()
182 result->saveMeasuresToArray(measure->measures);
183 // Show result dialog.
188 *This slot function is called when the server has finished guery.
190 void CarMainWindow::networkResponse(QNetworkReply *reply)
196 *This slot function is called when the user will to send data to server.
197 *@todo Where is this callback connected?
199 void CarMainWindow::on_pushButton_clicked()
205 *This slot function is called when login/logout button is clicked.
207 void CarMainWindow::on_loginLogoutButton_clicked()
209 //LoginWindow myLogin;
212 //ui->loginLogoutButton->setText("logout");
216 *This slot function is called when registrate button is clicked.
218 void CarMainWindow::on_registratePushButton_clicked()
220 myRegistration->show();
224 *This slot function is called when ever refresh button clicked. Top-tab view.
226 void CarMainWindow::on_buttonTopRefresh_clicked()
228 setCategoryCompoBox();
232 *This slot function is called when ever category combobox current index changed. Top-tab view.
233 *@param QString category
235 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
237 setListViewTopList(category);
241 *This slot function is called when ever category combobox activated. Top-tab view.
242 *@param QString category
244 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
246 setListViewTopList(category);
250 *This slot function is called when set/change user button is clicked.
252 void CarMainWindow::on_setUserPushButton_clicked()
256 ui->userNameLabel->setText( "User: " + myLogin->getUserName());
257 ui->setUserPushButton->setText( "Change User");
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();
273 QNetworkReply *currentDownload;
275 QUrl qurl("http//:api.speedfreak-app.com/register");
276 QNetworkRequest request(qurl);
278 //write also to a file during development, :
279 xmlwriter->writeXml(this->myRegistration->getUserName(),
280 this->myRegistration->getPassword(),
281 this->myRegistration->getEmail());
282 xmlwriter->writeRegistering(regbuffer,
283 this->myRegistration->getUserName(),
284 this->myRegistration->getPassword(),
285 this->myRegistration->getEmail());
286 //Tmp msgbox - later server responce
287 QMessageBox::about(this,"Registrate",this->myRegistration->getUserName() + this->myRegistration->getPassword() + this->myRegistration->getEmail());
289 currentDownload = manager->post(request, ("data=" + regbuffer->data()));
291 //ackFromServer function gets called when HTTP request is completed
292 connect(currentDownload, SIGNAL(finished()),SLOT(ackOfRegistration()));
296 *@brief Sends result(s) to the server in xml format with authentication information in the header.
297 *@todo Write error handling.
299 void CarMainWindow::sendXml()
301 qDebug() << "_sendXml";
303 QBuffer *xmlbuffer = new QBuffer();
304 QNetworkReply *currentDownload;
306 QString credentials = this->myRegistration->getUserName() + ":" + this->myRegistration->getPassword();
307 credentials = "Basic " + credentials.toAscii().toBase64();
309 QUrl qurl("http//:api.speedfreak-app.com/update/acceleration-0-40");
310 QNetworkRequest request(qurl);
312 request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
314 xmlwriter->writeResult(xmlbuffer);
316 currentDownload = manager->post(request, ("data=" + xmlbuffer->data()));
317 //QString data("abcdefg"); //testing
318 //currentDownload = manager->post(request,"data=" + QUrl::toPercentEncoding(data)); //testing
321 //ackFromServer function gets called when HTTP request is completed
322 connect(currentDownload, SIGNAL(finished()),SLOT(ackOfResult()));
327 *@brief React to servers responce after result has been sent.
328 *@todo Implement function and write error handling.
330 void CarMainWindow::ackOfResult()
332 qDebug() << "Server acknowledged posting of result";
336 *@brief React to servers responce after registration has been sent.
337 *@todo Implement function and write error handling.
340 void CarMainWindow::ackOfRegistration()
342 qDebug() << "Server acknowledged registration";
347 *@brief Just for development, for the real button is not shown until
348 *measurin started and there are results.
349 *@todo Implement with real code and yet leave sendXml in the bottom in use.
352 void CarMainWindow::on_manualStartButton_clicked()