Skip to content

Commit 301db5f

Browse files
committed
remove pipe arro item & cleanup
1 parent 30fa259 commit 301db5f

File tree

2 files changed

+68
-97
lines changed

2 files changed

+68
-97
lines changed

NodeGraphQt/widgets/pipe.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@
1616
}
1717

1818

19-
class PipeArrow(QtWidgets.QGraphicsPolygonItem):
20-
"""
21-
Base pipe arrow item for indicating pipe direction.
22-
"""
23-
24-
def __init__(self, parent=None, size=5.0):
25-
super(PipeArrow, self).__init__(parent)
26-
self.set_size(size)
27-
28-
def set_size(self, size=5.0):
29-
triangle = QtGui.QPolygonF()
30-
triangle.append(QtCore.QPointF(-size, size))
31-
triangle.append(QtCore.QPointF(0.0, -size*2))
32-
triangle.append(QtCore.QPointF(size, size))
33-
self.setPolygon(triangle)
34-
35-
3619
class Pipe(QtWidgets.QGraphicsPathItem):
3720
"""
3821
Base Pipe item used for drawing node connections.
@@ -48,8 +31,11 @@ def __init__(self, input_port=None, output_port=None):
4831
self._highlight = False
4932
self._input_port = input_port
5033
self._output_port = output_port
51-
self.__arrow = PipeArrow(self)
52-
self.reset()
34+
size = 5.0
35+
self._arrow = QtGui.QPolygonF()
36+
self._arrow.append(QtCore.QPointF(-size, size))
37+
self._arrow.append(QtCore.QPointF(0.0, -size * 2))
38+
self._arrow.append(QtCore.QPointF(size, size))
5339

5440
def __str__(self):
5541
in_name = self._input_port.name if self._input_port else ''
@@ -88,6 +74,10 @@ def paint(self, painter, option, widget):
8874
pen_width = PIPE_WIDTH
8975
if self._active:
9076
color = QtGui.QColor(*PIPE_ACTIVE_COLOR)
77+
if pen_style == QtCore.Qt.DashDotDotLine:
78+
pen_width += 1
79+
else:
80+
pen_width += 0.35
9181
elif self._highlight:
9282
color = QtGui.QColor(*PIPE_HIGHLIGHT_COLOR)
9383
pen_style = PIPE_STYLES.get(PIPE_STYLE_DEFAULT)
@@ -106,6 +96,28 @@ def paint(self, painter, option, widget):
10696
painter.setRenderHint(painter.Antialiasing, True)
10797
painter.drawPath(self.path())
10898

99+
# draw arrow
100+
if self.input_port and self.output_port:
101+
transform = QtGui.QTransform()
102+
transform.translate(self.path().pointAtPercent(0.5).x(),
103+
self.path().pointAtPercent(0.5).y())
104+
loc_pt = self.path().pointAtPercent(0.49)
105+
tgt_pt = self.path().pointAtPercent(0.51)
106+
radians = math.atan2(tgt_pt.y() - loc_pt.y(),
107+
tgt_pt.x() - loc_pt.x())
108+
degrees = math.degrees(radians) - 90
109+
transform.rotate(degrees)
110+
111+
color.setAlpha(255)
112+
if self._highlight:
113+
painter.setBrush(QtGui.QBrush(color.lighter(150)))
114+
elif self._active or self.disabled():
115+
painter.setBrush(QtGui.QBrush(color.darker(200)))
116+
else:
117+
painter.setBrush(QtGui.QBrush(color))
118+
painter.setPen(QtGui.QPen(color, 0.6))
119+
painter.drawPolygon(transform.map(self._arrow))
120+
109121
painter.restore() # QPaintDevice: Cannot destroy paint device that is being painted
110122

111123
def draw_path(self, start_port, end_port, cursor_pos=None):
@@ -162,19 +174,6 @@ def draw_path(self, start_port, end_port, cursor_pos=None):
162174
path.cubicTo(ctr_point1, ctr_point2, pos2)
163175
self.setPath(path)
164176

165-
# draw arrow
166-
if start_port.port_type == IN_PORT:
167-
loc_pt = self.path().pointAtPercent(0.49)
168-
tgt_pt = self.path().pointAtPercent(0.51)
169-
else:
170-
loc_pt = self.path().pointAtPercent(0.51)
171-
tgt_pt = self.path().pointAtPercent(0.49)
172-
173-
radians = math.atan2(tgt_pt.y() - loc_pt.y(), tgt_pt.x() - loc_pt.x())
174-
degrees = math.degrees(radians) - 90
175-
self.__arrow.setPos(self.path().pointAtPercent(0.5))
176-
self.__arrow.setRotation(degrees)
177-
178177
def calc_distance(self, p1, p2):
179178
x = math.pow((p2.x() - p1.x()), 2)
180179
y = math.pow((p2.y() - p1.y()), 2)
@@ -199,10 +198,8 @@ def viewer_pipe_layout(self):
199198
def activate(self):
200199
self._active = True
201200
color = QtGui.QColor(*PIPE_ACTIVE_COLOR)
202-
pen = QtGui.QPen(color, 2, PIPE_STYLES.get(PIPE_STYLE_DEFAULT))
201+
pen = QtGui.QPen(color, 2.5, PIPE_STYLES.get(PIPE_STYLE_DEFAULT))
203202
self.setPen(pen)
204-
self.__arrow.setBrush(QtGui.QBrush(color.darker(200)))
205-
self.__arrow.setPen(QtGui.QPen(color, 0.8))
206203

207204
def active(self):
208205
return self._active
@@ -212,8 +209,6 @@ def highlight(self):
212209
color = QtGui.QColor(*PIPE_HIGHLIGHT_COLOR)
213210
pen = QtGui.QPen(color, 2, PIPE_STYLES.get(PIPE_STYLE_DEFAULT))
214211
self.setPen(pen)
215-
self.__arrow.setBrush(QtGui.QBrush(color.darker(200)))
216-
self.__arrow.setPen(QtGui.QPen(color, 0.8))
217212

218213
def highlighted(self):
219214
return self._highlight
@@ -224,8 +219,6 @@ def reset(self):
224219
color = QtGui.QColor(*self.color)
225220
pen = QtGui.QPen(color, 2, PIPE_STYLES.get(self.style))
226221
self.setPen(pen)
227-
self.__arrow.setBrush(QtGui.QBrush(color.darker(130)))
228-
self.__arrow.setPen(QtGui.QPen(color, 0.6))
229222

230223
def set_connections(self, port1, port2):
231224
ports = {

NodeGraphQt/widgets/viewer.py

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -299,63 +299,47 @@ def sceneMousePressEvent(self, event):
299299
event (QtWidgets.QGraphicsScenePressEvent):
300300
The event handler from the QtWidgets.QGraphicsScene
301301
"""
302-
alt_modifier = event.modifiers() == QtCore.Qt.AltModifier
303-
304-
# (NOTE) The `QtWidgets.QGraphicsSceneMouseEvent` class doesn't have
305-
# `setModifiers` in PyQt, and can't find a workaround for it.
306-
# So the only option to work with PyQt for now was to drop the modifier
307-
# remapping, but judging from the event passing flow and running a few
308-
# tests, it looks like those remapping code didn't have any affect to
309-
# the UI function.
310-
# Should be safe to comment out the code below.
311-
"""
312-
ctrl_modifier = event.modifiers() == QtCore.Qt.ControlModifier
313-
shift_modifier = event.modifiers() == QtCore.Qt.ShiftModifier
314-
if shift_modifier:
315-
event.setModifiers(QtCore.Qt.ControlModifier)
316-
elif ctrl_modifier:
317-
event.setModifiers(QtCore.Qt.ShiftModifier)
318-
"""
302+
if event.modifiers() == QtCore.Qt.AltModifier:
303+
return
319304

