-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphicsNode.cpp
115 lines (95 loc) · 3.22 KB
/
GraphicsNode.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
#include "GraphicsNode.h"
#include <QPainter>
#include <QGraphicsTextItem>
#include <QGraphicsDropShadowEffect>
#include <QFontDatabase>
#include <QDebug>
#include <QStyleOptionGraphicsItem>
#include "Node.h"
GraphicsNode::GraphicsNode()
{
width = 100;
height = 100;
titleHeight = 25;
radius = 5;
headerColor = QColor("#4299E1");
headerBrush = QBrush(headerColor);
titleBrush = QBrush(Qt::white);
titleColor = QColor(Qt::white);
bodyBrush = QBrush(QColor("#2D3748"));
defaultOutlinePen = QPen(QColor(255, 255, 255, 150));
defaultOutlinePen.setWidth(4);
selectedOutlinePen = QPen(Qt::yellow);
selectedOutlinePen.setWidth(4);
textItem = new QGraphicsTextItem(this);
textItem->setPlainText("Untitled");
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
}
void GraphicsNode::setSize(QSize size)
{
width = size.width();
height = size.height();
}
void GraphicsNode::setTitle(QString title)
{
QFont font("Droid Sans");
font.setBold(true);
font.setPointSize(10);
textItem->setPlainText(title);
textItem->setFont(font);
textItem->setDefaultTextColor(QColor("#FFF"));
textItem->adjustSize();
textItem->setPos(QPoint(width / 2 - textItem->boundingRect().width() / 2,
(titleHeight / 2 - textItem->boundingRect().height() / 2) + 2));
}
void GraphicsNode::setColor(QColor color)
{
if (color.isValid()) {
headerColor = color;
}
}
QRectF GraphicsNode::boundingRect() const
{
return QRectF(0, 0,
width,
height).normalized();
}
void GraphicsNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
// painter->setClipRect(option->exposedRect);
headerBrush.setColor(headerColor);
// Paint the header
// Rounded corners at the top only https://stackoverflow.com/a/15289912
// Essentially draw a rounded rect then draw two overlapping rects that cover the gaps
QPainterPath headerPath;
headerPath.setFillRule( Qt::WindingFill );
headerPath.addRoundedRect(QRect(0, 0, width, titleHeight), radius, radius);
headerPath.addRect(0, titleHeight - radius, radius, radius); // Bottom left corner not rounded
headerPath.addRect(width - radius, titleHeight - radius, radius, radius); // Bottom right corner not rounded
painter->setPen(Qt::NoPen);
painter->setBrush(headerBrush);
painter->drawPath(headerPath.simplified());
// Paint the body
QPainterPath bodyPath;
bodyPath.addRect(0, titleHeight, width, height - titleHeight);
painter->setPen(Qt::NoPen);
painter->setBrush(bodyBrush);
painter->drawPath(bodyPath.simplified());
// Paint the border
QPainterPath borderPath;
borderPath.addRoundedRect(0, 0, width, height, radius, radius);
if (this->isSelected()) painter->setPen(selectedOutlinePen);
else painter->setPen(defaultOutlinePen);
painter->setBrush(Qt::NoBrush);
painter->drawPath(borderPath);
}
void GraphicsNode::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (model) {
model->setPosition(scenePos().x(), scenePos().y());
model->update();
}
QGraphicsItem::mouseMoveEvent(event);
}