1 #include "locationmodifydialog.h"
2 #include "ui_locationmodifydialog.h"
4 #include "../logic/locations.h"
7 #include <QApplication>
8 #include <QDesktopWidget>
10 Location* foundFromList(const QString address, const QList<Location*>& list);
12 LocationModifyDialog::LocationModifyDialog(QWidget *parent, Location *existing) :
14 ui(new Ui::LocationModifyDialog),
19 this->ui->result_widget->hide();
21 this->setAttribute(Qt::WA_DeleteOnClose);
24 this->setAttribute(Qt::WA_Maemo5AutoOrientation);
25 this->setWindowTitle(QCoreApplication::applicationName());
26 this->connect(QApplication::desktop(), SIGNAL(resized(int)), SLOT(orientationChanged()));
27 this->setFixedHeight(QApplication::desktop()->screenGeometry().height());
32 this->ui->label_edit->setText(existing->label());
33 this->ui->label_edit->setEnabled(false);
34 this->ui->address_edit->setText(existing->address());
40 LocationModifyDialog::~LocationModifyDialog()
42 qDebug() << "Modify dialog Destructor called.";
45 this->finder->deleteLater();
46 qDebug() << "Destructor ended.";
49 void LocationModifyDialog::on_search_button_clicked()
51 if (this->ui->label_edit->text().isEmpty())
53 this->ui->result_widget->setCurrentWidget(this->ui->labelEmpty);
54 this->ui->result_widget->show();
57 if (!this->edit && Locations::GetInstance()->getLocation(this->ui->label_edit->text().trimmed()))
59 this->ui->result_widget->setCurrentWidget(this->ui->labelReserved);
60 this->ui->result_widget->show();
64 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
66 this->ui->result_widget->setCurrentWidget(this->ui->searching);
67 this->ui->result_widget->show();
70 this->finder->disconnect(this);
71 this->finder->deleteLater();
74 this->finder = new LocationFinder(this->ui->address_edit->text().trimmed());
75 this->connect(this->finder, SIGNAL(finished()), SLOT(location_search_finished()));
78 void LocationModifyDialog::location_search_finished()
80 bool delete_finder = false;
81 if (!this->finder->responseWasValid())
83 this->ui->result_widget->setCurrentWidget(this->ui->invalid_response);
87 else if (this->finder->numberOfLocationsFound() == 0)
89 this->ui->result_widget->setCurrentWidget(this->ui->noResults);
94 this->ui->address_list->clear();
95 if (this->finder->getPlaces().size() > 0)
97 QListWidgetItem *item = new QListWidgetItem("Places", this->ui->address_list);
98 item->setFlags(item->flags() & (~Qt::ItemIsEnabled));
99 this->populateList(this->finder->getPlaces());
101 if (this->finder->getRoadNames().size() > 0)
103 QListWidgetItem *item = new QListWidgetItem("Street names", this->ui->address_list);
104 item->setFlags(item->flags() & (~Qt::ItemIsEnabled));
105 this->populateList(this->finder->getRoadNames());
107 if (this->finder->getStops().size() > 0)
109 QListWidgetItem *item = new QListWidgetItem("Stops", this->ui->address_list);
110 item->setFlags(item->flags() & (~Qt::ItemIsEnabled));
111 this->populateList(this->finder->getStops());
113 this->ui->result_widget->setCurrentWidget(this->ui->results);
118 this->finder->disconnect(this);
119 this->finder->deleteLater();
123 this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
127 void LocationModifyDialog::populateList(const QList<Location *> &list)
129 QList<Location*>::const_iterator it, ite;
130 for (it = list.constBegin(), ite = list.constEnd(); it != ite; ++it)
132 new QListWidgetItem((*it)->address(), this->ui->address_list);
136 void LocationModifyDialog::on_address_list_itemClicked(QListWidgetItem* item)
138 QString address(item->text());
139 Location *location = NULL;
140 location = foundFromList(address, this->finder->getPlaces());
142 location = foundFromList(address, this->finder->getRoadNames());
144 location = foundFromList(address, this->finder->getStops());
147 qDebug() << "Found location pointer: " << location;
148 Location *added = new Location(location->x(), location->y(), this->ui->label_edit->text().trimmed(), location->address());
149 Locations::GetInstance()->addEditLocation(added);
154 Location* foundFromList(const QString address, const QList<Location*>& list)
156 Location* ret = NULL;
157 QList<Location*>::const_iterator it, ite;
158 for (it = list.constBegin(), ite = list.constEnd(); it != ite && !ret; ++it)
160 if (address == (*it)->address())
168 void LocationModifyDialog::orientationChanged()
170 this->setFixedHeight(QApplication::desktop()->screenGeometry().height());