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