320-
if not alt_modifier:
321-
pos = event.scenePos()
322-
port_items = self._items_near(pos, PortItem, 5, 5)
323-
if port_items:
324-
port = port_items[0]
325-
if not port.multi_connection and port.connected_ports:
326-
self._detached_port = port.connected_ports[0]
327-
self.start_live_connection(port)
328-
if not port.multi_connection:
329-
[p.delete() for p in port.connected_pipes]
330-
return
305+
pos = event.scenePos()
306+
port_items = self._items_near(pos, PortItem, 5, 5)
307+
if port_items:
308+
port = port_items[0]
309+
if not port.multi_connection and port.connected_ports:
310+
self._detached_port = port.connected_ports[0]
311+
self.start_live_connection(port)
312+
if not port.multi_connection:
313+
[p.delete() for p in port.connected_pipes]
314+
return
331315

332-
node_items = self._items_near(pos, AbstractNodeItem, 3, 3)
333-
if node_items:
334-
node = node_items[0]
316+
node_items = self._items_near(pos, AbstractNodeItem, 3, 3)
317+
if node_items:
318+
node = node_items[0]
335319

336-
# record the node positions at selection time.
337-
for n in node_items:
338-
self._node_positions[n] = n.xy_pos
320+
# record the node positions at selection time.
321+
for n in node_items:
322+
self._node_positions[n] = n.xy_pos
339323

