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