Skip to content

Commit bb7cad5

Browse files
committed
wip pipe slicer
1 parent d36393e commit bb7cad5

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

NodeGraphQt/base/graph.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def __repr__(self):
5959
def _wire_signals(self):
6060
# internal signals.
6161
self._viewer.search_triggered.connect(self._on_search_triggered)
62+
self._viewer.connection_sliced.connect(self._on_connection_sliced)
6263
self._viewer.connection_changed.connect(self._on_connection_changed)
6364
self._viewer.moved_nodes.connect(self._on_nodes_moved)
6465
self._viewer.node_double_clicked.connect(self._on_node_double_clicked)
@@ -188,6 +189,17 @@ def _on_connection_changed(self, disconnected, connected):
188189
port1.connect_to(port2)
189190
self._undo_stack.endMacro()
190191

192+
def _on_connection_sliced(self, ports):
193+
"""
194+
slot when connection pipes have been sliced.
195+
196+
Args:
197+
ports (list[list[widgets.port.PortItem]]):
198+
pair list of port connections (in port, out port)
199+
"""
200+
for in_port, out_port in ports:
201+
print(in_port, out_port)
202+
191203
@property
192204
def model(self):
193205
"""

NodeGraphQt/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
PIPE_DISABLED_COLOR = (190, 20, 20, 255)
1414
PIPE_ACTIVE_COLOR = (70, 255, 220, 255)
1515
PIPE_HIGHLIGHT_COLOR = (232, 184, 13, 255)
16+
PIPE_SLICER_COLOR = (140, 255, 170)
1617
#: The draw the connection pipes as straight lines.
1718
PIPE_LAYOUT_STRAIGHT = 0
1819
#: The draw the connection pipes as curved lines.

NodeGraphQt/widgets/viewer.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from NodeGraphQt.constants import (IN_PORT, OUT_PORT,
77
PIPE_LAYOUT_CURVED,
88
PIPE_LAYOUT_STRAIGHT,
9+
PIPE_SLICER_COLOR,
910
PIPE_STYLE_DASHED,
11+
Z_VAL_NODE_WIDGET,
1012
SCENE_AREA)
1113
from NodeGraphQt.qgraphics.node_abstract import AbstractNodeItem
1214
from NodeGraphQt.qgraphics.node_backdrop import BackdropNodeItem
@@ -30,6 +32,7 @@ class NodeViewer(QtWidgets.QGraphicsView):
3032

3133
moved_nodes = QtCore.Signal(dict)
3234
search_triggered = QtCore.Signal(str, tuple)
35+
connection_sliced = QtCore.Signal(list)
3336
connection_changed = QtCore.Signal(list, list)
3437

3538
# pass through signals
@@ -63,6 +66,14 @@ def __init__(self, parent=None):
6366
self._rubber_band = QtWidgets.QRubberBand(
6467
QtWidgets.QRubberBand.Rectangle, self
6568
)
69+
slicer_color = QtGui.QColor(*PIPE_SLICER_COLOR)
70+
slicer_pen = QtGui.QPen(slicer_color, 1.5, QtCore.Qt.DashLine)
71+
self._pipe_slicer = QtWidgets.QGraphicsLineItem()
72+
self._pipe_slicer.setPen(slicer_pen)
73+
self._pipe_slicer.setVisible(False)
74+
self._pipe_slicer.setZValue(Z_VAL_NODE_WIDGET + 2)
75+
self.scene().addItem(self._pipe_slicer)
76+
6677
self._undo_stack = QtWidgets.QUndoStack(self)
6778
self._context_menu = QtWidgets.QMenu('main', self)
6879
self._context_menu.setStyleSheet(STYLE_QMENU)
@@ -126,6 +137,15 @@ def _on_search_submitted(self, node_type):
126137
pos = self.mapToScene(self._previous_pos)
127138
self.search_triggered.emit(node_type, (pos.x(), pos.y()))
128139

140+
def _on_pipes_sliced(self, line):
141+
path = QtGui.QPainterPath()
142+
path.moveTo(line.p1())
143+
path.lineTo(line.p2())
144+
self.connection_sliced.emit([
145+
[i.input_port, i.output_port]
146+
for i in self.scene().items(path) if isinstance(i, Pipe)
147+
])
148+
129149
# --- reimplemented events ---
130150

131151
def resizeEvent(self, event):
@@ -138,6 +158,7 @@ def contextMenuEvent(self, event):
138158
def mousePressEvent(self, event):
139159
alt_modifier = event.modifiers() == QtCore.Qt.AltModifier
140160
shift_modifier = event.modifiers() == QtCore.Qt.ShiftModifier
161+
141162
if event.button() == QtCore.Qt.LeftButton:
142163
self.LMB_state = True
143164
elif event.button() == QtCore.Qt.RightButton:
@@ -152,10 +173,19 @@ def mousePressEvent(self, event):
152173
if self._search_widget.isVisible():
153174
self.tab_search_toggle()
154175

176+
# cursor pos.
177+
map_pos = self.mapToScene(event.pos())
178+
179+
# pipe slicer enabled.
180+
if event.modifiers() == (QtCore.Qt.AltModifier | QtCore.Qt.ShiftModifier):
181+
self._pipe_slicer.setLine(QtCore.QLineF(map_pos, map_pos))
182+
self._pipe_slicer.setVisible(True)
183+
return
184+
155185
if alt_modifier:
156186
return
157187

158-
items = self._items_near(self.mapToScene(event.pos()), None, 20, 20)
188+
items = self._items_near(map_pos, None, 20, 20)
159189
nodes = [i for i in items if isinstance(i, AbstractNodeItem)]
160190

161191
# toggle extend node selection.
@@ -188,6 +218,12 @@ def mouseReleaseEvent(self, event):
188218
elif event.button() == QtCore.Qt.MiddleButton:
189219
self.MMB_state = False
190220

221+
# hide pipe slicer.
222+
if self._pipe_slicer.isVisible():
223+
self._on_pipes_sliced(self._pipe_slicer.line())
224+
self._pipe_slicer.setLine(QtCore.QLineF(0.0, 0.0, 0.0, 0.0))
225+
self._pipe_slicer.setVisible(False)
226+
191227
# hide selection marquee
192228
if self._rubber_band.isVisible():
193229
rect = self._rubber_band.rect()
@@ -211,6 +247,15 @@ def mouseReleaseEvent(self, event):
211247
def mouseMoveEvent(self, event):
212248
alt_modifier = event.modifiers() == QtCore.Qt.AltModifier
213249
shift_modifier = event.modifiers() == QtCore.Qt.ShiftModifier
250+
if event.modifiers() == (QtCore.Qt.AltModifier | QtCore.Qt.ShiftModifier):
251+
if self.LMB_state:
252+
p1 = self._pipe_slicer.line().p1()
253+
p2 = self.mapToScene(self._previous_pos)
254+
self._pipe_slicer.setLine(QtCore.QLineF(p1, p2))
255+
self._previous_pos = event.pos()
256+
super(NodeViewer, self).mouseMoveEvent(event)
257+
return
258+
214259
if self.MMB_state and alt_modifier:
215260
pos_x = (event.x() - self._previous_pos.x())
216261
zoom = 0.1 if pos_x > 0 else -0.1
@@ -296,6 +341,10 @@ def sceneMousePressEvent(self, event):
296341
event (QtWidgets.QGraphicsScenePressEvent):
297342
The event handler from the QtWidgets.QGraphicsScene
298343
"""
344+
# pipe slicer enabled.
345+
if event.modifiers() == (QtCore.Qt.AltModifier | QtCore.Qt.ShiftModifier):
346+
return
347+
# viewer pan mode.
299348
if event.modifiers() == QtCore.Qt.AltModifier:
300349
return
301350

0 commit comments

Comments
 (0)