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