Added ListView::listItemSelectionChanged signal. Added deselection of
[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::clearItemSelection()
60 {
61     clearSelection();
62
63     if (m_previousItem)
64         m_previousItem->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_previousItem = 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_previousItem == item)
88                     m_previousItem = 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->setHidden(true);
123     }
124
125     emit listItemSelectionChanged();
126 }
127
128 void ListView::filter(const QString &pattern)
129 {
130     qDebug() << __PRETTY_FUNCTION__;
131
132     if (m_filteredItemIDs.isEmpty()) {
133         foreach (ListItem *item, m_listItems) {
134             if (item->title().contains(pattern, Qt::CaseInsensitive))
135                 item->setHidden(false);
136             else {
137                 item->setSelected(false);
138                 item->setHidden(true);
139             }
140         }
141     } else {
142         foreach (QString key, m_filteredItemIDs) {
143             ListItem *item = m_listItems.value(key);
144             if (item) {
145                 if (item->title().contains(pattern, Qt::CaseInsensitive))
146                     item->setHidden(false);
147                 else {
148                     item->setSelected(false);
149                     item->setHidden(true);
150                 }
151             }
152         }
153     }
154
155     emit listItemSelectionChanged();
156 }
157
158 ListItem *ListView::takeListItemFromView(const QString &itemID)
159 {
160     qDebug() << __PRETTY_FUNCTION__;
161
162     ListItem *item = listItem(itemID);
163     takeItem(row(item));
164     return item;
165 }
166
167 bool ListView::listItemClicked(ListItem *item)
168 {
169     qDebug() << __PRETTY_FUNCTION__;
170
171     if (m_previousItem == item) {
172         item->toggleSelection();
173     } else {
174         if (m_previousItem)
175             m_previousItem->setSelected(false);
176
177         item->setSelected(true);
178     }
179     m_previousItem = item;
180
181     emit listItemSelectionChanged();
182
183     return item->isSelected();
184 }
185
186 void ListView::listItemClicked(QListWidgetItem *item)
187 {
188     qDebug() << __PRETTY_FUNCTION__;
189
190     ListItem *currentItem = dynamic_cast<ListItem*>(item);
191
192     if (currentItem)
193         listItemClicked(currentItem);
194 }
195
196 ListItem *ListView::listItem(const QString &itemID)
197 {
198     qDebug() << __PRETTY_FUNCTION__;
199
200     return dynamic_cast<ListItem*>(m_listItems.value(itemID));
201 }
202
203 ListItem *ListView::listItemAt(int index)
204 {
205     qDebug() << __PRETTY_FUNCTION__;
206
207     QHashIterator<QString, ListItem*> itemIterator(m_listItems);
208     ListItem *item = 0;
209     int counter = 0;
210
211     while (itemIterator.hasNext()) {
212         itemIterator.next();
213         if (index == counter) {
214             item = itemIterator.value();
215             break;
216         }
217         counter++;
218     }
219
220     return item;
221 }
222
223 ListItem *ListView::selectedItem()
224 {
225     qDebug() << __PRETTY_FUNCTION__;
226
227     QList<QListWidgetItem *> selectedListItems = selectedItems();
228
229     if (!selectedListItems.isEmpty())
230         return dynamic_cast<ListItem *>(selectedListItems.first());
231     else
232         return 0;
233 }
234
235 void ListView::setSelectedItem(ListItem *item)
236 {
237     qDebug() << __PRETTY_FUNCTION__;
238
239     listItemClicked(item);
240 }
241
242 ListView::~ListView()
243 {
244     qDebug() << __PRETTY_FUNCTION__;
245
246     clearList();
247 }