Added back button, removed list selection from location list view when
[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_currentItem(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::clearItemSelection()
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62
63     clearSelection();
64
65     if (m_currentItem)
66         m_currentItem->setSelected(false);
67
68     emit listItemSelectionChanged();
69 }
70
71 void ListView::clearList()
72 {
73     qDebug() << __PRETTY_FUNCTION__;
74
75     clearItemSelection();
76
77     qDeleteAll(m_listItems.begin(), m_listItems.end());
78     m_listItems.clear();
79     clear();
80
81     m_currentItem = 0;
82 }
83
84 void ListView::clearUnused(const QStringList &itemIDs)
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     foreach (QString key, m_listItems.keys()) {
89         if (!itemIDs.contains(key)) {
90             ListItem *item = m_listItems.take(key);
91             if (item) {
92                 takeItem(row(item));
93                 if (m_currentItem == item)
94                     m_currentItem = 0;
95                 delete item;
96             }
97         }
98     }
99 }
100
101 void ListView::clearFilter()
102 {
103     qDebug() << __PRETTY_FUNCTION__;
104
105     m_filteredItemIDs.clear();
106
107     foreach (ListItem *item, m_listItems)
108         setItemHidden(item, false);
109 }
110
111 bool ListView::contains(const QString &itemID)
112 {
113     qDebug() << __PRETTY_FUNCTION__;
114
115     return m_listItems.contains(itemID);
116 }
117
118 void ListView::filter(const QList<QString> &itemIDs)
119 {
120     qDebug() << __PRETTY_FUNCTION__;
121
122     m_filteredItemIDs = itemIDs;
123
124     foreach (ListItem *item, m_listItems) {
125         if (itemIDs.contains(m_listItems.key(item))) {
126             item->setHidden(false);
127         } else {
128             item->setSelected(false);
129             item->setHidden(true);
130         }
131     }
132
133     emit listItemSelectionChanged();
134 }
135
136 void ListView::filter(const QString &pattern)
137 {
138     qDebug() << __PRETTY_FUNCTION__;
139
140     if (m_filteredItemIDs.isEmpty()) {
141         foreach (ListItem *item, m_listItems) {
142             if (item->title().contains(pattern, Qt::CaseInsensitive)) {
143                 item->setHidden(false);
144             } else {
145                 item->setSelected(false);
146                 item->setHidden(true);
147             }
148         }
149     } else {
150         foreach (QString key, m_filteredItemIDs) {
151             ListItem *item = m_listItems.value(key);
152             if (item) {
153                 if (item->title().contains(pattern, Qt::CaseInsensitive)) {
154                     item->setHidden(false);
155                 } else {
156                     item->setSelected(false);
157                     item->setHidden(true);
158                 }
159             }
160         }
161     }
162
163     emit listItemSelectionChanged();
164 }
165
166 ListItem *ListView::takeListItemFromView(const QString &itemID)
167 {
168     qDebug() << __PRETTY_FUNCTION__;
169
170     ListItem *item = listItem(itemID);
171     takeItem(row(item));
172     return item;
173 }
174
175 bool ListView::listItemClicked(ListItem *clickedItem)
176 {
177     qDebug() << __PRETTY_FUNCTION__;
178
179     if (m_currentItem == clickedItem) {
180         clickedItem->toggleSelection();
181     } else {
182         if (m_currentItem)
183             m_currentItem->setSelected(false);
184
185         clickedItem->setSelected(true);
186     }
187     m_currentItem = clickedItem;
188
189     emit listItemSelectionChanged();
190
191     return clickedItem->isSelected();
192 }
193
194 void ListView::listItemClicked(QListWidgetItem *item)
195 {
196     qDebug() << __PRETTY_FUNCTION__;
197
198     ListItem *currentItem = dynamic_cast<ListItem*>(item);
199
200     if (currentItem)
201         listItemClicked(currentItem);
202 }
203
204 ListItem *ListView::listItem(const QString &itemID)
205 {
206     qDebug() << __PRETTY_FUNCTION__;
207
208     return dynamic_cast<ListItem*>(m_listItems.value(itemID));
209 }
210
211 ListItem *ListView::listItemAt(int index)
212 {
213     qDebug() << __PRETTY_FUNCTION__;
214
215     ListItem *listItem = 0;
216
217     if (index < count())
218         listItem = dynamic_cast<ListItem*>(item(index));
219
220     return listItem;
221 }
222
223 void ListView::prependListItem(const QString &key, ListItem *item)
224 {
225     qDebug() << __PRETTY_FUNCTION__;
226
227     if (!m_listItems.contains(key)) {
228         insertItem(0, item);
229         m_listItems.insert(key, item);
230     }
231 }
232
233 void ListView::removeLastItem()
234 {
235     qDebug() << __PRETTY_FUNCTION__;
236
237     ListItem *item = listItemAt(count() - 1);
238
239     if (item) {
240         if (item) {
241             QString key = m_listItems.key(item);
242             m_listItems.remove(key);
243             takeItem(row(item));
244             if (m_currentItem == item)
245                 m_currentItem = 0;
246             delete item;
247             item = 0;
248         }
249     }
250 }
251
252 ListItem *ListView::selectedItem()
253 {
254     qDebug() << __PRETTY_FUNCTION__;
255
256     QList<QListWidgetItem *> selectedListItems = selectedItems();
257
258     if (!selectedListItems.isEmpty())
259         return dynamic_cast<ListItem *>(selectedListItems.first());
260     else
261         return 0;
262 }
263
264 void ListView::setSelectedItem(ListItem *item)
265 {
266     qDebug() << __PRETTY_FUNCTION__;
267
268     listItemClicked(item);
269 }
270
271 ListView::~ListView()
272 {
273     qDebug() << __PRETTY_FUNCTION__;
274
275     clearList();
276 }