Updated packaging files in debian folder.
[ptas] / zouba / src / gui / locationmodifydialog.cpp
1 #include "locationmodifydialog.h"
2 #include "ui_locationmodifydialog.h"
3
4 #include "../logic/locations.h"
5
6 #include <QDebug>
7 #include <QApplication>
8 #include <QDesktopWidget>
9
10 Location* foundFromList(const QString address, const QList<Location*>& list);
11
12 LocationModifyDialog::LocationModifyDialog(QWidget *parent, Location *existing) :
13     QDialog(parent),
14     ui(new Ui::LocationModifyDialog),
15     finder(NULL),
16     edit(false)
17 {
18     ui->setupUi(this);
19     this->ui->result_widget->hide();
20
21     this->setAttribute(Qt::WA_DeleteOnClose);
22
23 #ifdef Q_WS_MAEMO_5
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());
28 #endif
29
30     if (existing)
31     {
32         this->ui->label_edit->setText(existing->label());
33         this->ui->label_edit->setEnabled(false);
34         this->ui->address_edit->setText(existing->address());
35         this->edit = true;
36     }
37
38 }
39
40 LocationModifyDialog::~LocationModifyDialog()
41 {
42     qDebug() << "Modify dialog Destructor called.";
43     delete ui;
44     if (this->finder)
45         this->finder->deleteLater();
46     qDebug() << "Destructor ended.";
47 }
48
49 void LocationModifyDialog::on_search_button_clicked()
50 {
51     if (this->ui->label_edit->text().isEmpty())
52     {
53         this->ui->result_widget->setCurrentWidget(this->ui->labelEmpty);
54         this->ui->result_widget->show();
55         return;
56     }
57     if (!this->edit && Locations::GetInstance()->getLocation(this->ui->label_edit->text().trimmed()))
58     {
59         this->ui->result_widget->setCurrentWidget(this->ui->labelReserved);
60         this->ui->result_widget->show();
61         return;
62     }
63 #ifdef Q_WS_MAEMO_5
64     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
65 #endif
66     this->ui->result_widget->setCurrentWidget(this->ui->searching);
67     this->ui->result_widget->show();
68     if (this->finder)
69     {
70         this->finder->disconnect(this);
71         this->finder->deleteLater();
72         this->finder = NULL;
73     }
74     this->finder = new LocationFinder(this->ui->address_edit->text().trimmed());
75     this->connect(this->finder, SIGNAL(finished()), SLOT(location_search_finished()));
76 }
77
78 void LocationModifyDialog::location_search_finished()
79 {
80     bool delete_finder = false;
81     if (!this->finder->responseWasValid())
82     {
83         this->ui->result_widget->setCurrentWidget(this->ui->invalid_response);
84         delete_finder = true;
85
86     }
87     else if (this->finder->numberOfLocationsFound() == 0)
88     {
89         this->ui->result_widget->setCurrentWidget(this->ui->noResults);
90         delete_finder = true;
91     }
92     else
93     {
94         this->ui->address_list->clear();
95         if (this->finder->getPlaces().size() > 0)
96         {
97             QListWidgetItem *item = new QListWidgetItem("Places", this->ui->address_list);
98             item->setFlags(item->flags() & (~Qt::ItemIsEnabled));
99             this->populateList(this->finder->getPlaces());
100         }
101         if (this->finder->getRoadNames().size() > 0)
102         {
103             QListWidgetItem *item = new QListWidgetItem("Street names", this->ui->address_list);
104             item->setFlags(item->flags() & (~Qt::ItemIsEnabled));
105             this->populateList(this->finder->getRoadNames());
106         }
107         if (this->finder->getStops().size() > 0)
108         {
109             QListWidgetItem *item = new QListWidgetItem("Stops", this->ui->address_list);
110             item->setFlags(item->flags() & (~Qt::ItemIsEnabled));
111             this->populateList(this->finder->getStops());
112         }
113         this->ui->result_widget->setCurrentWidget(this->ui->results);
114     }
115
116     if (delete_finder)
117     {
118         this->finder->disconnect(this);
119         this->finder->deleteLater();
120         this->finder = NULL;
121     }
122 #ifdef Q_WS_MAEMO_5
123     this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
124 #endif
125 }
126
127 void LocationModifyDialog::populateList(const QList<Location *> &list)
128 {
129     QList<Location*>::const_iterator it, ite;
130         for (it = list.constBegin(), ite = list.constEnd(); it != ite; ++it)
131         {
132             new QListWidgetItem((*it)->address(), this->ui->address_list);
133         }
134 }
135
136 void LocationModifyDialog::on_address_list_itemClicked(QListWidgetItem* item)
137 {
138     QString address(item->text());
139     Location *location = NULL;
140     location = foundFromList(address, this->finder->getPlaces());
141     if (!location)
142         location = foundFromList(address, this->finder->getRoadNames());
143     if (!location)
144         location = foundFromList(address, this->finder->getStops());
145     if (location)
146     {
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);
150         this->close();
151     }
152 }
153
154 Location* foundFromList(const QString address, const QList<Location*>& list)
155 {
156     Location* ret = NULL;
157     QList<Location*>::const_iterator it, ite;
158     for (it = list.constBegin(), ite = list.constEnd(); it != ite && !ret; ++it)
159     {
160         if (address == (*it)->address())
161         {
162             ret = *it;
163         }
164     }
165     return ret;
166 }
167
168 void LocationModifyDialog::orientationChanged()
169 {
170     this->setFixedHeight(QApplication::desktop()->screenGeometry().height());
171 }