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 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
23 //result = new ResultDialog();
24 //measure = new MeasureDialog();
25 welcomeDialog = new WelcomeDialog();
26 welcomeDialog->show();
28 initComboBoxStartTabUnits();
29 initListViewStartTabAccelerationCategories();
31 myLogin = new LoginWindow(this);
32 categorylist = new CategoryList();
33 myHttpClient = new HttpClient(this);
34 myRegistration = new Registration(this);
35 connect(myRegistration,SIGNAL(sendregistration()),this,SLOT(registrate()));
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()));
50 connect(myLogin, SIGNAL( userNameChanged()), this, SLOT(updateUserName()));
52 ui->labelMeasureTabResult->hide();
54 this->setWindowTitle("Speed Freak");
59 *Destructor of this class. Should be used to release all allocated resources.
61 CarMainWindow::~CarMainWindow()
71 *This function is used to .
74 void CarMainWindow::changeEvent(QEvent *e)
76 QMainWindow::changeEvent(e);
78 case QEvent::LanguageChange:
79 ui->retranslateUi(this);
87 *This slot function is called when ever list view is update. Start-tab view.
89 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
91 QString str = index.data().toString();
92 QStringList list = str.split("-");
93 QStringList list2 = list[1].split(" ");
95 ui->lineEditStartTabMin->setText(list[0]);
96 ui->lineEditStartTabMax->setText(list2[0]);
97 updateComboBoxStartTabUnits(list2[1]);
101 *This slot function is called when ever auto start button clicked. Start-tab view.
103 void CarMainWindow::on_autoStartButton_clicked()
108 //measure = new MeasureDialog();
109 // connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
110 accelerometer->start();
112 // Show measure dialog.
114 ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
118 *This slot function is called when ever list view is update. Start-tab view.
119 *@param QString unit.
121 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
123 ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
127 *This function is used to init unit combobox. Start-tab view.
129 void CarMainWindow::initComboBoxStartTabUnits()
131 units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
132 ui->comboBoxStartTabUnits->addItems(units);
136 *This function is used to set items to unit combobox. Start-tab view.
137 *@param QStringlist units
139 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
141 ui->comboBoxStartTabUnits->addItems(units);
145 *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
147 void CarMainWindow::initListViewStartTabAccelerationCategories()
149 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";
150 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
151 ui->listViewStartTabAccelerationCategories->setModel(model);
155 *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
156 *@param QStringlist accelerationCategoriesStartTab
158 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
160 QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
161 ui->listViewStartTabAccelerationCategories->setModel(model);
165 *This function is used to set items to category combobox. Top-tab view.
168 void CarMainWindow::setCategoryCompoBox()
170 ui->comboBoxTopCategory->addItems(categorylist->getCategoryList());
174 *This function is used to set items to labelTopList. Top-tab view.
175 *@param QString category
177 void CarMainWindow::setListViewTopList(QString category, int size)
180 topList.append( categorylist->getTopList(category, size));
181 ui->labelTopList->setText(topList);
185 *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
187 void CarMainWindow::openResultView()
189 //result->saveMeasuresToArray(measure->measures);
190 // Show result dialog.
192 ui->pushButtonSendResult->setEnabled(true);
194 timeInteger.setNum(this->measures->getTime40kmh());
195 //time = "0 - 40 km/h: ";
196 //time.append(timeInteger);
197 //ui->labelResult40kmh->setText(time);
198 ui->labelMeasureTabResult->show();
199 ui->labelMeasureTabResult->setText(timeInteger);
200 //ui->tabWidget->setCurrentWidget(this->ui->tabMeasureResult);
204 *This slot function is called when registrate button is clicked.
206 void CarMainWindow::on_registratePushButton_clicked()
208 myRegistration->show();
212 *This slot function is called when ever refresh button clicked. Top-tab view.
214 void CarMainWindow::on_buttonTopRefresh_clicked()
216 myHttpClient->requestCategories();
217 setCategoryCompoBox();
221 *This slot function is called when ever category combobox current index changed. Top-tab view.
222 *@param QString category
223 *@todo Check where limitNr is taken.
225 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
227 int limitNr = 5; //replace with real value?
228 QString limit = QString::number(limitNr);
229 category = "acceleration-0-100"; //replace with real value from category list/top window
230 myHttpClient->requestTopList(category, limit);
231 setListViewTopList(category,10);
235 *This slot function is called when ever category combobox activated. Top-tab view.
236 *@param QString category
238 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
240 setListViewTopList(category,10);
244 *This slot function is called when set/change user button is clicked.
246 void CarMainWindow::on_setUserPushButton_clicked()
252 *@brief Just for development, for the real button is not shown until
253 *measurin started and there are results.
254 *@todo Implement with real code and yet leave sendXml in the bottom in use.
256 void CarMainWindow::on_manualStartButton_clicked()
262 * This slot function is called when timer gives timeout signal. Checks current speed
263 * and stores times in measure class.
265 void CarMainWindow::after_timeout()
267 QString timeString, speedString;
269 time = accelerometer->getTotalTime();
270 speed = accelerometer->getCurrentSpeed();
273 if (floor(speed) == 10)
275 measures->setTime10kmh(time);
278 else if (floor(speed) == 20)
280 measures->setTime20kmh(time);
283 else if (floor(speed) == 30)
285 measures->setTime30kmh(time);
288 else if (floor(speed) == 40)
290 measures->setTime40kmh(time);
293 else if (floor(speed) == 50)
295 measures->setTime50kmh(time);
298 else if (floor(speed) == 60)
300 measures->setTime60kmh(time);
303 else if (floor(speed) == 70)
305 measures->setTime70kmh(time);
308 else if (floor(speed) == 80)
310 measures->setTime80kmh(time);
313 else if (floor(speed) == 90)
315 measures->setTime90kmh(time);
318 else if (floor(speed) == 100)
320 measures->setTime100kmh(time);
328 // If speed is over 40 km/h emits speedAchieved() signal and close this dialog.
332 accelerometer->stop();
335 //emit this->speedAchieved();
336 this->openResultView();
341 // Updates speed and time.
344 timeString.setNum(time);
345 speedString.setNum(speed);
346 ui->labelMeasureTabTime->setText(timeString);
347 ui->labelMeasureTabSpeed->setText(speedString);
355 * Initializes measures class's member variables.
357 void CarMainWindow::initializeMeasures()
359 measures->setTime10kmh(0);
360 measures->setTime20kmh(0);
361 measures->setTime30kmh(0);
362 measures->setTime40kmh(0);
363 measures->setTime50kmh(0);
364 measures->setTime60kmh(0);
365 measures->setTime70kmh(0);
366 measures->setTime80kmh(0);
367 measures->setTime90kmh(0);
368 measures->setTime100kmh(0);
372 * This slot function is called when Abort button is clicked.
374 void CarMainWindow::on_pushButtonMeasureTabAbort_clicked()
376 measures->setTime10kmh(0);
377 measures->setTime20kmh(0);
378 measures->setTime30kmh(0);
379 measures->setTime40kmh(0);
380 measures->setTime50kmh(0);
381 measures->setTime60kmh(0);
382 measures->setTime70kmh(0);
383 measures->setTime80kmh(0);
384 measures->setTime90kmh(0);
385 measures->setTime100kmh(0);
387 accelerometer->stop();
390 ui->tabWidget->setCurrentWidget(this->ui->StartTab);
394 void CarMainWindow::on_pushButtonSendResult_clicked()
396 myHttpClient->sendResultXml();
397 ui->pushButtonSendResult->setEnabled(false);
400 void CarMainWindow::updateUserName()
404 newUserName = myLogin->getUserName();
405 ui->userNameLabel->setText( "User: " + newUserName);
407 if (newUserName.length())
409 ui->setUserPushButton->setText( "Change User");
410 this->setWindowTitle("Speed freak - " + newUserName);
414 ui->setUserPushButton->setText( "Set User");
415 this->setWindowTitle("Speed freak");
419 void CarMainWindow::regUserToServer()
421 myHttpClient->requestRegistration();