1 #include "searchdisplay.h"
2 #include "ui_searchdisplay.h"
4 #include "routeresultwidget.h"
5 #include "favoriteselectiondialog.h"
6 #include "locationsdisplaywindow.h"
8 #include "src/logic/locations.h"
9 #include "src/logic/routefinder.h"
13 SearchDisplay::SearchDisplay(QWidget *parent) :
15 ui(new Ui::SearchDisplay),
18 qDebug() << "Start constructor";
20 this->setWindowTitle(QCoreApplication::applicationName());
21 qDebug() << "Application name:" << QCoreApplication::applicationName();
23 Locations *locations = Locations::GetInstance();
24 this->connect(locations, SIGNAL(locationsChanged()), SLOT(locations_changed()));
27 this->setAttribute(Qt::WA_Maemo5StackedWindow);
28 this->setAttribute(Qt::WA_Maemo5AutoOrientation);
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");
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");
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);
48 this->from_selected = NULL;
49 this->dest_selected = NULL;
50 this->edit_window = NULL;
53 QWidget *widget = new QWidget();
54 QVBoxLayout *layout = new QVBoxLayout();
55 widget->setLayout(layout);
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");
62 this->connect(this->tabs, SIGNAL(tabCloseRequested(int)), SLOT(tabclosed(int)));
64 layout->addWidget(this->tabs);
65 this->setCentralWidget(widget);
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();
72 this->updateLocationLists();
74 this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
75 this->ui->dest_combo->setCurrentIndex(1);
79 qDebug() << "Finish constructor";
82 void SearchDisplay::gps_status_change_requested(bool status)
88 SearchDisplay::~SearchDisplay()
93 void SearchDisplay::updateLocationLists()
95 qDebug() << "Start updateLocationLists";
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);
104 /// TODO: Add GPS location if GPS is selected.
110 Locations *locations = Locations::GetInstance();
111 for (int index = 1; index <= locations->size(); ++index)
113 Location* loc = locations->getLocation(index);
114 if (loc && loc->isValid())
116 qDebug() << "Adding location: " << loc->label();
117 locs << loc->label();
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);
126 this->ui->from_combo->blockSignals(false);
127 this->ui->dest_combo->blockSignals(false);
129 qDebug() << "Finish updateLocationLists";
132 void SearchDisplay::locations_changed()
134 qDebug() << "Start locations_changed";
137 QString from_old = this->ui->from_combo->currentText();
138 QString dest_old = this->ui->dest_combo->currentText();
140 this->updateLocationLists();
142 qDebug() << "Reselecting old items.";
143 int from_id = this->ui->from_combo->findText(from_old);
145 this->ui->from_combo->setCurrentIndex(from_id);
147 this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
149 int dest_id = this->ui->dest_combo->findText(dest_old);
151 this->ui->dest_combo->setCurrentIndex(dest_id);
153 this->on_dest_combo_currentIndexChanged(this->ui->dest_combo->currentText());
155 qDebug() << "Finish locations_changed";
158 void SearchDisplay::on_searchButton_clicked()
160 qDebug() << "Start on_search_button_clicked";
161 if (this->route_finder != NULL)
164 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
166 this->route_finder->disconnect(this, SLOT(route_finder_finished()));
167 delete this->route_finder;
169 // Check for empty search fields.
171 if (this->ui->from_edit->text() == empty)
173 qDebug() << "From field is empty. No search is made.";
176 if (this->ui->dest_edit->text() == empty)
178 qDebug() << "Dest field is empty. No search is made.";
182 Location *from, *dest;
184 from = this->from_selected;
185 dest = this->dest_selected;
187 Locations* locations = Locations::GetInstance();
188 from = locations->getLocation(this->ui->from_combo->currentText());
189 dest = locations->getLocation(this->ui->dest_combo->currentText());
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);
195 qDebug() << "Written text differs from the text in the combo box.";
196 from = new Location("Temp", this->ui->from_edit->text());
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);
203 qDebug() << "Written text differs from the text in the combo box.";
204 dest = new Location("Temp", this->ui->dest_edit->text());
207 qDebug() << "Starting route search";
209 this->route_finder = new RouteFinder(*from, *dest);
212 this->connect(this->route_finder, SIGNAL(finished()), SLOT(route_finder_finished()));
214 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
216 qDebug() << "Finished on_search_button_clicked.";
219 void SearchDisplay::route_finder_finished()
221 qDebug() << "Received signal from successful route finder";
223 RouteResultWidget *results = new RouteResultWidget(this);
225 RouteResultWidget *results = new RouteResultWidget();
228 for (int i = 0; i < this->route_finder->getNumberOfRoutes(); i++)
229 results->addRoute(this->route_finder->getRoute(i));
232 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
235 int cur = this->tabs->addTab(results, "Route" + QString::number(this->tabs->count()));
236 this->tabs->setCurrentIndex(cur);
238 qDebug() << "Finish route_finder_finished";
241 void SearchDisplay::setEditText(QLineEdit* edit, QString& text)
243 qDebug() << "Start setEditText";
244 Locations* locations = Locations::GetInstance();
245 Location* loc = locations->getLocation(text);
247 edit->setText(loc->address());
248 qDebug() << "Finish setEditText";
252 void SearchDisplay::on_from_combo_currentIndexChanged(QString text)
255 qDebug() << "New FROM location selected.";
256 setEditText(this->ui->from_edit, text);
257 qDebug() << "Finish on_from_combo_currentIndexChanged";
262 void SearchDisplay::on_dest_combo_currentIndexChanged(QString text)
265 qDebug() << "New DEST location selected.";
266 setEditText(this->ui->dest_edit, text);
267 qDebug() << "Finish on_dest_combo_currentIndexChanged";
273 void SearchDisplay::tabclosed(int index)
275 qDebug() << "Tab close requested. Nr" << index;
278 qDebug() << "First tab requested to be closed.";
281 this->tabs->removeTab(index);
283 #endif //Q_WS_MAEMO_5
285 void SearchDisplay::on_from_favorites_clicked()
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()));
297 void SearchDisplay::on_dest_favorites_clicked()
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()));
309 void SearchDisplay::from_selection_selected(Location *location)
311 if (location == NULL)
313 qDebug() << "NULL location received from FavoriteSelectionDialog.";
316 this->from_selected = location;
317 this->ui->from_edit->setText(this->from_selected->address());
320 void SearchDisplay::dest_selection_selected(Location *location)
322 if (location == NULL)
324 qDebug() << "NULL location received from FavoriteSelectionDialog.";
327 this->dest_selected = location;
328 this->ui->dest_edit->setText(this->dest_selected->address());
331 void SearchDisplay::customize_requested()
333 qDebug() << "Customizing favorites requested.";
335 if (!this->edit_window)
336 this->edit_window = new LocationsDisplayWindow(this);
337 this->edit_window->show();