List classes reviewed and fixed review observations.
[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     setAutoFillBackground(false);
35     viewport()->setAutoFillBackground(false);
36
37     connect(this, SIGNAL(itemClicked(QListWidgetItem*)),
38             this, SLOT(listItemClicked(QListWidgetItem*)));
39 }
40
41 void ListView::addListItem(const QString &key, ListItem *item)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44
45     if (!m_listItems.contains(key)) {
46         addItem(item);
47         m_listItems.insert(key, item);
48     }
49 }
50
51 void ListView::addListItemToView(ListItem *item)
52 {
53     qDebug() << __PRETTY_FUNCTION__;
54
55     addItem(item);
56 }
57
58 void ListView::clearList()
59 {
60     qDebug() << __PRETTY_FUNCTION__;
61
62     m_listItems.clear();
63     clear();
64
65     m_previousItem = 0;
66 }
67
68 void ListView::clearUnused(const QStringList &itemIDs)
69 {
70     qDebug() << __PRETTY_FUNCTION__;
71
72     foreach (QString key, m_listItems.keys()) {
73         if (!itemIDs.contains(key)) {
74             ListItem *item = m_listItems.take(key);
75             if (item) {
76                 takeItem(row(item));
77                 delete item;
78             }
79         }
80     }
81 }
82
83 void ListView::clearFilter()
84 {
85     qDebug() << __PRETTY_FUNCTION__;
86
87     if (m_previousItem)
88         m_previousItem->setSelected(false);
89
90     foreach (ListItem *item, m_listItems)
91         setItemHidden(item, false);
92 }
93
94 bool ListView::contains(const QString &itemID)
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     return m_listItems.contains(itemID);
99 }
100
101 void ListView::filter(const QList<QString> &itemIDs)
102 {
103     qDebug() << __PRETTY_FUNCTION__;
104
105     foreach (ListItem *item, m_listItems) {
106         if (itemIDs.contains(m_listItems.key(item)))
107             setItemHidden(item, false);
108         else
109             setItemHidden(item, true);
110     }
111 }
112
113 void ListView::filter(const QString &pattern)
114 {
115     qDebug() << __PRETTY_FUNCTION__;
116
117     foreach (ListItem *item, m_listItems) {
118         if (item->title().contains(pattern, Qt::CaseInsensitive))
119             setItemHidden(item, false);
120         else
121             setItemHidden(item, true);
122     }
123 }
124
125 ListItem *ListView::takeListItemFromView(const QString &itemID)
126 {
127     qDebug() << __PRETTY_FUNCTION__;
128
129     ListItem *item = listItem(itemID);
130     takeItem(row(item));
131     return item;
132 }
133
134 void ListView::listItemClicked(QListWidgetItem *item)
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     ListItem *currentItem = dynamic_cast<ListItem*>(item);
139
140     if (currentItem) {
141         if (m_previousItem == currentItem) {
142             currentItem->toggleSelection();
143
144         } else {
145             if (m_previousItem)
146                 m_previousItem->setSelected(false);
147
148             currentItem->setSelected(true);
149         }
150         m_previousItem = currentItem;
151     }
152 }
153
154 ListItem *ListView::listItem(const QString &itemID)
155 {
156     qDebug() << __PRETTY_FUNCTION__;
157
158     return dynamic_cast<ListItem*>(m_listItems.value(itemID));
159 }
160
161 ListItem *ListView::listItemAt(int index)
162 {
163     qDebug() << __PRETTY_FUNCTION__;
164
165     QHashIterator<QString, ListItem*> itemIterator(m_listItems);
166     ListItem *item = 0;
167     int counter = 0;
168
169     while (itemIterator.hasNext()) {
170         itemIterator.next();
171         if (index == counter) {
172             item = itemIterator.value();
173             break;
174         }
175         counter++;
176     }
177
178     return item;
179 }
180
181 ListView::~ListView()
182 {
183     qDebug() << __PRETTY_FUNCTION__;
184
185     clearList();
186 }