Skip to content

Commit b7ba289

Browse files
authored
Merge pull request #166 from jchanvfx/custom_port_shapes
Custom port shapes
2 parents 3382159 + 690e46e commit b7ba289

File tree

20 files changed

+621
-154
lines changed

20 files changed

+621
-154
lines changed

NodeGraphQt/base/node.py

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,12 @@ def __init__(self):
529529
self._view.text_item.editingFinished.connect(self.set_name)
530530

531531
def draw(self, force=True):
532+
"""
533+
Redraws the node in the scene.
534+
535+
Args:
536+
force (bool): force redraw if not visible.
537+
"""
532538
if force:
533539
if not self.model.visible:
534540
self._has_draw = False
@@ -544,14 +550,16 @@ def hide(self):
544550
Hide node.
545551
"""
546552
super(BaseNode, self).hide()
547-
[pipe.hide() for port in self._inputs + self._outputs for pipe in port.view.connected_pipes]
553+
[pipe.hide() for port in self._inputs + self._outputs
554+
for pipe in port.view.connected_pipes]
548555

549556
def show(self):
550557
"""
551558
Show node.
552559
"""
553560
super(BaseNode, self).show()
554-
[pipe.show() for port in self._inputs + self._outputs for pipe in port.view.connected_pipes]
561+
[pipe.show() for port in self._inputs + self._outputs
562+
for pipe in port.view.connected_pipes]
555563
self.draw(False)
556564

557565
def update_model(self):
@@ -755,7 +763,7 @@ def add_checkbox(self, name, label='', text='', state=False, tab=None):
755763
self.view.add_widget(widget)
756764

757765
def add_input(self, name='input', multi_input=False, display_name=True,
758-
color=None, data_type='None'):
766+
color=None, data_type='None', painter_func=None):
759767
"""
760768
Add input :class:`Port` to node.
761769
@@ -764,19 +772,26 @@ def add_input(self, name='input', multi_input=False, display_name=True,
764772
multi_input (bool): allow port to have more than one connection.
765773
display_name (bool): display the port name on the node.
766774
color (tuple): initial port color (r, g, b) ``0-255``.
767-
data_type(str): port data type name.
775+
data_type (str): port data type name.
776+
painter_func (function): custom function to override the drawing
777+
of the port shape see example: :ref:`Creating Custom Shapes`
768778
769779
Returns:
770780
NodeGraphQt.Port: the created port object.
771781
"""
772782
if name in self.inputs().keys():
773783
raise PortRegistrationError(
774784
'port name "{}" already registered.'.format(name))
775-
view = self.view.add_input(name, multi_input, display_name)
785+
786+
port_args = [name, multi_input, display_name]
787+
if painter_func and callable(painter_func):
788+
port_args.append(painter_func)
789+
view = self.view.add_input(*port_args)
776790

777791
if color:
778792
view.color = color
779793
view.border_color = [min([255, max([0, i + 80])]) for i in color]
794+
780795
port = Port(self, view)
781796
port.model.type_ = IN_PORT
782797
port.model.name = name
@@ -788,7 +803,7 @@ def add_input(self, name='input', multi_input=False, display_name=True,
788803
return port
789804

790805
def add_output(self, name='output', multi_output=True, display_name=True,
791-
color=None, data_type='None'):
806+
color=None, data_type='None', painter_func=None):
792807
"""
793808
Add output :class:`Port` to node.
794809
@@ -798,14 +813,21 @@ def add_output(self, name='output', multi_output=True, display_name=True,
798813
display_name (bool): display the port name on the node.
799814
color (tuple): initial port color (r, g, b) ``0-255``.
800815
data_type(str): port data type name.
816+
painter_func (function): custom function to override the drawing
817+
of the port shape see example: :ref:`Creating Custom Shapes`
801818
802819
Returns:
803820
NodeGraphQt.Port: the created port object.
804821
"""
805822
if name in self.outputs().keys():
806823
raise PortRegistrationError(
807824
'port name "{}" already registered.'.format(name))
808-
view = self.view.add_output(name, multi_output, display_name)
825+
826+
port_args = [name, multi_output, display_name]
827+
if painter_func and callable(painter_func):
828+
port_args.append(painter_func)
829+
view = self.view.add_output(*port_args)
830+
809831
if color:
810832
view.color = color
811833
view.border_color = [min([255, max([0, i + 80])]) for i in color]
@@ -914,9 +936,25 @@ def set_ports(self, port_data):
914936
Set node input and output ports.
915937
916938
Args:
917-
port_data(dict): {'input_ports':[{'name':...,'multi_connection':...,'display_name':...,'data_type':...}, ...],
918-
" 'output_ports':[{'name':...,'multi_connection':...,'display_name':...,'data_type':...}, ...]}
919-
"""
939+
port_data(dict): port data.
940+
"""
941+
942+
# port data eg.
943+
# {
944+
# 'input_ports':
945+
# [{'name': ...,
946+
# 'multi_connection': ...,
947+
# 'display_name': ...,
948+
# 'data_type': ...
949+
# }, ...],
950+
# 'output_ports':
951+
# [{'name': ...,
952+
# 'multi_connection': ...,
953+
# 'display_name': ...,
954+
# 'data_type': ...
955+
# }, ...]
956+
# }
957+
920958
for port in self._inputs:
921959
self._view.delete_input(port.view)
922960
port.model.node = None
@@ -927,11 +965,15 @@ def set_ports(self, port_data):
927965
self._outputs = []
928966
self._model.outputs = {}
929967
self._model.inputs = {}
930-
[self.add_input(name=port['name'], multi_input=port['multi_connection'],
931-
display_name=port['display_name'], data_type=port['data_type'])
968+
[self.add_input(name=port['name'],
969+
multi_input=port['multi_connection'],
970+
display_name=port['display_name'],
971+
data_type=port['data_type'])
932972
for port in port_data['input_ports']]
933-
[self.add_output(name=port['name'], multi_output=port['multi_connection'],
934-
display_name=port['display_name'], data_type=port['data_type'])
973+
[self.add_output(name=port['name'],
974+
multi_output=port['multi_connection'],
975+
display_name=port['display_name'],
976+
data_type=port['data_type'])
935977
for port in port_data['output_ports']]
936978
self.draw()
937979

@@ -1196,6 +1238,12 @@ class SubGraph(object):
11961238
"""
11971239
The ``NodeGraphQt.SubGraph`` class is the base class that all
11981240
Sub Graph Node inherit from.
1241+
1242+
*Implemented on NodeGraphQt: * ``v0.1.0``
1243+
1244+
.. image:: _images/example_subgraph.gif
1245+
:width: 80%
1246+
11991247
"""
12001248

12011249
def __init__(self):
@@ -1210,8 +1258,9 @@ def children(self):
12101258
def create_from_nodes(self, nodes):
12111259
"""
12121260
Create sub graph from the nodes.
1261+
12131262
Args:
1214-
nodes(list): nodes to create the sub graph.
1263+
nodes (list[NodeGraphQt.NodeObject]): nodes to create the sub graph.
12151264
"""
12161265
if self in nodes:
12171266
nodes.remove(self)
@@ -1220,16 +1269,18 @@ def create_from_nodes(self, nodes):
12201269
def add_child(self, node):
12211270
"""
12221271
Add a node to the sub graph.
1272+
12231273
Args:
1224-
node(NodeGraphQt.BaseNode).
1274+
node (NodeGraphQt.BaseNode): node object.
12251275
"""
12261276
self._children.add(node)
12271277

12281278
def remove_child(self, node):
12291279
"""
12301280
Remove a node from the sub graph.
1281+
12311282
Args:
1232-
node(NodeGraphQt.BaseNode).
1283+
node (NodeGraphQt.BaseNode): node object.
12331284
"""
12341285
if node in self._children:
12351286
self._children.remove(node)

NodeGraphQt/base/port.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/python
22
from .commands import (PortConnectedCmd,
3-
PortDisconnectedCmd,
4-
PortVisibleCmd,
5-
NodeInputConnectedCmd,
6-
NodeInputDisconnectedCmd)
3+
PortDisconnectedCmd,
4+
PortVisibleCmd,
5+
NodeInputConnectedCmd,
6+
NodeInputDisconnectedCmd)
77
from .model import PortModel
88
from ..constants import IN_PORT, OUT_PORT
99

NodeGraphQt/base/utils.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,12 @@ def topological_sort_by_down(start_nodes=None, all_nodes=None):
503503
'start_nodes' and 'all_nodes' only one needs to be given.
504504
505505
Args:
506-
start_nodes (list[NodeGraphQt.BaseNode])(Optional): the start update nodes of the graph.
507-
all_nodes (list[NodeGraphQt.BaseNode])(Optional): if 'start_nodes' is None the function can calculate start nodes from 'all_nodes'.
506+
start_nodes (list[NodeGraphQt.BaseNode]):
507+
(Optional) the start update nodes of the graph.
508+
all_nodes (list[NodeGraphQt.BaseNode]):
509+
(Optional) if 'start_nodes' is None the function can calculate
510+
start nodes from 'all_nodes'.
511+
508512
Returns:
509513
list[NodeGraphQt.BaseNode]: sorted nodes.
510514
"""
@@ -533,8 +537,11 @@ def topological_sort_by_up(start_nodes=None, all_nodes=None):
533537
'start_nodes' and 'all_nodes' only one needs to be given.
534538
535539
Args:
536-
start_nodes (list[NodeGraphQt.BaseNode])(Optional): the end update nodes of the graph.
537-
all_nodes (list[NodeGraphQt.BaseNode])(Optional): if 'start_nodes' is None the function can calculate start nodes from 'all_nodes'.
540+
start_nodes (list[NodeGraphQt.BaseNode]):
541+
(Optional) the end update nodes of the graph.
542+
all_nodes (list[NodeGraphQt.BaseNode]):
543+
(Optional) if 'start_nodes' is None the function can calculate
544+
start nodes from 'all_nodes'.
538545
Returns:
539546
list[NodeGraphQt.BaseNode]: sorted nodes.
540547
"""
@@ -636,11 +643,12 @@ def _compute_rank_down(start_nodes):
636643
Compute the rank of the down stream nodes.
637644
638645
Args:
639-
start_nodes (list[NodeGraphQt.BaseNode])(Optional): the start nodes of the graph.
646+
start_nodes (list[NodeGraphQt.BaseNode]):
647+
(Optional) the start nodes of the graph.
648+
640649
Returns:
641650
dict{NodeGraphQt.BaseNode: node_rank, ...}
642651
"""
643-
644652
nodes_rank = {}
645653
for node in start_nodes:
646654
nodes_rank[node] = 0
@@ -663,7 +671,9 @@ def _compute_rank_up(start_nodes):
663671
Compute the rank of the up stream nodes.
664672
665673
Args:
666-
start_nodes (list[NodeGraphQt.BaseNode])(Optional): the end nodes of the graph.
674+
start_nodes (list[NodeGraphQt.BaseNode]):
675+
(Optional) the end nodes of the graph.
676+
667677
Returns:
668678
dict{NodeGraphQt.BaseNode: node_rank, ...}
669679
"""
@@ -680,8 +690,11 @@ def auto_layout_up(start_nodes=None, all_nodes=None):
680690
Auto layout the nodes by up stream direction.
681691
682692
Args:
683-
start_nodes (list[NodeGraphQt.BaseNode])(Optional): the end nodes of the graph.
684-
all_nodes (list[NodeGraphQt.BaseNode])(Optional): if 'start_nodes' is None the function can calculate start nodes from 'all_nodes'.
693+
start_nodes (list[NodeGraphQt.BaseNode]):
694+
(Optional) the end nodes of the graph.
695+
all_nodes (list[NodeGraphQt.BaseNode]):
696+
(Optional) if 'start_nodes' is None the function can calculate
697+
start nodes from 'all_nodes'.
685698
"""
686699
if not start_nodes and not all_nodes:
687700
return
@@ -741,8 +754,11 @@ def auto_layout_down(start_nodes=None, all_nodes=None):
741754
Auto layout the nodes by down stream direction.
742755
743756
Args:
744-
start_nodes (list[NodeGraphQt.BaseNode])(Optional): the start update nodes of the graph.
745-
all_nodes (list[NodeGraphQt.BaseNode])(Optional): if 'start_nodes' is None the function can calculate start nodes from 'all_nodes'.
757+
start_nodes (list[NodeGraphQt.BaseNode]):
758+
(Optional) the start update nodes of the graph.
759+
all_nodes (list[NodeGraphQt.BaseNode]):
760+
(Optional) if 'start_nodes' is None the function can calculate
761+
start nodes from 'all_nodes'.
746762
"""
747763
if not start_nodes and not all_nodes:
748764
return

0 commit comments

Comments
 (0)