Location selector and edit window finished
[ptas] / zouba / src / locationsdisplay.cpp
1 #include <QPushButton>
2 #include <QVBoxLayout>
3 #include <QListWidget>
4 #include <QHash>
5 #include <QDebug>
6 #include <QListWidgetItem>
7 #include <QPoint>
8 #include <QMenu>
9 #include <QMenuBar>
10 #include <QAction>
11 #include <QWidget>
12 #ifdef Q_WS_MAEMO_5
13 #include <QMaemo5EditBar>
14 #endif
15
16 #include "locationsdisplay.h"
17 #include "locations.h"
18 #include "addressdialog.h"
19
20 const QString invalidPostText = " - Invalid address";
21 const QString editText = "Edit list";
22 const QString newLocText = "Add new location";
23 const QString removeText = "Remove";
24 const QString doneText = "Done";
25 const QString moveUpText = "Move up";
26 const QString moveDownText = "Move down";
27
28 QString getLocName(const QListWidgetItem *item);
29 Location* getSelectedLocation(QList<QListWidgetItem*> list);
30
31 LocationsDisplay::LocationsDisplay(QWidget *parent) :
32         QMainWindow(parent)
33 {
34 #ifdef Q_WS_MAEMO_5
35     this->setAttribute(Qt::WA_Maemo5StackedWindow);
36     //this->setWindowFlags(this->windowFlags() | Qt::Window);
37 #endif
38
39     QMenuBar *menu = this->menuBar();
40     QAction *editListAction = new QAction(editText, menu);
41     menu->addAction(editListAction);
42     connect(editListAction, SIGNAL(triggered()), this, SLOT(showEditOptions()));
43
44     this->m_centralWidget = new QWidget(this);
45     this->setCentralWidget(this->m_centralWidget);
46
47     QVBoxLayout *layout = new QVBoxLayout(this->m_centralWidget);
48     this->m_centralWidget->setLayout(layout);
49
50     this->m_topWidget = new QWidget(this->m_centralWidget);
51     layout->addWidget(this->m_topWidget);
52     QVBoxLayout *topLayout = new QVBoxLayout(this->m_topWidget);
53     this->m_topWidget->setLayout(topLayout);
54     this->m_addButton = new QPushButton(newLocText, this->m_topWidget);
55     connect(this->m_addButton, SIGNAL(clicked()), this, SLOT(addAddress()));
56     topLayout->addWidget(this->m_addButton);
57
58     this->m_list = new QListWidget(this->m_centralWidget);
59     connect(this->m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(editLocation(QListWidgetItem*)));
60     //this->m_list->setContextMenuPolicy(Qt::CustomContextMenu);
61     //connect(this->m_list, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenu(const QPoint&)));
62     layout->addWidget(this->m_list);
63
64     this->populateLocations();
65
66     this->m_bottomWidget = new QWidget(this->m_centralWidget);
67     layout->addWidget(this->m_bottomWidget);
68     QHBoxLayout *bottomLayout = new QHBoxLayout(this->m_bottomWidget);
69     this->m_bottomWidget->setLayout(bottomLayout);
70     QPushButton *removeButton = new QPushButton(removeText, this->m_bottomWidget);
71     connect(removeButton, SIGNAL(clicked()), this, SLOT(remove()));
72     bottomLayout->addWidget(removeButton);
73     QPushButton *moveUpButton = new QPushButton(moveUpText, this->m_bottomWidget);
74     connect(moveUpButton, SIGNAL(clicked()), this, SLOT(moveUp()));
75     bottomLayout->addWidget(moveUpButton);
76     QPushButton *moveDownButton = new QPushButton(moveDownText, this->m_bottomWidget);
77     connect(moveDownButton, SIGNAL(clicked()), this, SLOT(moveDown()));
78     bottomLayout->addWidget(moveDownButton);
79
80     QPushButton *doneButton = new QPushButton(doneText, this->m_bottomWidget);
81     connect(doneButton, SIGNAL(clicked()), this, SLOT(closeEditOptions()));
82     bottomLayout->addWidget(doneButton);
83     this->m_bottomWidget->hide();
84
85     Locations *locations = Locations::GetInstance();
86     connect(locations, SIGNAL(locationsChanged()), this, SLOT(populateLocations()));
87 }
88
89 void LocationsDisplay::populateLocations()
90 {
91     m_list->clear();
92     qDebug() << "Populating locations";
93     Locations *locations = Locations::GetInstance();
94
95     for (int index = 1; index <= locations->size(); ++index)
96     {
97         qDebug() << "Adding location: " << locations->getLocation(index)->label();
98         Location* loc = locations->getLocation(index);
99         QString dispName = loc->label();
100         if (!loc->isValid())
101             dispName.append(invalidPostText);
102         new QListWidgetItem(dispName, m_list);
103     }
104     qDebug() << "Locations populated";
105 }
106
107 void LocationsDisplay::addAddress()
108 {
109     AddressDialog *dialog = new AddressDialog(this);
110     dialog->show();
111 }
112
113 void LocationsDisplay::editLocation(QListWidgetItem *item)
114 {
115     if (!item) return;
116
117     Locations *locations = Locations::GetInstance();
118     QString findText = getLocName(item);
119     Location *loc = locations->getLocation(findText);
120     if (!loc)
121         qDebug() << "No location with label " << findText << " was found from locations.";
122     else
123     {
124         AddressDialog *dialog = new AddressDialog(this, loc);
125         dialog->show();
126     }
127 }
128
129 QString getLocNameold(const QListWidgetItem *item)
130 {
131     if (!item) return 0;
132     QString retText = item->text();
133     if (retText.contains(" - Invalid address", Qt::CaseSensitive))
134         retText.chop(18);
135     return retText;
136 }
137
138 /*void LocationsDisplay::contextMenu(const QPoint &point)
139 {
140     qDebug() << "ContextMenu requested";
141     this->m_point = point;
142     QMenu *menu = new QMenu(this->m_list);
143     menu->addAction("Delete", this, SLOT(remove()));
144     menu->exec(this->mapToGlobal(point));
145 }*/
146
147 void LocationsDisplay::remove()
148 {
149     qDebug() << "Remove called";
150     Location* loc = getSelectedLocation(this->m_list->selectedItems());
151     if (!loc)
152         qDebug() << "No location with selected label was found from locations.";
153     else
154     {
155         Locations *locations = Locations::GetInstance();
156         locations->removeLocation(loc);
157     }
158 }
159
160 Location* getSelectedLocationold(QList<QListWidgetItem*> list)
161 {
162     if (list.size() == 0)
163     {
164         qDebug() << "No item is selected";
165         return 0;
166     }
167     QListWidgetItem *item = list.at(0);
168     QString name = getLocName(item);
169     qDebug() << "Selected item is" << name;
170     Locations *locations = Locations::GetInstance();
171     return locations->getLocation(name);
172 }
173
174 void LocationsDisplay::moveUp()
175 {
176     qDebug() << "Move up called";
177     Location* loc = getSelectedLocation(this->m_list->selectedItems());
178     if (!loc)
179         qDebug() << "No location with selected label was found from locations.";
180     else
181     {
182         Locations *locations = Locations::GetInstance();
183         locations->lowerLocationIndex(loc->label());
184     }
185 }
186
187 void LocationsDisplay::moveDown()
188 {
189     qDebug() << "Move down called";
190     Location* loc = getSelectedLocation(this->m_list->selectedItems());
191     if (!loc)
192         qDebug() << "No location with selected label was found from locations.";
193     else
194     {
195         Locations *locations = Locations::GetInstance();
196         locations->increaseLocationIndex(loc->label());
197     }
198 }
199
200 void LocationsDisplay::showEditOptions()
201 {
202     disconnect(this->m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(editLocation(QListWidgetItem*)));
203     this->m_topWidget->hide();
204     this->m_bottomWidget->show();
205 }
206
207 void LocationsDisplay::closeEditOptions()
208 {
209     connect(this->m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(editLocation(QListWidgetItem*)));
210     this->m_topWidget->show();
211     this->m_bottomWidget->hide();
212 }