1 #include "searchdisplay.h"
2 #include "ui_searchdisplay.h"
4 #include "routeresultwidget.h"
5 #include "favoriteselectiondialog.h"
6 #include "locationsdisplaywidget.h"
8 #include "src/logic/locations.h"
9 #include "src/logic/routefinder.h"
13 SearchDisplay::SearchDisplay(QWidget *parent) :
15 ui(new Ui::SearchDisplay),
18 search_restarted(false),
21 qDebug() << "Start constructor";
23 this->setWindowTitle(QCoreApplication::applicationName());
24 qDebug() << "Application name:" << QCoreApplication::applicationName();
26 Locations *locations = Locations::GetInstance();
27 this->connect(locations, SIGNAL(locationsChanged()), SLOT(locations_changed()));
29 QMenuBar *menu = this->ui->menubar;
32 this->setAttribute(Qt::WA_Maemo5StackedWindow);
33 this->setAttribute(Qt::WA_Maemo5AutoOrientation);
35 this->ui->fromHorizontalLayout->removeWidget(this->ui->from_combo);
36 this->ui->from_combo->deleteLater();
37 //this->ui->from_favorites->setIcon(QIcon::fromTheme("edit-copy"));
38 //this->ui->from_favorites->setText("Fav");
40 this->ui->destHorizontalLayout->removeWidget(this->ui->dest_combo);
41 this->ui->dest_combo->deleteLater();
42 //this->ui->from_favorites->setIcon(QIcon::fromTheme("edit-copy"));
43 //this->ui->dest_favorites->setText("Fav");
45 QAction *use_gps = new QAction("Use GPS", this);
46 use_gps->setCheckable(true);
47 use_gps->setChecked(false);
48 this->connect(use_gps, SIGNAL(toggled(bool)), SLOT(gps_status_change_requested(bool)));
49 this->connect(use_gps, SIGNAL(toggled(bool)), locations->getGpsLocation(), SLOT(enableGps(bool)));
50 menu->addAction(use_gps);
52 this->from_selected = NULL;
53 this->dest_selected = NULL;
56 QWidget *widget = new QWidget();
57 QVBoxLayout *layout = new QVBoxLayout();
58 widget->setLayout(layout);
60 this->tabs = new QTabWidget();
61 this->tabs->setTabsClosable(true);
62 this->ui->centralwidget->setParent(this->tabs);
63 this->tabs->addTab(this->ui->centralwidget, "Search");
65 this->connect(this->tabs, SIGNAL(tabCloseRequested(int)), SLOT(tabclosed(int)));
67 layout->addWidget(this->tabs);
68 this->setCentralWidget(widget);
70 this->ui->fromHorizontalLayout->removeWidget(this->ui->from_favorites);
71 this->ui->from_favorites->deleteLater();
72 this->ui->destHorizontalLayout->removeWidget(this->ui->dest_favorites);
73 this->ui->dest_favorites->deleteLater();
75 this->updateLocationLists();
77 this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
78 this->ui->dest_combo->setCurrentIndex(1);
80 QAction *modify_locations = new QAction("Modify locations", this);
81 this->connect(modify_locations, SIGNAL(triggered()), SLOT(customize_requested()));
82 menu->addAction(modify_locations);
86 qDebug() << "Finish constructor";
89 void SearchDisplay::gps_status_change_requested(bool status)
95 SearchDisplay::~SearchDisplay()
100 void SearchDisplay::updateLocationLists()
102 qDebug() << "Start updateLocationLists";
104 this->ui->from_combo->blockSignals(true);
105 this->ui->dest_combo->blockSignals(true);
106 this->ui->from_combo->clear();
107 this->ui->dest_combo->clear();
108 //this->ui->from_combo->blockSignals(false);
109 //this->ui->dest_combo->blockSignals(false);
111 /// TODO: Add GPS location if GPS is selected.
117 Locations *locations = Locations::GetInstance();
118 for (int index = 1; index <= locations->size(); ++index)
120 Location* loc = locations->getLocation(index);
121 if (loc && loc->isValid())
123 qDebug() << "Adding location: " << loc->label();
124 locs << loc->label();
128 //this->ui->from_combo->blockSignals(true);
129 //this->ui->dest_combo->blockSignals(true);
130 this->ui->from_combo->addItems(locs);
131 this->ui->dest_combo->addItems(locs);
133 this->ui->from_combo->blockSignals(false);
134 this->ui->dest_combo->blockSignals(false);
136 qDebug() << "Finish updateLocationLists";
139 void SearchDisplay::locations_changed()
142 qDebug() << "Start locations_changed";
144 QString from_old = this->ui->from_combo->currentText();
145 QString dest_old = this->ui->dest_combo->currentText();
147 this->updateLocationLists();
149 qDebug() << "Reselecting old items.";
150 int from_id = this->ui->from_combo->findText(from_old);
152 this->ui->from_combo->setCurrentIndex(from_id);
154 this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
156 int dest_id = this->ui->dest_combo->findText(dest_old);
158 this->ui->dest_combo->setCurrentIndex(dest_id);
160 this->on_dest_combo_currentIndexChanged(this->ui->dest_combo->currentText());
162 qDebug() << "Finish locations_changed";
166 void SearchDisplay::on_searchButton_clicked()
168 qDebug() << "Start on_search_button_clicked";
169 if (!this->one_search.tryLock())
170 return; // If mutex is locked ie. the results are being processed, do nothing.
171 if (this->route_finder)
174 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
176 this->route_finder->disconnect(this);
177 this->route_finder->deleteLater();
178 this->route_finder = NULL;
179 this->search_restarted = true;
180 qDebug() << "Tried to start new search. Restart set to true.";
183 // Check for empty search fields.
185 if (this->ui->from_edit->text() == empty)
187 qDebug() << "From field is empty. No search is made.";
188 this->one_search.unlock();
189 this->search_restarted = false;
192 if (this->ui->dest_edit->text() == empty)
194 qDebug() << "Dest field is empty. No search is made.";
195 this->one_search.unlock();
196 this->search_restarted = false;
200 Location *from, *dest;
202 from = this->from_selected;
203 dest = this->dest_selected;
205 Locations* locations = Locations::GetInstance();
206 from = locations->getLocation(this->ui->from_combo->currentText());
207 dest = locations->getLocation(this->ui->dest_combo->currentText());
209 if (from != NULL && this->ui->from_edit->text() == from->address()) {
210 qDebug() << "Written text matches the text in the combo box";
211 from = new Location(*from);
213 qDebug() << "Written text differs from the text in the combo box.";
214 from = new Location("Temp", this->ui->from_edit->text());
217 if (dest != NULL && this->ui->dest_edit->text() == dest->address()) {
218 qDebug() << "Written text matches the text in the combo box";
219 dest = new Location(*dest);
221 qDebug() << "Written text differs from the text in the combo box.";
222 dest = new Location("Temp", this->ui->dest_edit->text());
225 qDebug() << "Starting route search";
227 this->route_finder = new RouteFinder(*from, *dest);
230 this->connect(this->route_finder, SIGNAL(finished()), SLOT(route_finder_finished()));
232 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
234 this->one_search.unlock();
235 qDebug() << "Finished on_search_button_clicked.";
238 void SearchDisplay::route_finder_finished()
240 qDebug() << "Received signal from successful route finder";
241 if (!this->one_search.tryLock())
243 if (this->search_restarted)
245 qDebug() << "Restart had been requested.";
246 this->search_restarted = false;
247 this->one_search.unlock();
250 if(!this->route_finder)
252 qDebug() << "Route finder is NULL.";
253 this->one_search.unlock();
258 RouteResultWidget *results = new RouteResultWidget(this);
259 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
262 RouteResultWidget *results = new RouteResultWidget();
263 int cur = this->tabs->addTab(results, "Route" + QString::number(this->tabs->count()));
264 this->tabs->setCurrentIndex(cur);
267 for (int i = 0; i < this->route_finder->getNumberOfRoutes(); i++)
268 results->addRoute(this->route_finder->getRoute(i));
270 this->route_finder->disconnect(this);
271 this->route_finder->deleteLater();
272 this->route_finder = NULL;
274 this->one_search.unlock();
275 qDebug() << "Finish route_finder_finished";
278 void SearchDisplay::setEditText(QLineEdit* edit, QString& text)
280 qDebug() << "Start setEditText";
281 Locations* locations = Locations::GetInstance();
282 Location* loc = locations->getLocation(text);
284 edit->setText(loc->address());
285 qDebug() << "Finish setEditText";
289 void SearchDisplay::on_from_combo_currentIndexChanged(QString text)
292 qDebug() << "New FROM location selected.";
293 setEditText(this->ui->from_edit, text);
294 qDebug() << "Finish on_from_combo_currentIndexChanged";
299 void SearchDisplay::on_dest_combo_currentIndexChanged(QString text)
302 qDebug() << "New DEST location selected.";
303 setEditText(this->ui->dest_edit, text);
304 qDebug() << "Finish on_dest_combo_currentIndexChanged";
310 void SearchDisplay::tabclosed(int index)
312 qDebug() << "Tab close requested. Nr" << index;
315 qDebug() << "First tab requested to be closed.";
318 QWidget *widget = this->tabs->widget(index);
319 this->tabs->removeTab(index);
320 if (widget == this->edit_window)
321 this->edit_window = NULL;
322 widget->deleteLater();
324 #endif //Q_WS_MAEMO_5
326 void SearchDisplay::on_from_favorites_clicked()
329 qDebug() << "FROM Favorites button clicked";
330 FavoriteSelectionDialog *dialog = new FavoriteSelectionDialog();
331 this->connect(dialog, SIGNAL(selectedLocation(Location*)), SLOT(from_selection_selected(Location*)));
332 this->connect(dialog, SIGNAL(customizeRequested()), SLOT(customize_requested()));
338 void SearchDisplay::on_dest_favorites_clicked()
341 qDebug() << "DEST Favorites button clicked";
342 FavoriteSelectionDialog *dialog = new FavoriteSelectionDialog();
343 this->connect(dialog, SIGNAL(selectedLocation(Location*)), SLOT(dest_selection_selected(Location*)));
344 this->connect(dialog, SIGNAL(customizeRequested()), SLOT(customize_requested()));
350 void SearchDisplay::from_selection_selected(Location *location)
352 if (location == NULL)
354 qDebug() << "NULL location received from FavoriteSelectionDialog.";
357 this->from_selected = location;
358 this->ui->from_edit->setText(this->from_selected->address());
361 void SearchDisplay::dest_selection_selected(Location *location)
363 if (location == NULL)
365 qDebug() << "NULL location received from FavoriteSelectionDialog.";
368 this->dest_selected = location;
369 this->ui->dest_edit->setText(this->dest_selected->address());
373 void SearchDisplay::customize_requested()
375 qDebug() << "Customizing favorites requested.";
378 if (!this->edit_window)
379 this->edit_window = new LocationsDisplayWidget(this);
380 this->edit_window->showWidget();
382 if (!this->edit_window)
384 this->edit_window = new LocationsDisplayWidget();
385 this->tabs->addTab(this->edit_window, "Modify locations");
387 this->tabs->setCurrentWidget(this->edit_window);