From 0c6b6f390d79e697e218955c7d17c09220e17405 Mon Sep 17 00:00:00 2001 From: KV Date: Sun, 16 Aug 2020 23:53:12 +0200 Subject: [PATCH] Compute sensible default values for unspecified image attributes The goal is to enable the user to avoid specifying more attributes than strictly needed. --- examples/ex08.yml | 3 +-- src/wireviz/DataClasses.py | 19 +++++++++++++++++-- src/wireviz/wv_helper.py | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/examples/ex08.yml b/examples/ex08.yml index af3df55..ed7802f 100644 --- a/examples/ex08.yml +++ b/examples/ex08.yml @@ -9,8 +9,6 @@ connectors: show_pincount: false image: src: resources/stereo-phone-plug-TRS.png - scale: true - height: 100 caption: Tip, Ring, and Sleeve cables: @@ -23,6 +21,7 @@ cables: shield: SN image: src: resources/cable-WH+BN+GN+shield.png + height: 75 caption: Cross-section connections: diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index 6ffc044..622912a 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -11,15 +11,30 @@ from wireviz import wv_colors class Image: # Attributes of the image object : src: str - scale: Optional[str] = "false" # false | true | width | height | both + scale: Optional[str] = None # false | true | width | height | both # Attributes of the image cell containing the image: width: Optional[int] = None height: Optional[int] = None - fixedsize: Optional[bool] = False + fixedsize: Optional[bool] = None # Contents of the cell just below the image cell: caption: Optional[str] = None # See also HTML doc at https://graphviz.org/doc/info/shapes.html#html + def __post_init__(self): + + if self.scale is None: + self.scale = "false" if self.width is None and self.height is None \ + else "both" if self.width is not None and self.height is not None \ + else "true" # When only one dimension is specified. + + if self.fixedsize is None: + self.fixedsize = self.width is not None or self.height is not None + + if self.width is None and self.height is not None: + self.width = self.height # Assuming 1:1 aspect ratio for now. + elif self.height is None and self.width is not None: + self.height = self.width # Assuming 1:1 aspect ratio for now. + @dataclass class Connector: diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 33936ed..0bc8139 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -75,8 +75,8 @@ def html_caption(image): def html_size_attr(image): # Return Graphviz HTML attributes to specify minimum or fixed size of a TABLE or TD object - return ((f' width="{image.width}"' if image.width else '') - + (f' height="{image.height}"' if image.height else '') + return ((f' width="{image.width}"' if image.width is not None else '') + + (f' height="{image.height}"' if image.height is not None else '') + ( ' fixedsize="true"' if image.fixedsize else '')) if image else ''