Fixed defects found in review
[situare] / src / map / friendgroupitem.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 #include <QDebug>
23 #include <QList>
24 #include <QPainter>
25 #include <QRect>
26
27 #include "friendlocationitem.h"
28 #include "mapcommon.h"
29
30 #include "friendgroupitem.h"
31
32 FriendGroupItem::FriendGroupItem(FriendLocationItem *item)
33 {
34     qDebug() << __PRETTY_FUNCTION__;
35
36     QPixmap pixmap(":/res/images/friend_group.png");
37     setPixmap(pixmap);
38
39     setOffset(-pixmap.width()/2, -pixmap.height()/2);
40     setZValue(FRIEND_LOCATION_ICON_Z_LEVEL);
41
42     joinFriend(item);
43     setPos(item->pos());
44 }
45
46 void FriendGroupItem::dropFriend(FriendLocationItem *item)
47 {
48     qDebug() << __PRETTY_FUNCTION__;
49
50     m_friends.removeOne(item);
51     item->setPartOfGroup(false);
52 }
53
54 bool FriendGroupItem::dropFriends(int zoomLevel)
55 {
56     qDebug() << __PRETTY_FUNCTION__;
57
58     foreach (FriendLocationItem *friendItem, m_friends) {
59         if (!friendItem->sceneTransformedBoundingRect(zoomLevel)
60             .intersects(sceneTransformedBoundingRect(zoomLevel)))
61             dropFriend(friendItem);
62     }
63
64     // group can be deleted if it includes max one friend
65     if (m_friends.size() <= 1) {
66         // drop remaining items
67         foreach (FriendLocationItem *friendItem, m_friends)
68             dropFriend(friendItem);
69
70         return true;
71     }
72     return false;
73 }
74
75 void FriendGroupItem::joinFriend(FriendLocationItem *item)
76 {
77     qDebug() << __PRETTY_FUNCTION__;
78
79     m_friends.append(item);
80     item->setPartOfGroup(true);
81 }
82
83 void FriendGroupItem::mergeWithGroup(FriendGroupItem *group)
84 {
85     qDebug() << __PRETTY_FUNCTION__;
86
87     foreach (FriendLocationItem *item, m_friends) {
88         m_friends.removeOne(item);
89         group->joinFriend(item);
90     }
91 }
92
93 void FriendGroupItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
94                             QWidget *widget)
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     BaseLocationItem::paint(painter, option, widget);
99
100     QFont font = painter->font();
101     font.setPointSize(painter->font().pointSize() * 2); // double default font size
102     painter->setFont(font);
103     painter->drawText(boundingRect(), Qt::AlignCenter, QString::number(m_friends.count()));
104 }