From f11f95e5a07948a578deb38cc71eb0dc44ca2cc9 Mon Sep 17 00:00:00 2001 From: Loic Huder Date: Fri, 29 May 2026 17:13:10 +0200 Subject: [PATCH] Place nodes on a horizontal line rather than randomly --- src/ewoksdraw/__init__.py | 29 +++++++++++++----------- src/ewoksdraw/svg/svg_canvas.py | 4 ++++ src/ewoksdraw/svg/svg_task.py | 23 ++++++++++++------- src/ewoksdraw/tests/test_graph_to_svg.py | 2 +- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/ewoksdraw/__init__.py b/src/ewoksdraw/__init__.py index 13fd854..f699db1 100644 --- a/src/ewoksdraw/__init__.py +++ b/src/ewoksdraw/__init__.py @@ -1,23 +1,19 @@ -import random from pathlib import Path from ewokscore.graph import TaskGraph from ewokscore.graph.inputs import _get_all_node_inputs from ewokscore.graph.inputs import _get_all_task_output_names -from .svg import SvgBackground from .svg import SvgCanvas from .svg import SvgTask +GAP = 10 +DEFAULT_HEIGHT = 500 -def graph_to_svg(graph: TaskGraph, output_path: str | Path): - canvas_width = 500 - canvas_height = 500 - - canvas = SvgCanvas(width=canvas_width, height=canvas_height) - svg_background = SvgBackground(canvas_width, canvas_height) - canvas.add_element(svg_background) +def graph_to_svg(graph: TaskGraph, output_path: str | Path): + svg_tasks = [] + width = GAP for node_id, node_attrs in graph.graph.nodes.items(): node_inputs = _get_all_node_inputs(node_id, node_attrs) node_outputs = _get_all_task_output_names( @@ -28,9 +24,16 @@ def graph_to_svg(graph: TaskGraph, output_path: str | Path): input_names=[n.name for n in node_inputs], output_names=node_outputs, ) - - svg_task.translate(x=random.randint(5, 400), y=random.randint(5, 400)) - + svg_tasks.append(svg_task) + svg_task.translate(x=width, y=GAP) + width += svg_task.width + GAP + + if len(svg_tasks) > 0: + height = max([svg_task.height for svg_task in svg_tasks]) + else: + height = DEFAULT_HEIGHT + canvas = SvgCanvas(width=width, height=height + 2 * GAP) + canvas.add_background() + for svg_task in svg_tasks: canvas.add_element(svg_task) - canvas.draw(output_path) diff --git a/src/ewoksdraw/svg/svg_canvas.py b/src/ewoksdraw/svg/svg_canvas.py index 6a79b9f..f6af204 100644 --- a/src/ewoksdraw/svg/svg_canvas.py +++ b/src/ewoksdraw/svg/svg_canvas.py @@ -8,6 +8,7 @@ import xmltodict +from .svg_background import SvgBackground from .svg_element import SvgElement from .svg_group import SvgGroup @@ -43,6 +44,9 @@ def __init__(self, width: int, height: int): self.height = height self.elements: List[Union[SvgElement, SvgGroup]] = [] + def add_background(self): + self.add_element(SvgBackground(self.width, self.height)) + def add_element(self, element: Union[SvgElement, SvgGroup]) -> None: """ Adds an SvgElement or SvgGroup to the drawing. diff --git a/src/ewoksdraw/svg/svg_task.py b/src/ewoksdraw/svg/svg_task.py index da077ce..e1458f7 100644 --- a/src/ewoksdraw/svg/svg_task.py +++ b/src/ewoksdraw/svg/svg_task.py @@ -95,14 +95,7 @@ def _scale_vertical(self) -> None: Adjusts the vertical layout and sizes of elements within the task group. """ - total_height = ( - self._title.height - + self._inputs.height - + self._outputs.height - + self._interspace_input_output - + self._interspace_title_input - ) - self._box.set_height(total_height) + self._box.set_height(self.height) self._title.set_position(y=self._title.vertical_margin // 2) @@ -117,3 +110,17 @@ def _scale_vertical(self) -> None: x2=self._box.width, y2=self._title.height - self._title.vertical_margin // 2, ) + + @property + def width(self) -> float: + return self._box.width + + @property + def height(self) -> float: + return ( + self._title.height + + self._inputs.height + + self._outputs.height + + self._interspace_input_output + + self._interspace_title_input + ) diff --git a/src/ewoksdraw/tests/test_graph_to_svg.py b/src/ewoksdraw/tests/test_graph_to_svg.py index 5010968..99be1c3 100644 --- a/src/ewoksdraw/tests/test_graph_to_svg.py +++ b/src/ewoksdraw/tests/test_graph_to_svg.py @@ -17,7 +17,7 @@ def _get_svg_groups(output_path: Path): @pytest.mark.parametrize("graph_name", graph_names()) def test_groups_are_matching_nodes(graph_name, tmp_path: Path): - output_path = tmp_path / "test.svg" + output_path = tmp_path / f"{graph_name}.svg" graph, _ = get_graph(graph_name) ewoksgraph = load_graph(graph)