Skip to content

Commit a84c108

Browse files
committed
Auto layout support vertical layout.
1 parent 9002c0e commit a84c108

File tree

2 files changed

+85
-33
lines changed

2 files changed

+85
-33
lines changed

NodeGraphQt/base/utils.py

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from .. import QtGui, QtCore
55
from ..constants import (PIPE_LAYOUT_CURVED,
66
PIPE_LAYOUT_STRAIGHT,
7-
PIPE_LAYOUT_ANGLE)
7+
PIPE_LAYOUT_ANGLE,
8+
NODE_LAYOUT_VERTICAL,
9+
NODE_LAYOUT_HORIZONTAL,
10+
NODE_LAYOUT_DIRECTION)
811

912

1013
# menu
@@ -489,7 +492,7 @@ def __remove_BackdropNode(nodes):
489492
return nodes
490493

491494

492-
def topological_sort_by_down(start_nodes=[], all_nodes=[]):
495+
def topological_sort_by_down(start_nodes=None, all_nodes=None):
493496
"""
494497
Topological sort method by down stream direction.
495498
'start_nodes' and 'all_nodes' only one needs to be given.
@@ -500,6 +503,8 @@ def topological_sort_by_down(start_nodes=[], all_nodes=[]):
500503
Returns:
501504
list[NodeGraphQt.BaseNode]: sorted nodes.
502505
"""
506+
if not start_nodes and not all_nodes:
507+
return []
503508
if start_nodes:
504509
start_nodes = __remove_BackdropNode(start_nodes)
505510
if all_nodes:
@@ -517,7 +522,7 @@ def topological_sort_by_down(start_nodes=[], all_nodes=[]):
517522
return _sort_nodes(graph, start_nodes, True)
518523

519524

520-
def topological_sort_by_up(start_nodes=[], all_nodes=[]):
525+
def topological_sort_by_up(start_nodes=None, all_nodes=None):
521526
"""
522527
Topological sort method by up stream direction.
523528
'start_nodes' and 'all_nodes' only one needs to be given.
@@ -528,6 +533,8 @@ def topological_sort_by_up(start_nodes=[], all_nodes=[]):
528533
Returns:
529534
list[NodeGraphQt.BaseNode]: sorted nodes.
530535
"""
536+
if not start_nodes and not all_nodes:
537+
return []
531538
if start_nodes:
532539
start_nodes = __remove_BackdropNode(start_nodes)
533540
if all_nodes:
@@ -664,14 +671,16 @@ def _compute_rank_up(start_nodes):
664671
return nodes_rank
665672

666673

667-
def auto_layout_up(start_nodes=[], all_nodes=[]):
674+
def auto_layout_up(start_nodes=None, all_nodes=None):
668675
"""
669676
Auto layout the nodes by up stream direction.
670677
671678
Args:
672679
start_nodes (list[NodeGraphQt.BaseNode])(Optional): the end nodes of the graph.
673680
all_nodes (list[NodeGraphQt.BaseNode])(Optional): if 'start_nodes' is None the function can calculate start nodes from 'all_nodes'.
674681
"""
682+
if not start_nodes and not all_nodes:
683+
return
675684
if start_nodes:
676685
start_nodes = __remove_BackdropNode(start_nodes)
677686
if all_nodes:
@@ -680,7 +689,7 @@ def auto_layout_up(start_nodes=[], all_nodes=[]):
680689
if not start_nodes:
681690
start_nodes = [n for n in all_nodes if not _has_output_node(n)]
682691
if not start_nodes:
683-
return []
692+
return
684693

685694
nodes_rank = _compute_rank_up(start_nodes)
686695

@@ -691,30 +700,48 @@ def auto_layout_up(start_nodes=[], all_nodes=[]):
691700
else:
692701
rank_map[rank] = [node]
693702

694-
current_x = 0
695-
node_height = 50
696-
for rank in reversed(range(len(rank_map))):
697-
nodes = rank_map[rank]
698-
max_width = max([node.view.width for node in nodes])
699-
current_x += max_width
703+
if NODE_LAYOUT_DIRECTION is NODE_LAYOUT_HORIZONTAL:
704+
current_x = 0
705+
node_height = 80
706+
for rank in reversed(range(len(rank_map))):
707+
nodes = rank_map[rank]
708+
max_width = max([node.view.width for node in nodes])
709+
current_x += max_width
710+
current_y = 0
711+
for idx, node in enumerate(nodes):
712+
dy = max(node_height, node.view.height)
713+
current_y += 0 if idx == 0 else dy
714+
node.set_pos(current_x, current_y)
715+
current_y += dy * 0.5 + 10
716+
717+
current_x += max_width * 0.5 + 100
718+
elif NODE_LAYOUT_DIRECTION is NODE_LAYOUT_VERTICAL:
700719
current_y = 0
701-
for idx, node in enumerate(nodes):
702-
dy = max(node_height, node.view.height)
703-
current_y += 0 if idx == 0 else dy
704-
node.set_pos(current_x, current_y)
705-
current_y += dy * 0.5 + 10
720+
node_width = 250
721+
for rank in reversed(range(len(rank_map))):
722+
nodes = rank_map[rank]
723+
max_height = max([node.view.height for node in nodes])
724+
current_y += max_height
725+
current_x = 0
726+
for idx, node in enumerate(nodes):
727+
dx = max(node_width, node.view.width)
728+
current_x += 0 if idx == 0 else dx
729+
node.set_pos(current_x, current_y)
730+
current_x += dx * 0.5 + 10
706731

