6 #include <QListWidgetItem>
13 #include <QMaemo5EditBar>
16 #include "locationsdisplay.h"
17 #include "locations.h"
18 #include "addressdialog.h"
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";
28 QString getLocName(const QListWidgetItem *item);
29 Location* getSelectedLocation(QList<QListWidgetItem*> list);
31 LocationsDisplay::LocationsDisplay(QWidget *parent) :
35 this->setAttribute(Qt::WA_Maemo5StackedWindow);
36 //this->setWindowFlags(this->windowFlags() | Qt::Window);
39 QMenuBar *menu = this->menuBar();
40 QAction *editListAction = new QAction(editText, menu);
41 menu->addAction(editListAction);
42 connect(editListAction, SIGNAL(triggered()), this, SLOT(showEditOptions()));
44 this->m_centralWidget = new QWidget(this);
45 this->setCentralWidget(this->m_centralWidget);
47 QVBoxLayout *layout = new QVBoxLayout(this->m_centralWidget);
48 this->m_centralWidget->setLayout(layout);
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);
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);
64 this->populateLocations();
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);
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();
85 Locations *locations = Locations::GetInstance();
86 connect(locations, SIGNAL(locationsChanged()), this, SLOT(populateLocations()));
89 void LocationsDisplay::populateLocations()
92 qDebug() << "Populating locations";
93 Locations *locations = Locations::GetInstance();
95 for (int index = 1; index <= locations->size(); ++index)
97 qDebug() << "Adding location: " << locations->getLocation(index)->label();
98 Location* loc = locations->getLocation(index);
99 QString dispName = loc->label();
101 dispName.append(invalidPostText);
102 new QListWidgetItem(dispName, m_list);
104 qDebug() << "Locations populated";
107 void LocationsDisplay::addAddress()
109 AddressDialog *dialog = new AddressDialog(this);
113 void LocationsDisplay::editLocation(QListWidgetItem *item)
117 Locations *locations = Locations::GetInstance();
118 QString findText = getLocName(item);
119 Location *loc = locations->getLocation(findText);
121 qDebug() << "No location with label " << findText << " was found from locations.";
124 AddressDialog *dialog = new AddressDialog(this, loc);
129 QString getLocNameold(const QListWidgetItem *item)
132 QString retText = item->text();
133 if (retText.contains(" - Invalid address", Qt::CaseSensitive))
138 /*void LocationsDisplay::contextMenu(const QPoint &point)
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));
147 void LocationsDisplay::remove()
149 qDebug() << "Remove called";
150 Location* loc = getSelectedLocation(this->m_list->selectedItems());
152 qDebug() << "No location with selected label was found from locations.";
155 Locations *locations = Locations::GetInstance();
156 locations->removeLocation(loc);
160 Location* getSelectedLocationold(QList<QListWidgetItem*> list)
162 if (list.size() == 0)
164 qDebug() << "No item is selected";
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);
174 void LocationsDisplay::moveUp()
176 qDebug() << "Move up called";
177 Location* loc = getSelectedLocation(this->m_list->selectedItems());
179 qDebug() << "No location with selected label was found from locations.";
182 Locations *locations = Locations::GetInstance();
183 locations->lowerLocationIndex(loc->label());
187 void LocationsDisplay::moveDown()
189 qDebug() << "Move down called";
190 Location* loc = getSelectedLocation(this->m_list->selectedItems());
192 qDebug() << "No location with selected label was found from locations.";
195 Locations *locations = Locations::GetInstance();
196 locations->increaseLocationIndex(loc->label());
200 void LocationsDisplay::showEditOptions()
202 disconnect(this->m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(editLocation(QListWidgetItem*)));
203 this->m_topWidget->hide();
204 this->m_bottomWidget->show();
207 void LocationsDisplay::closeEditOptions()
209 connect(this->m_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(editLocation(QListWidgetItem*)));
210 this->m_topWidget->show();
211 this->m_bottomWidget->hide();