NodeItem.cpp
1.4 KB
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
#include "NodeItem.h"
#include <QtWidgets>
void NodeItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
{
}
void NodeItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{
}
void NodeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
{
}
NodeItem::NodeItem(int x, int y, QString label)
{
this->x = x;
this->y = y;
this->color = QColor(Qt::green);
this->label = label;
setZValue(1);
setFlags(ItemIsSelectable | ItemIsMovable);
setAcceptHoverEvents(true);
}
QRectF NodeItem::boundingRect() const
{
return QRectF(0, 0, NODE_SIZE, NODE_SIZE);
}
QPainterPath NodeItem::shape() const
{
QPainterPath path;
//path.addRect(14, 14, 82, 42);
path.addRect(0, 0, NODE_SIZE, NODE_SIZE);
return path;
}
void NodeItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
Q_UNUSED(widget);
//label
QFont font("Gulim", 10);
font.setStyleStrategy(QFont::ForceOutline);
painter->setFont(font);
painter->save();
painter->scale(0.3, 0.3);
painter->drawText(0, 0, label);
painter->restore();
//Rectangle
QColor fillColor = (option->state & QStyle::State_Selected) ? color.dark(150) : color;
if (option->state & QStyle::State_MouseOver) fillColor = fillColor.light(125);
QPen pen = painter->pen();
pen.setWidth(0);
pen.setColor(QColor(Qt::black));
painter->setPen(pen);
painter->setBrush(QBrush(fillColor));
painter->drawRect(0, 0, NODE_SIZE, NODE_SIZE);
}