NodeItem.cpp
1.65 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
65
66
67
68
69
70
71
72
73
74
75
#include "NodeItem.h"
#include <QtWidgets>
#include <QMessageBox>
void NodeItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
{
/*if (event->button() == Qt::LeftButton) {
QMessageBox msgbox;
msgbox.setText("hi");
msgbox.setInformativeText("hi2");
msgbox.setStandardButtons(QMessageBox::Ok);
msgbox.setDefaultButton(QMessageBox::Ok);
msgbox.exec();
}*/
}
void NodeItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{
}
void NodeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
{
}
NodeItem::NodeItem(double x, double y, QColor color, QString label)
{
//node constructor
this->x = x;
this->y = y;
this->color = color;
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(0, 0, NODE_SIZE, NODE_SIZE);
return path;
}
void NodeItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
Q_UNUSED(widget);
//set node pen style
QPen oldPen = painter->pen();
QPen pen = oldPen;
pen.setWidth(0);
pen.setColor(QColor(Qt::black));
painter->setPen(pen);
//label
QFont font("Gulim", 3); //set font, font size
painter->setFont(font);
painter->save();
painter->drawText(x, y, label);
painter->restore();
//node rectangle
QColor fillColor = (option->state & QStyle::State_Selected) ? color.dark(150) : color;
if (option->state & QStyle::State_MouseOver) fillColor = fillColor.light(125);
painter->setBrush(QBrush(fillColor));
painter->drawRect(x, y, NODE_SIZE, NODE_SIZE);
}