Skip to content

Commit 3394c75

Browse files
committed
push undo arg
1 parent b0c7be7 commit 3394c75

File tree

2 files changed

+83
-35
lines changed

2 files changed

+83
-35
lines changed

NodeGraphQt/base/node.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,14 @@ def get_property(self, name):
367367

368368
return self.model.get_property(name)
369369

370-
def set_property(self, name, value):
370+
def set_property(self, name, value, push_undo=True):
371371
"""
372372
Set the value on the node custom property.
373373
374374
Args:
375375
name (str): name of the property.
376376
value (object): property data (python built in types).
377+
push_undo (bool): register the command to the undo stack. (default: True)
377378
"""
378379

379380
# prevent signals from causing a infinite loop.
@@ -385,8 +386,11 @@ def set_property(self, name, value):
385386
self.NODE_NAME = value
386387

387388
if self.graph:
388-
undo_stack = self.graph.undo_stack()
389-
undo_stack.push(PropertyChangedCmd(self, name, value))
389+
if push_undo:
390+
undo_stack = self.graph.undo_stack()
391+
undo_stack.push(PropertyChangedCmd(self, name, value))
392+
else:
393+
PropertyChangedCmd(self, name, value).redo()
390394
else:
391395
if hasattr(self.view, name):
392396
setattr(self.view, name, value)

NodeGraphQt/base/port.py

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,32 @@ def unlock(self):
157157
"""
158158
self.set_locked(False, connected_ports=True)
159159

160-
def set_locked(self, state=False, connected_ports=True):
160+
def set_locked(self, state=False, connected_ports=True, push_undo=True):
161161
"""
162162
Sets the port locked state. When locked pipe connections can't be
163163
connected or disconnected from this port.
164164
165165
Args:
166166
state (Bool): port lock state.
167167
connected_ports (Bool): apply to lock state to connected ports.
168+
push_undo (bool): register the command to the undo stack. (default: True)
169+
168170
"""
169171
graph = self.node().graph
170172
undo_stack = graph.undo_stack()
171173
if state:
172-
undo_stack.push(PortLockedCmd(self))
174+
undo_cmd = PortLockedCmd(self)
175+
else:
176+
undo_cmd = PortUnlockedCmd(self)
177+
if push_undo:
178+
undo_stack.push(undo_cmd)
173179
else:
174-
undo_stack.push(PortUnlockedCmd(self))
180+
undo_cmd.redo()
175181
if connected_ports:
176182
for port in self.connected_ports():
177-
port.set_locked(state, connected_ports=False)
183+
port.set_locked(state,
184+
connected_ports=False,
185+
push_undo=push_undo)
178186

179187
def connected_ports(self):
180188
"""
@@ -194,13 +202,14 @@ def connected_ports(self):
194202
ports.append(node.inputs()[port_name])
195203
return ports
196204

197-
def connect_to(self, port=None):
205+
def connect_to(self, port=None, push_undo=True):
198206
"""
199207
Create connection to the specified port and emits the
200208
:attr:`NodeGraph.port_connected` signal from the parent node graph.
201209
202210
Args:
203211
port (NodeGraphQt.Port): port object.
212+
push_undo (bool): register the command to the undo stack. (default: True)
204213
"""
205214
if not port:
206215
return
@@ -216,8 +225,9 @@ def connect_to(self, port=None):
216225
graph = self.node().graph
217226
viewer = graph.viewer()
218227

219-
undo_stack = graph.undo_stack()
220-
undo_stack.beginMacro('connect port')
228+
if push_undo:
229+
undo_stack = graph.undo_stack()
230+
undo_stack.beginMacro('connect port')
221231

222232
pre_conn_port = None
223233
src_conn_ports = self.connected_ports()
@@ -226,41 +236,64 @@ def connect_to(self, port=None):
226236

227237
if not port:
228238
if pre_conn_port:
229-
undo_stack.push(PortDisconnectedCmd(self, port))
230-
undo_stack.push(NodeInputDisconnectedCmd(self, port))
239+
if push_undo:
240+
undo_stack.push(PortDisconnectedCmd(self, port))
241+
undo_stack.push(NodeInputDisconnectedCmd(self, port))
242+
undo_stack.endMacro()
243+
else:
244+
PortDisconnectedCmd(self, port).redo()
245+
NodeInputDisconnectedCmd(self, port).redo()
231246
return
232247

233248
if graph.acyclic() and viewer.acyclic_check(self.view, port.view):
234249
if pre_conn_port:
235-
undo_stack.push(PortDisconnectedCmd(self, pre_conn_port))
236-
undo_stack.push(NodeInputDisconnectedCmd(self, pre_conn_port))
250+
if push_undo:
251+
undo_stack.push(PortDisconnectedCmd(self, pre_conn_port))
252+
undo_stack.push(NodeInputDisconnectedCmd(
253+
self, pre_conn_port))
254+
undo_stack.endMacro()
255+
else:
256+
PortDisconnectedCmd(self, pre_conn_port).redo()
257+
NodeInputDisconnectedCmd(self, pre_conn_port).redo()
237258
return
238259

