Skip to content

Commit

Permalink
feat(realization-view): Add layer_sizing render parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ewuerger committed Jan 9, 2024
1 parent 042ef60 commit 9386ce4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
41 changes: 25 additions & 16 deletions capellambse_context_diagrams/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,24 +422,10 @@ def _create_diagram(self, params: dict[str, t.Any]) -> cdiagram.Diagram:
"search_direction", params.get("search_direction", "ALL")
)
params.setdefault("show_owners", params.get("show_owners", True))
params.setdefault("layer_sizing", params.get("layer_sizing", "UNION"))
data, edges = realization_view.collector(self, params)
layout = try_to_layout(data)
min_width = max(child["size"]["width"] for child in layout["children"]) # type: ignore[typeddict-item]
min_width += 15.0
for layer in data["children"]:
min_height: int | float = 0
for layout_layer in layout["children"]:
if layer["id"] != layout_layer["id"]:
continue
assert layout_layer["type"] != "edge"
min_height = layout_layer["size"]["height"]

assert min_height > 0
layer["width"] = min_width
layer["layoutOptions"][
"nodeSize.minimum"
] = f"({min_width},{min_height})"

adjust_layer_sizing(data, layout, params["layer_sizing"])
layout = try_to_layout(data)
for edge in edges:
layout["children"].append(
Expand Down Expand Up @@ -490,6 +476,29 @@ def try_to_layout(data: _elkjs.ELKInputData) -> _elkjs.ELKOutputData:
raise error


def adjust_layer_sizing(
data: _elkjs.ELKInputData,
layout: _elkjs.ELKOutputData,
layer_sizing: t.Literal["UNION", "WIDTH", "HEIGHT"],
) -> None:
"""Set `nodeSize.minimum` config in the layoutOptions."""

def calculate_min(key: t.Literal["width", "height"] = "width") -> float:
return max(child["size"][key] for child in layout["children"]) # type: ignore[typeddict-item]

if layer_sizing not in {"UNION", "WIDTH", "HEIGHT"}:
raise NotImplementedError(
"For ``layer_sizing`` only UNION, WIDTH or HEIGHT is supported"
)

min_w = calculate_min() + 15.0 if layer_sizing in {"UNION", "WIDTH"} else 0
min_h = (
calculate_min("height") if layer_sizing in {"UNION", "HEIGHT"} else 0
)
for layer in data["children"]:
layer["layoutOptions"]["nodeSize.minimum"] = f"({min_w},{min_h})"


def stack_diagrams(
first: cdiagram.Diagram,
second: cdiagram.Diagram,
Expand Down
7 changes: 5 additions & 2 deletions docs/realization_view.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ implement currently. The diagram elements are collected from the
depth=3, # 1-3
search_direction="ALL", # BELOW; ABOVE and ALL
show_owners=True,
layer_sizing="UNION", # UNION; WIDTH and HEIGHT
).save_drawing(pretty=True)
```
<figure markdown>
Expand All @@ -51,6 +52,7 @@ implement currently. The diagram elements are collected from the
depth=3,
search_direction="ALL",
show_owners=True,
layer_sizing="UNION",
).save_drawing(pretty=True)
```
<figure markdown>
Expand All @@ -59,8 +61,9 @@ implement currently. The diagram elements are collected from the
</figure>

Additional rendering parameters enable showing owning functions or components,
as well as the depth of traversion (i.e. `1`-`3`). They are put to display the
maximum amount of diagram elements per default.
as well as the depth of traversion (i.e. `1`-`3`) and control on sizing of the
layer boxes. They are put to display the maximum amount of diagram elements per
default.

??? bug "Alignment of diagram elements"

Expand Down
3 changes: 3 additions & 0 deletions tests/test_realization_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ def test_tree_view_gets_rendered_successfully(
@pytest.mark.parametrize("depth", list(range(1, 4)))
@pytest.mark.parametrize("search_direction", ["ABOVE", "BELOW"])
@pytest.mark.parametrize("show_owners", [True, False])
@pytest.mark.parametrize("layer_sizing", ["UNION", "WIDTH", "HEIGHT"])
def test_tree_view_renders_with_additional_params(
model: capellambse.MelodyModel,
depth: int,
search_direction: str,
show_owners: bool,
layer_sizing: str,
uuid: str,
) -> None:
obj = model.by_uuid(uuid)
Expand All @@ -43,4 +45,5 @@ def test_tree_view_renders_with_additional_params(
depth=depth,
search_direction=search_direction,
show_owners=show_owners,
layer_sizing=layer_sizing,
)

0 comments on commit 9386ce4

Please sign in to comment.