Merge branch 'test/route'
[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 acceleration dialog
162   */
163 void MainWindow::on_pushButtonAccelerate_clicked()
164 {
165     if(!accstart)
166         accstart = new accelerationstart(this);
167
168     connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
169     connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
170     accstart->show();
171 }
172
173 /**
174   * This slot function opens the top results dialog
175   */
176 void MainWindow::on_pushButtonResults_clicked()
177 {
178     if (!topResultDialog)
179         topResultDialog = new TopResultDialog;
180
181     clientRequestCategoryList();
182     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
183     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
184     connect(topResultDialog, SIGNAL(rejected()), this, SLOT(killDialog()));
185     topResultDialog->show();
186 }
187
188 /**
189   *This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
190   */
191 void MainWindow::clientRequestCategoryList()
192 {
193     if(httpClient)
194         httpClient->requestCategories();
195 }
196
197 /**
198   *This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
199   */
200 void MainWindow::clientRequestTopList(int index)
201 {
202     QString limit;
203
204     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
205     {
206         limit = QString::number(topResultDialog->getLimitNr());
207         httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
208     }
209 }
210
211 /**
212   *This function is used to set items to category combobox. Top-tab view.
213   *@param
214   */
215 void MainWindow::setCategoryCompoBox()
216 {
217     if(topResultDialog && httpClient->myXmlreader->myCategoryList)
218         topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
219 }
220
221 /**
222   *This function prcesses UI updating after a new top10List has been received.
223   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
224   */
225 void MainWindow::showTop10()
226 {
227     int ind;
228
229     if(topResultDialog && httpClient->myXmlreader->myCategoryList && topResultDialog)
230     {
231         ind = topResultDialog->getRecentCategoryIndex();
232         setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
233     }
234 }
235
236 /**
237   *This function is used to set items to labelTopList. Top-tab view.
238   *@param Category
239   *@param Size, number of results.
240   */
241 void MainWindow::setListViewTopList(QString category, int size)
242 {
243     QString topList;
244
245     if(httpClient->myXmlreader->myCategoryList && topResultDialog)
246     {
247         topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
248         topResultDialog->showTopList(topList);
249     }
250 }
251
252 /**
253   * This function register user to server.
254   */
255 void MainWindow::clientRegUserToServer()
256 {
257     if(httpClient)
258         httpClient->requestRegistration();
259 }
260
261 /**
262   * This function performs login to server.
263   */
264 void MainWindow::clientUserLogin()
265 {
266     
267     if(httpClient)
268     {
269         connect(httpClient, SIGNAL(loginOK()), this, SLOT(setUsernameToMainPanel()));
270         httpClient->checkLogin();
271     }
272 }
273
274 /**
275   * This function send route data to server.
276   */
277 void MainWindow::clientSendRoute()
278 {
279     if(httpClient)
280         httpClient->sendRouteXml();
281 }
282
283 /**
284   * This function send acceleration data to server.
285   */
286 void MainWindow::clientSendResult(QString category, double result)
287 {
288     qDebug() << "__clientSendResult";
289     if(accstart) {
290         qDebug() << "_clientSendResult, calling server";
291         if(httpClient)
292             httpClient->sendResultXml(category, result);
293     }
294 }
295 /**
296   * This slot function called when ever dialog finished.
297   */
298 void MainWindow::killDialog()
299 {
300     if(topResultDialog)
301     {
302         qDebug() << "__MW kill: topResultDialog";
303         delete topResultDialog;
304         topResultDialog = NULL;
305     }
306     if(routeSaveDialog)
307     {
308         qDebug() << "__MW kill: routeSaveDialog";
309         //delete routeSaveDialog;
310         //routeSaveDialog = NULL;
311     }
312     if(accstart)
313     {
314         qDebug() << "__MW kill: accstart";
315         delete accstart;
316         accstart = NULL;
317     }
318     if(welcomeDialog)
319     {
320         qDebug() << "__MW kill: welcomeDialog";
321         delete welcomeDialog;
322         welcomeDialog = NULL;
323     }
324     if(helpDialog)
325     {
326         qDebug() << "__MW kill: helpDialog";
327         delete helpDialog;
328         helpDialog = NULL;
329     }
330 }
331 /**
332   *
333   */
334 void MainWindow::setUsernameToMainPanel()
335 {
336     if (loginSaved())
337     {
338         this->setWindowTitle("SpeedFreak - " + settingsDialog->getUserName());
339     }
340     else
341     {
342         this->setWindowTitle("SpeedFreak - Not logged");
343     }
344 }
345 /**
346   * This slot function opens acceleration start dialog.
347   */
348 void MainWindow::OpenAccStartDialog()
349 {
350     if(!accstart)
351         accstart = new accelerationstart(this);
352     connect(accstart, SIGNAL(sendresult(QString, double)), this, SLOT(clientSendResult(QString, double)));
353     connect(accstart, SIGNAL(rejected()), this, SLOT(killDialog()));
354     accstart->show();
355 }