1717from NodeGraphQt .base .node import NodeObject
1818from NodeGraphQt .base .port import Port
1919from 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
0 commit comments