Added listcommon.h file.
[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     foreach (ListItem *item, m_listItems)
83         setItemHidden(item, false);
84 }
85
86 bool ListView::contains(const QString &userID)
87 {
88     qDebug() << __PRETTY_FUNCTION__;
89
90     return m_listItems.contains(userID);
91 }
92
93 void ListView::filter(const QList<QString> &userIDs)
94 {
95     qDebug() << __PRETTY_FUNCTION__;
96
97     foreach (ListItem *item, m_listItems) {
98         if (userIDs.contains(item->id()))
99             setItemHidden(item, false);
100         else
101             setItemHidden(item, true);
102     }
103 }
104
105 ListItem *ListView::takeListItemFromView(const QString &userID)
106 {
107     qDebug() << __PRETTY_FUNCTION__;
108
109     ListItem *item = listItem(userID);
110     takeItem(row(item));
111     return item;
112 }
113
114 void ListView::listItemClicked(QListWidgetItem *item)
115 {
116     qDebug() << __PRETTY_FUNCTION__;
117
118     ListItem *currentItem = static_cast<ListItem*>(item);
119
120     if (currentItem) {
121
122         if (previousItem == currentItem) {
123             bool expanded = currentItem->toggleSelection();
124
125             if (expanded)
126                 emit listItemClicked(currentItem->coordinates());
127
128         } else {
129             if (previousItem)
130                 previousItem->setSelected(false);
131
132             currentItem->setSelected(true);
133             emit listItemClicked(currentItem->coordinates());
134         }
135
136         previousItem = currentItem;
137
138     }
139 }
140
141 ListItem *ListView::listItem(const QString &userID)
142 {
143     qDebug() << __PRETTY_FUNCTION__;
144
145     return static_cast<ListItem*>(m_listItems.value(userID));
146 }