Skip to content

Commit 508f907

Browse files
committed
port locking updates
1 parent 6b8f94c commit 508f907

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

NodeGraphQt/base/model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def to_dict(self):
3939
'name': 'port',
4040
'display_name': True,
4141
'multi_connection': False,
42+
'visible': True,
43+
'locked': False,
4244
'connected_ports': {<node_id>: [<port_name>, <port_name>]}
4345
}
4446
"""

NodeGraphQt/base/port.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
NodeInputDisconnectedCmd)
77
from .model import PortModel
88
from ..constants import IN_PORT, OUT_PORT
9+
from ..errors import PortError
910

1011

1112
class Port(object):
@@ -102,15 +103,6 @@ def visible(self):
102103
"""
103104
return self.model.visible
104105

105-
def locked(self):
106-
"""
107-
Port locked state.
108-
109-
Returns:
110-
bool: true if visible.
111-
"""
112-
return self.model.locked
113-
114106
def set_visible(self, visible=True):
115107
"""
116108
Sets weather the port should be visible or not.
@@ -129,14 +121,42 @@ def set_visible(self, visible=True):
129121
undo_stack.push(PortVisibleCmd(self))
130122
undo_stack.endMacro()
131123

132-
def set_locked(self, locked=False):
124+
def locked(self):
125+
"""
126+
Returns the locked state.
127+
128+
If ports are locked then new pipe connections can't be connected
129+
and current connected pipes can't be disconnected.
130+
131+
Returns:
132+
bool: true if locked.
133133
"""
134-
Sets the port locked state when locked new pipe connections can't be
135-
connected or disconnected from the node graph gui.
134+
return self.model.locked
135+
136+
def lock(self):
137+
"""
138+
Lock the port so new pipe connections can't be connected and
139+
current connected pipes can't be disconnected.
140+
141+
This is the same as calling :meth:`Port.set_locked` with the arg
142+
set to ``True``
143+
"""
144+
self.set_locked(True)
145+
146+
def unlock(self):
147+
"""
148+
Unlock the port so new pipe connections can be connected and
149+
existing connected pipes can be disconnected.
136150
137-
Note:
138-
pipes can still be connected/disconnected to locked ports through
139-
the api.
151+
This is the same as calling :meth:`Port.set_locked` with the arg
152+
set to ``False``
153+
"""
154+
self.set_locked(False)
155+
156+
def set_locked(self, locked=False):
157+
"""
158+
Sets the port locked state. When locked pipe connections can't be
159+
connected or disconnected from this port.
140160
141161
Args:
142162
locked (Bool): true if locked.
@@ -176,6 +196,11 @@ def connect_to(self, port=None):
176196
if self in port.connected_ports():
177197
return
178198

199+
if self.locked() or port.locked():
200+
name = [p.name() for p in [self, port] if p.locked()][0]
201+
raise PortError(
202+
'Can\'t connect port because "{}" is locked.'.format(name))
203+
179204
graph = self.node().graph
180205
viewer = graph.viewer()
181206

@@ -227,6 +252,12 @@ def disconnect_from(self, port=None):
227252
"""
228253
if not port:
229254
return
255+
256+
if self.locked() or port.locked():
257+
name = [p.name() for p in [self, port] if p.locked()][0]
258+
raise PortError(
259+
'Can\'t disconnect port because "{}" is locked.'.format(name))
260+
230261
graph = self.node().graph
231262
graph.undo_stack().beginMacro('disconnect port')
232263
graph.undo_stack().push(PortDisconnectedCmd(self, port))

NodeGraphQt/errors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ class NodeWidgetError(Exception): pass
1414
class NodeRegistrationError(Exception): pass
1515

1616

17+
class PortError(Exception): pass
18+
19+
1720
class PortRegistrationError(Exception): pass

NodeGraphQt/qgraphics/port.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ def paint(self, painter, option, widget):
316316
'multi_connection': self.multi_connection,
317317
'connected': bool(self.connected_pipes),
318318
'hovered': self.hovered,
319+
'locked': self.locked,
319320
}
320321
self._port_painter(painter, port_rect, port_info)
321322
else:

0 commit comments

Comments
 (0)