Merge branch 'master' into scalable_panels
[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 #include <QGraphicsSceneMouseEvent>
27 #include <QIcon>
28
29 #include "friendlocationitem.h"
30 #include "friendgroupitem.h"
31 #include "mapcommon.h"
32 #include "common.h"
33
34 FriendGroupItem::FriendGroupItem(FriendLocationItem *item)
35     : m_clickEvent(false)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38
39     setPixmap(QPixmap(":/res/images/friend_group.png"));
40
41     setOffset(-pixmap().width() / 2, -pixmap().height() / 2);
42     setZValue(FRIEND_LOCATION_ICON_Z_LEVEL);
43
44     joinFriend(item);
45     setPos(item->pos());
46 }
47
48 void FriendGroupItem::dropFriend(FriendLocationItem *item)
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     if (m_friends.removeOne(item))
53         item->setPartOfGroup(false);
54 }
55
56 bool FriendGroupItem::dropFriends(int zoomLevel)
57 {
58     qDebug() << __PRETTY_FUNCTION__;
59
60     foreach (FriendLocationItem *friendItem, m_friends) {
61         if (!friendItem->sceneTransformedBoundingRect(zoomLevel)
62             .intersects(sceneTransformedBoundingRect(zoomLevel)))
63             dropFriend(friendItem);
64     }
65
66     // group can be deleted if it includes max one friend
67     if (m_friends.size() <= 1) {
68         // drop remaining items
69         foreach (FriendLocationItem *friendItem, m_friends)
70             dropFriend(friendItem);
71
72         return true;
73     }
74     return false;
75 }
76
77 void FriendGroupItem::joinFriend(FriendLocationItem *item)
78 {
79     qDebug() << __PRETTY_FUNCTION__;
80
81     m_friends.append(item);
82     item->setPartOfGroup(true);
83 }
84
85 void FriendGroupItem::mergeWithGroup(FriendGroupItem *group)
86 {
87     qDebug() << __PRETTY_FUNCTION__;
88
89     foreach (FriendLocationItem *item, m_friends) {
90         m_friends.removeOne(item);
91         group->joinFriend(item);
92     }
93 }
94
95 void FriendGroupItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
96                             QWidget *widget)
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99
100     if (m_clickEvent) {
101         QIcon icon;
102         icon.addPixmap(pixmap());
103         painter->drawPixmap(offset(), icon.pixmap(pixmap().size(), QIcon::Selected,
104                                                                  QIcon::On));
105     } else {
106         QGraphicsPixmapItem::paint(painter, option, widget);
107     }
108
109     painter->setPen(Qt::white);
110     painter->setFont(NOKIA_FONT_SMALL);
111     painter->drawText(QRect(GROUP_ITEM_FRIENDS_COUNT_X, GROUP_ITEM_FRIENDS_COUNT_Y,
112                             GROUP_ITEM_FRIENDS_COUNT_WIDTH, GROUP_ITEM_FRIENDS_COUNT_HEIGHT),
113                       Qt::AlignCenter, QString::number(m_friends.count()));
114 }
115
116 void FriendGroupItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     if (m_clickEvent) {
121         if ((event->pos() - m_mousePressPosition).manhattanLength() > PRESS_MANHATTAN_LENGTH) {
122             m_clickEvent = false;
123             update();
124         }
125     }
126 }
127
128 void FriendGroupItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
129 {
130     qDebug() << __PRETTY_FUNCTION__;
131
132     m_mousePressPosition = event->pos();
133     m_clickEvent = true;
134     update();
135 }
136
137 void FriendGroupItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
138 {
139     Q_UNUSED(event);
140
141     qDebug() << __PRETTY_FUNCTION__;
142
143     if (m_clickEvent) {
144         m_clickEvent = false;
145         QList<QString> userIDs;
146
147         foreach (FriendLocationItem *item, m_friends)
148             userIDs.append(item->userId());
149
150         emit locationItemClicked(userIDs);
151     }
152
153     update();
154 }