Merge branch 'locationlistview' of https://vcs.maemo.org/git/situare into locationlis...
[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                 delete item;
80             }
81         }
82     }
83 }
84
85 void ListView::clearFilter()
86 {
87     qDebug() << __PRETTY_FUNCTION__;
88
89     m_filteredItemIDs.clear();
90
91     if (m_previousItem)
92         m_previousItem->setSelected(false);
93
94     foreach (ListItem *item, m_listItems)
95         setItemHidden(item, false);
96 }
97
98 bool ListView::contains(const QString &itemID)
99 {
100     qDebug() << __PRETTY_FUNCTION__;
101
102     return m_listItems.contains(itemID);
103 }
104
105 void ListView::filter(const QList<QString> &itemIDs)
106 {
107     qDebug() << __PRETTY_FUNCTION__;
108
109     m_filteredItemIDs = itemIDs;
110
111     foreach (ListItem *item, m_listItems) {
112         if (itemIDs.contains(m_listItems.key(item)))
113             setItemHidden(item, false);
114         else
115             setItemHidden(item, true);
116     }
117 }
118
119 void ListView::filter(const QString &pattern)
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122
123     if (m_filteredItemIDs.isEmpty()) {
124         foreach (ListItem *item, m_listItems) {
125             if (item->title().contains(pattern, Qt::CaseInsensitive))
126                 setItemHidden(item, false);
127             else
128                 setItemHidden(item, true);
129         }
130     } else {
131         foreach (QString key, m_filteredItemIDs) {
132             ListItem *item = m_listItems.value(key);
133             if (item) {
134                 if (item->title().contains(pattern, Qt::CaseInsensitive))
135                     setItemHidden(item, false);
136                 else
137                     setItemHidden(item, true);
138             }
139         }
140     }
141 }
142
143 ListItem *ListView::takeListItemFromView(const QString &itemID)
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     ListItem *item = listItem(itemID);
148     takeItem(row(item));
149     return item;
150 }
151
152 void ListView::listItemClicked(ListItem *item)
153 {
154     qDebug() << __PRETTY_FUNCTION__;
155
156     if (m_previousItem == item) {
157         item->toggleSelection();
158
159     } else {
160         if (m_previousItem)
161             m_previousItem->setSelected(false);
162
163         item->setSelected(true);
164     }
165     m_previousItem = item;
166 }
167
168 void ListView::listItemClicked(QListWidgetItem *item)
169 {
170     qDebug() << __PRETTY_FUNCTION__;
171
172     ListItem *currentItem = dynamic_cast<ListItem*>(item);
173
174     if (currentItem)
175         listItemClicked(currentItem);
176 }
177
178 ListItem *ListView::listItem(const QString &itemID)
179 {
180     qDebug() << __PRETTY_FUNCTION__;
181
182     return dynamic_cast<ListItem*>(m_listItems.value(itemID));
183 }
184
185 ListItem *ListView::listItemAt(int index)
186 {
187     qDebug() << __PRETTY_FUNCTION__;
188
189     QHashIterator<QString, ListItem*> itemIterator(m_listItems);
190     ListItem *item = 0;
191     int counter = 0;
192
193     while (itemIterator.hasNext()) {
194         itemIterator.next();
195         if (index == counter) {
196             item = itemIterator.value();
197             break;
198         }
199         counter++;
200     }
201
202     return item;
203 }
204
205 ListItem *ListView::selectedItem()
206 {
207     qDebug() << __PRETTY_FUNCTION__;
208
209     return m_previousItem;
210 }
211
212 void ListView::setSelectedItem(ListItem *item)
213 {
214     qDebug() << __PRETTY_FUNCTION__;
215
216     listItemClicked(item);
217 }
218
219 ListView::~ListView()
220 {
221     qDebug() << __PRETTY_FUNCTION__;
222
223     clearList();
224 }