Skip to content

Commit eaa8acb

Browse files
committed
wip dynamic layout switching.
1 parent bf4adc9 commit eaa8acb

File tree

5 files changed

+244
-56
lines changed

5 files changed

+244
-56
lines changed

NodeGraphQt/base/graph.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
from NodeGraphQt.base.node import NodeObject
1818
from NodeGraphQt.base.port import Port
1919
from NodeGraphQt.constants import (
20-
NODE_LAYOUT_DIRECTION, NODE_LAYOUT_HORIZONTAL, NODE_LAYOUT_VERTICAL,
20+
NODE_LAYOUT_DIRECTION,
21+
NODE_LAYOUT_HORIZONTAL,
22+
NODE_LAYOUT_VERTICAL,
23+
URI_SCHEME,
24+
URN_SCHEME,
2125
PipeLayoutEnum,
22-
URI_SCHEME, URN_SCHEME,
2326
PortTypeEnum,
2427
ViewerEnum
2528
)
@@ -786,6 +789,26 @@ def set_pipe_style(self, style=PipeLayoutEnum.CURVED.value):
786789
style = style if 0 <= style <= pipe_max else PipeLayoutEnum.CURVED.value
787790
self._viewer.set_pipe_layout(style)
788791

792+
def set_layout_direction(self, direction):
793+
"""
794+
Sets the node graph layout direction to horizontal or vertical.
795+
796+
Note:
797+
By default node graph direction is set to "NODE_LAYOUT_HORIZONTAL".
798+
799+
Node Graph Layout Types:
800+
801+
* :attr:`NodeGraphQt.constants.NODE_LAYOUT_HORIZONTAL`
802+
* :attr:`NodeGraphQt.constants.NODE_LAYOUT_VERTICAL`
803+
804+
Args:
805+
direction (int): layout direction.
806+
"""
807+
direction_types = [NODE_LAYOUT_HORIZONTAL, NODE_LAYOUT_VERTICAL]
808+
if direction not in direction_types:
809+
direction = NODE_LAYOUT_HORIZONTAL
810+
self._viewer.set_layout_direction(direction)
811+
789812
def fit_to_selection(self):
790813
"""
791814
Sets the zoom level to fit selected nodes.
@@ -1672,7 +1695,9 @@ def auto_layout_nodes(self, nodes=None, down_stream=True, start_nodes=None):
16721695
else:
16731696
rank_map[rank] = [node]
16741697

1675-
if NODE_LAYOUT_DIRECTION is NODE_LAYOUT_HORIZONTAL:
1698+
node_layout_direction = self._viewer.get_layout_direction()
1699+
1700+
if node_layout_direction is NODE_LAYOUT_HORIZONTAL:
16761701
current_x = 0
16771702
node_height = 120
16781703
for rank in sorted(range(len(rank_map)), reverse=not down_stream):
@@ -1687,7 +1712,7 @@ def auto_layout_nodes(self, nodes=None, down_stream=True, start_nodes=None):
16871712
current_y += dy * 0.5 + 10
16881713

16891714
current_x += max_width * 0.5 + 100
1690-
elif NODE_LAYOUT_DIRECTION is NODE_LAYOUT_VERTICAL:
1715+
elif node_layout_direction is NODE_LAYOUT_VERTICAL:
16911716
current_y = 0
16921717
node_width = 250
16931718
for rank in sorted(range(len(rank_map)), reverse=not down_stream):
@@ -1953,6 +1978,8 @@ def _build_port_nodes(self):
19531978
Returns:
19541979
tuple(dict, dict): input nodes, output nodes.
19551980
"""
1981+
node_layout_direction = self._viewer.get_layout_direction()
1982+
19561983
# build the parent input port nodes.
19571984
input_nodes = {n.name(): n for n in
19581985
self.get_nodes_by_type(PortInputNode.type_)}
@@ -1965,9 +1992,9 @@ def _build_port_nodes(self):
19651992
input_nodes[port.name()] = input_node
19661993
self.add_node(input_node, selected=False, push_undo=False)
19671994
x, y = input_node.pos()
1968-
if NODE_LAYOUT_DIRECTION is NODE_LAYOUT_HORIZONTAL:
1995+
if node_layout_direction is NODE_LAYOUT_HORIZONTAL:
19691996
x -= 100
1970-
elif NODE_LAYOUT_DIRECTION is NODE_LAYOUT_VERTICAL:
1997+
elif node_layout_direction is NODE_LAYOUT_VERTICAL:
19711998
y -= 100
19721999
input_node.set_property('pos', [x, y], push_undo=False)
19732000

