-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvasNode.cpp
More file actions
122 lines (98 loc) · 3.05 KB
/
CanvasNode.cpp
File metadata and controls
122 lines (98 loc) · 3.05 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
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
122
#include "CanvasNode.h"
#include <QPainter>
#include "Wire.h"
#include <QGraphicsScene>
#include <QFileInfo>
#include "helper/CustomNodeEditor.h"
static constexpr qreal HOOK_OFFSET = 12.0;
static constexpr qreal HOOK_HITBOX = 15.0;
CanvasNode::CanvasNode(const QString& text)
: m_text(text)
{
label = QFileInfo(text).baseName();
if (label == "custom")
isCustom = true;
setFlag(ItemIsMovable);
setFlag(ItemIsSelectable);
setFlag(ItemSendsGeometryChanges);
}
QRectF CanvasNode::boundingRect() const
{
return QRectF(-HOOK_OFFSET - HOOK_HITBOX,
-HOOK_HITBOX,
m_width + 2*(HOOK_OFFSET + HOOK_HITBOX),
m_height + 2*HOOK_HITBOX);
}
QPainterPath CanvasNode::shape() const
{
QPainterPath path;
path.addRect(0, 0, m_width, m_height);
path.addEllipse(hookPosition(Left), HOOK_HITBOX, HOOK_HITBOX);
path.addEllipse(hookPosition(Right), HOOK_HITBOX, HOOK_HITBOX);
return path;
}
QPointF CanvasNode::hookPosition(HookType hook) const
{
if (hook == Left)
return QPointF(-HOOK_OFFSET, m_height / 2.0);
else // Right
return QPointF(m_width + HOOK_OFFSET, m_height / 2.0);
}
void CanvasNode::paint(QPainter* painter,
const QStyleOptionGraphicsItem*,
QWidget*)
{
// node body
painter->setBrush(QColor(60, 60, 60));
painter->setPen(QPen(Qt::black, 1));
painter->drawRoundedRect(0, 0, m_width, m_height, 6, 6);
painter->setPen(Qt::white);
painter->drawText(0, 0, m_width, m_height, Qt::AlignCenter, label);
// hooks
QPointF leftHook = hookPosition(Left);
painter->setBrush(Qt::green);
painter->drawEllipse(leftHook, m_hookRadius, m_hookRadius);
QPointF rightHook = hookPosition(Right);
painter->setBrush(Qt::red);
painter->drawEllipse(rightHook, m_hookRadius, m_hookRadius);
}
QString CanvasNode::getPath() const { return m_text; }
QVariant CanvasNode::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionHasChanged && scene())
{
if (leftWire) leftWire->updatePosition();
if (rightWire) rightWire->updatePosition();
scene()->update();
}
return QGraphicsItem::itemChange(change, value);
}
CanvasNode::HookType CanvasNode::hookAt(const QPointF& pos) const
{
const qreal r = HOOK_HITBOX;
QRectF leftBox(hookPosition(Left) - QPointF(r, r),
QSizeF(r*2, r*2));
QRectF rightBox(hookPosition(Right) - QPointF(r, r),
QSizeF(r*2, r*2));
if (leftBox.contains(pos))
return Left;
if (rightBox.contains(pos))
return Right;
return None;
}
CanvasNode::~CanvasNode()
{
if (leftConnection)
leftConnection->rightConnection = nullptr;
if (rightConnection)
rightConnection->leftConnection = nullptr;
}
void CanvasNode::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
if (isCustom)
{
CustomNodeEditor dlg(customCode);
dlg.exec();
}
QGraphicsItem::mouseDoubleClickEvent(event);
}