Created new class GpsLocation that handles communication with gps. It
[ptas] / zouba / src / gui / searchdisplay.cpp
1 #include "searchdisplay.h"
2 #include "ui_searchdisplay.h"
3
4 #include "routeresultwidget.h"
5 #include "favoriteselectiondialog.h"
6 #include "locationsdisplaywindow.h"
7
8 #include "src/logic/locations.h"
9 #include "src/logic/routefinder.h"
10
11 #include <QDebug>
12
13 SearchDisplay::SearchDisplay(QWidget *parent) :
14     QMainWindow(parent),
15     ui(new Ui::SearchDisplay),
16     route_finder(NULL)
17 {
18     qDebug() << "Start constructor";
19     ui->setupUi(this);
20     this->setWindowTitle(QCoreApplication::applicationName());
21     qDebug() << "Application name:" << QCoreApplication::applicationName();
22
23     Locations *locations = Locations::GetInstance();
24     this->connect(locations, SIGNAL(locationsChanged()), SLOT(locations_changed()));
25
26 #ifdef Q_WS_MAEMO_5
27     this->setAttribute(Qt::WA_Maemo5StackedWindow);
28     this->setAttribute(Qt::WA_Maemo5AutoOrientation);
29
30     this->ui->fromHorizontalLayout->removeWidget(this->ui->from_combo);
31     this->ui->from_combo->deleteLater();
32     //this->ui->from_favorites->setIcon(QIcon::fromTheme("edit-copy"));
33     //this->ui->from_favorites->setText("Fav");
34
35     this->ui->destHorizontalLayout->removeWidget(this->ui->dest_combo);
36     this->ui->dest_combo->deleteLater();
37     //this->ui->from_favorites->setIcon(QIcon::fromTheme("edit-copy"));
38     //this->ui->dest_favorites->setText("Fav");
39
40     this->from_selected = NULL;
41     this->dest_selected = NULL;
42     this->edit_window = NULL;
43
44 #else
45     QWidget *widget = new QWidget();
46     QVBoxLayout *layout = new QVBoxLayout();
47     widget->setLayout(layout);
48
49     this->tabs = new QTabWidget();
50     this->tabs->setTabsClosable(true);
51     this->ui->centralwidget->setParent(this->tabs);
52     this->tabs->addTab(this->ui->centralwidget, "Search");
53
54     this->connect(this->tabs, SIGNAL(tabCloseRequested(int)), SLOT(tabclosed(int)));
55
56     layout->addWidget(this->tabs);
57     this->setCentralWidget(widget);
58
59     this->ui->fromHorizontalLayout->removeWidget(this->ui->from_favorites);
60     this->ui->from_favorites->deleteLater();
61     this->ui->destHorizontalLayout->removeWidget(this->ui->dest_favorites);
62     this->ui->dest_favorites->deleteLater();
63
64     this->updateLocationLists();
65
66     this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
67     this->ui->dest_combo->setCurrentIndex(1);
68
69 #endif
70
71     qDebug() << "Finish constructor";
72 }
73
74 SearchDisplay::~SearchDisplay()
75 {
76     delete ui;
77 }
78
79 void SearchDisplay::updateLocationLists()
80 {
81     qDebug() << "Start updateLocationLists";
82 #ifndef Q_WS_MAEMO_5
83     this->ui->from_combo->blockSignals(true);
84     this->ui->dest_combo->blockSignals(true);
85     this->ui->from_combo->clear();
86     this->ui->dest_combo->clear();
87     //this->ui->from_combo->blockSignals(false);
88     //this->ui->dest_combo->blockSignals(false);
89
90     /// TODO: Add GPS location if GPS is selected.
91
92
93
94
95     QStringList locs;
96     Locations *locations = Locations::GetInstance();
97     for (int index = 1; index <= locations->size(); ++index)
98     {
99         Location* loc = locations->getLocation(index);
100         if (loc && loc->isValid())
101         {
102             qDebug() << "Adding location: " << loc->label();
103             locs << loc->label();
104
105         }
106     }
107     //this->ui->from_combo->blockSignals(true);
108     //this->ui->dest_combo->blockSignals(true);
109     this->ui->from_combo->addItems(locs);
110     this->ui->dest_combo->addItems(locs);
111
112     this->ui->from_combo->blockSignals(false);
113     this->ui->dest_combo->blockSignals(false);
114 #endif
115     qDebug() << "Finish updateLocationLists";
116 }
117
118 void SearchDisplay::locations_changed()
119 {
120     qDebug() << "Start locations_changed";
121
122 #ifndef Q_WS_MAEMO_5
123     QString from_old = this->ui->from_combo->currentText();
124     QString dest_old = this->ui->dest_combo->currentText();
125
126     this->updateLocationLists();
127
128     qDebug() << "Reselecting old items.";
129     int from_id = this->ui->from_combo->findText(from_old);
130     if (from_id >= 0)
131         this->ui->from_combo->setCurrentIndex(from_id);
132     else
133         this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
134
135     int dest_id = this->ui->dest_combo->findText(dest_old);
136     if (dest_id >= 0)
137         this->ui->dest_combo->setCurrentIndex(dest_id);
138     else
139         this->on_dest_combo_currentIndexChanged(this->ui->dest_combo->currentText());
140 #endif
141     qDebug() << "Finish locations_changed";
142 }
143
144 void SearchDisplay::on_searchButton_clicked()
145 {
146     qDebug() << "Start on_search_button_clicked";
147     if (this->route_finder != NULL)
148     {
149 #ifdef Q_WS_MAEMO_5
150         this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
151 #endif
152         this->route_finder->disconnect(this, SLOT(route_finder_finished()));
153         delete this->route_finder;
154     }
155     // Check for empty search fields.
156     QString empty;
157     if (this->ui->from_edit->text() == empty)
158     {
159         qDebug() << "From field is empty. No search is made.";
160         return;
161     }
162     if (this->ui->dest_edit->text() == empty)
163     {
164         qDebug() << "Dest field is empty. No search is made.";
165         return;
166     }
167
168     Location *from, *dest;
169 #ifdef Q_WS_MAEMO_5
170     from = this->from_selected;
171     dest = this->dest_selected;
172 #else
173     Locations* locations = Locations::GetInstance();
174     from = locations->getLocation(this->ui->from_combo->currentText());
175     dest = locations->getLocation(this->ui->dest_combo->currentText());
176 #endif
177     if (from != NULL && this->ui->from_edit->text() == from->address()) {
178         qDebug() << "Written text matches the text in the combo box";
179         from = new Location(*from);
180     } else {
181         qDebug() << "Written text differs from the text in the combo box.";
182         from = new Location("Temp", this->ui->from_edit->text());
183     }
184
185     if (dest != NULL && this->ui->dest_edit->text() == dest->address()) {
186         qDebug() << "Written text matches the text in the combo box";
187         dest = new Location(*dest);
188     } else {
189         qDebug() << "Written text differs from the text in the combo box.";
190         dest = new Location("Temp", this->ui->dest_edit->text());
191     }
192
193     qDebug() << "Starting route search";
194
195     this->route_finder = new RouteFinder(*from, *dest);
196     delete from;
197     delete dest;
198     this->connect(this->route_finder, SIGNAL(finished()), SLOT(route_finder_finished()));
199 #ifdef Q_WS_MAEMO_5
200     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
201 #endif
202     qDebug() << "Finished on_search_button_clicked.";
203 }
204
205 void SearchDisplay::route_finder_finished()
206 {
207     qDebug() << "Received signal from successful route finder";
208 #ifdef Q_WS_MAEMO_5
209     RouteResultWidget *results = new RouteResultWidget(this);
210 #else
211     RouteResultWidget *results = new RouteResultWidget();
212 #endif
213
214     for (int i = 0; i < this->route_finder->getNumberOfRoutes(); i++)
215         results->addRoute(this->route_finder->getRoute(i));
216
217 #ifdef Q_WS_MAEMO_5
218     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
219     results->show();
220 #else
221     int cur = this->tabs->addTab(results, "Route" + QString::number(this->tabs->count()));
222     this->tabs->setCurrentIndex(cur);
223 #endif
224     qDebug() << "Finish route_finder_finished";
225 }
226
227 void SearchDisplay::setEditText(QLineEdit* edit, QString& text)
228 {
229     qDebug() << "Start setEditText";
230     Locations* locations = Locations::GetInstance();
231     Location* loc = locations->getLocation(text);
232     if (loc)
233         edit->setText(loc->address());
234     qDebug() << "Finish setEditText";
235 }
236
237
238 void SearchDisplay::on_from_combo_currentIndexChanged(QString text)
239 {
240 #ifndef Q_WS_MAEMO_5
241     qDebug() << "New FROM location selected.";
242     setEditText(this->ui->from_edit, text);
243     qDebug() << "Finish on_from_combo_currentIndexChanged";
244 #endif
245 }
246
247
248 void SearchDisplay::on_dest_combo_currentIndexChanged(QString text)
249 {
250 #ifndef Q_WS_MAEMO_5
251     qDebug() << "New DEST location selected.";
252     setEditText(this->ui->dest_edit, text);
253     qDebug() << "Finish on_dest_combo_currentIndexChanged";
254 #endif
255 }
256
257
258 #ifndef Q_WS_MAEMO_5
259 void SearchDisplay::tabclosed(int index)
260 {
261     qDebug() << "Tab close requested. Nr" << index;
262     if (index == 0)
263     {
264         qDebug() << "First tab requested to be closed.";
265         return;
266     }
267     this->tabs->removeTab(index);
268 }
269 #endif //Q_WS_MAEMO_5
270
271 void SearchDisplay::on_from_favorites_clicked()
272 {
273 #ifdef Q_WS_MAEMO_5
274     qDebug() << "FROM Favorites button clicked";
275     FavoriteSelectionDialog *dialog = new FavoriteSelectionDialog();
276     this->connect(dialog, SIGNAL(selectedLocation(Location*)), SLOT(from_selection_selected(Location*)));
277     this->connect(dialog, SIGNAL(customizeRequested()), SLOT(customize_requested()));
278     dialog->show();
279 #endif
280 }
281
282
283 void SearchDisplay::on_dest_favorites_clicked()
284 {
285 #ifdef Q_WS_MAEMO_5
286     qDebug() << "DEST Favorites button clicked";
287     FavoriteSelectionDialog *dialog = new FavoriteSelectionDialog();
288     this->connect(dialog, SIGNAL(selectedLocation(Location*)), SLOT(dest_selection_selected(Location*)));
289     this->connect(dialog, SIGNAL(customizeRequested()), SLOT(customize_requested()));
290     dialog->show();
291 #endif
292 }
293
294 #ifdef Q_WS_MAEMO_5
295 void SearchDisplay::from_selection_selected(Location *location)
296 {
297     if (location == NULL)
298     {
299         qDebug() << "NULL location received from FavoriteSelectionDialog.";
300         return;
301     }
302     this->from_selected = location;
303     this->ui->from_edit->setText(this->from_selected->address());
304 }
305
306 void SearchDisplay::dest_selection_selected(Location *location)
307 {
308     if (location == NULL)
309     {
310         qDebug() << "NULL location received from FavoriteSelectionDialog.";
311         return;
312     }
313     this->dest_selected = location;
314     this->ui->dest_edit->setText(this->dest_selected->address());
315 }
316
317 void SearchDisplay::customize_requested()
318 {
319     qDebug() << "Customizing favorites requested.";
320
321     if (!this->edit_window)
322         this->edit_window = new LocationsDisplayWindow(this);
323     this->edit_window->show();
324 }
325
326 #endif