Skip to content

Commit c491e45

Browse files
authored
Merge pull request #126 from jchanvfx/shift_extend_live_pipe
node viewer live pipe connection update.
2 parents 52fb62e + 8657d4e commit c491e45

File tree

4 files changed

+187
-64
lines changed

4 files changed

+187
-64
lines changed

NodeGraphQt/base/graph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ def set_pipe_style(self, style=None):
390390
Set node graph pipes to be drawn straight or curved by default
391391
all pipes are set curved. (default=0)
392392
393-
PIPE_LAYOUT_CURVED = 0
394-
PIPE_LAYOUT_STRAIGHT = 1
393+
``NodeGraphQt.constants.PIPE_LAYOUT_CURVED`` = 0
394+
``NodeGraphQt.constants.PIPE_LAYOUT_STRAIGHT`` = 1
395395
396396
Args:
397397
style (int): pipe style.

NodeGraphQt/qgraphics/pipe.py

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
PIPE_DEFAULT_COLOR, PIPE_ACTIVE_COLOR,
77
PIPE_HIGHLIGHT_COLOR, PIPE_DISABLED_COLOR,
88
PIPE_STYLE_DASHED, PIPE_STYLE_DEFAULT, PIPE_STYLE_DOTTED,
9-
PIPE_LAYOUT_STRAIGHT, PIPE_WIDTH, IN_PORT, OUT_PORT, Z_VAL_PIPE
9+
PIPE_LAYOUT_STRAIGHT, PIPE_WIDTH, IN_PORT, OUT_PORT, Z_VAL_PIPE,
10+
Z_VAL_NODE_WIDGET
1011
)
1112
from NodeGraphQt.qgraphics.port import PortItem
1213

@@ -130,7 +131,7 @@ def paint(self, painter, option, widget):
130131

131132
painter.restore() # QPaintDevice: Cannot destroy paint device that is being painted
132133

133-
def draw_path(self, start_port, end_port, cursor_pos=None):
134+
def draw_path(self, start_port, end_port=None, cursor_pos=None):
134135
"""
135136
Draws the path between ports.
136137
@@ -182,6 +183,10 @@ def draw_path(self, start_port, end_port, cursor_pos=None):
182183
path.cubicTo(ctr_point1, ctr_point2, pos2)
183184
self.setPath(path)
184185

186+
def reset_path(self):
187+
path = QtGui.QPainterPath(QtCore.QPointF(0.0, 0.0))
188+
self.setPath(path)
189+
185190
def calc_distance(self, p1, p2):
186191
x = math.pow((p2.x() - p1.x()), 2)
187192
y = math.pow((p2.y() - p1.y()), 2)
@@ -293,3 +298,76 @@ def delete(self):
293298
# TODO: not sure if we need this...?
294299
del self
295300

301+
302+
class LivePipe(Pipe):
303+
304+
def __init__(self):
305+
super(LivePipe, self).__init__()
306+
self.setZValue(Z_VAL_NODE_WIDGET + 1)
307+
self.shift_selected = False
308+
309+
def paint(self, painter, option, widget):
310+
"""
311+
Draws the connection line.
312+
313+
Args:
314+
painter (QtGui.QPainter): painter used for drawing the item.
315+
option (QtGui.QStyleOptionGraphicsItem):
316+
used to describe the parameters needed to draw.
317+
widget (QtWidgets.QWidget): not used.
318+
"""
319+
color = QtGui.QColor(*PIPE_ACTIVE_COLOR)
320+
pen_style = PIPE_STYLES.get(PIPE_STYLE_DASHED)
321+
pen_width = PIPE_WIDTH + 0.35
322+
323+
pen = QtGui.QPen(color, pen_width)
324+
pen.setStyle(pen_style)
325+
pen.setCapStyle(QtCore.Qt.RoundCap)
326+
327+
painter.save()
328+
painter.setPen(pen)
329+
painter.setRenderHint(painter.Antialiasing, True)
330+
painter.drawPath(self.path())
331+
332+
cen_x = self.path().pointAtPercent(0.5).x()
333+
cen_y = self.path().pointAtPercent(0.5).y()
334+
loc_pt = self.path().pointAtPercent(0.9)
335+
tgt_pt = self.path().pointAtPercent(1.0)
336+
337+
dist = math.hypot(tgt_pt.x() - cen_x, tgt_pt.y() - cen_y)
338+
if dist < 0.05:
339+
painter.restore()
340+
return
341+
342+
# draw circle
343+
size = 10.0
344+
if dist < 50.0:
345+
size *= (dist / 50.0)
346+
rect = QtCore.QRectF(cen_x-(size/2), cen_y-(size/2), size, size)
347+
painter.setBrush(color)
348+
painter.setPen(QtGui.QPen(color.darker(130), pen_width))
349+
painter.drawEllipse(rect)
350+
351+
# draw arrow
352+
color.setAlpha(255)
353+
painter.setBrush(color.darker(200))
354+
355+
pen_width = 0.6
356+
if dist < 1.0:
357+
pen_width *= 1.0 + dist
358+
painter.setPen(QtGui.QPen(color, pen_width))
359+
360+
transform = QtGui.QTransform()
361+
transform.translate(tgt_pt.x(), tgt_pt.y())
362+
363+
radians = math.atan2(tgt_pt.y() - loc_pt.y(),
364+
tgt_pt.x() - loc_pt.x())
365+
degrees = math.degrees(radians) + 90
366+
transform.rotate(degrees)
367+
368+
scale = 1.0
369+
if dist < 20.0:
370+
scale = dist / 20.0
371+
transform.scale(scale, scale)
372+
painter.drawPolygon(transform.map(self._arrow))
373+
painter.restore()

NodeGraphQt/qgraphics/port.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ def connected_ports(self):
166166
ports.append(getattr(pipe, port_types[self.port_type]))
167167
return ports
168168

169+
@property
170+
def hovered(self):
171+
return self._hovered
172+
173+
@hovered.setter
174+
def hovered(self, value=False):
175+
self._hovered = value
176+
169177
@property
170178
def node(self):
171179
return self.parentItem()

0 commit comments

Comments
 (0)