22from distutils .version import LooseVersion
33
44from .. import QtGui , QtCore
5- from .node import SubGraph
65from ..constants import (PIPE_LAYOUT_CURVED ,
76 PIPE_LAYOUT_STRAIGHT ,
87 PIPE_LAYOUT_ANGLE )
@@ -250,9 +249,7 @@ def _fit_to_selection(graph):
250249def _jump_in (graph ):
251250 nodes = graph .selected_nodes ()
252251 if nodes :
253- node = nodes [0 ]
254- if isinstance (node , SubGraph ):
255- graph .set_node_space (node )
252+ graph .set_node_space (nodes [0 ])
256253
257254
258255def _jump_out (graph ):
@@ -312,7 +309,7 @@ def _has_output_node(node):
312309 return False
313310
314311
315- def _build_graph (start_nodes ):
312+ def _build_down_stream_graph (start_nodes ):
316313 graph = {}
317314 for node in start_nodes :
318315 output_nodes = get_output_nodes (node )
@@ -325,19 +322,26 @@ def _build_graph(start_nodes):
325322 graph [n ] = nodes
326323 _output_nodes .extend (nodes )
327324 output_nodes = _output_nodes
328-
329325 return graph
330326
331327
332- def topological_sort (start_nodes = [], all_nodes = []):
333- if not start_nodes :
334- start_nodes = [n for n in all_nodes if not _has_input_node (n )]
335- if not start_nodes :
336- return []
337- if not [n for n in start_nodes if _has_output_node (n )]:
338- return start_nodes
328+ def _build_up_stream_graph (start_nodes ):
329+ graph = {}
330+ for node in start_nodes :
331+ input_nodes = get_input_nodes (node )
332+ graph [node ] = input_nodes
333+ while input_nodes :
334+ _input_nodes = []
335+ for n in input_nodes :
336+ if n not in graph :
337+ nodes = get_input_nodes (n )
338+ graph [n ] = nodes
339+ _input_nodes .extend (nodes )
340+ input_nodes = _input_nodes
341+ return graph
342+
339343
340- graph = _build_graph ( start_nodes )
344+ def _sort_nodes ( graph , start_nodes , reverse = True ):
341345 if not graph :
342346 return []
343347
@@ -357,6 +361,57 @@ def dfs(graph, start_node):
357361 visit [start_node ] = True
358362 dfs (graph , start_node )
359363
360- sorted_nodes .reverse ()
364+ if reverse :
365+ sorted_nodes .reverse ()
361366
362367 return sorted_nodes
368+
369+
370+ def topological_sort_by_down (start_nodes = [], all_nodes = []):
371+ if not start_nodes :
372+ start_nodes = [n for n in all_nodes if not _has_input_node (n )]
373+ if not start_nodes :
374+ return []
375+ if not [n for n in start_nodes if _has_output_node (n )]:
376+ return start_nodes
377+
378+ graph = _build_down_stream_graph (start_nodes )
379+
380+ return _sort_nodes (graph , start_nodes , True )
381+
382+
383+ def topological_sort_by_up (start_nodes = [], all_nodes = []):
384+ if not start_nodes :
385+ start_nodes = [n for n in all_nodes if not _has_output_node (n )]
386+ if not start_nodes :
387+ return []
388+ if not [n for n in start_nodes if _has_input_node (n )]:
389+ return start_nodes
390+
391+ graph = _build_up_stream_graph (start_nodes )
392+
393+ return _sort_nodes (graph , start_nodes , False )
394+
395+
396+ def _update_nodes (nodes ):
397+ for node in nodes :
398+ if node .disabled ():
399+ node .when_disabled ()
400+ else :
401+ node .run ()
402+
403+
404+ def update_node_down_stream (node ):
405+ _update_nodes (topological_sort_by_down (start_nodes = [node ]))
406+
407+
408+ def update_node_up_stream (node ):
409+ _update_nodes (topological_sort_by_up (start_nodes = [node ]))
410+
411+
412+ def update_nodes_by_down (nodes ):
413+ _update_nodes (topological_sort_by_down (all_nodes = nodes ))
414+
415+
416+ def update_nodes_by_up (nodes ):
417+ _update_nodes (topological_sort_by_up (all_nodes = nodes ))
0 commit comments