-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathmap.cpp
63 lines (57 loc) · 1.42 KB
/
map.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "map.h"
Map::Map()
{
dragOver = false;
setAcceptDrops(true);
}
QRectF Map::boundingRect() const
{
return QRectF(-369, -235, 738, 470);
}
void Map::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(painter)
Q_UNUSED(option)
Q_UNUSED(widget)
// painter->setPen(Qt::black);
// painter->drawRect(boundingRect());
}
void Map::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
if (event->mimeData()->hasText())
{
event->setAccepted(true);
dragOver = true;
update();
}
else
event->setAccepted(false);
}
void Map::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
Q_UNUSED(event);
dragOver = false;
update();
}
void Map::dropEvent(QGraphicsSceneDragDropEvent *event)
{
dragOver = false;
if (event->mimeData()->hasText())
{
QString s = event->mimeData()->text();
QPointF pos = mapToScene(event->pos());
pos.setX((int(pos.x()) - 249) / 82 * 82 + 290);
pos.setY((int(pos.y()) - 81) / 98 * 98 + 130);
if (s == "Shovel")
{
Shovel *shovel = qgraphicsitem_cast<Shovel *>(scene()->items(QPointF(830, 15))[0]);
shovel->removePlant(pos);
}
else
{
Shop *shop = qgraphicsitem_cast<Shop *>(scene()->items(QPointF(300, 15))[0]);
shop->addPlant(s, pos);
}
}
update();
}