Skip to content

Commit bd431af

Browse files
committed
implemented BaseNode.on_input_connected callback func.
1 parent 5794a1c commit bd431af

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

NodeGraphQt/base/graph.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from NodeGraphQt.base.port import Port
1717
from NodeGraphQt.constants import (DRAG_DROP_ID,
1818
PIPE_LAYOUT_CURVED,
19-
PIPE_LAYOUT_STRAIGHT, PIPE_LAYOUT_ANGLE)
19+
PIPE_LAYOUT_STRAIGHT,
20+
PIPE_LAYOUT_ANGLE,
21+
IN_PORT, OUT_PORT)
2022
from NodeGraphQt.widgets.viewer import NodeViewer
2123

2224

@@ -73,6 +75,9 @@ def _wire_signals(self):
7375
self._viewer.node_selected.connect(self._on_node_selected)
7476
self._viewer.data_dropped.connect(self._on_node_data_dropped)
7577

78+
# node events.
79+
self.port_connected.connect(self._on_port_connected)
80+
7681
def _toggle_tab_search(self):
7782
"""
7883
toggle the tab search widget.
@@ -96,6 +101,22 @@ def _on_property_bin_changed(self, node_id, prop_name, prop_value):
96101
if node.get_property(prop_name) != prop_value:
97102
node.set_property(prop_name, prop_value)
98103

104+
def _on_port_connected(self, src_port, tgt_port):
105+
"""
106+
called when a port connection has been made.
107+
(executes the "on_input_connected" method on the node.)
108+
109+
Args:
110+
src_port (NodeGraphQt.Port): source port.
111+
tgt_port (NodeGraphQt.Port): target port.
112+
"""
113+
if tgt_port.type_() == IN_PORT:
114+
node = tgt_port.node()
115+
node.on_input_connected(tgt_port, src_port)
116+
elif tgt_port.type_() == OUT_PORT:
117+
node = src_port.node()
118+
node.on_input_connected(src_port, tgt_port)
119+
99120
def _on_node_double_clicked(self, node_id):
100121
"""
101122
called when a node in the viewer is double click.
@@ -177,7 +198,7 @@ def _on_connection_changed(self, disconnected, connected):
177198
return
178199

179200
label = 'connect node(s)' if connected else 'disconnect node(s)'
180-
ptypes = {'in': 'inputs', 'out': 'outputs'}
201+
ptypes = {IN_PORT: 'inputs', OUT_PORT: 'outputs'}
181202

182203
self._undo_stack.beginMacro(label)
183204
for p1_view, p2_view in disconnected:
@@ -204,7 +225,7 @@ def _on_connection_sliced(self, ports):
204225
"""
205226
if not ports:
206227
return
207-
ptypes = {'in': 'inputs', 'out': 'outputs'}
228+
ptypes = {IN_PORT: 'inputs', OUT_PORT: 'outputs'}
208229
self._undo_stack.beginMacro('slice connections')
209230
for p1_view, p2_view in ports:
210231
node1 = self._model.nodes[p1_view.node.id]

NodeGraphQt/base/node.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class NodeObject(object):
3333
qgraphics_item (AbstractNodeItem): graphic item used for drawing.
3434
"""
3535

36-
#: (str) unique node identifier domain.
36+
#: (str) unique node identifier domain. (default: "nodeGraphQt.nodes")
3737
__identifier__ = 'nodeGraphQt.nodes'
3838

3939
#: (str) base node name.
@@ -602,6 +602,44 @@ def set_output(self, index, port):
602602
src_port = self.output(index)
603603
src_port.connect_to(port)
604604

605+
def connected_input_nodes(self):
606+
"""
607+
Returns all nodes connected from the input ports.
608+
609+
Returns:
610+
dict: {<input_port>: <node_list>}
611+
"""
612+
nodes = {}
613+
for p in self.input_ports():
614+
nodes[p] = [cp.node() for cp in p.connected_ports()]
615+
return nodes
616+
617+
def connected_output_nodes(self):
618+
"""
619+
Returns all nodes connected from the output ports.
620+
621+
Returns:
622+
dict: {<output_port>: <node_list>}
623+
"""
624+
nodes = {}
625+
for p in self.output_ports():
626+
nodes[p] = [cp.node() for cp in p.connected_ports()]
627+
return nodes
628+
629+
def on_input_connected(self, in_port, out_port):
630+
"""
631+
Callback function that is triggered when a OUTPUT port from another
632+
node has connected to a INPUT port for this node.
633+
634+
Note:
635+
re-implement this function if you require logic to run for this event.
636+
637+
Args:
638+
in_port (NodeGraphQt.Port): input port from this node.
639+
out_port (NodeGraphQt.Port): output port connecting to this node.
640+
"""
641+
return
642+
605643

606644
class BackdropNode(NodeObject):
607645
"""

NodeGraphQt/base/port.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def node(self):
6666
Return the parent node.
6767
6868
Returns:
69-
NodeGraphQt.NodeObject: parent node object.
69+
NodeGraphQt.BaseNode: parent node object.
7070
"""
7171
return self.model.node
7272

NodeGraphQt/constants.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
# === PIPE ===
1010

1111
PIPE_WIDTH = 1.2
12-
PIPE_STYLE_DEFAULT = 'line'
13-
PIPE_STYLE_DASHED = 'dashed'
14-
PIPE_STYLE_DOTTED = 'dotted'
12+
PIPE_STYLE_DEFAULT = 0
13+
PIPE_STYLE_DASHED = 1
14+
PIPE_STYLE_DOTTED = 2
1515
PIPE_DEFAULT_COLOR = (175, 95, 30, 255)
1616
PIPE_DISABLED_COLOR = (190, 20, 20, 255)
1717
PIPE_ACTIVE_COLOR = (70, 255, 220, 255)

0 commit comments

Comments
 (0)