Skip to content

Commit 89d07db

Browse files
committed
emit port connection signals from undo #373
1 parent 4a645b4 commit 89d07db

File tree

2 files changed

+74
-37
lines changed

2 files changed

+74
-37
lines changed

NodeGraphQt/base/commands.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,14 @@ class PortConnectedCmd(QtWidgets.QUndoCommand):
276276
Args:
277277
src_port (NodeGraphQt.Port): source port.
278278
trg_port (NodeGraphQt.Port): target port.
279+
emit_signal (bool): emit port connection signals.
279280
"""
280281

281-
def __init__(self, src_port, trg_port):
282+
def __init__(self, src_port, trg_port, emit_signal):
282283
QtWidgets.QUndoCommand.__init__(self)
283284
self.source = src_port
284285
self.target = trg_port
286+
self.emit_signal = emit_signal
285287

286288
def undo(self):
287289
src_model = self.source.model
@@ -303,6 +305,13 @@ def undo(self):
303305

304306
self.source.view.disconnect_from(self.target.view)
305307

308+
# emit "port_disconnected" signal from the parent graph.
309+
if self.emit_signal:
310+
ports = {p.type_(): p for p in [self.source, self.target]}
311+
graph = self.source.node().graph
312+
graph.port_disconnected.emit(ports[PortTypeEnum.IN.value],
313+
ports[PortTypeEnum.OUT.value])
314+
306315
def redo(self):
307316
src_model = self.source.model
308317
trg_model = self.target.model
@@ -314,6 +323,13 @@ def redo(self):
314323

315324
self.source.view.connect_to(self.target.view)
316325

326+
# emit "port_connected" signal from the parent graph.
327+
if self.emit_signal:
328+
ports = {p.type_(): p for p in [self.source, self.target]}
329+
graph = self.source.node().graph
330+
graph.port_connected.emit(ports[PortTypeEnum.IN.value],
331+
ports[PortTypeEnum.OUT.value])
332+
317333

318334
class PortDisconnectedCmd(QtWidgets.QUndoCommand):
319335
"""
@@ -322,12 +338,14 @@ class PortDisconnectedCmd(QtWidgets.QUndoCommand):
322338
Args:
323339
src_port (NodeGraphQt.Port): source port.
324340
trg_port (NodeGraphQt.Port): target port.
341+
emit_signal (bool): emit port connection signals.
325342
"""
326343

327-
def __init__(self, src_port, trg_port):
344+
def __init__(self, src_port, trg_port, emit_signal):
328345
QtWidgets.QUndoCommand.__init__(self)
329346
self.source = src_port
330347
self.target = trg_port
348+
self.emit_signal = emit_signal
331349

332350
def undo(self):
333351
src_model = self.source.model
@@ -340,6 +358,13 @@ def undo(self):
340358

341359
self.source.view.connect_to(self.target.view)
342360

361+
# emit "port_connected" signal from the parent graph.
362+
if self.emit_signal:
363+
ports = {p.type_(): p for p in [self.source, self.target]}
364+
graph = self.source.node().graph
365+
graph.port_connected.emit(ports[PortTypeEnum.IN.value],
366+
ports[PortTypeEnum.OUT.value])
367+
343368
def redo(self):
344369
src_model = self.source.model
345370
trg_model = self.target.model
@@ -360,6 +385,13 @@ def redo(self):
360385

361386
self.source.view.disconnect_from(self.target.view)
362387

388+
# emit "port_disconnected" signal from the parent graph.
389+
if self.emit_signal:
390+
ports = {p.type_(): p for p in [self.source, self.target]}
391+
graph = self.source.node().graph
392+
graph.port_disconnected.emit(ports[PortTypeEnum.IN.value],
393+
ports[PortTypeEnum.OUT.value])
394+
363395

364396
class PortLockedCmd(QtWidgets.QUndoCommand):
365397
"""

NodeGraphQt/base/port.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,15 @@ def connected_ports(self):
210210
ports.append(node.inputs()[port_name])
211211
return ports
212212

