Just testing...one header added to UI_design document.
[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->pushButtonSettings->setAutoFillBackground(true);
50     ui->pushButtonSettings->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
51     ui->pushButtonWWW->setAutoFillBackground(true);
52     ui->pushButtonWWW->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
53     ui->pushButtonCredits->setAutoFillBackground(true);
54     ui->pushButtonCredits->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
55
56     //Create icon for acceleration start button
57     QIcon* icon = new QIcon();
58     icon->addFile(QString(":/new/prefix1/Graphics/Speedometer.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
59     icon->addFile(QString(":/new/prefix1/Graphics/Speedometer2.png"), QSize(125,125), QIcon::Normal, QIcon::On);
60
61     //Acceleration start button
62
63     customButtonAccelerate = new CustomButton(this,icon);
64     delete icon;
65
66     int buttons_x = 50,buttons_y = 165;
67     customButtonAccelerate->setGeometry(buttons_x,buttons_y,130,130);
68     connect(customButtonAccelerate, SIGNAL(OpenDialog()), this, SLOT(OpenAccStartDialog()));
69     customButtonAccelerate->show();
70
71     //Create icon for route dialog button
72     icon = new QIcon();
73     icon->addFile(QString(":/new/prefix1/Graphics/route.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
74     icon->addFile(QString(":/new/prefix1/Graphics/route_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
75
76     //Route dialog button
77
78     customButtonRoute = new CustomButton(this,icon);
79     delete icon;
80
81     buttons_x += 140;
82     customButtonRoute->setGeometry(buttons_x,buttons_y,130,130);
83     connect(customButtonRoute, SIGNAL(OpenDialog()), this, SLOT(OpenRouteDialog()));
84     customButtonRoute->show();
85
86     //Create icon for results dialog button
87     icon = new QIcon();
88     icon->addFile(QString(":/new/prefix1/Graphics/trophy_gold.png"), QSize(125,125), QIcon::Normal, QIcon::Off);
89     icon->addFile(QString(":/new/prefix1/Graphics/trophy_gold_selected.png"), QSize(125,125), QIcon::Normal, QIcon::On);
90
91     //Results dialog button
92
93     customButtonResults = new CustomButton(this,icon);
94     delete icon;
95
96     buttons_x += 140;
97     customButtonResults->setGeometry(buttons_x,buttons_y,130,130);
98     connect(customButtonResults, SIGNAL(OpenDialog()), this, SLOT(OpenResultDialog()));
99     customButtonResults->show();
100 }
101
102 MainWindow::~MainWindow()
103 {
104     delete ui;
105
106     if(routeSaveDialog)
107         delete routeSaveDialog;
108
109     if(accstart)
110         delete accstart;
111
112     if(topResultDialog)
113         delete topResultDialog;
114
115     if(settingsDialog)
116         delete settingsDialog;
117
118     if(welcomeDialog)
119         delete welcomeDialog;
120
121     if(httpClient)
122         delete httpClient;
123
124     if(helpDialog)
125         delete helpDialog;
126
127     if(customButtonAccelerate)
128         delete customButtonAccelerate;
129     if(customButtonRoute)
130         delete customButtonRoute;
131     if(customButtonResults)
132         delete customButtonResults;
133
134 }
135
136 void MainWindow::changeEvent(QEvent *e)
137 {
138     QMainWindow::changeEvent(e);
139     switch (e->type()) {
140     case QEvent::LanguageChange:
141         ui->retranslateUi(this);
142         break;
143     default:
144         break;
145     }
146 }
147
148 /**
149   * This slot function opens browser to project www page.
150   */
151 void MainWindow::on_pushButtonWWW_clicked()
152 {
153     QDesktopServices::openUrl(QUrl("http://garage.maemo.org/projects/speedfreak/"));
154 }
155
156 /**
157   * This slot function opens the credits dialog
158   */
159 void MainWindow::on_pushButtonCredits_clicked()
160 {
161     if(!helpDialog)
162         helpDialog = new HelpDialog;
163
164     connect(helpDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
165     helpDialog->show();
166 }
167
168 /**
169   * This slot function opens the settings dialog
170   */
171 void MainWindow::on_pushButtonSettings_clicked()
172 {
173     settingsDialog->show();
174 }
175
176 /**
177   *This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
178   */
179 void MainWindow::clientRequestCategoryList()
180 {
181     if(httpClient)
182         httpClient->requestCategories();
183 }
184
185 /**
186   *This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
187   */
188 void MainWindow::clientRequestTopList(int index)
189 {
190     QString limit;
191
192     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
193     {
194         limit = QString::number(topResultDialog->getLimitNr());
195         httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
196     }
197 }
198
199 /**
200   *This function is used to set items to category combobox. Top-tab view.
201   *@param
202   */
203 void MainWindow::setCategoryCompoBox()
204 {
205     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
206         topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
207 }
208
209 /**
210   *This function prcesses UI updating after a new top10List has been received.
211   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
212   */
213 void MainWindow::showTop10()
214 {
215     int ind;
216
217     if(topResultDialog && httpClient->myXmlreader->myCategoryList && topResultDialog)
218     {
219         ind = topResultDialog->getRecentCategoryIndex();
220         setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
221     }
222 }
223
224 /**
225   *This function is used to set items to labelTopList. Top-tab view.
226   *@param Category
227   *@param Size, number of results.
228   */
229 void MainWindow::setListViewTopList(QString category, int size)
230 {
231     QString topList;
232
233     if(httpClient->myXmlreader->myCategoryList && topResultDialog)
234     {
235         topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
236         topResultDialog->showTopList(topList);
237     }
238 }
239
240 /**
241   * This function register user to server.
242   */
243 void MainWindow::clientRegUserToServer()
244 {
245     if(httpClient)
246         httpClient->requestRegistration();
247 }
248
249 /**
250   * This function performs login to server.
251   */
252 void MainWindow::clientUserLogin()
253 {
254     
255     if(httpClient)
256     {
257         connect(httpClient, SIGNAL(loginOK()), this, SLOT(setUsernameToMainPanel()));
258         httpClient->checkLogin();
259     }
260 }
261
262 /**
263   * This function send route data to server.
264   */
265 void MainWindow::clientSendRoute()
266 {
267     if(httpClient)
268         httpClient->sendRouteXml();
269 }
270
271 /**
272   * This function send acceleration data to server.
273   */
274 void MainWindow::clientSendResult(QString category, double result)
275 {
276     qDebug() << "__clientSendResult";
277     if(accstart) {
278         qDebug() << "_clientSendResult, calling server";
279         if(httpClient)
280             httpClient->sendResultXml(category, result);
281     }
282 }
283 /**
284   * This slot function called when ever dialog rejected.
285   */
286 void MainWindow::killDialog()
287 {
288     if(topResultDialog)
289     {
290         qDebug() << "__MW kill: topResultDialog";
291         delete topResultDialog;
292         topResultDialog = NULL;
293     }
294     if(routeSaveDialog)
295     {
296         qDebug() << "__MW kill: routeSaveDialog";
297         //delete routeSaveDialog;
298         //routeSaveDialog = NULL;
299     }
300     if(accstart)
301     {
302         qDebug() << "__MW kill: accstart";
303         delete accstart;
304         accstart = NULL;
305     }
306     if(welcomeDialog)
307     {
308         qDebug() << "__MW kill: welcomeDialog";
309         delete welcomeDialog;
310         welcomeDialog = NULL;
311     }
312     if(helpDialog)
313     {
314         qDebug() << "__MW kill: helpDialog";
315         delete helpDialog;
316         helpDialog = NULL;
317     }
318 }
319 /**
320   *
321   */
322 void MainWindow::setUsernameToMainPanel()
323 {
324     if (loginSaved())
325     {
326         this->setWindowTitle("SpeedFreak - " + settingsDialog->getUserName());
327     }
328     else
329     {
330         this->setWindowTitle("SpeedFreak - Not logged");
331     }
332 }
333 /**
334   * This slot function opens acceleration start dialog.
335   */
336 void MainWindow::OpenAccStartDialog()
337 {
338     if(!accstart)
339         accstart = new accelerationstart(this);
340
341     connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
342     connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
343     accstart->show();
344 }
345 /**
346   * This slot function opens the route save dialog
347   */
348 void MainWindow::OpenRouteDialog()
349 {
350     if(!routeSaveDialog)
351         routeSaveDialog = new RouteSaveDialog;
352
353     connect(routeSaveDialog, SIGNAL(sendroute()), this, SLOT(clientSendRoute()));
354     connect(routeSaveDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
355     routeSaveDialog->show();
356 }
357 /**
358   * This slot function opens the top results dialog
359   */
360 void MainWindow::OpenResultDialog()
361 {
362     if (!topResultDialog)
363         topResultDialog = new TopResultDialog;
364
365     clientRequestCategoryList();
366     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
367     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
368     connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
369     topResultDialog->show();
370 }