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