@@ -1983,9 +2010,9 @@ def _build_port_nodes(self):
19832010
output_nodes[port.name()] = output_node
19842011
self.add_node(output_node, selected=False, push_undo=False)
19852012
x, y = output_node.pos()
1986-
if NODE_LAYOUT_DIRECTION is NODE_LAYOUT_HORIZONTAL:
2013+
if node_layout_direction is NODE_LAYOUT_HORIZONTAL:
19872014
x += 100
1988-
elif NODE_LAYOUT_DIRECTION is NODE_LAYOUT_VERTICAL:
2015+
elif node_layout_direction is NODE_LAYOUT_VERTICAL:
19892016
y += 100
19902017
output_node.set_property('pos', [x, y], push_undo=False)
19912018

NodeGraphQt/constants.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,24 @@
1717
URI_SCHEME = 'nodegraphqt://'
1818
URN_SCHEME = 'nodegraphqt::'
1919

20-
# === PATHS ===
21-
20+
# PATHS
2221
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
2322
ICON_PATH = os.path.join(BASE_PATH, 'widgets', 'icons')
2423
ICON_DOWN_ARROW = os.path.join(ICON_PATH, 'down_arrow.png')
2524
ICON_NODE_BASE = os.path.join(ICON_PATH, 'node_base.png')
2625

27-
# === DRAW STACK ORDER ===
28-
26+
# DRAW STACK ORDER
2927
Z_VAL_PIPE = -1
3028
Z_VAL_NODE = 1
3129
Z_VAL_PORT = 2
3230
Z_VAL_NODE_WIDGET = 3
3331

34-
# === ITEM CACHE MODE ===
35-
32+
# ITEM CACHE MODE
3633
# QGraphicsItem.NoCache
3734
# QGraphicsItem.DeviceCoordinateCache
3835
# QGraphicsItem.ItemCoordinateCache
39-
4036
ITEM_CACHE_MODE = QtWidgets.QGraphicsItem.DeviceCoordinateCache
4137

42-
# === NODE LAYOUT DIRECTION ===
43-
44-
#: Mode for vertical node layout.
45-
NODE_LAYOUT_VERTICAL = 0
46-
#: Mode for horizontal node layout.
47-
NODE_LAYOUT_HORIZONTAL = 1
48-
#: Variable for setting the node layout direction.
49-
# NODE_LAYOUT_DIRECTION = NODE_LAYOUT_VERTICAL
50-
NODE_LAYOUT_DIRECTION = NODE_LAYOUT_HORIZONTAL
51-
5238
# =================================== GLOBAL ===================================
5339

5440

@@ -66,6 +52,23 @@ class VersionEnum(Enum):
6652
#:
6753
PATCH = int(_v.split('.')[2])
6854

55+
56+
class LayoutDirectionEnum(Enum):
57+
"""
58+
Node graph nodes layout direction:
59+
:py:mod:`NodeGraphQt.constants.ViewerLayoutEnum`
60+
"""
61+
#: layout nodes top to bottom.
62+
VERTICAL = 0
63+
#: layout nodes left to right.
64+
HORIZONTAL = 1
65+
66+
67+
#: Variable for setting the node layout direction.
68+
# NODE_LAYOUT_DIRECTION = LayoutDirectionEnum.VERTICAL.value
69+
NODE_LAYOUT_DIRECTION = LayoutDirectionEnum.HORIZONTAL.value
70+
71+
6972
# =================================== VIEWER ===================================
7073

7174

0 commit comments

Comments
 (0)