Updated tests cases matching the new tabs
[situare] / src / map / friendgroupitem.cpp
index 8ae761e..18a1c82 100644 (file)
 #include <QList>
 #include <QPainter>
 #include <QRect>
+#include <QGraphicsSceneMouseEvent>
+#include <QIcon>
 
 #include "friendlocationitem.h"
-#include "mapcommon.h"
-
 #include "friendgroupitem.h"
+#include "mapcommon.h"
+#include "common.h"
 
 FriendGroupItem::FriendGroupItem(FriendLocationItem *item)
+    : m_clickEvent(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QPixmap pixmap(":/res/images/friend_group.png");
-    setPixmap(pixmap);
+    setPixmap(QPixmap(":/res/images/friend_group.png"));
 
-    setOffset(-pixmap.width()/2, -pixmap.height()/2);
-    setZValue(FRIEND_LOCATION_ICON_Z_LEVEL);
+    setOffset(-pixmap().width() / 2, -pixmap().height() / 2);
+    setZValue(FriendLocationItemZValue);
 
     joinFriend(item);
     setPos(item->pos());
 }
 
-void FriendGroupItem::dropFriend(FriendLocationItem *item)
+void FriendGroupItem::dropFriends()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_friends.removeOne(item);
-    item->setPartOfGroup(false);
+    foreach (FriendLocationItem *friendItem, m_friends) {
+        friendItem->setPartOfGroup(false);
+    }
+
+    m_friends.clear();
 }
 
-bool FriendGroupItem::dropFriends(int zoomLevel)
+void FriendGroupItem::joinFriend(FriendLocationItem *item)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    foreach (FriendLocationItem *friendItem, m_friends) {
-        if (!friendItem->sceneTransformedBoundingRect(zoomLevel)
-            .intersects(sceneTransformedBoundingRect(zoomLevel)))
-            dropFriend(friendItem);
-    }
+    m_friends.append(item);
+    item->setPartOfGroup(true);
+}
 
-    // group can be deleted if it includes max one friend
-    if (m_friends.size() <= 1) {
-        // drop remaining items
-        foreach (FriendLocationItem *friendItem, m_friends)
-            dropFriend(friendItem);
+void FriendGroupItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
+                            QWidget *widget)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-        return true;
+    if (m_clickEvent) {
+        QIcon icon;
+        icon.addPixmap(pixmap());
+        painter->drawPixmap(offset(), icon.pixmap(pixmap().size(), QIcon::Selected,
+                                                                 QIcon::On));
+    } else {
+        QGraphicsPixmapItem::paint(painter, option, widget);
     }
-    return false;
+
+    painter->setPen(Qt::white);
+    painter->setFont(NOKIA_FONT_SMALL);
+    painter->drawText(QRect(GROUP_ITEM_FRIENDS_COUNT_X, GROUP_ITEM_FRIENDS_COUNT_Y,
+                            GROUP_ITEM_FRIENDS_COUNT_WIDTH, GROUP_ITEM_FRIENDS_COUNT_HEIGHT),
+                      Qt::AlignCenter, QString::number(m_friends.count()));
 }
 
-void FriendGroupItem::joinFriend(FriendLocationItem *item)
+void FriendGroupItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_friends.append(item);
-    item->setPartOfGroup(true);
+    if (m_clickEvent) {
+        if ((event->pos() - m_mousePressPosition).manhattanLength() > PRESS_MANHATTAN_LENGTH) {
+            m_clickEvent = false;
+            update();
+        }
+    }
 }
 
-void FriendGroupItem::mergeWithGroup(FriendGroupItem *group)
+void FriendGroupItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    foreach (FriendLocationItem *item, m_friends) {
-        m_friends.removeOne(item);
-        group->joinFriend(item);
-    }
+    m_mousePressPosition = event->pos();
+    m_clickEvent = true;
+    update();
 }
 
-void FriendGroupItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
-                            QWidget *widget)
+void FriendGroupItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
 {
+    Q_UNUSED(event);
+
     qDebug() << __PRETTY_FUNCTION__;
 
-    BaseLocationItem::paint(painter, option, widget);
+    if (m_clickEvent) {
+        m_clickEvent = false;
+        QList<QString> userIDs;
+
+        foreach (FriendLocationItem *item, m_friends)
+            userIDs.append(item->userId());
+
+        emit locationItemClicked(userIDs);
+    }
 
-    QFont font = painter->font();
-    font.setPointSize(painter->font().pointSize() * 2); // double default font size
-    painter->setFont(font);
-    painter->drawText(boundingRect(), Qt::AlignCenter, QString::number(m_friends.count()));
+    update();
 }