239260
trg_conn_ports = port.connected_ports()
240261
if not port.multi_connection() and trg_conn_ports:
241262
dettached_port = trg_conn_ports[0]
242-
undo_stack.push(PortDisconnectedCmd(port, dettached_port))
243-
undo_stack.push(NodeInputDisconnectedCmd(port, dettached_port))
263+
if push_undo:
264+
undo_stack.push(PortDisconnectedCmd(port, dettached_port))
265+
undo_stack.push(NodeInputDisconnectedCmd(port, dettached_port))
266+
else:
267+
PortDisconnectedCmd(port, dettached_port).redo()
268+
NodeInputDisconnectedCmd(port, dettached_port).redo()
244269
if pre_conn_port:
245-
undo_stack.push(PortDisconnectedCmd(self, pre_conn_port))
246-
undo_stack.push(NodeInputDisconnectedCmd(self, pre_conn_port))
247-
248-
undo_stack.push(PortConnectedCmd(self, port))
249-
undo_stack.push(NodeInputConnectedCmd(self, port))
250-
251-
undo_stack.endMacro()
270+
if push_undo:
271+
undo_stack.push(PortDisconnectedCmd(self, pre_conn_port))
272+
undo_stack.push(NodeInputDisconnectedCmd(self, pre_conn_port))
273+
else:
274+
PortDisconnectedCmd(self, pre_conn_port).redo()
275+
NodeInputDisconnectedCmd(self, pre_conn_port).redo()
276+
277+
if push_undo:
278+
undo_stack.push(PortConnectedCmd(self, port))
279+
undo_stack.push(NodeInputConnectedCmd(self, port))
280+
undo_stack.endMacro()
281+
else:
282+
PortConnectedCmd(self, port).redo()
283+
NodeInputConnectedCmd(self, port).redo()
252284

253285
# emit "port_connected" signal from the parent graph.
254286
ports = {p.type_(): p for p in [self, port]}
255287
graph.port_connected.emit(ports[IN_PORT], ports[OUT_PORT])
256288

257-
def disconnect_from(self, port=None):
289+
def disconnect_from(self, port=None, push_undo=True):
258290
"""
259291
Disconnect from the specified port and emits the
260292
:attr:`NodeGraph.port_disconnected` signal from the parent node graph.
261293
262294
Args:
263295
port (NodeGraphQt.Port): port object.
296+
push_undo (bool): register the command to the undo stack. (default: True)
264297
"""
265298
if not port:
266299
return
@@ -271,16 +304,20 @@ def disconnect_from(self, port=None):
271304
'Can\'t disconnect port because "{}" is locked.'.format(name))
272305

273306
graph = self.node().graph
274-
graph.undo_stack().beginMacro('disconnect port')
275-
graph.undo_stack().push(PortDisconnectedCmd(self, port))
276-
graph.undo_stack().push(NodeInputDisconnectedCmd(self, port))
277-
graph.undo_stack().endMacro()
307+
if push_undo:
308+
graph.undo_stack().beginMacro('disconnect port')
309+
graph.undo_stack().push(PortDisconnectedCmd(self, port))
310+
graph.undo_stack().push(NodeInputDisconnectedCmd(self, port))
311+
graph.undo_stack().endMacro()
312+
else:
313+
PortDisconnectedCmd(self, port).redo()
314+
NodeInputDisconnectedCmd(self, port).redo()
278315

279316
# emit "port_disconnected" signal from the parent graph.
280317
ports = {p.type_(): p for p in [self, port]}
281318
graph.port_disconnected.emit(ports[IN_PORT], ports[OUT_PORT])
282319

283-
def clear_connections(self):
320+
def clear_connections(self, push_undo=True):
284321
"""
285322
Disconnect from all port connections and emit the
286323
:attr:`NodeGraph.port_disconnected` signals from the node graph.
@@ -289,6 +326,9 @@ def clear_connections(self):
289326
:meth:`Port.disconnect_from`,
290327
:meth:`Port.connect_to`,
291328
:meth:`Port.connected_ports`
329+
330+
Args:
331+
push_undo (bool): register the command to the undo stack. (default: True)
292332
"""
293333
if self.locked():
294334
err = 'Can\'t clear connections because port "{}" is locked.'
@@ -297,12 +337,16 @@ def clear_connections(self):
297337
if not self.connected_ports():
298338
return
299339

300-
graph = self.node().graph
301-
undo_stack = graph.undo_stack()
302-
undo_stack.beginMacro('"{}" clear connections')
303-
for cp in self.connected_ports():
304-
self.disconnect_from(cp)
305-
undo_stack.endMacro()
340+
if push_undo:
341+
graph = self.node().graph
342+
undo_stack = graph.undo_stack()
343+
undo_stack.beginMacro('"{}" clear connections')
344+
for cp in self.connected_ports():
345+
self.disconnect_from(cp)
346+
undo_stack.endMacro()
347+
else:
348+
for cp in self.connected_ports():
349+
self.disconnect_from(cp, push_undo=False)
306350

307351
@property
308352
def color(self):

0 commit comments

Comments
 (0)