-
Notifications
You must be signed in to change notification settings - Fork 4
/
trap.cpp
121 lines (95 loc) · 2.52 KB
/
trap.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "trap.h"
#include <QtWidgets>
Trap ::Trap (const QColor &color, int x, int y)
{
this->x = x;
this->y = y;
this->color = color;
this->dispText = "Trap";
setZValue((x + y) % 2);
setFlags(ItemIsSelectable | ItemIsMovable);
setAcceptHoverEvents(true);
}
Trap ::Trap (const QColor &color, int x, int y, QString text)
{
this->x = x;
this->y = y;
this->color = color;
this->dispText = "Trap";
setZValue((x + y) % 2);
setFlags(ItemIsSelectable | ItemIsMovable);
setAcceptHoverEvents(true);
}
int Trap::type() const
{
return 3;
}
QRectF Trap ::boundingRect() const
{
//window location
return QRectF(0, 0, 110, 110);
//return QRectF(0, 0, 110, 70);
}
QPainterPath Trap ::shape() const
{
QPainterPath path;
path.addRect(0, 0, 110, 110);
return path;
}
void Trap ::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
QColor fillColor = color;
QPen oldPen = painter->pen();
QPen pen = oldPen;
int width = 0;
if (option->state & QStyle::State_Selected)
width += 2;
pen.setWidth(width);
QBrush b = painter->brush();
painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
//painter->drawRect(QRect(1, 1, 100, 60));
//commented out
//painter->setBrush(b);
painter->setPen(QPen(Qt::black, 1));
//added by me (Dennis
QBrush fillbrush;
fillbrush.setColor(fillColor);
fillbrush.setStyle(Qt::SolidPattern);
//Make trapezoid
QPolygon poly;
poly << QPoint(20,35); //A A->B->C->D
poly << QPoint(90,35); //B
poly << QPoint(110,1); //C
poly << QPoint(1,1); //D
painter->drawPolygon(poly);
QPainterPath p;
p.addRegion(poly);
painter->fillPath(p,fillbrush);
QFont font("Times", 10);
font.setStyleStrategy(QFont::ForceOutline);
painter->setFont(font);
painter->save();
painter->scale(1, 1);
painter->drawText(30, 20, dispText);
painter->restore();
}
void Trap ::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mousePressEvent(event);
update();
}
void Trap ::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (event->modifiers() & Qt::ShiftModifier) {
stuff << event->pos();
update();
return;
}
QGraphicsItem::mouseMoveEvent(event);
}
void Trap ::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mouseReleaseEvent(event);
update();
}