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", this->ui->from_edit->text());
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);
189 qDebug() << "Written text differs from the text in the combo box.";
190 dest = new Location("Temp", this->ui->dest_edit->text());
193 qDebug() << "Starting route search";
195 this->route_finder = new RouteFinder(*from, *dest);
198 this->connect(this->route_finder, SIGNAL(finished()), SLOT(route_finder_finished()));
200 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
202 qDebug() << "Finished on_search_button_clicked.";
205 void SearchDisplay::route_finder_finished()
207 qDebug() << "Received signal from successful route finder";
209 RouteResultWidget *results = new RouteResultWidget(this);
211 RouteResultWidget *results = new RouteResultWidget();
214 for (int i = 0; i < this->route_finder->getNumberOfRoutes(); i++)
215 results->addRoute(this->route_finder->getRoute(i));
218 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
221 int cur = this->tabs->addTab(results, "Route" + QString::number(this->tabs->count()));
222 this->tabs->setCurrentIndex(cur);
224 qDebug() << "Finish route_finder_finished";
227 void SearchDisplay::setEditText(QLineEdit* edit, QString& text)
229 qDebug() << "Start setEditText";
230 Locations* locations = Locations::GetInstance();
231 Location* loc = locations->getLocation(text);
233 edit->setText(loc->address());
234 qDebug() << "Finish setEditText";
238 void SearchDisplay::on_from_combo_currentIndexChanged(QString text)
241 qDebug() << "New FROM location selected.";
242 setEditText(this->ui->from_edit, text);
243 qDebug() << "Finish on_from_combo_currentIndexChanged";
248 void SearchDisplay::on_dest_combo_currentIndexChanged(QString text)
251 qDebug() << "New DEST location selected.";
252 setEditText(this->ui->dest_edit, text);
253 qDebug() << "Finish on_dest_combo_currentIndexChanged";
259 void SearchDisplay::tabclosed(int index)
261 qDebug() << "Tab close requested. Nr" << index;
264 qDebug() << "First tab requested to be closed.";
267 this->tabs->removeTab(index);
269 #endif //Q_WS_MAEMO_5
271 void SearchDisplay::on_from_favorites_clicked()
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()));
283 void SearchDisplay::on_dest_favorites_clicked()
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()));
295 void SearchDisplay::from_selection_selected(Location *location)
297 if (location == NULL)
299 qDebug() << "NULL location received from FavoriteSelectionDialog.";
302 this->from_selected = location;
303 this->ui->from_edit->setText(this->from_selected->address());
306 void SearchDisplay::dest_selection_selected(Location *location)
308 if (location == NULL)
310 qDebug() << "NULL location received from FavoriteSelectionDialog.";
313 this->dest_selected = location;
314 this->ui->dest_edit->setText(this->dest_selected->address());
317 void SearchDisplay::customize_requested()
319 qDebug() << "Customizing favorites requested.";
321 if (!this->edit_window)
322 this->edit_window = new LocationsDisplayWindow(this);
323 this->edit_window->show();