Skip to content

Commit 3977fa3

Browse files
authored
Merge pull request #338 from jchanvfx/pipe_drawing_updates
pipe optimizations.
2 parents c7e1fad + 16ffbe2 commit 3977fa3

File tree

5 files changed

+71
-14
lines changed

5 files changed

+71
-14
lines changed

NodeGraphQt/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class PipeEnum(Enum):
164164
#: default color.
165165
COLOR = (175, 95, 30, 255)
166166
#: pipe color to a node when it's disabled.
167-
DISABLED_COLOR = (190, 20, 20, 255)
167+
DISABLED_COLOR = (200, 60, 60, 255)
168168
#: pipe color when selected or mouse over.
169169
ACTIVE_COLOR = (70, 255, 220, 255)
170170
#: pipe color to a node when it's selected.

NodeGraphQt/nodes/base_node.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,16 @@ def set_property(self, name, value, push_undo=True):
8686
if self.graph:
8787
undo_cmd = NodeVisibleCmd(self, value)
8888
if push_undo:
89-
undo_stack = self.graph.undo_stack()
90-
undo_stack.push(undo_cmd)
89+
self.graph.undo_stack().push(undo_cmd)
9190
else:
9291
undo_cmd.redo()
9392
return
93+
elif name == 'disabled':
94+
# redraw the connected pipes in the scene.
95+
ports = self.view.inputs + self.view.outputs
96+
for port in ports:
97+
for pipe in port.connected_pipes:
98+
pipe.update()
9499
super(BaseNode, self).set_property(name, value, push_undo)
95100

96101
def set_layout_direction(self, value=0):

NodeGraphQt/pkg_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
__version__ = '0.5.11'
3+
__version__ = '0.5.12'
44
__status__ = 'Work in Progress'
55
__license__ = 'MIT'
66

NodeGraphQt/qgraphics/pipe.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ def hoverLeaveEvent(self, event):
7373

7474
def itemChange(self, change, value):
7575
if change == self.ItemSelectedChange and self.scene():
76-
self.reset()
7776
if value:
7877
self.highlight()
78+
else:
79+
self.reset()
7980
return super(PipeItem, self).itemChange(change, value)
8081

8182
def paint(self, painter, option, widget):
@@ -95,6 +96,7 @@ def paint(self, painter, option, widget):
9596
if not self._active:
9697
pen.setColor(QtGui.QColor(*PipeEnum.DISABLED_COLOR.value))
9798
pen.setStyle(PIPE_STYLES.get(PipeEnum.DRAW_TYPE_DOTTED.value))
99+
pen.setWidth(pen.width() * 1.25)
98100

99101
painter.setPen(pen)
100102
painter.setBrush(self.brush())
@@ -118,6 +120,14 @@ def _draw_direction_pointer(self):
118120
self._dir_pointer.setVisible(False)
119121
return
120122

123+
if self.disabled():
124+
if not (self._active or self._highlight):
125+
color = QtGui.QColor(*PipeEnum.DISABLED_COLOR.value)
126+
pen = self._dir_pointer.pen()
127+
pen.setColor(color)
128+
self._dir_pointer.setPen(pen)
129+
self._dir_pointer.setBrush(color.darker(200))
130+
121131
self._dir_pointer.setVisible(True)
122132
loc_pt = self.path().pointAtPercent(0.49)
123133
tgt_pt = self.path().pointAtPercent(0.51)
@@ -130,6 +140,11 @@ def _draw_direction_pointer(self):
130140
cen_x = self.path().pointAtPercent(0.5).x()
131141
cen_y = self.path().pointAtPercent(0.5).y()
132142
dist = math.hypot(tgt_pt.x() - cen_x, tgt_pt.y() - cen_y)
143+
144+
self._dir_pointer.setVisible(True)
145+
if dist < 0.3:
146+
self._dir_pointer.setVisible(False)
147+
return
133148
if dist < 1.0:
134149
self._dir_pointer.setScale(dist)
135150

@@ -285,6 +300,8 @@ def draw_path(self, start_port, end_port=None, cursor_pos=None):
285300
"""
286301
if not start_port:
287302
return
303+
304+
# get start / end positions.
288305
pos1 = start_port.scenePos()
289306
pos1.setX(pos1.x() + (start_port.boundingRect().width() / 2))
290307
pos1.setY(pos1.y() + (start_port.boundingRect().height() / 2))
@@ -352,6 +369,7 @@ def reset_path(self):
352369
"""
353370
path = QtGui.QPainterPath(QtCore.QPointF(0.0, 0.0))
354371
self.setPath(path)
372+
self._draw_direction_pointer()
355373

