Added routing feature to friend and location list.
[situare] / src / ui / listview.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QDebug>
23
24 #include "listitem.h"
25 #include "listview.h"
26 #include "friendlistitem.h"
27
28 ListView::ListView(QWidget *parent)
29     : QListWidget(parent),
30       m_previousItem(0)
31 {
32     qDebug() << __PRETTY_FUNCTION__;
33
34     setSelectionMode(QAbstractItemView::SingleSelection);
35     setAutoFillBackground(false);
36     viewport()->setAutoFillBackground(false);
37
38     connect(this, SIGNAL(itemClicked(QListWidgetItem*)),
39             this, SLOT(listItemClicked(QListWidgetItem*)));
40 }
41
42 void ListView::addListItem(const QString &key, ListItem *item)
43 {
44     qDebug() << __PRETTY_FUNCTION__;
45
46     if (!m_listItems.contains(key)) {
47         addItem(item);
48         m_listItems.insert(key, item);
49     }
50 }
51
52 void ListView::addListItemToView(ListItem *item)
53 {
54     qDebug() << __PRETTY_FUNCTION__;
55
56     addItem(item);
57 }
58
59 void ListView::clearList()
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62
63     m_listItems.clear();
64     clear();
65
66     m_previousItem = 0;
67 }
68
69 void ListView::clearUnused(const QStringList &itemIDs)
70 {
71     qDebug() << __PRETTY_FUNCTION__;
72
73     foreach (QString key, m_listItems.keys()) {
74         if (!itemIDs.contains(key)) {
75             ListItem *item = m_listItems.take(key);
76             if (item) {
77                 takeItem(row(item));
78                 delete item;
79             }
80         }
81     }
82 }
83
84 void ListView::clearFilter()
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     if (m_previousItem)
89         m_previousItem->setSelected(false);
90
91     foreach (ListItem *item, m_listItems)
92         setItemHidden(item, false);
93 }
94
95 bool ListView::contains(const QString &itemID)
96 {
97     qDebug() << __PRETTY_FUNCTION__;
98
99     return m_listItems.contains(itemID);
100 }
101
102 void ListView::filter(const QList<QString> &itemIDs)
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     foreach (ListItem *item, m_listItems) {
107         if (itemIDs.contains(m_listItems.key(item)))
108             setItemHidden(item, false);
109         else
110             setItemHidden(item, true);
111     }
112 }
113
114 void ListView::filter(const QString &pattern)
115 {
116     qDebug() << __PRETTY_FUNCTION__;
117
118     foreach (ListItem *item, m_listItems) {
119         if (item->title().contains(pattern, Qt::CaseInsensitive))
120             setItemHidden(item, false);
121         else
122             setItemHidden(item, true);
123     }
124 }
125
126 ListItem *ListView::takeListItemFromView(const QString &itemID)
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129
130     ListItem *item = listItem(itemID);
131     takeItem(row(item));
132     return item;
133 }
134
135 void ListView::listItemClicked(ListItem *item)
136 {
137     qDebug() << __PRETTY_FUNCTION__;
138
139     if (m_previousItem == item) {
140         item->toggleSelection();
141
142     } else {
143         if (m_previousItem)
144             m_previousItem->setSelected(false);
145
146         item->setSelected(true);
147     }
148     m_previousItem = item;
149 }
150
151 void ListView::listItemClicked(QListWidgetItem *item)
152 {
153     qDebug() << __PRETTY_FUNCTION__;
154
155     ListItem *currentItem = dynamic_cast<ListItem*>(item);
156
157     if (currentItem)
158         listItemClicked(currentItem);
159 }
160
161 ListItem *ListView::listItem(const QString &itemID)
162 {
163     qDebug() << __PRETTY_FUNCTION__;
164
165     return dynamic_cast<ListItem*>(m_listItems.value(itemID));
166 }
167
168 ListItem *ListView::listItemAt(int index)
169 {
170     qDebug() << __PRETTY_FUNCTION__;
171
172     QHashIterator<QString, ListItem*> itemIterator(m_listItems);
173     ListItem *item = 0;
174     int counter = 0;
175
176     while (itemIterator.hasNext()) {
177         itemIterator.next();
178         if (index == counter) {
179             item = itemIterator.value();
180             break;
181         }
182         counter++;
183     }
184
185     return item;
186 }
187
188 ListItem *ListView::selectedItem()
189 {
190     qDebug() << __PRETTY_FUNCTION__;
191
192     return m_previousItem;
193 }
194
195 void ListView::setSelectedItem(ListItem *item)
196 {
197     qDebug() << __PRETTY_FUNCTION__;
198
199     listItemClicked(item);
200 }
201
202 ListView::~ListView()
203 {
204     qDebug() << __PRETTY_FUNCTION__;
205
206     clearList();
207 }