213-
def connect_to(self, port=None, push_undo=True):
213+
def connect_to(self, port=None, push_undo=True, emit_signal=True):
214214
"""
215215
Create connection to the specified port and emits the
216216
:attr:`NodeGraph.port_connected` signal from the parent node graph.
217217
218218
Args:
219219
port (NodeGraphQt.Port): port object.
220220
push_undo (bool): register the command to the undo stack. (default: True)
221+
emit_signal (bool): emit the port connection signals. (default: True)
221222
"""
222223
if not port:
223224
return
@@ -274,64 +275,69 @@ def connect_to(self, port=None, push_undo=True):
274275
if not port:
275276
if pre_conn_port:
276277
if push_undo:
277-
undo_stack.push(PortDisconnectedCmd(self, port))
278+
undo_stack.push(
279+
PortDisconnectedCmd(self, port, emit_signal)
280+
)
278281
undo_stack.push(NodeInputDisconnectedCmd(self, port))
279282
undo_stack.endMacro()
280283
else:
281-
PortDisconnectedCmd(self, port).redo()
284+
PortDisconnectedCmd(self, port, emit_signal).redo()
282285
NodeInputDisconnectedCmd(self, port).redo()
283286
return
284287

285288
if graph.acyclic() and viewer.acyclic_check(self.view, port.view):
286289
if pre_conn_port:
287290
if push_undo:
288-
undo_stack.push(PortDisconnectedCmd(self, pre_conn_port))
291+
undo_stack.push(
292+
PortDisconnectedCmd(self, pre_conn_port, emit_signal)
293+
)
289294
undo_stack.push(NodeInputDisconnectedCmd(
290-
self, pre_conn_port))
295+
self, pre_conn_port)
296+
)
291297
undo_stack.endMacro()
292298
else:
293-
PortDisconnectedCmd(self, pre_conn_port).redo()
299+
PortDisconnectedCmd(self, pre_conn_port, emit_signal).redo()
294300
NodeInputDisconnectedCmd(self, pre_conn_port).redo()
295301
return
296302

297303
trg_conn_ports = port.connected_ports()
298304
if not port.multi_connection() and trg_conn_ports:
299305
dettached_port = trg_conn_ports[0]
300306
if push_undo:
301-
undo_stack.push(PortDisconnectedCmd(port, dettached_port))
307+
undo_stack.push(
308+
PortDisconnectedCmd(port, dettached_port, emit_signal)
309+
)
302310
undo_stack.push(NodeInputDisconnectedCmd(port, dettached_port))
303311
else:
304-
PortDisconnectedCmd(port, dettached_port).redo()
312+
PortDisconnectedCmd(port, dettached_port, emit_signal).redo()
305313
NodeInputDisconnectedCmd(port, dettached_port).redo()
306314
if pre_conn_port:
307315
if push_undo:
308-
undo_stack.push(PortDisconnectedCmd(self, pre_conn_port))
316+
undo_stack.push(
317+
PortDisconnectedCmd(self, pre_conn_port, emit_signal)
318+
)
309319
undo_stack.push(NodeInputDisconnectedCmd(self, pre_conn_port))
310320
else:
311-
PortDisconnectedCmd(self, pre_conn_port).redo()
321+
PortDisconnectedCmd(self, pre_conn_port, emit_signal).redo()
312322
NodeInputDisconnectedCmd(self, pre_conn_port).redo()
313323

314324
if push_undo:
315-
undo_stack.push(PortConnectedCmd(self, port))
325+
undo_stack.push(PortConnectedCmd(self, port, emit_signal))
316326
undo_stack.push(NodeInputConnectedCmd(self, port))
317327
undo_stack.endMacro()
318328
else:
319-
PortConnectedCmd(self, port).redo()
329+
PortConnectedCmd(self, port, emit_signal).redo()
320330
NodeInputConnectedCmd(self, port).redo()
321331

