Skip to content

Commit 9a1e3bf

Browse files
committed
group node tweaks
1 parent 313fe61 commit 9a1e3bf

File tree

6 files changed

+141
-16
lines changed

6 files changed

+141
-16
lines changed

NodeGraphQt/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__(self):
7676
from .pkg_info import __license__ as LICENSE
7777

7878
# node graph
79-
from .base.graph import NodeGraph
79+
from .base.graph import NodeGraph, SubGraph
8080
from .base.menu import NodesMenu, NodeGraphMenu, NodeGraphCommand
8181

8282
# nodes & ports
@@ -109,6 +109,7 @@ def __init__(self):
109109
'NodesMenu',
110110
'Port',
111111
'PropertiesBinWidget',
112+
'SubGraph',
112113
'VERSION',
113114
'constants',
114115
'custom_widgets'

NodeGraphQt/base/graph.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,18 +1947,30 @@ def _build_port_nodes(self):
19471947
input_node.add_output(port.name())
19481948
input_nodes[port.name()] = input_node
19491949
self.add_node(input_node, selected=False, push_undo=False)
1950+
x, y = input_node.pos()
1951+
if NODE_LAYOUT_DIRECTION is NODE_LAYOUT_HORIZONTAL:
1952+
x -= 100
1953+
elif NODE_LAYOUT_DIRECTION is NODE_LAYOUT_VERTICAL:
1954+
y -= 100
1955+
input_node.set_property('pos', [x, y], push_undo=False)
19501956

19511957
# build the parent output port nodes.
19521958
output_nodes = {n.name(): n for n in
19531959
self.get_nodes_by_type(PortOutputNode.type_)}
19541960
for port in self.node.output_ports():
19551961
if port.name() not in output_nodes:
1956-
output_port = PortOutputNode(parent_port=port)
1957-
output_port.NODE_NAME = port.name()
1958-
output_port.model.set_property('name', port.name())
1959-
output_port.add_input(port.name())
1960-
output_nodes[port.name()] = output_port
1961-
self.add_node(output_port, selected=False, push_undo=False)
1962+
output_node = PortOutputNode(parent_port=port)
1963+
output_node.NODE_NAME = port.name()
1964+
output_node.model.set_property('name', port.name())
1965+
output_node.add_input(port.name())
1966+
output_nodes[port.name()] = output_node
1967+
self.add_node(output_node, selected=False, push_undo=False)
1968+
x, y = output_node.pos()
1969+
if NODE_LAYOUT_DIRECTION is NODE_LAYOUT_HORIZONTAL:
1970+
x += 100
1971+
elif NODE_LAYOUT_DIRECTION is NODE_LAYOUT_VERTICAL:
1972+
y += 100
1973+
output_node.set_property('pos', [x, y], push_undo=False)
19621974

19631975
return input_nodes, output_nodes
19641976

NodeGraphQt/nodes/group_node.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
NODE_LAYOUT_HORIZONTAL)
44

55
from NodeGraphQt.nodes.base_node import BaseNode
6+
from NodeGraphQt.nodes.port_node import PortInputNode, PortOutputNode
67
from NodeGraphQt.qgraphics.node_group import (GroupNodeItem,
78
GroupNodeVerticalItem)
89

@@ -14,6 +15,9 @@ class GroupNode(BaseNode):
1415
inside of it.
1516
1617
**Inherited from:** :class:`NodeGraphQt.BaseNode`
18+
19+
.. image:: ../_images/group_node.png
20+
:width: 250px
1721
"""
1822

1923
NODE_NAME = 'Group'
@@ -35,11 +39,14 @@ def is_expanded(self):
3539
Returns:
3640
bool: true if the node is expanded.
3741
"""
42+
if not self.graph:
43+
return False
3844
return bool(self.id in self.graph.sub_graphs)
3945

