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