322-
# emit "port_connected" signal from the parent graph.
323-
ports = {p.type_(): p for p in [self, port]}
324-
graph.port_connected.emit(ports[PortTypeEnum.IN.value],
325-
ports[PortTypeEnum.OUT.value])
326-
327-
def disconnect_from(self, port=None, push_undo=True):
332+
def disconnect_from(self, port=None, push_undo=True, emit_signal=True):
328333
"""
329334
Disconnect from the specified port and emits the
330335
:attr:`NodeGraph.port_disconnected` signal from the parent node graph.
331336
332337
Args:
333338
port (NodeGraphQt.Port): port object.
334339
push_undo (bool): register the command to the undo stack. (default: True)
340+
emit_signal (bool): emit the port connection signals. (default: True)
335341
"""
336342
if not port:
337343
return
@@ -344,19 +350,14 @@ def disconnect_from(self, port=None, push_undo=True):
344350
graph = self.node().graph
345351
if push_undo:
346352
graph.undo_stack().beginMacro('disconnect port')
347-
graph.undo_stack().push(PortDisconnectedCmd(self, port))
353+
graph.undo_stack().push(PortDisconnectedCmd(self, port, emit_signal))
348354
graph.undo_stack().push(NodeInputDisconnectedCmd(self, port))
349355
graph.undo_stack().endMacro()
350356
else:
351-
PortDisconnectedCmd(self, port).redo()
357+
PortDisconnectedCmd(self, port, emit_signal).redo()
352358
NodeInputDisconnectedCmd(self, port).redo()
353359

354-
# emit "port_disconnected" signal from the parent graph.
355-
ports = {p.type_(): p for p in [self, port]}
356-
graph.port_disconnected.emit(ports[PortTypeEnum.IN.value],
357-
ports[PortTypeEnum.OUT.value])
358-
359-
def clear_connections(self, push_undo=True):
360+
def clear_connections(self, push_undo=True, emit_signal=True):
360361
"""
361362
Disconnect from all port connections and emit the
362363
:attr:`NodeGraph.port_disconnected` signals from the node graph.
@@ -368,6 +369,7 @@ def clear_connections(self, push_undo=True):
368369
369370
Args:
370371
push_undo (bool): register the command to the undo stack. (default: True)
372+
emit_signal (bool): emit the port connection signals. (default: True)
371373
"""
372374
if self.locked():
373375
err = 'Can\'t clear connections because port "{}" is locked.'
@@ -381,17 +383,20 @@ def clear_connections(self, push_undo=True):
381383
undo_stack = graph.undo_stack()
382384
undo_stack.beginMacro('"{}" clear connections')
383385
for cp in self.connected_ports():
384-
self.disconnect_from(cp)
386+
self.disconnect_from(cp, emit_signal=emit_signal)
385387
undo_stack.endMacro()
386-
else:
387-
for cp in self.connected_ports():
388-
self.disconnect_from(cp, push_undo=False)
388+
return
389+
390+
for cp in self.connected_ports():
391+
self.disconnect_from(
392+
cp, push_undo=False, emit_signal=emit_signal
393+
)
389394

390395
def add_accept_port_type(self, port_name, port_type, node_type):
391396
"""
392-
Add a constrain to "accept" a pipe connection.
397+
Add a constraint to "accept" a pipe connection.
393398
394-
Once a constrain has been added only ports of that type specified will
399+
Once a constraint has been added only ports of that type specified will
395400
be allowed a pipe connection.
396401
397402
`Implemented in` ``v0.6.0``
@@ -406,7 +411,7 @@ def add_accept_port_type(self, port_name, port_type, node_type):
406411
node_type (str): port node type.
407412
"""
408413
# storing the connection constrain at the graph level instead of the
409-
# port level so we don't serialize the same data for every port
414+
# port level, so we don't serialize the same data for every port
410415
# instance.
411416
self.node().add_accept_port_type(
412417
port=self,
@@ -432,9 +437,9 @@ def accepted_port_types(self):
432437

433438
def add_reject_port_type(self, port_name, port_type, node_type):
434439
"""
435-
Add a constrain to "reject" a pipe connection.
440+
Add a constraint to "reject" a pipe connection.
436441
437-
Once a constrain has been added only ports of that type specified will
442+
Once a constraint has been added only ports of that type specified will
438443
be rejected a pipe connection.
439444
440445
`Implemented in` ``v0.6.0``

0 commit comments

Comments
 (0)