707-
current_x += max_width * 0.5 + 100
732+
current_y += max_height * 0.5 + 100
708733

709734

710-
def auto_layout_down(start_nodes=[], all_nodes=[]):
735+
def auto_layout_down(start_nodes=None, all_nodes=None):
711736
"""
712737
Auto layout the nodes by down stream direction.
713738
714739
Args:
715740
start_nodes (list[NodeGraphQt.BaseNode])(Optional): the start update nodes of the graph.
716741
all_nodes (list[NodeGraphQt.BaseNode])(Optional): if 'start_nodes' is None the function can calculate start nodes from 'all_nodes'.
717742
"""
743+
if not start_nodes and not all_nodes:
744+
return
718745
if start_nodes:
719746
start_nodes = __remove_BackdropNode(start_nodes)
720747
if all_nodes:
@@ -723,7 +750,7 @@ def auto_layout_down(start_nodes=[], all_nodes=[]):
723750
if not start_nodes:
724751
start_nodes = [n for n in all_nodes if not _has_input_node(n)]
725752
if not start_nodes:
726-
return []
753+
return
727754

728755
nodes_rank = _compute_rank_down(start_nodes)
729756

@@ -734,20 +761,36 @@ def auto_layout_down(start_nodes=[], all_nodes=[]):
734761
else:
735762
rank_map[rank] = [node]
736763

737-
current_x = 0
738-
node_height = 50
739-
for rank in range(len(rank_map)):
740-
nodes = rank_map[rank]
741-
max_width = max([node.view.width for node in nodes])
742-
current_x += max_width
764+
if NODE_LAYOUT_DIRECTION is NODE_LAYOUT_HORIZONTAL:
765+
current_x = 0
766+
node_height = 120
767+
for rank in range(len(rank_map)):
768+
nodes = rank_map[rank]
769+
max_width = max([node.view.width for node in nodes])
770+
current_x += max_width
771+
current_y = 0
772+
for idx, node in enumerate(nodes):
773+
dy = max(node_height, node.view.height)
774+
current_y += 0 if idx == 0 else dy
775+
node.set_pos(current_x, current_y)
776+
current_y += dy * 0.5 + 10
777+
778+
current_x += max_width * 0.5 + 100
779+
elif NODE_LAYOUT_DIRECTION is NODE_LAYOUT_VERTICAL:
743780
current_y = 0
744-
for idx, node in enumerate(nodes):
745-
dy = max(node_height, node.view.height)
746-
current_y += 0 if idx == 0 else dy
747-
node.set_pos(current_x, current_y)
748-
current_y += dy * 0.5 + 10
749-
750-
current_x += max_width * 0.5 + 100
781+
node_width = 250
782+
for rank in range(len(rank_map)):
783+
nodes = rank_map[rank]
784+
max_height = max([node.view.height for node in nodes])
785+
current_y += max_height
786+
current_x = 0
787+
for idx, node in enumerate(nodes):
788+
dx = max(node_width, node.view.width)
789+
current_x += 0 if idx == 0 else dx
790+
node.set_pos(current_x, current_y)
791+
current_x += dx * 0.5 + 10
792+
793+
current_y += max_height * 0.5 + 100
751794

752795

753796
# garbage collect

NodeGraphQt/qgraphics/node_base.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,17 +489,26 @@ def post_init(self, viewer=None, pos=None):
489489
self.xy_pos = pos
490490

491491
def auto_switch_mode(self):
492+
"""
493+
Decide whether to draw the node with proxy mode.
494+
"""
492495
if ITEM_CACHE_MODE is QtWidgets.QGraphicsItem.ItemCoordinateCache:
493496
return
494497
rect = self.sceneBoundingRect()
495498
l = self.viewer().mapToGlobal(self.viewer().mapFromScene(rect.topLeft()))
496499
r = self.viewer().mapToGlobal(self.viewer().mapFromScene(rect.topRight()))
497-
# with is the node with in screen
500+
# width is the node with in screen
498501
width = r.x() - l.x()
499502

500503
self.set_proxy_mode(width < self._proxy_mode_threshold)
501504

502505
def set_proxy_mode(self, mode):
506+
"""
507+
Set whether to draw the node with proxy mode.
508+
509+
Args:
510+
mode (bool).
511+
"""
503512
if mode is self._proxy_mode:
504513
return
505514

0 commit comments

Comments
 (0)