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