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 this->from_selected = NULL;
41 this->dest_selected = NULL;
42 this->edit_window = NULL;
45 QWidget *widget = new QWidget();
46 QVBoxLayout *layout = new QVBoxLayout();
47 widget->setLayout(layout);
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");
54 this->connect(this->tabs, SIGNAL(tabCloseRequested(int)), SLOT(tabclosed(int)));
56 layout->addWidget(this->tabs);
57 this->setCentralWidget(widget);
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();
64 this->updateLocationLists();
66 this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
67 this->ui->dest_combo->setCurrentIndex(1);
71 qDebug() << "Finish constructor";
74 SearchDisplay::~SearchDisplay()
79 void SearchDisplay::updateLocationLists()
81 qDebug() << "Start updateLocationLists";
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);
90 /// TODO: Add GPS location if GPS is selected.
96 Locations *locations = Locations::GetInstance();
97 for (int index = 1; index <= locations->size(); ++index)
99 Location* loc = locations->getLocation(index);
100 if (loc && loc->isValid())
102 qDebug() << "Adding location: " << loc->label();
103 locs << loc->label();
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);
112 this->ui->from_combo->blockSignals(false);
113 this->ui->dest_combo->blockSignals(false);
115 qDebug() << "Finish updateLocationLists";
118 void SearchDisplay::locations_changed()
120 qDebug() << "Start locations_changed";
123 QString from_old = this->ui->from_combo->currentText();
124 QString dest_old = this->ui->dest_combo->currentText();
126 this->updateLocationLists();
128 qDebug() << "Reselecting old items.";
129 int from_id = this->ui->from_combo->findText(from_old);
131 this->ui->from_combo->setCurrentIndex(from_id);
133 this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
135 int dest_id = this->ui->dest_combo->findText(dest_old);
137 this->ui->dest_combo->setCurrentIndex(dest_id);
139 this->on_dest_combo_currentIndexChanged(this->ui->dest_combo->currentText());
141 qDebug() << "Finish locations_changed";
144 void SearchDisplay::on_searchButton_clicked()
146 qDebug() << "Start on_search_button_clicked";
147 if (this->route_finder != NULL)
150 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
152 this->route_finder->disconnect(this, SLOT(route_finder_finished()));
153 delete this->route_finder;
155 // Check for empty search fields.
157 if (this->ui->from_edit->text() == empty)
159 qDebug() << "From field is empty. No search is made.";
162 if (this->ui->dest_edit->text() == empty)
164 qDebug() << "Dest field is empty. No search is made.";
168 Location *from, *dest;
170 from = this->from_selected;
171 dest = this->dest_selected;
173 Locations* locations = Locations::GetInstance();
174 from = locations->getLocation(this->ui->from_combo->currentText());
175 dest = locations->getLocation(this->ui->dest_combo->currentText());
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);
181 qDebug() << "Written text differs from the text in the combo box.";
182 from = new Location("Temp");
183 from->setAddress(this->ui->from_edit->text());
186 if (dest != NULL && this->ui->dest_edit->text() == dest->address()) {
187 qDebug() << "Written text matches the text in the combo box";
188 dest = new Location(*dest);
190 qDebug() << "Written text differs from the text in the combo box.";
191 dest = new Location("Temp");
192 dest->setAddress(this->ui->dest_edit->text());
195 qDebug() << "Starting route search";
197 this->route_finder = new RouteFinder(*from, *dest);
200 this->connect(this->route_finder, SIGNAL(finished()), SLOT(route_finder_finished()));
202 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
204 qDebug() << "Finished on_search_button_clicked.";
207 void SearchDisplay::route_finder_finished()
209 qDebug() << "Received signal from successful route finder";
211 RouteResultWidget *results = new RouteResultWidget(this);
213 RouteResultWidget *results = new RouteResultWidget();
216 for (int i = 0; i < this->route_finder->getNumberOfRoutes(); i++)
217 results->addRoute(this->route_finder->getRoute(i));
220 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
223 int cur = this->tabs->addTab(results, "Route" + QString::number(this->tabs->count()));
224 this->tabs->setCurrentIndex(cur);
226 qDebug() << "Finish route_finder_finished";
229 void SearchDisplay::setEditText(QLineEdit* edit, QString& text)
231 qDebug() << "Start setEditText";
232 Locations* locations = Locations::GetInstance();
233 Location* loc = locations->getLocation(text);
235 edit->setText(loc->address());
236 qDebug() << "Finish setEditText";
240 void SearchDisplay::on_from_combo_currentIndexChanged(QString text)
243 qDebug() << "New FROM location selected.";
244 setEditText(this->ui->from_edit, text);
245 qDebug() << "Finish on_from_combo_currentIndexChanged";
250 void SearchDisplay::on_dest_combo_currentIndexChanged(QString text)
253 qDebug() << "New DEST location selected.";
254 setEditText(this->ui->dest_edit, text);
255 qDebug() << "Finish on_dest_combo_currentIndexChanged";
261 void SearchDisplay::tabclosed(int index)
263 qDebug() << "Tab close requested. Nr" << index;
266 qDebug() << "First tab requested to be closed.";
269 this->tabs->removeTab(index);
271 #endif //Q_WS_MAEMO_5
273 void SearchDisplay::on_from_favorites_clicked()
276 qDebug() << "FROM Favorites button clicked";
277 FavoriteSelectionDialog *dialog = new FavoriteSelectionDialog();
278 this->connect(dialog, SIGNAL(selectedLocation(Location*)), SLOT(from_selection_selected(Location*)));
279 this->connect(dialog, SIGNAL(customizeRequested()), SLOT(customize_requested()));
285 void SearchDisplay::on_dest_favorites_clicked()
288 qDebug() << "DEST Favorites button clicked";
289 FavoriteSelectionDialog *dialog = new FavoriteSelectionDialog();
290 this->connect(dialog, SIGNAL(selectedLocation(Location*)), SLOT(dest_selection_selected(Location*)));
291 this->connect(dialog, SIGNAL(customizeRequested()), SLOT(customize_requested()));
297 void SearchDisplay::from_selection_selected(Location *location)
299 if (location == NULL)
301 qDebug() << "NULL location received from FavoriteSelectionDialog.";
304 this->from_selected = location;
305 this->ui->from_edit->setText(this->from_selected->address());
308 void SearchDisplay::dest_selection_selected(Location *location)
310 if (location == NULL)
312 qDebug() << "NULL location received from FavoriteSelectionDialog.";
315 this->dest_selected = location;
316 this->ui->dest_edit->setText(this->dest_selected->address());
319 void SearchDisplay::customize_requested()
321 qDebug() << "Customizing favorites requested.";
323 if (!this->edit_window)
324 this->edit_window = new LocationsDisplayWindow(this);
325 this->edit_window->show();