Fixed bug 5709. Now result dialog shows only two decimals in time labels.
[speedfreak] / Client / mainwindow.cpp
1 /*
2  * Mainwindow for speedFreak project
3  *
4  * @author      Rikhard Kuutti  <rikhard.kuutti@fudeco.com>
5  * @author      Toni Jussila    <toni.jussila@fudeco.com>
6  * @copyright   (c) 2010 Speed Freak team
7  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
8  */
9
10 #include "mainwindow.h"
11 #include "ui_mainwindow.h"
12
13 #include <QDesktopServices>
14 #include <QUrl>
15 #include <QSettings>
16 #include <QDebug>
17 #include "usersettings.h"
18
19 /**
20   *
21   */
22 MainWindow::MainWindow(QWidget *parent) :
23     QMainWindow(parent),
24     ui(new Ui::MainWindow)
25 {
26     ui->setupUi(this);
27
28     QCoreApplication::setOrganizationName("Fudeco Oy");
29     QCoreApplication::setOrganizationDomain("fudeco.com");
30     QCoreApplication::setApplicationName("Speed Freak");
31
32     helpDialog = NULL;
33     accstart = NULL;
34     routeSaveDialog = NULL;
35     topResultDialog = NULL;
36
37     usersDialog = NULL;
38
39     settingsDialog = new SettingsDialog;
40     connect(settingsDialog, SIGNAL(sendregistration()), this, SLOT(clientRegUserToServer()));
41     connect(settingsDialog, SIGNAL(userNameChanged()),  this, SLOT(clientUserLogin()));
42     connect(settingsDialog, SIGNAL(logout()),           this, SLOT(setUsernameToMainPanel()));
43     connect(settingsDialog, SIGNAL(saveprofile()),      this, SLOT(saveProfile()));
44
45     httpClient = new HttpClient(this);
46     connect(httpClient->myXmlreader, SIGNAL(receivedCategoryList()), this, SLOT(setCategoryCompoBox()));
47     connect(httpClient->myXmlreader, SIGNAL(receivedTop10List()), this, SLOT(showTop10()));    
48
49     welcomeDialog = new WelcomeDialog;
50     welcomeDialog->show();
51
52     this->setUsernameToMainPanel();
53
54     //Create icon for acceleration start button
55     QIcon* icon = new QIcon();
56     icon->addFile(QString(":/new/prefix1/Graphics/Speedometer.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
57     icon->addFile(QString(":/new/prefix1/Graphics/Speedometer2.png"), QSize(125,125), QIcon::Normal, QIcon::On);
58
59     //Acceleration start button
60
61     customButtonAccelerate = new CustomButton(this,icon);
62     delete icon;
63
64     int buttons_x = 50,buttons_y = 165;
65     customButtonAccelerate->setGeometry(buttons_x,buttons_y,130,130);
66     connect(customButtonAccelerate, SIGNAL(OpenDialog()), this, SLOT(OpenAccStartDialog()));
67     customButtonAccelerate->show();
68
69     //Create icon for route dialog button
70     icon = new QIcon();
71     icon->addFile(QString(":/new/prefix1/Graphics/route.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
72     icon->addFile(QString(":/new/prefix1/Graphics/route_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
73
74     //Route dialog button
75
76     customButtonRoute = new CustomButton(this,icon);
77     delete icon;
78
79     buttons_x += 140;
80     customButtonRoute->setGeometry(buttons_x,buttons_y,130,130);
81     connect(customButtonRoute, SIGNAL(OpenDialog()), this, SLOT(OpenRouteDialog()));
82     customButtonRoute->show();
83
84     //Create icon for results dialog button
85     icon = new QIcon();
86     icon->addFile(QString(":/new/prefix1/Graphics/trophy_gold.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
87     icon->addFile(QString(":/new/prefix1/Graphics/trophy_gold_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
88
89     //Results dialog button
90
91     customButtonResults = new CustomButton(this,icon);
92     delete icon;
93
94     buttons_x += 140;
95     customButtonResults->setGeometry(buttons_x,buttons_y,130,130);
96     connect(customButtonResults, SIGNAL(OpenDialog()), this, SLOT(OpenResultDialog()));
97     customButtonResults->show();
98     //Create icon for settings dialog button
99     icon = new QIcon();
100     icon->addFile(QString(":/new/prefix1/Graphics/settings.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
101     icon->addFile(QString(":/new/prefix1/Graphics/settings_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
102
103     //Settings dialog button
104
105     customButtonSettings = new CustomButton(this,icon);
106     delete icon;
107
108     buttons_x += 140;
109     customButtonSettings->setGeometry(buttons_x,buttons_y,130,130);
110     connect(customButtonSettings, SIGNAL(OpenDialog()), this, SLOT(OpenSettingsDialog()));
111     customButtonSettings->show();
112
113     //Create icon for users dialog button
114     icon = new QIcon();
115     icon->addFile(QString(":/new/prefix1/Graphics/users.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
116     icon->addFile(QString(":/new/prefix1/Graphics/users_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
117
118     //Users dialog button
119     customButtonUsers = new CustomButton(this,icon);
120     delete icon;
121
122     buttons_x += 140;
123     customButtonUsers->setGeometry(buttons_x,buttons_y,130,130);
124     connect(customButtonUsers, SIGNAL(OpenDialog()), this, SLOT(openUsersDialog()));
125     customButtonUsers->show();
126
127     //Create icon for help dialog button
128     icon = new QIcon();
129     icon->addFile(QString(":/new/prefix1/Graphics/info.png"), QSize(85,85), QIcon::Normal, QIcon::Off);
130     icon->addFile(QString(":/new/prefix1/Graphics/info_selected.png"), QSize(85,85), QIcon::Normal, QIcon::On);
131
132     //Help dialog button
133
134     customButtonHelp = new CustomButton(this,icon);
135     delete icon;
136
137     customButtonHelp->setGeometry(670,10,105,105);
138     connect(customButtonHelp, SIGNAL(OpenDialog()), this, SLOT(OpenHelpDialog()));
139     customButtonHelp->show();
140
141
142     //Create icon for www page button
143     icon = new QIcon();
144     icon->addFile(QString(":/new/prefix1/Graphics/applications_internet.png"), QSize(85,85), QIcon::Normal, QIcon::Off);
145     icon->addFile(QString(":/new/prefix1/Graphics/applications_internet_selected.png"), QSize(85,85), QIcon::Normal, QIcon::On);
146
147     //WWW page button
148     customButtonWWW = new CustomButton(this,icon);
149     delete icon;
150
151     customButtonWWW->setGeometry(670,320,105,105);
152     connect(customButtonWWW, SIGNAL(OpenDialog()), this, SLOT(OpenWWWPage()));
153     customButtonWWW->show();
154
155 }
156
157 /**
158   *
159   */
160 MainWindow::~MainWindow()
161 {
162     delete ui;
163
164     if(routeSaveDialog)
165         delete routeSaveDialog;
166
167     if(accstart)
168         delete accstart;
169
170     if(topResultDialog)
171         delete topResultDialog;
172
173     if(settingsDialog)
174         delete settingsDialog;
175
176     if(welcomeDialog)
177         delete welcomeDialog;
178
179     if(httpClient)
180         delete httpClient;
181
182     if(helpDialog)
183         delete helpDialog;
184
185     if(usersDialog)
186         delete usersDialog;
187
188     if(customButtonAccelerate)
189         delete customButtonAccelerate;
190     if(customButtonRoute)
191         delete customButtonRoute;
192     if(customButtonResults)
193         delete customButtonResults;
194     if(customButtonSettings)
195         delete customButtonSettings;
196     if(customButtonWWW)
197         delete customButtonWWW;
198     if(customButtonHelp)
199         delete customButtonHelp;
200 }
201
202 /**
203   *
204   */
205 void MainWindow::changeEvent(QEvent *e)
206 {
207     QMainWindow::changeEvent(e);
208     switch (e->type()) {
209     case QEvent::LanguageChange:
210         ui->retranslateUi(this);
211         break;
212     default:
213         break;
214     }
215 }
216
217 /**
218   *This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
219   */
220 void MainWindow::clientRequestCategoryList()
221 {
222     if(httpClient)
223         httpClient->requestCategories();
224 }
225
226 /**
227   *This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
228   */
229 void MainWindow::clientRequestTopList(int index)
230 {
231     QString limit;
232
233     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
234     {
235         limit = QString::number(topResultDialog->getLimitNr());
236         httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
237     }
238 }
239
240 /**
241   *This function is used to set items to category combobox.
242   *@param
243   */
244 void MainWindow::setCategoryCompoBox()
245 {
246     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
247         topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
248 }
249
250 /**
251   *This function prcesses UI updating after a new top10List has been received.
252   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
253   */
254 void MainWindow::showTop10()
255 {
256     int ind;
257
258     if(topResultDialog && httpClient->myXmlreader->myCategoryList && topResultDialog)
259     {
260         ind = topResultDialog->getRecentCategoryIndex();
261         setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
262     }
263 }
264
265 /**
266   *This function is used to set items to labelTopList. Top-tab view.
267   *@param Category
268   *@param Size, number of results.
269   */
270 void MainWindow::setListViewTopList(QString category, int size)
271 {
272     QString topList;
273
274     if(httpClient->myXmlreader->myCategoryList && topResultDialog)
275     {
276         topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
277         topResultDialog->showTopList(topList);
278     }
279 }
280
281 /**
282   * This function register user to server.
283   */
284 void MainWindow::clientRegUserToServer()
285 {
286     if(httpClient)
287         httpClient->requestRegistration();
288 }
289
290 /**
291   * This function performs login to server.
292   */
293 void MainWindow::clientUserLogin()
294 {
295     
296     if(httpClient)
297     {
298         connect(httpClient, SIGNAL(loginOK()), this, SLOT(setUsernameToMainPanel()));
299         httpClient->checkLogin();
300     }
301 }
302
303 /**
304   * This function send route data to server.
305   * @param QString oldName, old file name
306   * @param QString newName, new file name
307   * @param int i
308   */
309 void MainWindow::clientSendRoute(QString oldName, QString newName, int i)
310 {
311     if(httpClient)
312         httpClient->sendRouteXml(oldName, newName, i);
313 }
314
315 /**
316   * This function send acceleration data to server.
317   */
318 void MainWindow::clientSendResult(QString category, double result)
319 {
320     qDebug() << "__clientSendResult";
321     if(accstart) {
322         qDebug() << "_clientSendResult, calling server";
323         if(httpClient)
324             httpClient->sendResultXml(category, result);
325     }
326 }
327
328 /**
329   * This slot function called when ever dialog rejected.
330   */
331 void MainWindow::killDialog()
332 {
333     if(topResultDialog)
334     {
335         qDebug() << "__MW kill: topResultDialog";
336         delete topResultDialog;
337         topResultDialog = NULL;
338     }
339     if(routeSaveDialog)
340     {
341         //qDebug() << "__MW kill: routeSaveDialog";
342         //delete routeSaveDialog;
343         //routeSaveDialog = NULL;
344     }
345     if(accstart)
346     {
347         qDebug() << "__MW kill: accstart";
348         delete accstart;
349         accstart = NULL;
350     }
351     if(welcomeDialog)
352     {
353         qDebug() << "__MW kill: welcomeDialog";
354         delete welcomeDialog;
355         welcomeDialog = NULL;
356     }
357     if(helpDialog)
358     {
359         qDebug() << "__MW kill: helpDialog";
360         delete helpDialog;
361         helpDialog = NULL;
362     }
363     if(usersDialog)
364     {
365         qDebug() << "__MW kill: usersDialog";
366         delete usersDialog;
367         usersDialog = NULL;
368     }
369 }
370
371 /**
372   *
373   */
374 void MainWindow::setUsernameToMainPanel()
375 {
376     if (loginSaved())
377     {
378         this->setWindowTitle("SpeedFreak - " + settingsDialog->getUserName());
379     }
380     else
381     {
382         this->setWindowTitle("SpeedFreak - Not logged");
383     }
384 }
385 /**
386   * This slot function opens acceleration start dialog.
387   */
388 void MainWindow::OpenAccStartDialog()
389 {
390     if(!accstart)
391         accstart = new accelerationstart(this);
392
393     connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
394     connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
395     accstart->show();
396 }
397 /**
398   * This slot function opens the route save dialog
399   */
400 void MainWindow::OpenRouteDialog()
401 {
402     if(!routeSaveDialog)
403         routeSaveDialog = new RouteSaveDialog;
404
405     connect(routeSaveDialog, SIGNAL(sendroute(QString,QString,int)), this, SLOT(clientSendRoute(QString,QString,int)));
406     connect(routeSaveDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
407     routeSaveDialog->show();
408 }
409 /**
410   * This slot function opens the top results dialog
411   */
412 void MainWindow::OpenResultDialog()
413 {
414     if (!topResultDialog)
415         topResultDialog = new TopResultDialog;
416
417     clientRequestCategoryList();
418     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
419     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
420     connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
421     topResultDialog->show();
422 }
423 /**
424   * This slot function opens the settings dialog
425   */
426 void MainWindow::OpenSettingsDialog()
427 {
428     settingsDialog->show();
429 }
430 /**
431   * This slot function opens browser to project www page.
432   */
433 void MainWindow::OpenWWWPage()
434 {
435     QDesktopServices::openUrl(QUrl("http://www.speedfreak-app.com/"));
436 }
437 /**
438   * This slot function opens the main help dialog
439   */
440 void MainWindow::OpenHelpDialog()
441 {
442     if(!helpDialog)
443         helpDialog = new HelpDialog;
444
445     connect(helpDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
446     helpDialog->show();
447 }
448
449 /**
450   * This slot function save user profile data to server
451   */
452 void MainWindow::saveProfile()
453 {
454     if(httpClient)
455         httpClient->sendProfileXml();
456 }
457
458 /**
459   * This slot function calls httpClients requestUserInfo for getting user's information from server.
460   */
461 void MainWindow::requestGetUserInfo(QString name)
462 {
463     qDebug() << "getUserInfo signal " + name;
464     if(httpClient)
465     {
466         httpClient->requestUserInfo(name);
467     }
468 }
469
470 /**
471   * This slot function calls httpClients requestUsers for getting usernames from server.
472   */
473 void MainWindow::requestGetUsers()
474 {
475     qDebug() << "getUsers signal";
476     if(httpClient)
477     {
478         httpClient->requestUsers();
479     }
480 }
481
482 void MainWindow::openUsersDialog()
483 {
484     if(!usersDialog)
485         usersDialog = new UsersDialog;
486
487     connect(usersDialog, SIGNAL(getUserInfo(QString)), this, SLOT(requestGetUserInfo(QString)));
488     //connect(usersDialog, SIGNAL(getUsers()), this, SLOT(requestGetUsers()));
489     requestGetUsers();
490     connect(usersDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
491     usersDialog->show();
492 }