1 #include "carddragitem.h"
2 
3 #include "carditem.h"
4 #include "cardzone.h"
5 #include "gamescene.h"
6 #include "tablezone.h"
7 #include "zoneviewzone.h"
8 
9 #include <QCursor>
10 #include <QGraphicsSceneMouseEvent>
11 #include <QPainter>
12 
CardDragItem(CardItem * _item,int _id,const QPointF & _hotSpot,bool _faceDown,AbstractCardDragItem * parentDrag)13 CardDragItem::CardDragItem(CardItem *_item,
14                            int _id,
15                            const QPointF &_hotSpot,
16                            bool _faceDown,
17                            AbstractCardDragItem *parentDrag)
18     : AbstractCardDragItem(_item, _hotSpot, parentDrag), id(_id), faceDown(_faceDown), occupied(false), currentZone(0)
19 {
20 }
21 
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)22 void CardDragItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
23 {
24     AbstractCardDragItem::paint(painter, option, widget);
25 
26     if (occupied)
27         painter->fillRect(boundingRect(), QColor(200, 0, 0, 100));
28 }
29 
updatePosition(const QPointF & cursorScenePos)30 void CardDragItem::updatePosition(const QPointF &cursorScenePos)
31 {
32     QList<QGraphicsItem *> colliding =
33         scene()->items(cursorScenePos, Qt::IntersectsItemBoundingRect, Qt::DescendingOrder,
34                        static_cast<GameScene *>(scene())->getViewTransform());
35 
36     CardZone *cardZone = 0;
37     ZoneViewZone *zoneViewZone = 0;
38     for (int i = colliding.size() - 1; i >= 0; i--) {
39         CardZone *temp = qgraphicsitem_cast<CardZone *>(colliding.at(i));
40         if (!cardZone)
41             cardZone = temp;
42         if (!zoneViewZone)
43             zoneViewZone = qobject_cast<ZoneViewZone *>(temp);
44     }
45     CardZone *cursorZone = 0;
46     if (zoneViewZone)
47         cursorZone = zoneViewZone;
48     else if (cardZone)
49         cursorZone = cardZone;
50     if (!cursorZone)
51         return;
52     currentZone = cursorZone;
53 
54     QPointF zonePos = currentZone->scenePos();
55     QPointF cursorPosInZone = cursorScenePos - zonePos;
56     QPointF cardTopLeft = cursorPosInZone - hotSpot;
57     QPointF closestGridPoint = cursorZone->closestGridPoint(cardTopLeft);
58     QPointF newPos = zonePos + closestGridPoint;
59 
60     if (newPos != pos()) {
61         for (int i = 0; i < childDrags.size(); i++)
62             childDrags[i]->setPos(newPos + childDrags[i]->getHotSpot());
63         setPos(newPos);
64 
65         bool newOccupied = false;
66         TableZone *table = qobject_cast<TableZone *>(cursorZone);
67         if (table)
68             if (table->getCardFromCoords(closestGridPoint))
69                 newOccupied = true;
70         if (newOccupied != occupied) {
71             occupied = newOccupied;
72             update();
73         }
74     }
75 }
76 
mouseReleaseEvent(QGraphicsSceneMouseEvent * event)77 void CardDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
78 {
79     setCursor(Qt::OpenHandCursor);
80     QGraphicsScene *sc = scene();
81     QPointF sp = pos();
82     sc->removeItem(this);
83 
84     QList<CardDragItem *> dragItemList;
85     CardZone *startZone = static_cast<CardItem *>(item)->getZone();
86     if (currentZone && !(static_cast<CardItem *>(item)->getAttachedTo() && (startZone == currentZone))) {
87         dragItemList.append(this);
88         for (int i = 0; i < childDrags.size(); i++) {
89             CardDragItem *c = static_cast<CardDragItem *>(childDrags[i]);
90             if (!(static_cast<CardItem *>(c->item)->getAttachedTo() && (startZone == currentZone)) && !c->occupied)
91                 dragItemList.append(c);
92             sc->removeItem(c);
93         }
94     }
95 
96     if (currentZone)
97         currentZone->handleDropEvent(dragItemList, startZone, (sp - currentZone->scenePos()).toPoint());
98 
99     event->accept();
100 }
101