More connecting client-server functionality to GUI.
[speedfreak] / Client / mainwindow.cpp
1 /*
2  * Mainwindow for speedFreak project
3  *
4  * @author      Rikhard Kuutti <rikhard.kuutti@fudeco.com>
5  * @copyright   (c) 2010 Speed Freak team
6  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "mainwindow.h"
10 #include "ui_mainwindow.h"
11
12 #include <QDesktopServices>
13 #include <QUrl>
14 #include <QSettings>
15 #include <QDebug>
16
17 MainWindow::MainWindow(QWidget *parent) :
18     QMainWindow(parent),
19     ui(new Ui::MainWindow)
20 {
21     ui->setupUi(this);
22
23     QCoreApplication::setOrganizationName("Fudeco Oy");
24     QCoreApplication::setOrganizationDomain("fudeco.com");
25     QCoreApplication::setApplicationName("Speed Freak");
26
27     accstart = NULL;
28
29     creditsDialog = new CreditsDialog;
30     routeSaveDialog = new RouteSaveDialog;
31     routeDialog = new RouteDialog;
32     connect(routeDialog,SIGNAL(sendroute()),this,SLOT(clientSendRoute()));
33     settingsDialog = new SettingsDialog;
34     connect(settingsDialog,SIGNAL(sendregistration()),this,SLOT(clientRegUserToServer()));
35     connect(settingsDialog,SIGNAL(userNameChanged()),this,SLOT(clientUserLogin()));
36     topResultDialog = new TopResultDialog;
37     connect(topResultDialog, SIGNAL(refreshCategoryList()), this, SLOT(clientRequestCategoryList()));
38     connect(topResultDialog, SIGNAL(refreshTopList(int)), this, SLOT(clientRequestTopList(int)));
39     httpClient = new HttpClient(this);
40     connect(httpClient->myXmlreader, SIGNAL(receivedCategoryList()), this, SLOT(setCategoryCompoBox()));
41     connect(httpClient->myXmlreader, SIGNAL(receivedTop10List()), this, SLOT(showTop10()));
42     resultDialog = new ResultDialog;
43     connect(resultDialog, SIGNAL(sendResult(double)), this, SLOT(clientSendResult(double)));
44
45     welcomeDialog = new WelcomeDialog;
46     welcomeDialog->show();
47 }
48
49 MainWindow::~MainWindow()
50 {
51     delete ui;
52
53     delete routeSaveDialog;
54
55     if(!accstart)
56         delete accstart;
57 }
58
59 void MainWindow::changeEvent(QEvent *e)
60 {
61     QMainWindow::changeEvent(e);
62     switch (e->type()) {
63     case QEvent::LanguageChange:
64         ui->retranslateUi(this);
65         break;
66     default:
67         break;
68     }
69 }
70
71 void MainWindow::on_pushButtonWWW_clicked()
72 {
73     QDesktopServices::openUrl(QUrl("http://garage.maemo.org/projects/speedfreak/"));
74 }
75
76 void MainWindow::on_pushButtonCredits_clicked()
77 {
78     creditsDialog->show();
79 }
80
81 void MainWindow::on_pushButtonRoute_clicked()
82 {
83     routeSaveDialog->show();
84 }
85
86 void MainWindow::on_pushButtonSettings_clicked()
87 {
88     settingsDialog->show();
89 }
90
91 void MainWindow::on_pushButtonAccelerate_clicked()
92 {
93     if(!accstart)
94         accstart = new accelerationstart(this);
95     accstart->show();
96 }
97
98 void MainWindow::on_pushButtonResults_clicked()
99 {
100     topResultDialog->show();
101 }
102
103 /**
104   *This slot function is called when ever mytTopResultDialog emits signal refreshCategoryList button clicked.
105   */
106 void MainWindow::clientRequestCategoryList()
107 {
108     httpClient->requestCategories();
109 }
110
111 /**
112   *This slot function is called when ever mytTopResultDialog emits signal refreshTopList button clicked.
113   */
114 void MainWindow::clientRequestTopList(int index)
115 {
116     QString limit = QString::number(topResultDialog->getLimitNr());
117     httpClient->requestTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(index), limit);
118 }
119
120 /**
121   *This function is used to set items to category combobox. Top-tab view.
122   *@param
123   */
124 void MainWindow::setCategoryCompoBox()
125 {
126     topResultDialog->setCompoBoxCategories(httpClient->myXmlreader->myCategoryList->getCategoryList());
127 }
128
129 /**
130   *This function prcesses UI updating after a new top10List has been received.
131   *@todo Check where limitNr is taken, fixed or user input, see on_comboBoxTopCategory_currentIndexChanged.
132   */
133 void MainWindow::showTop10()
134 {
135     int ind = topResultDialog->getRecentCategoryIndex();
136     setListViewTopList(httpClient->myXmlreader->myCategoryList->getRecentCategory(ind), topResultDialog->getLimitNr());
137 }
138
139 /**
140   *This function is used to set items to labelTopList. Top-tab view.
141   *@param Category
142   *@param Size, number of results.
143   */
144 void MainWindow::setListViewTopList(QString category, int size)
145 {
146     QString topList;
147     topList.append(httpClient->myXmlreader->myCategoryList->getTopList(category, size));
148     topResultDialog->showTopList(topList);
149 }
150
151 void MainWindow::clientRegUserToServer()
152 {
153     httpClient->requestRegistration();
154 }
155
156 void MainWindow::clientUserLogin()
157 {
158     httpClient->checkLogin();
159 }
160
161 void MainWindow::clientSendRoute()
162 {
163     httpClient->sendRouteXml();
164 }
165
166 void MainWindow::clientSendResult(double result)
167 {
168     if(accstart) {
169         httpClient->sendResultXml(accstart->getMeasureCategory(), result);
170     }
171 }