Created new ui for the program. Almost everything that worked previously
[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 "locationsdisplaywindow.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 {
18     qDebug() << "Start constructor";
19     ui->setupUi(this);
20     this->setWindowTitle(QCoreApplication::applicationName());
21     qDebug() << "Application name:" << QCoreApplication::applicationName();
22
23     Locations *locations = Locations::GetInstance();
24     this->connect(locations, SIGNAL(locationsChanged()), SLOT(locations_changed()));
25
26 #ifdef Q_WS_MAEMO_5
27     this->setAttribute(Qt::WA_Maemo5StackedWindow);
28     this->setAttribute(Qt::WA_Maemo5AutoOrientation);
29
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");
34
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");
39
40     this->from_selected = NULL;
41     this->dest_selected = NULL;
42     this->edit_window = NULL;
43
44 #else
45     QWidget *widget = new QWidget();
46     QVBoxLayout *layout = new QVBoxLayout();
47     widget->setLayout(layout);
48
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");
53
54     this->connect(this->tabs, SIGNAL(tabCloseRequested(int)), SLOT(tabclosed(int)));
55
56     layout->addWidget(this->tabs);
57     this->setCentralWidget(widget);
58
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();
63
64     this->updateLocationLists();
65
66     this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
67     this->ui->dest_combo->setCurrentIndex(1);
68
69 #endif
70
71     qDebug() << "Finish constructor";
72 }
73
74 SearchDisplay::~SearchDisplay()
75 {
76     delete ui;
77 }
78
79 void SearchDisplay::updateLocationLists()
80 {
81     qDebug() << "Start updateLocationLists";
82 #ifndef Q_WS_MAEMO_5
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);
89
90     /// TODO: Add GPS location if GPS is selected.
91
92
93
94
95     QStringList locs;
96     Locations *locations = Locations::GetInstance();
97     for (int index = 1; index <= locations->size(); ++index)
98     {
99         Location* loc = locations->getLocation(index);
100         if (loc && loc->isValid())
101         {
102             qDebug() << "Adding location: " << loc->label();
103             locs << loc->label();
104
105         }
106     }
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);
111
112     this->ui->from_combo->blockSignals(false);
113     this->ui->dest_combo->blockSignals(false);
114 #endif
115     qDebug() << "Finish updateLocationLists";
116 }
117
118 void SearchDisplay::locations_changed()
119 {
120     qDebug() << "Start locations_changed";
121
122 #ifndef Q_WS_MAEMO_5
123     QString from_old = this->ui->from_combo->currentText();
124     QString dest_old = this->ui->dest_combo->currentText();
125
126     this->updateLocationLists();
127
128     qDebug() << "Reselecting old items.";
129     int from_id = this->ui->from_combo->findText(from_old);
130     if (from_id >= 0)
131         this->ui->from_combo->setCurrentIndex(from_id);
132     else
133         this->on_from_combo_currentIndexChanged(this->ui->from_combo->currentText());
134
135     int dest_id = this->ui->dest_combo->findText(dest_old);
136     if (dest_id >= 0)
137         this->ui->dest_combo->setCurrentIndex(dest_id);
138     else
139         this->on_dest_combo_currentIndexChanged(this->ui->dest_combo->currentText());
140 #endif
141     qDebug() << "Finish locations_changed";
142 }
143
144 void SearchDisplay::on_searchButton_clicked()
145 {
146     qDebug() << "Start on_search_button_clicked";
147     if (this->route_finder != NULL)
148     {
149 #ifdef Q_WS_MAEMO_5
150         this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
151 #endif
152         this->route_finder->disconnect(this, SLOT(route_finder_finished()));
153         delete this->route_finder;
154     }
155     // Check for empty search fields.
156     QString empty;
157     if (this->ui->from_edit->text() == empty)
158     {
159         qDebug() << "From field is empty. No search is made.";
160         return;
161     }
162     if (this->ui->dest_edit->text() == empty)
163     {
164         qDebug() << "Dest field is empty. No search is made.";
165         return;
166     }
167
168     Location *from, *dest;
169 #ifdef Q_WS_MAEMO_5
170     from = this->from_selected;
171     dest = this->dest_selected;
172 #else
173     Locations* locations = Locations::GetInstance();
174     from = locations->getLocation(this->ui->from_combo->currentText());
175     dest = locations->getLocation(this->ui->dest_combo->currentText());
176 #endif
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);
180     } else {
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());
184     }
185
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);
189     } else {
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());
193     }
194
195     qDebug() << "Starting route search";
196
197     this->route_finder = new RouteFinder(*from, *dest);
198     delete from;
199     delete dest;
200     this->connect(this->route_finder, SIGNAL(finished()), SLOT(route_finder_finished()));
201 #ifdef Q_WS_MAEMO_5
202     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
203 #endif
204     qDebug() << "Finished on_search_button_clicked.";
205 }
206
207 void SearchDisplay::route_finder_finished()
208 {
209     qDebug() << "Received signal from successful route finder";
210 #ifdef Q_WS_MAEMO_5
211     RouteResultWidget *results = new RouteResultWidget(this);
212 #else
213     RouteResultWidget *results = new RouteResultWidget();
214 #endif
215
216     for (int i = 0; i < this->route_finder->getNumberOfRoutes(); i++)
217         results->addRoute(this->route_finder->getRoute(i));
218
219 #ifdef Q_WS_MAEMO_5
220     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
221     results->show();
222 #else
223     int cur = this->tabs->addTab(results, "Route" + QString::number(this->tabs->count()));
224     this->tabs->setCurrentIndex(cur);
225 #endif
226     qDebug() << "Finish route_finder_finished";
227 }
228
229 void SearchDisplay::setEditText(QLineEdit* edit, QString& text)
230 {
231     qDebug() << "Start setEditText";
232     Locations* locations = Locations::GetInstance();
233     Location* loc = locations->getLocation(text);
234     if (loc)
235         edit->setText(loc->address());
236     qDebug() << "Finish setEditText";
237 }
238
239
240 void SearchDisplay::on_from_combo_currentIndexChanged(QString text)
241 {
242 #ifndef Q_WS_MAEMO_5
243     qDebug() << "New FROM location selected.";
244     setEditText(this->ui->from_edit, text);
245     qDebug() << "Finish on_from_combo_currentIndexChanged";
246 #endif
247 }
248
249
250 void SearchDisplay::on_dest_combo_currentIndexChanged(QString text)
251 {
252 #ifndef Q_WS_MAEMO_5
253     qDebug() << "New DEST location selected.";
254     setEditText(this->ui->dest_edit, text);
255     qDebug() << "Finish on_dest_combo_currentIndexChanged";
256 #endif
257 }
258
259
260 #ifndef Q_WS_MAEMO_5
261 void SearchDisplay::tabclosed(int index)
262 {
263     qDebug() << "Tab close requested. Nr" << index;
264     if (index == 0)
265     {
266         qDebug() << "First tab requested to be closed.";
267         return;
268     }
269     this->tabs->removeTab(index);
270 }
271 #endif //Q_WS_MAEMO_5
272
273 void SearchDisplay::on_from_favorites_clicked()
274 {
275 #ifdef Q_WS_MAEMO_5
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()));
280     dialog->show();
281 #endif
282 }
283
284
285 void SearchDisplay::on_dest_favorites_clicked()
286 {
287 #ifdef Q_WS_MAEMO_5
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()));
292     dialog->show();
293 #endif
294 }
295
296 #ifdef Q_WS_MAEMO_5
297 void SearchDisplay::from_selection_selected(Location *location)
298 {
299     if (location == NULL)
300     {
301         qDebug() << "NULL location received from FavoriteSelectionDialog.";
302         return;
303     }
304     this->from_selected = location;
305     this->ui->from_edit->setText(this->from_selected->address());
306 }
307
308 void SearchDisplay::dest_selection_selected(Location *location)
309 {
310     if (location == NULL)
311     {
312         qDebug() << "NULL location received from FavoriteSelectionDialog.";
313         return;
314     }
315     this->dest_selected = location;
316     this->ui->dest_edit->setText(this->dest_selected->address());
317 }
318
319 void SearchDisplay::customize_requested()
320 {
321     qDebug() << "Customizing favorites requested.";
322
323     if (!this->edit_window)
324         this->edit_window = new LocationsDisplayWindow(this);
325     this->edit_window->show();
326 }
327
328 #endif