Added missing comments to list classes.
[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       previousItem(0)
31 {
32     qDebug() << __PRETTY_FUNCTION__;
33
34     connect(this, SIGNAL(itemClicked(QListWidgetItem*)),
35             this, SLOT(listItemClicked(QListWidgetItem*)));
36 }
37
38 void ListView::addListItem(const QString &key, ListItem *item)
39 {
40     qDebug() << __PRETTY_FUNCTION__;
41
42     if (!m_listItems.contains(key)) {
43         addItem(item);
44         m_listItems.insert(key, item);
45     }
46 }
47
48 void ListView::addListItemToView(ListItem *item)
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     addItem(item);
53 }
54
55 void ListView::clearList()
56 {
57     qDebug() << __PRETTY_FUNCTION__;
58
59     m_listItems.clear();
60     clear();
61 }
62
63 void ListView::clearUnused(const QStringList &userIDs)
64 {
65     qDebug() << __PRETTY_FUNCTION__;
66
67     foreach (QString key, m_listItems.keys()) {
68         if (!userIDs.contains(key)) {
69             ListItem *item = m_listItems.take(key);
70             if (item) {
71                 takeItem(row(item));
72                 delete item;
73             }
74         }
75     }
76 }
77
78 void ListView::clearFilter()
79 {
80     qDebug() << __PRETTY_FUNCTION__;
81
82     if (previousItem)
83         previousItem->setSelected(false);
84
85     foreach (ListItem *item, m_listItems)
86         setItemHidden(item, false);
87 }
88
89 bool ListView::contains(const QString &userID)
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92
93     return m_listItems.contains(userID);
94 }
95
96 void ListView::filter(const QList<QString> &userIDs)
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99
100     foreach (ListItem *item, m_listItems) {
101         if (userIDs.contains(item->id()))
102             setItemHidden(item, false);
103         else
104             setItemHidden(item, true);
105     }
106 }
107
108 ListItem *ListView::takeListItemFromView(const QString &userID)
109 {
110     qDebug() << __PRETTY_FUNCTION__;
111
112     ListItem *item = listItem(userID);
113     takeItem(row(item));
114     return item;
115 }
116
117 void ListView::listItemClicked(QListWidgetItem *item)
118 {
119     qDebug() << __PRETTY_FUNCTION__;
120
121     ListItem *currentItem = static_cast<ListItem*>(item);
122
123     if (currentItem) {
124
125         if (previousItem == currentItem) {
126             bool selected = currentItem->toggleSelection();
127
128             if (selected)
129                 emit listItemClicked(currentItem->coordinates());
130
131         } else {
132             if (previousItem)
133                 previousItem->setSelected(false);
134
135             currentItem->setSelected(true);
136             emit listItemClicked(currentItem->coordinates());
137         }
138
139         previousItem = currentItem;
140     }
141 }
142
143 ListItem *ListView::listItem(const QString &userID)
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     return static_cast<ListItem*>(m_listItems.value(userID));
148 }