Merge branch 'master' into scalable_panels
[situare] / src / map / friendlocationitem.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Ville Tiensuu - ville.tiensuu@ixonos.com
6        Sami Rämö - sami.ramo@ixonos.com
7
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.
11
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.
16
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,
20    USA.
21 */
22
23 #include <QDebug>
24 #include <QGraphicsSceneMouseEvent>
25 #include <QIcon>
26 #include "friendlocationitem.h"
27 #include "mapcommon.h"
28
29 FriendLocationItem::FriendLocationItem(const QString &userId,
30                                        QObject *parent)
31     : BaseLocationItem(parent),
32       m_partOfGroup(false),
33       m_userId(userId),
34       m_mousePressPosition(QPoint(0, 0)),
35       m_clickEvent(false)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38
39     setPos(UNDEFINED, UNDEFINED);
40     setZValue(FRIEND_LOCATION_ICON_Z_LEVEL);
41 }
42
43 bool FriendLocationItem::isPartOfGroup() const
44 {
45     qDebug() << __PRETTY_FUNCTION__;
46
47     return m_partOfGroup;
48 }
49
50 void FriendLocationItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
51 {
52     qDebug() << __PRETTY_FUNCTION__;
53
54     m_mousePressPosition = event->pos();
55     m_clickEvent = true;
56     update();
57 }
58
59 void FriendLocationItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
60 {
61     qDebug() << __PRETTY_FUNCTION__;
62
63     if (m_clickEvent) {
64         if ((event->pos() - m_mousePressPosition).manhattanLength() > PRESS_MANHATTAN_LENGTH) {
65             m_clickEvent = false;
66             update();
67         }
68     }
69 }
70
71 void FriendLocationItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
72 {
73     qDebug() << __PRETTY_FUNCTION__;
74
75     Q_UNUSED(event);
76
77     if (m_clickEvent) {
78         m_clickEvent = false;
79         QList<QString> userIDs;
80         userIDs.append(m_userId);
81
82         emit locationItemClicked(userIDs);
83     }
84
85     update();
86 }
87
88 void FriendLocationItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
89                                QWidget *widget)
90 {
91     qDebug() << __PRETTY_FUNCTION__;
92
93     if (m_clickEvent) {
94         QIcon icon;
95         icon.addPixmap(pixmap());
96         painter->drawPixmap(offset(), icon.pixmap(pixmap().size(), QIcon::Selected,
97                                                                  QIcon::On));
98     } else {
99         BaseLocationItem::paint(painter, option, widget);
100     }
101 }
102
103 void FriendLocationItem::setPartOfGroup(bool value)
104 {
105     qDebug() << __PRETTY_FUNCTION__;
106
107     m_partOfGroup = value;
108
109     if (value)
110         hide();
111     else
112         show();
113 }
114
115 QString FriendLocationItem::userId() const
116 {
117     qDebug() << __PRETTY_FUNCTION__;
118
119     return m_userId;
120 }
121
122 void FriendLocationItem::setProfileImage(const QPixmap image, const QUrl &url)
123 {
124     qDebug() << __PRETTY_FUNCTION__;
125
126     setPixmap(image);
127     setOffset(-image.width()/2, -image.height()/2);
128     m_profileImageUrl = url;
129 }
130
131 QUrl FriendLocationItem::profileImageUrl() const
132 {
133     qDebug() << __PRETTY_FUNCTION__;
134
135     return m_profileImageUrl;
136 }
137