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