Send result button added in result dialog.
[speedfreak] / Client / carmainwindow.cpp
1 /**
2   * CarMainWindow main class
3   *
4   * @author     Toni Jussila <toni.jussila@fudeco.com>
5   * @author     Janne Änäkkälä <janne.anakkala@fudeco.com>
6   * @author     Tiina Kivilinna-Korhola <tiina.kivilinna-korhola@fudeco.com>
7   * @author     Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
8   * @copyright  (c) 2010 Speed Freak team
9   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
10   */
11
12 #include "carmainwindow.h"
13
14 /**
15   *Constructor of this class.
16   *@param QWidget pointer to parent object. By default the value is NULL.
17   */
18 CarMainWindow::CarMainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::CarMainWindow)
19 {
20     ui->setupUi(this);
21     result = new ResultDialog();
22     measure = new MeasureDialog();
23     xmlreader = new XmlReader();
24
25     initComboBoxStartTabUnits();
26     initListViewStartTabAccelerationCategories();
27
28     myLogin = new LoginWindow(this);
29     myRegistration = new Registration(this);
30     manager = new QNetworkAccessManager(this);
31     connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(networkResponse(QNetworkReply*)));
32 }
33
34 /**
35   *Destructor of this class. Should be used to release all allocated resources.
36   */
37 CarMainWindow::~CarMainWindow()
38 {
39     delete ui;
40     delete result;
41     delete measure;
42     delete xmlreader;
43 }
44
45 /**
46   *This function is used to .
47   *@param
48   */
49 void CarMainWindow::changeEvent(QEvent *e)
50 {
51     QMainWindow::changeEvent(e);
52     switch (e->type()) {
53     case QEvent::LanguageChange:
54         ui->retranslateUi(this);
55         break;
56     default:
57         break;
58     }
59 }
60
61 /**
62   *This slot function is called when ever list view is update. Start-tab view.
63   */
64 void CarMainWindow::on_listViewStartTabAccelerationCategories_clicked(QModelIndex index)
65 {
66     QString str = index.data().toString();
67     QStringList list = str.split("-");
68     QStringList list2 = list[1].split(" ");
69
70     ui->lineEditStartTabMin->setText(list[0]);
71     ui->lineEditStartTabMax->setText(list2[0]);
72     updateComboBoxStartTabUnits(list2[1]);
73 }
74
75 /**
76   *This slot function is called when ever auto start button clicked. Start-tab view.
77   */
78 void CarMainWindow::on_autoStartButton_clicked()
79 {
80
81     delete measure;
82     measure = NULL;
83     measure = new MeasureDialog();
84
85     connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
86     // Show measure dialog.
87     measure->show();
88 }
89
90 /**
91   *This slot function is called when ever list view is update. Start-tab view.
92   *@param QString unit.
93   */
94 void CarMainWindow::updateComboBoxStartTabUnits(QString unit)
95 {
96     ui->comboBoxStartTabUnits->setCurrentIndex(ui->comboBoxStartTabUnits->findText(unit, Qt::MatchExactly));
97 }
98
99 /**
100   *This function is used to init unit combobox. Start-tab view.
101   */
102 void CarMainWindow::initComboBoxStartTabUnits()
103 {
104     units << "km/h" << "km" << "h" << "m" << "min" << "Mile" << "Mph" << "in" << "ft" << "yrd";
105     ui->comboBoxStartTabUnits->addItems(units);
106 }
107
108 /**
109   *This function is used to set items to unit combobox. Start-tab view.
110   *@param QStringlist units
111   */
112 void CarMainWindow::setComboBoxStartTabUnits(QStringList units)
113 {
114     ui->comboBoxStartTabUnits->addItems(units);
115 }
116
117 /**
118   *This function is used to init listViewStartTabAccelerationCategories. Start-tab view.
119   */
120 void CarMainWindow::initListViewStartTabAccelerationCategories()
121 {
122     accelerationCategoriesStartTab << "0-40 km/h" << "0-100 km/h"; //<< "0-1/4 Mile" << "0-1/8 Mile" << "0-50 km" << "50-100 Mile" << "0-60 Mph" << "0-100 m" << "0-50 ft" << "0-50 yrd" << "0-500 in";
123     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
124     ui->listViewStartTabAccelerationCategories->setModel(model);
125 }
126
127 /**
128   *This function is used to set items to listViewStartTabAccelerationCategories. Start-tab view.
129   *@param QStringlist accelerationCategoriesStartTab
130   */
131 void CarMainWindow::setListViewStartTabAccelerationCategories(QStringList accelerationCategoriesStartTab)
132 {
133     QAbstractItemModel *model = new StringListModel(accelerationCategoriesStartTab);
134     ui->listViewStartTabAccelerationCategories->setModel(model);
135 }
136
137 /**
138   *This function is used to set items to category combobox. Top-tab view.
139   *@param
140   */
141 void CarMainWindow::setCategoryCompoBox()
142 {
143     ui->comboBoxTopCategory->addItems(xmlreader->getTop10List());
144 }
145
146 /**
147   *This function is used to set items to labelTopList. Top-tab view.
148   *@param QString category
149   */
150 void CarMainWindow::setListViewTopList(QString category)
151 {
152     QString topList;
153
154     if (category == "acceleration-0-100")
155     {
156         topList.append(xmlreader->getTop10AccelerationList());
157     }
158
159     else if (category == "Speed")
160     {
161         topList.append(xmlreader->getTop10SpeedList());
162     }
163
164     else if (category == "G-force")
165     {
166         topList.append(xmlreader->getTop10GforceList());
167     }
168     ui->labelTopList->setText(topList);
169 }
170
171 /**
172   *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
173   */
174 void CarMainWindow::openResultView()
175 {
176     result->saveMeasuresToArray(measure->measures);
177     // Show result dialog.
178     result->show();
179 }
180
181 /**
182   *This slot function is called when the server has finished guery.
183   */
184 void CarMainWindow::networkResponse(QNetworkReply *reply)
185 {
186 }
187
188 /**
189   *This slot function is called when the user will to send data to server.
190   */
191 void CarMainWindow::on_pushButton_clicked()
192 {
193      QNetworkRequest postData;
194      postData.setUrl(QString("http://weather.yahooapis.com/forecastrss?p=FIXX0013&u=c"));
195      manager->get(postData);
196
197 }
198
199 /**
200   *This slot function is called when login/logout button is clicked.
201   */
202 void CarMainWindow::on_loginLogoutButton_clicked()
203 {
204     //LoginWindow myLogin;
205
206     myLogin->show();
207     //ui->loginLogoutButton->setText("logout");
208 }
209
210 /**
211   *This slot function is called when registrate button is clicked.
212   */
213 void CarMainWindow::on_registratePushButton_clicked()
214 {
215     myRegistration->show();
216 }
217
218 /**
219   *This slot function is called when ever refresh button clicked. Top-tab view.
220   */
221 void CarMainWindow::on_buttonTopRefresh_clicked()
222 {
223     setCategoryCompoBox();
224 }
225
226 /**
227   *This slot function is called when ever category combobox current index changed. Top-tab view.
228   *@param QString category
229   */
230 void CarMainWindow::on_comboBoxTopCategory_currentIndexChanged(QString category)
231 {
232     setListViewTopList(category);
233 }
234
235 /**
236   *This slot function is called when ever category combobox activated. Top-tab view.
237   *@param QString category
238   */
239 void CarMainWindow::on_comboBoxTopCategory_activated(QString category)
240 {
241     setListViewTopList(category);
242 }
243
244 /**
245   *This slot function is called when set/change user button is clicked.
246   */
247 void CarMainWindow::on_setUserPushButton_clicked()
248 {
249     myLogin->show();
250
251     ui->userNameLabel->setText( "User: " + myLogin->getUserName());
252     ui->setUserPushButton->setText( "Change User");
253 }