Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/ewoksdraw/__init__.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
4 changes: 4 additions & 0 deletions src/ewoksdraw/svg/svg_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import xmltodict

from .svg_background import SvgBackground
from .svg_element import SvgElement
from .svg_group import SvgGroup

Expand Down Expand Up @@ -43,6 +44,9 @@ def __init__(self, width: int, height: int):
self.height = height
self.elements: List[Union[SvgElement, SvgGroup]] = []

def add_background(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current canvas definition.
We are force to have the following sequence:

  1. Elements creation
  2. Elements Placement
  3. Compute Canvas size
  4. Create Canvas
  5. Create Canvas background
  6. Add elements to Canvas

It's a little counter intuitive to "draw" first and paste into canvas after.
Why not :

  1. Create Canvas (0 size or random)
  2. Create and add elements to canvas
  3. Place elements
  4. Resize background

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I see.

If you agree though, I would like to first implement the complete SVG generation (with edges) before changing this. New abstractions may emerge so that the sequence may more intuitive or be more easily changed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sound good to me

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.
Expand Down
23 changes: 15 additions & 8 deletions src/ewoksdraw/svg/svg_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -117,3 +110,17 @@ def _scale_vertical(self) -> None:
x2=self._box.width,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x2=self._box.width,
x2=self.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
)
2 changes: 1 addition & 1 deletion src/ewoksdraw/tests/test_graph_to_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading