Updated tests cases matching the new tabs
[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(FriendLocationItemZValue);
43
44     joinFriend(item);
45     setPos(item->pos());
46 }
47
48 void FriendGroupItem::dropFriends()
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     foreach (FriendLocationItem *friendItem, m_friends) {
53         friendItem->setPartOfGroup(false);
54     }
55
56     m_friends.clear();
57 }
58
59 void FriendGroupItem::joinFriend(FriendLocationItem *item)
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62
63     m_friends.append(item);
64     item->setPartOfGroup(true);
65 }
66
67 void FriendGroupItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
68                             QWidget *widget)
69 {
70     qDebug() << __PRETTY_FUNCTION__;
71
72     if (m_clickEvent) {
73         QIcon icon;
74         icon.addPixmap(pixmap());
75         painter->drawPixmap(offset(), icon.pixmap(pixmap().size(), QIcon::Selected,
76                                                                  QIcon::On));
77     } else {
78         QGraphicsPixmapItem::paint(painter, option, widget);
79     }
80
81     painter->setPen(Qt::white);
82     painter->setFont(NOKIA_FONT_SMALL);
83     painter->drawText(QRect(GROUP_ITEM_FRIENDS_COUNT_X, GROUP_ITEM_FRIENDS_COUNT_Y,
84                             GROUP_ITEM_FRIENDS_COUNT_WIDTH, GROUP_ITEM_FRIENDS_COUNT_HEIGHT),
85                       Qt::AlignCenter, QString::number(m_friends.count()));
86 }
87
88 void FriendGroupItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
92     if (m_clickEvent) {
93         if ((event->pos() - m_mousePressPosition).manhattanLength() > PRESS_MANHATTAN_LENGTH) {
94             m_clickEvent = false;
95             update();
96         }
97     }
98 }
99
100 void FriendGroupItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
101 {
102     qDebug() << __PRETTY_FUNCTION__;
103
104     m_mousePressPosition = event->pos();
105     m_clickEvent = true;
106     update();
107 }
108
109 void FriendGroupItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
110 {
111     Q_UNUSED(event);
112
113     qDebug() << __PRETTY_FUNCTION__;
114
115     if (m_clickEvent) {
116         m_clickEvent = false;
117         QList<QString> userIDs;
118
119         foreach (FriendLocationItem *item, m_friends)
120             userIDs.append(item->userId());
121
122         emit locationItemClicked(userIDs);
123     }
124
125     update();
126 }