Added start and finish text for route dialog.
[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 MainWindow::MainWindow(QWidget *parent) :
20     QMainWindow(parent),
21     ui(new Ui::MainWindow)
22 {
23     ui->setupUi(this);
24
25     QCoreApplication::setOrganizationName("Fudeco Oy");
26     QCoreApplication::setOrganizationDomain("fudeco.com");
27     QCoreApplication::setApplicationName("Speed Freak");
28
29     helpDialog = NULL;
30     accstart = NULL;
31     routeSaveDialog = NULL;
32     topResultDialog = NULL;
33
34     settingsDialog = new SettingsDialog;
35     connect(settingsDialog,SIGNAL(sendregistration()),this,SLOT(clientRegUserToServer()));
36     connect(settingsDialog,SIGNAL(userNameChanged()),this,SLOT(clientUserLogin()));
37     connect(settingsDialog, SIGNAL(logout()), this, SLOT(setUsernameToMainPanel()));
38
39     httpClient = new HttpClient(this);
40     connect(httpClient->myXmlreader, SIGNAL(receivedCategoryList()), this, SLOT(setCategoryCompoBox()));
41     connect(httpClient->myXmlreader, SIGNAL(receivedTop10List()), this, SLOT(showTop10()));    
42
43     welcomeDialog = new WelcomeDialog;
44     //welcomeDialog->show();
45
46     this->setUsernameToMainPanel();
47
48     //Button settings
49     ui->pushButtonAccelerate->setAutoFillBackground(true);
50     ui->pushButtonAccelerate->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
51     ui->pushButtonRoute->setAutoFillBackground(true);
52     ui->pushButtonRoute->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
53     ui->pushButtonResults->setAutoFillBackground(true);
54     ui->pushButtonResults->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
55     ui->pushButtonSettings->setAutoFillBackground(true);
56     ui->pushButtonSettings->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
57     ui->pushButtonWWW->setAutoFillBackground(true);
58     ui->pushButtonWWW->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
59     ui->pushButtonCredits->setAutoFillBackground(true);
60     ui->pushButtonCredits->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
61 }
62
63 MainWindow::~MainWindow()
64 {
65     delete ui;
66
67     if(routeSaveDialog)
68         delete routeSaveDialog;
69
70     if(accstart)
71         delete accstart;
72
73     if(topResultDialog)
74         delete topResultDialog;
75
76     if(settingsDialog)
77         delete settingsDialog;
78
79     if(welcomeDialog)
80         delete welcomeDialog;
81
82     if(httpClient)
83         delete httpClient;
84
85     if(helpDialog)
86         delete helpDialog;
87 }
88
89 void MainWindow::changeEvent(QEvent *e)
90 {
91     QMainWindow::changeEvent(e);
92     switch (e->type()) {
93     case QEvent::LanguageChange:
94         ui->retranslateUi(this);
95         break;
96     default:
97         break;
98     }
99 }
100
101 /**
102   * This slot function opens browser to project www page.
103   */
104 void MainWindow::on_pushButtonWWW_clicked()
105 {
106     QDesktopServices::openUrl(QUrl("http://garage.maemo.org/projects/speedfreak/"));
107 }
108
109 /**
110   * This slot function opens the credits dialog
111   */
112 void MainWindow::on_pushButtonCredits_clicked()
113 {
114     if(!helpDialog)
115         helpDialog = new HelpDialog;
116
117     helpDialog->show();
118 }
119
120 /**
121   * This slot function opens the route save dialog
122   */
123 void MainWindow::on_pushButtonRoute_clicked()
124 {
125     if(!routeSaveDialog)
126         routeSaveDialog = new RouteSaveDialog;
127
128     connect(routeSaveDialog, SIGNAL(sendroute()), this, SLOT(clientSendRoute()));
129     connect(routeSaveDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
130     routeSaveDialog->show();
131 }
132
133 /**
134   * This slot function opens the settings dialog
135   */
136 void MainWindow::on_pushButtonSettings_clicked()
137 {
138     settingsDialog->show();
139 }
140
141 /**
142   * This slot function opens the acceleration dialog
143   */
144 void MainWindow::on_pushButtonAccelerate_clicked()
145 {
146     if(!accstart)
147         accstart = new accelerationstart(this);
148
149     connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
150     connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
151     accstart->show();
152 }
153
154 /**
155   * This slot function opens the top results dialog
156   */
157 void MainWindow::on_pushButtonResults_clicked()
158 {
159     if (!topResultDialog)
160         topResultDialog = new TopResultDialog;
161
162     clientRequestCategoryList();
163     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
164     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
165     connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
166     topResultDialog->show();
167 }
168
169 /**
170   *This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
171   */
172 void MainWindow::clientRequestCategoryList()
173 {
174     if(httpClient)
175         httpClient->requestCategories();
176 }
177
178 /**
179   *This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
180   */
181 void MainWindow::clientRequestTopList(int index)
182 {
183     QString limit;
184
185     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
186     {
187         limit = QString::number(topResultDialog->getLimitNr());
188         httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
189     }
190 }
191
192 /**
193   *This function is used to set items to category combobox. Top-tab view.
194   *@param
195   */
196 void MainWindow::setCategoryCompoBox()
197 {
198     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
199         topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
200 }
201
202 /**
203   *This function prcesses UI updating after a new top10List has been received.
204   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
205   */
206 void MainWindow::showTop10()
207 {
208     int ind;
209
210     if(topResultDialog && httpClient->myXmlreader->myCategoryList && topResultDialog)
211     {
212         ind = topResultDialog->getRecentCategoryIndex();
213         setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
214     }
215 }
216
217 /**
218   *This function is used to set items to labelTopList. Top-tab view.
219   *@param Category
220   *@param Size, number of results.
221   */
222 void MainWindow::setListViewTopList(QString category, int size)
223 {
224     QString topList;
225
226     if(httpClient->myXmlreader->myCategoryList && topResultDialog)
227     {
228         topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
229         topResultDialog->showTopList(topList);
230     }
231 }
232
233 /**
234   * This function register user to server.
235   */
236 void MainWindow::clientRegUserToServer()
237 {
238     if(httpClient)
239         httpClient->requestRegistration();
240 }
241
242 /**
243   * This function performs login to server.
244   */
245 void MainWindow::clientUserLogin()
246 {
247     
248     if(httpClient)
249     {
250         connect(httpClient, SIGNAL(loginOK()), this, SLOT(setUsernameToMainPanel()));
251         httpClient->checkLogin();
252     }
253 }
254
255 /**
256   * This function send route data to server.
257   */
258 void MainWindow::clientSendRoute()
259 {
260     if(httpClient)
261         httpClient->sendRouteXml();
262 }
263
264 /**
265   * This function send acceleration data to server.
266   */
267 void MainWindow::clientSendResult(QString category, double result)
268 {
269     qDebug() << "__clientSendResult";
270     if(accstart) {
271         qDebug() << "_clientSendResult, calling server";
272         if(httpClient)
273             httpClient->sendResultXml(category, result);
274     }
275 }
276 /**
277   * This slot function called when ever dialog finished.
278   */
279 void MainWindow::killDialog()
280 {
281     if(topResultDialog)
282     {
283         qDebug() << "__MW kill: topResultDialog";
284         delete topResultDialog;
285         topResultDialog = NULL;
286     }
287     if(routeSaveDialog)
288     {
289         qDebug() << "__MW kill: routeSaveDialog";
290         //delete routeSaveDialog;
291         //routeSaveDialog = NULL;
292     }
293     if(accstart)
294     {
295         qDebug() << "__MW kill: accstart";
296         delete accstart;
297         accstart = NULL;
298     }
299     if(welcomeDialog)
300     {
301         qDebug() << "__MW kill: welcomeDialog";
302         delete welcomeDialog;
303         welcomeDialog = NULL;
304     }
305     if(helpDialog)
306     {
307         qDebug() << "__MW kill: helpDialog";
308         delete helpDialog;
309         helpDialog = NULL;
310     }
311 }
312
313 void MainWindow::setUsernameToMainPanel()
314 {
315     if (loginSaved())
316     {
317         this->setWindowTitle("SpeedFreak - " + settingsDialog->getUserName());
318     }
319     else
320     {
321         this->setWindowTitle("SpeedFreak - Not logged");
322     }
323 }