-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_graphics_node.py
More file actions
181 lines (147 loc) · 5.73 KB
/
node_graphics_node.py
File metadata and controls
181 lines (147 loc) · 5.73 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from node_graphics_socket import QDMGraphicsSocket
LEFT_TOP = 1
LEFT_BOTTOM = 2
RIGHT_TOP = 3
RIGHT_BOTTOM = 4
IOTYPE_INPUT = 0
IOTYPE_OUTPUT = 1
class QDMGraphicsNode(QGraphicsItem):
def __init__(self, node, parent=None):
super().__init__(parent)
self.node = node
self.console = self.node.console
self.content = self.node.content
self._title_color = Qt.white
self._title_font = QFont("Ubuntu", 15)
self._pen_default = QPen(QColor("#7F000000"))
self._pen_selected = QPen(QColor("#FFFFA637"))
self._brush_title = QBrush(QColor("#FF313131"))
self._brush_background = QBrush(QColor("#E3212121"))
# Override if specified
if self.content.width is not None:
self.width = self.content.width
else:
self.width = 180
if self.content.height is not None:
self.height = self.content.height
else:
self.height = 240
if self.content.edge_size is not None:
self.edge_size = self.content.edge_size
else:
self.edge_size = 10.0
if self.content.title_height is not None:
self.title_height = self.content.title_height
else:
self.title_height = 24
if self.content.padding is not None:
self._padding = self.content.padding
else:
self._padding = 5.0
self.socketSpacing = 25
self.grContent = None
self.widgetSlots = self.node.content.widgetSlots
# init title
self.initTitle()
self.title = self.node.title
self.initContent()
self.initUI()
# init sockets
self.initSockets(self.node.inputSockets)
self.initSockets(self.node.outputSockets)
self.setAcceptDrops(True)
def __str__(self):
return "<Node %s..%s>" % (hex(id(self))[2:5], hex(id(self))[-3:])
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
for item in self.scene().scene.grScene.selectedItems():
if isinstance(item, QDMGraphicsNode):
item.node.updateConnectedEdges()
@property
def title(self):
return self._title
@title.setter
def title(self, value):
self._title = value
self.title_item.setPlainText(self._title)
def boundingRect(self):
return QRectF(
0,
0,
self.width,
self.height
).normalized()
def initUI(self):
self.setFlag(QGraphicsItem.ItemIsSelectable)
self.setFlag(QGraphicsItem.ItemIsMovable)
def initTitle(self):
title_item = QGraphicsTextItem(self)
title_item.setDefaultTextColor(self._title_color)
title_item.setFont(self._title_font)
title_item.setPos(self._padding, 0)
title_item.setTextWidth(
self.width - 2*self._padding
)
self.title_item = title_item
def initContent(self):
edgeSize = self.edge_size
self.grContent = QGraphicsProxyWidget(self)
self.content.setGeometry(int(edgeSize), int(self.title_height + edgeSize),
int(self.width - 2*edgeSize),
int(self.height - 2*edgeSize - self.title_height)
)
self.grContent.setWidget(self.content)
def initSockets(self, socketlist):
for socket in socketlist:
grSocket = QDMGraphicsSocket(socket, self)
socket.grSocket = grSocket
grSocket.setPos(*self.calculateSocketPosition(socket))
def calculateSocketPosition(self, socket):
iotype = socket.iotype
index = socket.index
padding = self._padding
edgeSize = self.edge_size
socketSlot = self.widgetSlots[index]
mappedPoint = socketSlot.mapToParent(socketSlot.rect().center())
y = mappedPoint.y() + self.title_height + edgeSize
if iotype == 0: # if iotype == input
x = 0
else:
x = self.width
return x, y
def setHeight(self, num):
self.height = num
def setWidth(self, num):
self.width = num
def paint(self, painter,QStyleOptionGraphicsItem, widget= None):
height = self.height
width = self.width
title_height = self.title_height
edge_size = self.edge_size
# title
path_title = QPainterPath()
path_title.setFillRule(Qt.WindingFill)
path_title.addRoundedRect(0,0,width, title_height, edge_size, edge_size)
path_title.addRect(0, title_height-edge_size, edge_size, edge_size)
path_title.addRect(width-edge_size, title_height-edge_size, edge_size, edge_size)
painter.setPen(Qt.NoPen)
painter.setBrush(self._brush_title)
painter.drawPath(path_title.simplified())
# content
path_content = QPainterPath()
path_content.setFillRule(Qt.WindingFill)
path_content.addRoundedRect(0,title_height, width, height - title_height, edge_size, edge_size)
path_content.addRect(0,title_height, edge_size, edge_size)
path_content.addRect(width-edge_size, title_height, edge_size, edge_size)
painter.setPen(Qt.NoPen)
painter.setBrush(self._brush_background)
painter.drawPath(path_content.simplified())
# outline
path_outline = QPainterPath()
path_outline.addRoundedRect(0, 0, width, height, edge_size, edge_size)
painter.setPen(self._pen_default if not self.isSelected() else self._pen_selected)
painter.setBrush(Qt.NoBrush)
painter.drawPath(path_outline.simplified())