Merge branch 'master' of https://vcs.maemo.org/git/speedfreak
[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     accstart = NULL;
30
31     creditsDialog = new CreditsDialog;
32     routeSaveDialog = new RouteSaveDialog;
33     routeDialog = new RouteDialog;
34     connect(routeDialog,SIGNAL(sendroute()),this,SLOT(clientSendRoute()));
35     settingsDialog = new SettingsDialog;
36     connect(settingsDialog,SIGNAL(sendregistration()),this,SLOT(clientRegUserToServer()));
37     connect(settingsDialog,SIGNAL(userNameChanged()),this,SLOT(clientUserLogin()));
38     topResultDialog = new TopResultDialog;
39     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
40     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
41     httpClient = new HttpClient(this);
42     connect(httpClient->myXmlreader, SIGNAL(receivedCategoryList()), this, SLOT(setCategoryCompoBox()));
43     connect(httpClient->myXmlreader, SIGNAL(receivedTop10List()), this, SLOT(showTop10()));
44     resultDialog = new ResultDialog;
45     connect(resultDialog, SIGNAL(sendResult(double)), this, SLOT(clientSendResult(double)));
46
47     welcomeDialog = new WelcomeDialog;
48     welcomeDialog->show();
49
50     //Button settings
51     ui->pushButtonAccelerate->setAutoFillBackground(true);
52     ui->pushButtonAccelerate->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
53     ui->pushButtonRoute->setAutoFillBackground(true);
54     ui->pushButtonRoute->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
55     ui->pushButtonResults->setAutoFillBackground(true);
56     ui->pushButtonResults->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
57     ui->pushButtonSettings->setAutoFillBackground(true);
58     ui->pushButtonSettings->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
59     ui->pushButtonWWW->setAutoFillBackground(true);
60     ui->pushButtonWWW->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
61     ui->pushButtonCredits->setAutoFillBackground(true);
62     ui->pushButtonCredits->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
63 }
64
65 MainWindow::~MainWindow()
66 {
67     delete ui;
68     delete routeSaveDialog;
69     delete routeDialog;
70
71     if(accstart)
72         delete accstart;
73 }
74
75 void MainWindow::changeEvent(QEvent *e)
76 {
77     QMainWindow::changeEvent(e);
78     switch (e->type()) {
79     case QEvent::LanguageChange:
80         ui->retranslateUi(this);
81         break;
82     default:
83         break;
84     }
85 }
86
87 /**
88   * This slot function opens browser to project www page.
89   */
90 void MainWindow::on_pushButtonWWW_clicked()
91 {
92     QDesktopServices::openUrl(QUrl("http://garage.maemo.org/projects/speedfreak/"));
93 }
94
95 /**
96   * This slot function opens the credits dialog
97   */
98 void MainWindow::on_pushButtonCredits_clicked()
99 {
100     creditsDialog->show();
101 }
102
103 /**
104   * This slot function opens the route save dialog
105   */
106 void MainWindow::on_pushButtonRoute_clicked()
107 {
108     routeSaveDialog->show();
109 }
110
111 /**
112   * This slot function opens the settings dialog
113   */
114 void MainWindow::on_pushButtonSettings_clicked()
115 {
116     settingsDialog->show();
117 }
118
119 /**
120   * This slot function opens the acceleration dialog
121   */
122 void MainWindow::on_pushButtonAccelerate_clicked()
123 {
124     if(!accstart)
125         accstart = new accelerationstart(this);
126     accstart->show();
127 }
128
129 /**
130   * This slot function opens the top results dialog
131   */
132 void MainWindow::on_pushButtonResults_clicked()
133 {
134     topResultDialog->show();
135 }
136
137 /**
138   *This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
139   */
140 void MainWindow::clientRequestCategoryList()
141 {
142     httpClient->requestCategories();
143 }
144
145 /**
146   *This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
147   */
148 void MainWindow::clientRequestTopList(int index)
149 {
150     QString limit = QString::number(topResultDialog->getLimitNr());
151     httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
152 }
153
154 /**
155   *This function is used to set items to category combobox. Top-tab view.
156   *@param
157   */
158 void MainWindow::setCategoryCompoBox()
159 {
160     topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
161 }
162
163 /**
164   *This function prcesses UI updating after a new top10List has been received.
165   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
166   */
167 void MainWindow::showTop10()
168 {
169     int ind = topResultDialog->getRecentCategoryIndex();
170     setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
171 }
172
173 /**
174   *This function is used to set items to labelTopList. Top-tab view.
175   *@param Category
176   *@param Size, number of results.
177   */
178 void MainWindow::setListViewTopList(QString category, int size)
179 {
180     QString topList;
181     topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
182     topResultDialog->showTopList(topList);
183 }
184
185 void MainWindow::clientRegUserToServer()
186 {
187     httpClient->requestRegistration();
188 }
189
190 /**
191   * This function performs login to server
192   */
193 void MainWindow::clientUserLogin()
194 {
195     httpClient->checkLogin();
196 }
197
198 void MainWindow::clientSendRoute()
199 {
200     httpClient->sendRouteXml();
201 }
202
203 void MainWindow::clientSendResult(double result)
204 {
205     if(accstart) {
206         httpClient->sendResultXml(accstart->getMeasureCategory(), result);
207     }
208 }