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