4046
def get_sub_graph(self):
4147
"""
42-
Returns the initialized sub node graph controller to the group node.
48+
Returns the sub graph controller to the group node if initialized
49+
or returns None.
4350
4451
Returns:
4552
NodeGraphQt.SubGraph or None: sub graph controller.
@@ -84,3 +91,71 @@ def collapse(self):
8491
:meth:`SubGraph.collapse_group_node`.
8592
"""
8693
self.graph.collapse_group_node(self)
94+
95+
def add_input(self, name='input', multi_input=False, display_name=True,
96+
color=None, locked=False, painter_func=None):
97+
port = super(GroupNode, self).add_input(
98+
name=name,
99+
multi_input=multi_input,
100+
display_name=display_name,
101+
color=color,
102+
locked=locked,
103+
painter_func=painter_func
104+
)
105+
if self.is_expanded:
106+
input_node = PortInputNode(parent_port=port)
107+
input_node.NODE_NAME = port.name()
108+
input_node.model.set_property('name', port.name())
109+
input_node.add_output(port.name())
110+
sub_graph = self.get_sub_graph()
111+
sub_graph.add_node(input_node, selected=False, push_undo=False)
112+
113+
return port
114+
115+
def add_output(self, name='output', multi_output=True, display_name=True,
116+
color=None, locked=False, painter_func=None):
117+
port = super(GroupNode, self).add_output(
118+
name=name,
119+
multi_output=multi_output,
120+
display_name=display_name,
121+
color=color,
122+
locked=locked,
123+
painter_func=painter_func
124+
)
125+
if self.is_expanded:
126+
output_port = PortOutputNode(parent_port=port)
127+
output_port.NODE_NAME = port.name()
128+
output_port.model.set_property('name', port.name())
129+
output_port.add_input(port.name())
130+
sub_graph = self.get_sub_graph()
131+
sub_graph.add_node(output_port, selected=False, push_undo=False)
132+
133+
return port
134+
135+
def delete_input(self, port):
136+
if type(port) in [int, str]:
137+
port = self.get_output(port)
138+
if port is None:
139+
return
140+
141+
if self.is_expanded:
142+
sub_graph = self.get_sub_graph()
143+
port_node = sub_graph.get_node_by_port(port)
144+
if port_node:
145+
sub_graph.remove_node(port_node, push_undo=False)
146+
147+
super(GroupNode, self).delete_input(port)
148+
149+
def delete_output(self, port):
150+
if type(port) in [int, str]:
151+
port = self.get_output(port)
152+
if port is None:
153+
return
154+
155+
if self.is_expanded:
156+
sub_graph = self.get_sub_graph()
157+
port_node = sub_graph.get_node_by_port(port)
158+
if port_node:
159+
sub_graph.remove_node(port_node, push_undo=False)
160+
161+
super(GroupNode, self).delete_output(port)

NodeGraphQt/nodes/port_node.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from NodeGraphQt.constants import (NODE_LAYOUT_VERTICAL,
33
NODE_LAYOUT_HORIZONTAL)
44

5+
from NodeGraphQt.errors import PortRegistrationError
56
from NodeGraphQt.nodes.base_node import BaseNode
67
from NodeGraphQt.qgraphics.node_port_in import (PortInputNodeItem,
78
PortInputNodeVerticalItem)
@@ -16,6 +17,10 @@ class PortInputNode(BaseNode):
1617
:class:`NodeGraphQt.SubGraph`.
1718
1819
**Inherited from:** :class:`NodeGraphQt.BaseNode`
20+
21+
.. image:: ../_images/port_in_node.png
22+
:width: 150px
23+
1924
"""
2025

2126
NODE_NAME = 'InputPort'
@@ -40,12 +45,21 @@ def parent_port(self):
4045

4146
def add_input(self, name='input', multi_input=False, display_name=True,
4247
color=None, locked=False, painter_func=None):
43-
return
48+
"""
49+
This is not available for the `PortInputNode` class.
50+
"""
51+
raise PortRegistrationError(
52+
'"{}.add_input()" is not available for {}.'
53+
.format(self.__class__.__name__, self)
54+
)
4455

4556
def add_output(self, name='output', multi_output=True, display_name=True,
4657
color=None, locked=False, painter_func=None):
4758
if self._outputs:
48-
return
59+
raise PortRegistrationError(
60+
'"{}.add_output()" only ONE output is allowed for this node.'
61+
.format(self.__class__.__name__, self)
62+
)
4963
super(PortInputNode, self).add_output(
5064
name=name,
5165
multi_output=multi_output,
@@ -63,6 +77,10 @@ class PortOutputNode(BaseNode):
6377
:class:`NodeGraphQt.SubGraph`.
6478
6579
**Inherited from:** :class:`NodeGraphQt.BaseNode`
80+
81+
.. image:: ../_images/port_out_node.png
82+
:width: 150px
83+
6684
"""
6785

