Skip to content

Commit 16d6df2

Browse files
author
Bryan
committed
ports can now only have one connection at a time to them
1 parent b79b804 commit 16d6df2

File tree

4 files changed

+64
-46
lines changed

4 files changed

+64
-46
lines changed

node_editor/gui/connection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def __init__(self, parent):
2222
def delete(self):
2323
for port in (self._start_port, self._end_port):
2424
if port:
25-
port.remove_connection(self)
25+
# port.remove_connection(self)
26+
port.connection = None
2627
port = None
2728

2829
self.scene().removeItem(self)
@@ -38,12 +39,12 @@ def end_port(self):
3839
@start_port.setter
3940
def start_port(self, port):
4041
self._start_port = port
41-
self._start_port.add_connection(self)
42+
self._start_port.connection = self
4243

4344
@end_port.setter
4445
def end_port(self, port):
4546
self._end_port = port
46-
self._end_port.add_connection(self)
47+
self._end_port.connection = self
4748

4849
def nodes(self):
4950
return (self._start_port().node(), self._end_port().node())
@@ -54,7 +55,7 @@ def update_start_and_end_pos(self):
5455
Get the start and end ports and use them to set the start and end positions.
5556
"""
5657

57-
if not self.start_port.is_output():
58+
if self.start_port and not self.start_port.is_output():
5859
print("flipping connection")
5960
temp = self.end_port
6061
self._end_port = self.start_port

node_editor/gui/node.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,12 @@ def build(self):
158158

159159
def select_connections(self, value):
160160
for port in self._ports:
161-
for connection in port.connections():
162-
connection._do_highlight = value
163-
connection.update_path()
161+
if port.connection:
162+
port.connection._do_highlight = value
163+
port.connection.update_path()
164+
# for connection in port.connections():
165+
# connection._do_highlight = value
166+
# connection.update_path()
164167

165168
def contextMenuEvent(self, event):
166169
menu = QtWidgets.QMenu(self)
@@ -196,8 +199,8 @@ def delete(self):
196199
to_delete = []
197200

198201
for port in self._ports:
199-
for connection in port.connections():
200-
to_delete.append(connection)
202+
if port.connection:
203+
to_delete.append(port.connection)
201204

202205
for connection in to_delete:
203206
connection.delete()

node_editor/gui/node_editor.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class NodeEditor(QtCore.QObject):
99
def __init__(self, parent):
1010
super(NodeEditor, self).__init__(parent)
1111
self.connection = None
12+
self.port = None
1213
self.scene = None
1314
self._last_selected = None
1415

@@ -37,16 +38,19 @@ def eventFilter(self, watched, event):
3738
if isinstance(item, Port):
3839
self.connection = Connection(None)
3940
self.scene.addItem(self.connection)
40-
self.connection.start_port = item
41+
# self.connection.start_port = item
42+
self.port = item
4143
self.connection.start_pos = item.scenePos()
4244
self.connection.end_pos = event.scenePos()
4345
self.connection.update_path()
4446
return True
4547

4648
elif isinstance(item, Connection):
4749
self.connection = Connection(None)
50+
self.connection.start_pos = item.start_pos
4851
self.scene.addItem(self.connection)
49-
self.connection.start_port = item.start_port
52+
# self.connection.start_port = item.start_port
53+
self.port = item.start_port
5054
self.connection.end_pos = event.scenePos()
5155
self.connection.update_start_and_end_pos() # to fix the offset
5256
return True
@@ -94,28 +98,35 @@ def eventFilter(self, watched, event):
9498
elif event.type() == QtCore.QEvent.GraphicsSceneMouseRelease:
9599
if self.connection and event.button() == QtCore.Qt.LeftButton:
96100
item = self.item_at(event.scenePos())
101+
102+
# connecting a port
97103
if isinstance(item, Port):
98-
start_port = self.connection.start_port
99-
end_port = item
104+
if self.port.can_connect_to(item):
105+
print("Making connection")
100106

101-
if (
102-
start_port.node() != end_port.node()
103-
and start_port.is_output() != end_port.is_output()
104-
and not start_port.is_connected(end_port)
105-
):
106-
self.connection.end_port = end_port
107+
# delete existing connection on the new port
108+
if item.connection:
109+
item.connection.delete()
107110

108-
self.connection.update_start_and_end_pos()
109-
self.connection = None
111+
# delete existing connection to the original port
112+
self.port.clear_connection()
113+
item.clear_connection()
114+
115+
self.connection.start_port = self.port
110116

111-
# # Flip them aroud if wired the wrong way
112-
# if not self.connection.start_port.is_output():
113-
# self.connection.flip_connections()
117+
self.connection.end_port = item
114118

115-
return True
119+
self.connection.update_start_and_end_pos()
120+
self.connection = None
121+
else:
122+
print("Deleting connection")
123+
self.connection.delete()
124+
self.connection = None
116125

117-
self.connection.delete()
126+
if self.connection:
127+
self.connection.delete()
118128
self.connection = None
129+
self.port = None
119130
return True
120131

121132
return super(NodeEditor, self).eventFilter(watched, event)

node_editor/gui/port.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ def __init__(self, parent, scene):
2020

2121
self.port_text_height = self.font_metrics.height()
2222

23-
self.is_output_ = False
23+
self._is_output = False
2424
self._name = None
2525
self.margin = 2
2626

2727
self.m_node = None
28-
self.m_connections = []
28+
self.connection = None
2929

3030
self.text_path = QtGui.QPainterPath()
3131

3232
def set_is_output(self, is_output):
33-
self.is_output_ = is_output
33+
self._is_output = is_output
3434

3535
def set_name(self, name):
3636
self._name = name
3737
nice_name = self._name.replace("_", " ").title()
3838
self.port_text_width = self.font_metrics.width(nice_name)
3939

40-
if self.is_output_:
40+
if self._is_output:
4141
x = -self.radius_ - self.margin - self.port_text_width
4242
y = self.port_text_height / 4
4343

@@ -62,7 +62,7 @@ def name(self):
6262
return self._name
6363

6464
def is_output(self):
65-
return self.is_output_
65+
return self._is_output
6666

6767
def node(self):
6868
return self.m_node
@@ -76,27 +76,30 @@ def paint(self, painter, option=None, widget=None):
7676
painter.setBrush(QtCore.Qt.white)
7777
painter.drawPath(self.text_path)
7878

79-
def add_connection(self, connection):
80-
self.m_connections.append(connection)
79+
def clear_connection(self):
80+
if self.connection:
81+
self.connection.delete()
8182

82-
def remove_connection(self, connection):
83-
try:
84-
self.m_connections.remove(connection)
85-
except:
86-
pass
83+
def can_connect_to(self, port):
84+
print(port.node(), self.node())
85+
if not port:
86+
return False
87+
if port.node() == self.node():
88+
return False
8789

88-
def connections(self):
89-
return self.m_connections
90+
if self._is_output == port._is_output:
91+
return False
9092

91-
def is_connected(self, other):
92-
for connection in self.m_connections:
93-
if connection.start_port == other or connection.end_port == other:
94-
return True
93+
return True
94+
95+
def is_connected(self):
96+
if self.connection:
97+
return True
9598
return False
9699

97100
def itemChange(self, change, value):
98101
if change == QtWidgets.QGraphicsItem.ItemScenePositionHasChanged:
99-
for connection in self.m_connections:
100-
connection.update_start_and_end_pos()
102+
if self.connection:
103+
self.connection.update_start_and_end_pos()
101104

102105
return value

0 commit comments

Comments
 (0)