340-
# emit selected node id with LMB.
341-
if event.button() == QtCore.Qt.LeftButton:
342-
self.node_selected.emit(node.id)
324+
# emit selected node id with LMB.
325+
if event.button() == QtCore.Qt.LeftButton:
326+
self.node_selected.emit(node.id)
343327

344-
if not isinstance(node, BackdropNodeItem):
345-
return
328+
if not isinstance(node, BackdropNodeItem):
329+
return
346330

347-
pipe_items = self._items_near(pos, Pipe, 3, 3)
348-
if pipe_items:
349-
if not self.LMB_state:
350-
return
351-
pipe = pipe_items[0]
352-
attr = {IN_PORT: 'output_port', OUT_PORT: 'input_port'}
353-
from_port = pipe.port_from_pos(pos, True)
331+
pipe_items = self._items_near(pos, Pipe, 3, 3)
332+
if pipe_items:
333+
if not self.LMB_state:
334+
return
335+
pipe = pipe_items[0]
336+
attr = {IN_PORT: 'output_port', OUT_PORT: 'input_port'}
337+
from_port = pipe.port_from_pos(pos, True)
354338

355-
self._detached_port = getattr(pipe, attr[from_port.port_type])
356-
self.start_live_connection(from_port)
357-
self._live_pipe.draw_path(self._start_port, None, pos)
358-
pipe.delete()
339+
self._detached_port = getattr(pipe, attr[from_port.port_type])
340+
self.start_live_connection(from_port)
341+
self._live_pipe.draw_path(self._start_port, None, pos)
342+
pipe.delete()
359343

360344
def sceneMouseReleaseEvent(self, event):
361345
"""
@@ -366,14 +350,6 @@ def sceneMouseReleaseEvent(self, event):
366350
event (QtWidgets.QGraphicsSceneMouseEvent):
367351
The event handler from the QtWidgets.QGraphicsScene
368352
"""
369-
370-
# (NOTE) The `QtWidgets.QGraphicsSceneMouseEvent` class doesn't have
371-
# `setModifiers` in PyQt, please see the note above.
372-
"""
373-
if event.modifiers() == QtCore.Qt.ShiftModifier:
374-
event.setModifiers(QtCore.Qt.ControlModifier)
375-
"""
376-
377353
if not self._live_pipe:
378354
return
379355

@@ -480,6 +456,8 @@ def establish_connection(self, start_port, end_port):
480456
self.scene().addItem(pipe)
481457
pipe.set_connections(start_port, end_port)
482458
pipe.draw_path(pipe.input_port, pipe.output_port)
459+
if start_port.node.selected or end_port.node.selected:
460+
pipe.highlight()
483461

484462
def acyclic_check(self, start_port, end_port):
485463
"""

0 commit comments

Comments
 (0)