6886
NODE_NAME = 'OutputPort'
@@ -88,7 +106,10 @@ def parent_port(self):
88106
def add_input(self, name='input', multi_input=False, display_name=True,
89107
color=None, locked=False, painter_func=None):
90108
if self._inputs:
91-
return
109+
raise PortRegistrationError(
110+
'"{}.add_input()" only ONE input is allowed for this node.'
111+
.format(self.__class__.__name__, self)
112+
)
92113
super(PortOutputNode, self).add_input(
93114
name=name,
94115
multi_input=multi_input,
@@ -100,4 +121,10 @@ def add_input(self, name='input', multi_input=False, display_name=True,
100121

101122
def add_output(self, name='output', multi_output=True, display_name=True,
102123
color=None, locked=False, painter_func=None):
103-
return
124+
"""
125+
This is not available for the `PortOutputNode` class.
126+
"""
127+
raise PortRegistrationError(
128+
'"{}.add_output()" is not available for {}.'
129+
.format(self.__class__.__name__, self)
130+
)

NodeGraphQt/qgraphics/node_port_in.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def paint(self, painter, option, widget):
5656
text_rect.height()
5757
)
5858

59+
painter.setBrush(QtGui.QColor(255, 255, 255, 20))
60+
painter.drawRoundedRect(rect, 20, 20)
61+
5962
painter.setBrush(QtGui.QColor(0, 0, 0, 100))
6063
painter.drawRoundedRect(text_rect, 3, 3)
6164

@@ -81,8 +84,10 @@ def paint(self, painter, option, widget):
8184
painter.setPen(pen)
8285
painter.drawPolygon(poly)
8386

84-
edge_rect = QtCore.QRectF(
85-
rect.width() - (size * 1.8), rect.center().y() - 15, 4, 30)
87+
edge_size = 30
88+
edge_rect = QtCore.QRectF(rect.width() - (size * 1.7),
89+
rect.center().y() - (edge_size / 2),
90+
4, edge_size)
8691
painter.drawRect(edge_rect)
8792

8893
painter.restore()

NodeGraphQt/qgraphics/node_port_out.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def paint(self, painter, option, widget):
5656
text_rect.height()
5757
)
5858

59+
painter.setBrush(QtGui.QColor(255, 255, 255, 20))
60+
painter.drawRoundedRect(rect, 20, 20)
61+
5962
painter.setBrush(QtGui.QColor(0, 0, 0, 100))
6063
painter.drawRoundedRect(text_rect, 3, 3)
6164

@@ -81,8 +84,10 @@ def paint(self, painter, option, widget):
8184
painter.setPen(pen)
8285
painter.drawPolygon(poly)
8386

84-
edge_rect = QtCore.QRectF(
85-
rect.x() + (size * 1.7), rect.center().y() - 15, 4, 30)
87+
edge_size = 30
88+
edge_rect = QtCore.QRectF(rect.x() + (size * 1.6),
89+
rect.center().y() - (edge_size / 2),
90+
4, edge_size)
8691
painter.drawRect(edge_rect)
8792

8893
painter.restore()

0 commit comments

Comments
 (0)