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