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