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