Updated packaging files in debian folder.
[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 "locationsdisplaywidget.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     one_search(),
18     search_restarted(false),
19     edit_window(NULL)
20 {
21     qDebug() << "Start constructor";
22     ui->setupUi(this);
23     this->setWindowTitle(QCoreApplication::applicationName());
24     qDebug() << "Application name:" << QCoreApplication::applicationName();
25
26     Locations *locations = Locations::GetInstance();
27     this->connect(locations, SIGNAL(locationsChanged()), SLOT(locations_changed()));
28
29     QMenuBar *menu = this->ui->menubar;
30
31 #ifdef Q_WS_MAEMO_5
32     this->setAttribute(Qt::WA_Maemo5StackedWindow);
33     this->setAttribute(Qt::WA_Maemo5AutoOrientation);
34
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");
39
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");
44
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);
51
52     this->from_selected = NULL;
53     this->dest_selected = NULL;
54
55 #else
56     QWidget *widget = new QWidget();
57     QVBoxLayout *layout = new QVBoxLayout();
58     widget->setLayout(layout);
59
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");
64
65     this->connect(this->tabs, SIGNAL(tabCloseRequested(int)), SLOT(tabclosed(int)));
66
67     layout->addWidget(this->tabs);
68     this->setCentralWidget(widget);
69
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();
74
75     this->updateLocationLists();
76
77     this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
78     this->ui->dest_combo->setCurrentIndex(1);
79
80     QAction *modify_locations = new QAction("Modify locations", this);
81     this->connect(modify_locations, SIGNAL(triggered()), SLOT(customize_requested()));
82     menu->addAction(modify_locations);
83
84 #endif
85
86     qDebug() << "Finish constructor";
87 }
88
89 void SearchDisplay::gps_status_change_requested(bool status)
90 {
91
92
93 }
94
95 SearchDisplay::~SearchDisplay()
96 {
97     delete ui;
98 }
99
100 void SearchDisplay::updateLocationLists()
101 {
102     qDebug() << "Start updateLocationLists";
103 #ifndef Q_WS_MAEMO_5
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);
110
111     /// TODO: Add GPS location if GPS is selected.
112
113
114
115
116     QStringList locs;
117     Locations *locations = Locations::GetInstance();
118     for (int index = 1; index <= locations->size(); ++index)
119     {
120         Location* loc = locations->getLocation(index);
121         if (loc && loc->isValid())
122         {
123             qDebug() << "Adding location: " << loc->label();
124             locs << loc->label();
125
126         }
127     }
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);
132
133     this->ui->from_combo->blockSignals(false);
134     this->ui->dest_combo->blockSignals(false);
135 #endif
136     qDebug() << "Finish updateLocationLists";
137 }
138
139 void SearchDisplay::locations_changed()
140 {
141 #ifndef Q_WS_MAEMO_5
142     qDebug() << "Start locations_changed";
143
144     QString from_old = this->ui->from_combo->currentText();
145     QString dest_old = this->ui->dest_combo->currentText();
146
147     this->updateLocationLists();
148
149     qDebug() << "Reselecting old items.";
150     int from_id = this->ui->from_combo->findText(from_old);
151     if (from_id >= 0)
152         this->ui->from_combo->setCurrentIndex(from_id);
153     else
154         this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
155
156     int dest_id = this->ui->dest_combo->findText(dest_old);
157     if (dest_id >= 0)
158         this->ui->dest_combo->setCurrentIndex(dest_id);
159     else
160         this->on_dest_combo_currentIndexChanged(this->ui->dest_combo->currentText());
161
162     qDebug() << "Finish locations_changed";
163 #endif
164 }
165
166 void SearchDisplay::on_searchButton_clicked()
167 {
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)
172     {
173 #ifdef Q_WS_MAEMO_5
174         this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
175 #endif
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.";
181     }
182
183     // Check for empty search fields.
184     QString empty;
185     if (this->ui->from_edit->text() == empty)
186     {
187         qDebug() << "From field is empty. No search is made.";
188         this->one_search.unlock();
189         this->search_restarted = false;
190         return;
191     }
192     if (this->ui->dest_edit->text() == empty)
193     {
194         qDebug() << "Dest field is empty. No search is made.";
195         this->one_search.unlock();
196         this->search_restarted = false;
197         return;
198     }
199
200     Location *from, *dest;
201 #ifdef Q_WS_MAEMO_5
202     from = this->from_selected;
203     dest = this->dest_selected;
204 #else
205     Locations* locations = Locations::GetInstance();
206     from = locations->getLocation(this->ui->from_combo->currentText());
207     dest = locations->getLocation(this->ui->dest_combo->currentText());
208 #endif
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);
212     } else {
213         qDebug() << "Written text differs from the text in the combo box.";
214         from = new Location("Temp", this->ui->from_edit->text());
215     }
216
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);
220     } else {
221         qDebug() << "Written text differs from the text in the combo box.";
222         dest = new Location("Temp", this->ui->dest_edit->text());
223     }
224
225     qDebug() << "Starting route search";
226
227     this->route_finder = new RouteFinder(*from, *dest);
228     delete from;
229     delete dest;
230     this->connect(this->route_finder, SIGNAL(finished()), SLOT(route_finder_finished()));
231 #ifdef Q_WS_MAEMO_5
232     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
233 #endif
234     this->one_search.unlock();
235     qDebug() << "Finished on_search_button_clicked.";
236 }
237
238 void SearchDisplay::route_finder_finished()
239 {
240     qDebug() << "Received signal from successful route finder";
241     if (!this->one_search.tryLock())
242         return;
243     if (this->search_restarted)
244     {
245         qDebug() << "Restart had been requested.";
246         this->search_restarted = false;
247         this->one_search.unlock();
248         return;
249     }
250     if(!this->route_finder)
251     {
252         qDebug() << "Route finder is NULL.";
253         this->one_search.unlock();
254         return;
255     }
256
257 #ifdef Q_WS_MAEMO_5
258     RouteResultWidget *results = new RouteResultWidget(this);
259     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
260     results->show();
261 #else
262     RouteResultWidget *results = new RouteResultWidget();
263     int cur = this->tabs->addTab(results, "Route" + QString::number(this->tabs->count()));
264     this->tabs->setCurrentIndex(cur);
265 #endif
266
267     for (int i = 0; i < this->route_finder->getNumberOfRoutes(); i++)
268         results->addRoute(this->route_finder->getRoute(i));
269
270     this->route_finder->disconnect(this);
271     this->route_finder->deleteLater();
272     this->route_finder = NULL;
273
274     this->one_search.unlock();
275     qDebug() << "Finish route_finder_finished";
276 }
277
278 void SearchDisplay::setEditText(QLineEdit* edit, QString& text)
279 {
280     qDebug() << "Start setEditText";
281     Locations* locations = Locations::GetInstance();
282     Location* loc = locations->getLocation(text);
283     if (loc)
284         edit->setText(loc->address());
285     qDebug() << "Finish setEditText";
286 }
287
288
289 void SearchDisplay::on_from_combo_currentIndexChanged(QString text)
290 {
291 #ifndef Q_WS_MAEMO_5
292     qDebug() << "New FROM location selected.";
293     setEditText(this->ui->from_edit, text);
294     qDebug() << "Finish on_from_combo_currentIndexChanged";
295 #endif
296 }
297
298
299 void SearchDisplay::on_dest_combo_currentIndexChanged(QString text)
300 {
301 #ifndef Q_WS_MAEMO_5
302     qDebug() << "New DEST location selected.";
303     setEditText(this->ui->dest_edit, text);
304     qDebug() << "Finish on_dest_combo_currentIndexChanged";
305 #endif
306 }
307
308
309 #ifndef Q_WS_MAEMO_5
310 void SearchDisplay::tabclosed(int index)
311 {
312     qDebug() << "Tab close requested. Nr" << index;
313     if (index == 0)
314     {
315         qDebug() << "First tab requested to be closed.";
316         return;
317     }
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();
323 }
324 #endif //Q_WS_MAEMO_5
325
326 void SearchDisplay::on_from_favorites_clicked()
327 {
328 #ifdef Q_WS_MAEMO_5
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()));
333     dialog->show();
334 #endif
335 }
336
337
338 void SearchDisplay::on_dest_favorites_clicked()
339 {
340 #ifdef Q_WS_MAEMO_5
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()));
345     dialog->show();
346 #endif
347 }
348
349 #ifdef Q_WS_MAEMO_5
350 void SearchDisplay::from_selection_selected(Location *location)
351 {
352     if (location == NULL)
353     {
354         qDebug() << "NULL location received from FavoriteSelectionDialog.";
355         return;
356     }
357     this->from_selected = location;
358     this->ui->from_edit->setText(this->from_selected->address());
359 }
360
361 void SearchDisplay::dest_selection_selected(Location *location)
362 {
363     if (location == NULL)
364     {
365         qDebug() << "NULL location received from FavoriteSelectionDialog.";
366         return;
367     }
368     this->dest_selected = location;
369     this->ui->dest_edit->setText(this->dest_selected->address());
370 }
371 #endif
372
373 void SearchDisplay::customize_requested()
374 {
375     qDebug() << "Customizing favorites requested.";
376
377 #ifdef Q_WS_MAEMO_5
378     if (!this->edit_window)
379         this->edit_window = new LocationsDisplayWidget(this);
380     this->edit_window->showWidget();
381 #else
382     if (!this->edit_window)
383     {
384         this->edit_window = new LocationsDisplayWidget();
385         this->tabs->addTab(this->edit_window, "Modify locations");
386     }
387     this->tabs->setCurrentWidget(this->edit_window);
388 #endif
389 }