Friend filtering redone
[situare] / src / user / friendmodel.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Sami Rämö - sami.ramo@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
23 #include "friendmodel.h"
24
25 FriendModel::FriendModel(QObject *parent) :
26     QAbstractListModel(parent)
27 {
28     QHash<int, QByteArray> roles;
29     roles[AddressRole] = "address";
30     roles[CoordinateRole] = "coordinate";
31     roles[NameRole] = "name";
32     roles[MessageRole] = "message";
33     roles[ImageUrlRole] = "imageUrl";
34     roles[ImageRole] = "image";
35     roles[TimeStampRole] = "time";
36     roles[DistanceRole] = "distance";
37     roles[DistanceUnitRole] = "distanceUnit";
38     roles[UserIdRole] = "userId";
39     setRoleNames(roles);
40 }
41
42 //void RouteModel::addSegment(const RouteSegment &segment)
43 void FriendModel::addFriend(const User &singleFriend)
44 {
45     beginInsertRows(QModelIndex(), rowCount(), rowCount());
46     /// @todo implement adding a friend
47 //    m_friends << singleFriend;
48     endInsertRows();
49 }
50
51 void FriendModel::setFriends(const QList<User *>& friends)
52 {
53     beginResetModel();
54     m_friends = friends;
55     endResetModel();
56 }
57
58 void FriendModel::resetFriends()
59 {
60     beginResetModel();
61     endResetModel();
62 }
63
64 int FriendModel::rowCount(const QModelIndex & parent) const {
65     Q_UNUSED(parent);
66     return m_friends.count();
67 }
68
69 QVariant FriendModel::data(const QModelIndex & index, int role) const {
70     if (index.row() < 0 || index.row() > m_friends.count())
71         return QVariant();
72
73     const User *user = m_friends[index.row()];
74     switch (role) {
75         case CoordinateRole:
76             return QVariant::fromValue(user->coordinates());
77         case AddressRole:
78             return user->address();
79         case NameRole:
80             return user->name();
81         case MessageRole:
82             return user->note();
83         case ImageUrlRole:
84             return user->profileImageUrl();
85         case ImageRole:
86             return user->profileImage();
87         case TimeStampRole:
88             return user->timestamp();
89         case DistanceRole:
90             {
91                 double distance;
92                 QString unit;
93                 user->distance(distance, unit);
94                 return distance;
95             }
96         case DistanceUnitRole:
97             {
98                 double distance;
99                 QString unit;
100                 user->distance(distance, unit);
101                 return unit;
102             }
103         case UserIdRole:
104             return user->userId();
105         case UserRole:
106             return QVariant::fromValue((void*)user);
107         default:
108             return QVariant();
109     }
110 }
111
112 FilteredFriendModel::FilteredFriendModel(QObject *parent)
113     : QSortFilterProxyModel(parent)
114 {}
115
116 void FilteredFriendModel::setFilter(const QList<User*>& filter)
117 {
118     m_filter = filter;
119     invalidateFilter();
120 }
121
122 void FilteredFriendModel::resetFilter()
123 {
124     setFilter(QList<User*>());
125 }
126
127 bool FilteredFriendModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
128 {
129     Q_UNUSED(source_parent);
130     const QModelIndex index = sourceModel()->index(source_row, 0);
131     User* user = static_cast<User*>(sourceModel()->data(index, FriendModel::UserRole).value<void*>());
132     if (!m_filter.isEmpty())
133         return m_filter.contains(user);
134     return true;
135 }