2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Sami Rämö - sami.ramo@ixonos.com
6 Henri Lampela - henri.lampela@ixonos.com
8 Situare is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 Situare is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Situare; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 #include "coordinates/scenecoordinate.h"
27 #include "friendgroupitem.h"
28 #include "friendlocationitem.h"
29 #include "mapcommon.h"
31 #include "user/user.h"
33 #include "frienditemshandler.h"
35 FriendItemsHandler::FriendItemsHandler(MapScene *mapScene, QObject *parent)
40 qDebug() << __PRETTY_FUNCTION__;
43 void FriendItemsHandler::addFriendItem(User *friendData)
45 qDebug() << __PRETTY_FUNCTION__;
47 FriendLocationItem *item = new FriendLocationItem(friendData->userId());
49 item->setProfileImage(friendData->profileImage(), friendData->profileImageUrl());
50 item->setPos(SceneCoordinate(friendData->coordinates()).toPointF());
51 m_friendItems.append(item);
52 m_mapScene->addItem(item);
54 connect(item, SIGNAL(locationItemClicked(QList<QString>)),
55 this, SIGNAL(locationItemClicked(QList<QString>)));
58 void FriendItemsHandler::checkAllFriendsForCollidingFriends()
60 qDebug() << __PRETTY_FUNCTION__;
62 QLinkedList<FriendLocationItem *>::iterator iter = m_friendItems.begin();
63 while (iter != m_friendItems.end()) {
64 // check only friends which are not part of group already
65 if (!(*iter)->isPartOfGroup()) {
66 checkFriendForCollidingFriends(*iter);
72 void FriendItemsHandler::checkFriendForCollidingFriends(FriendLocationItem *item)
74 // checkGroupsForCollisions() is used for checking if groups collide with another
75 // groups or friend items, so this method doesn't have to check against groups
77 qDebug() << __PRETTY_FUNCTION__;
79 FriendGroupItem *group = 0;
81 // loop through all friend items
82 QLinkedList<FriendLocationItem *>::iterator iter = m_friendItems.begin();
83 while (iter != m_friendItems.end()) {
84 // but don't check myself and friends which are already part of a group
85 if (item != *iter && !(*iter)->isPartOfGroup()) {
86 if (collides(item, *iter)) {
88 group = new FriendGroupItem(item);
89 m_mapScene->addItem(group);
90 m_friendGroupItems.append(group);
92 connect(group, SIGNAL(locationItemClicked(QList<QString>)),
93 this, SIGNAL(locationItemClicked(QList<QString>)));
95 group->joinFriend(*iter);
102 bool FriendItemsHandler::collides(BaseLocationItem *item1, BaseLocationItem *item2)
104 QRect item1Rect = item1->sceneTransformedBoundingRect(m_zoomLevel);
105 QRect item2Rect = item2->sceneTransformedBoundingRect(m_zoomLevel);
107 if (item1Rect.intersects(item2Rect))
110 if (item1Rect.left() < (OSM_MAP_MIN_PIXEL_X + item1Rect.width() / 2)) {
111 QRect translated = item1Rect.translated(OSM_MAP_PIXELS_X, 0);
112 if (translated.intersects(item2Rect))
114 } else if (item1Rect.right() > (OSM_MAP_MAX_PIXEL_X - item1Rect.width() / 2)) {
115 QRect translated = item1Rect.translated(-OSM_MAP_PIXELS_X, 0);
116 if (translated.intersects(item2Rect))
123 void FriendItemsHandler::deleteFriendItem(FriendLocationItem *item)
125 qDebug() << __PRETTY_FUNCTION__;
127 m_mapScene->removeItem(item);
131 void FriendItemsHandler::destructGroups()
133 foreach (FriendGroupItem *group, m_friendGroupItems)
134 group->dropFriends();
136 qDeleteAll(m_friendGroupItems);
137 m_friendGroupItems.clear();
140 void FriendItemsHandler::friendImageReady(User *user)
142 qDebug() << __PRETTY_FUNCTION__;
144 foreach (FriendLocationItem *friendItem, m_friendItems) {
145 if (user->userId() == friendItem->userId()) {
146 friendItem->setProfileImage(user->profileImage(), user->profileImageUrl());
152 void FriendItemsHandler::friendListUpdated(QList<User *> &friendsList)
154 qDebug() << __PRETTY_FUNCTION__;
158 // loop through friend items and find matching friend data. If matching data
159 // is not found then remove item
160 QLinkedList<FriendLocationItem *>::iterator iter = m_friendItems.begin();
161 while (iter != m_friendItems.end()) {
163 foreach (User * friendData, friendsList) {
164 if (friendData->userId() == (*iter)->userId()) {
170 // data for friend item was not found so item must be deleted
171 deleteFriendItem(*iter);
172 iter = m_friendItems.erase(iter);
179 // loop through new friend data, find matching friend items and update them, or add new items
180 // if old items are not found
181 foreach (User * friendData, friendsList) {
183 foreach (FriendLocationItem *friendItem, m_friendItems) {
184 if (friendData->userId() == friendItem->userId()) {
185 // friend item was found so update the data
186 updateFriendItem(friendItem, friendData);
192 // friend item was not found so new item must be added
193 addFriendItem(friendData);
197 checkAllFriendsForCollidingFriends();
200 void FriendItemsHandler::refactorFriendItems(int zoomLevel)
202 qDebug() << __PRETTY_FUNCTION__;
204 m_zoomLevel = zoomLevel;
207 checkAllFriendsForCollidingFriends();
210 void FriendItemsHandler::updateFriendItem(FriendLocationItem *friendItem, User *friendData)
212 qDebug() << __PRETTY_FUNCTION__;
215 friendItem->setPos(SceneCoordinate(friendData->coordinates()).toPointF());
218 if (friendItem->profileImageUrl() != friendData->profileImageUrl())
219 friendItem->setProfileImage(friendData->profileImage(), friendData->profileImageUrl());