356374
def port_from_pos(self, pos, reverse=False):
357375
"""
@@ -451,8 +469,14 @@ def reset(self):
451469
self._active = False
452470
self._highlight = False
453471
self.set_pipe_styling(color=self.color, width=1.2, style=self.style)
472+
self._draw_direction_pointer()
454473

455474
def set_connections(self, port1, port2):
475+
"""
476+
Args:
477+
port1 (PortItem): port item object.
478+
port2 (PortItem): port item object.
479+
"""
456480
ports = {
457481
port1.port_type: port1,
458482
port2.port_type: port2
@@ -463,6 +487,10 @@ def set_connections(self, port1, port2):
463487
ports[PortTypeEnum.OUT.value].add_pipe(self)
464488

465489
def disabled(self):
490+
"""
491+
Returns:
492+
bool: true if pipe is a disabled connection.
493+
"""
466494
if self.input_port and self.input_port.node.disabled:
467495
return True
468496
if self.output_port and self.output_port.node.disabled:
@@ -525,12 +553,11 @@ def __init__(self):
525553
super(LivePipeItem, self).__init__()
526554
self.setZValue(Z_VAL_NODE_WIDGET + 1)
527555

528-
self.shift_selected = False
529-
530-
self._color = PipeEnum.ACTIVE_COLOR.value
531-
self._style = PipeEnum.DRAW_TYPE_DASHED.value
556+
self.color = PipeEnum.ACTIVE_COLOR.value
557+
self.style = PipeEnum.DRAW_TYPE_DASHED.value
558+
self.set_pipe_styling(color=self.color, width=2.8, style=self.style)
532559

533-
self.set_pipe_styling(color=self.color, width=2.5, style=self.style)
560+
self.shift_selected = False
534561

535562
self._idx_pointer = LivePipePolygonItem(self)
536563
self._idx_pointer.setPolygon(self._poly)
@@ -549,6 +576,13 @@ def __init__(self):
549576
font.setPointSize(7)
550577
self._idx_text.setFont(font)
551578

579+
def hoverEnterEvent(self, event):
580+
"""
581+
re-implemented back to the base default behaviour or the pipe will
582+
lose it styling when another pipe is selected.
583+
"""
584+
QtWidgets.QGraphicsPathItem.hoverEnterEvent(self, event)
585+
552586
def draw_path(self, start_port, end_port=None, cursor_pos=None,
553587
color_mode=None):
554588
"""

NodeGraphQt/widgets/viewer.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
PortTypeEnum,
1212
PipeLayoutEnum,
1313
ViewerEnum,
14-
Z_VAL_NODE_WIDGET
14+
Z_VAL_PIPE,
1515
)
1616
from NodeGraphQt.qgraphics.node_abstract import AbstractNodeItem
1717
from NodeGraphQt.qgraphics.node_backdrop import BackdropNodeItem
@@ -99,8 +99,9 @@ def __init__(self, parent=None, undo_stack=None):
9999
)))
100100
text_color.setAlpha(50)
101101
self._cusor_text = QtWidgets.QGraphicsTextItem()
102+
self._cusor_text.setFlag(self._cusor_text.ItemIsSelectable, False)
102103
self._cusor_text.setDefaultTextColor(text_color)
103-
self._cusor_text.setZValue(Z_VAL_NODE_WIDGET + 1)
104+
self._cusor_text.setZValue(Z_VAL_PIPE - 1)
104105
font = self._cusor_text.font()
105106
font.setPointSize(7)
106107
self._cusor_text.setFont(font)
@@ -468,7 +469,19 @@ def mousePressEvent(self, event):
468469
self._rubber_band.setGeometry(rect)
469470
self._rubber_band.isActive = True
470471

471-
if self.LMB_state and (self.SHIFT_state or self.CTRL_state):
472+
# stop here so we don't select a node.
473+
if self.CTRL_state:
474+
return
475+
476+
# allow new live pipe with the shift modifier on port that allow
477+
# for multi connection.
478+
if self.SHIFT_state and pipes:
479+
pipes[0].reset()
480+
port = pipes[0].port_from_pos(map_pos, reverse=True)
481+
if not port.locked and port.multi_connection:
482+
self._cusor_text.setPlainText('')
483+
self._cusor_text.setVisible(False)
484+
self.start_live_connection(port)
472485
return
473486

474487
if not self._LIVE_PIPE.isVisible():
@@ -567,7 +580,8 @@ def mouseMoveEvent(self, event):
567580

568581
if not self.ALT_state:
569582
if self.SHIFT_state or self.CTRL_state:
570-
self._cusor_text.setPos(self.mapToScene(event.pos()))
583+
if not self._LIVE_PIPE.isVisible():
584+
self._cusor_text.setPos(self.mapToScene(event.pos()))
571585

572586
if self.LMB_state and self._rubber_band.isActive:
573587
rect = QtCore.QRect(self._origin_pos, event.pos()).normalized()
@@ -673,6 +687,10 @@ def keyPressEvent(self, event):
673687
self.ALT_state = True
674688
self.SHIFT_state = True
675689

690+
if self._LIVE_PIPE.isVisible():
691+
super(NodeViewer, self).keyPressEvent(event)
692+
return
693+
676694
# show cursor text
677695
overlay_text = None
678696
self._cusor_text.setVisible(False)

0 commit comments

Comments
 (0)