Ui support added for GpsLocation. Menu with button to toggle gps on or
[ptas] / zouba / src / gui / locationsdisplaywindow.cpp
1 #include "locationsdisplaywindow.h"
2 #include "ui_locationsdisplaywindow.h"
3
4 #include "src/logic/locations.h"
5
6 #include "src/addressdialog.h"
7
8 #include <QDebug>
9 #include <QListWidgetItem>
10 #include <QListWidget>
11 #include <QList>
12
13 const QString invalidPostText = " - Invalid address";
14 const QString editText = "Edit list";
15
16 QString getLocName(const QListWidgetItem *item);
17 Location* getSelectedLocation(QList<QListWidgetItem*> list);
18 /*QString getLocName(const QListWidgetItem *item);
19 Location* getSelectedLocation(QList<QListWidgetItem*> list);*/
20
21 LocationsDisplayWindow::LocationsDisplayWindow(QWidget *parent) :
22     QMainWindow(parent),
23     ui(new Ui::LocationsDisplayWindow)
24 {
25     ui->setupUi(this);
26
27 #ifdef Q_WS_MAEMO_5
28     this->setAttribute(Qt::WA_Maemo5StackedWindow);
29 #endif
30
31     this->populateLocations();
32
33     Locations *locations = Locations::GetInstance();
34     connect(locations, SIGNAL(locationsChanged()), this, SLOT(populateLocations()));
35
36     this->ui->buttonsStacked->adjustSize();
37     this->ui->locationsWidget->adjustSize();
38 }
39
40 LocationsDisplayWindow::~LocationsDisplayWindow()
41 {
42     delete ui;
43 }
44
45 void LocationsDisplayWindow::populateLocations()
46 {
47     this->ui->locationsWidget->clear();
48     qDebug() << "Populating locations";
49     Locations *locations = Locations::GetInstance();
50
51     QStringList locs;
52     for (int index = 1; index <= locations->size(); ++index)
53     {
54         qDebug() << "Adding location: " << locations->getLocation(index)->label();
55         Location* loc = locations->getLocation(index);
56         QString dispName = loc->label();
57         if (!loc->isValid())
58             dispName.append(invalidPostText);
59         locs << dispName;
60     }
61     this->ui->locationsWidget->addItems(locs);
62     qDebug() << "Locations populated";
63 }
64
65
66 void LocationsDisplayWindow::editLocation(QListWidgetItem *item)
67 {
68     /// TODO
69     /*if (!item) return;
70
71     Locations *locations = Locations::GetInstance();
72     QString findText = getLocName(item);
73     Location *loc = locations->getLocation(findText);
74     if (!loc)
75         qDebug() << "No location with label " << findText << " was found from locations.";
76     else
77     {
78         AddressDialog *dialog = new AddressDialog(this, loc);
79         dialog->show();
80     }*/
81 }
82
83 QString LocationsDisplayWindow::getLocName(const QListWidgetItem *item)
84 {
85     if (!item) return NULL;
86     QString retText = item->text();
87     if (retText.contains(" - Invalid address", Qt::CaseSensitive))
88         retText.chop(18);
89     return retText;
90 }
91
92 Location* LocationsDisplayWindow::getSelectedLocation(QList<QListWidgetItem*> list)
93 {
94     if (list.size() == 0)
95     {
96         qDebug() << "No item is selected";
97         return NULL;
98     }
99     QListWidgetItem *item = list.at(0);
100     QString name = getLocName(item);
101     qDebug() << "Selected item is" << name;
102     Locations *locations = Locations::GetInstance();
103     return locations->getLocation(name);
104 }
105
106 void LocationsDisplayWindow::on_newLocButton_clicked()
107 {
108     /// TODO
109     /*AddressDialog *dialog = new AddressDialog(this);
110     dialog->show();*/
111 }
112
113 void LocationsDisplayWindow::on_upButton_clicked()
114 {
115     qDebug() << "Move up called";
116     Location* loc = this->getSelectedLocation(this->ui->locationsWidget->selectedItems());
117     if (!loc)
118         qDebug() << "No location with selected label was found from locations.";
119     else
120     {
121         Locations *locations = Locations::GetInstance();
122         locations->lowerLocationIndex(loc->label());
123     }
124 }
125
126 void LocationsDisplayWindow::on_downButton_clicked()
127 {
128     qDebug() << "Move down called";
129     Location* loc = this->getSelectedLocation(this->ui->locationsWidget->selectedItems());
130     if (!loc)
131         qDebug() << "No location with selected label was found from locations.";
132     else
133     {
134         Locations *locations = Locations::GetInstance();
135         locations->increaseLocationIndex(loc->label());
136     }
137 }
138
139 void LocationsDisplayWindow::on_deleteButton_clicked()
140 {
141     qDebug() << "Remove called";
142     Location* loc = this->getSelectedLocation(this->ui->locationsWidget->selectedItems());
143     if (!loc)
144         qDebug() << "No location with selected label was found from locations.";
145     else
146     {
147         Locations *locations = Locations::GetInstance();
148         locations->removeLocation(loc);
149     }
150 }
151
152 void LocationsDisplayWindow::on_customizeButton_clicked()
153 {
154     this->ui->buttonsStacked->setCurrentIndex(1);
155 }
156
157 void LocationsDisplayWindow::on_doneButton_clicked()
158 {
159     this->ui->buttonsStacked->setCurrentIndex(0);
160 }
161
162 void LocationsDisplayWindow::on_locationsWidget_itemClicked(QListWidgetItem* item)
163 {
164     if (this->ui->buttonsStacked->currentIndex() == 0)
165         this->editLocation(item);
166 }
167
168 void LocationsDisplayWindow::on_locationsWidget_clicked(QModelIndex index){}