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