From 00716d0c616c5b8ca890da79b8753cc0c9cdaea1 Mon Sep 17 00:00:00 2001 From: Miklos Marton Date: Mon, 19 Aug 2024 22:16:38 +0200 Subject: [PATCH 1/2] Added alignment option to the connector images --- src/wireviz/DataClasses.py | 2 ++ src/wireviz/Harness.py | 42 +++++++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index d14e4459..8cce386b 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -23,6 +23,7 @@ PlainText # = Literal['wirecount', 'terminations', 'length', 'total_length'] ) ImageScale = PlainText # = Literal['false', 'true', 'width', 'height', 'both'] +ImageAlignment = PlainText # = Literal['under', 'above', 'left', 'right'] # Type combinations Pin = Union[int, PlainText] # Pin identifier @@ -86,6 +87,7 @@ class Image: height: Optional[int] = None fixedsize: Optional[bool] = None bgcolor: Optional[Color] = None + alignment: Optional[ImageAlignment] = None # Contents of the text cell just below the image cell: caption: Optional[MultilineHypertext] = None # See also HTML doc at https://graphviz.org/doc/info/shapes.html#html diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index c4af2364..367e2c24 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -191,6 +191,12 @@ def create_graph(self) -> Graph: connector.ports_left = True # Use left side pins. html = [] + + image_rows = [] + if connector.image: + image_rows = [[html_image(connector.image)], + [html_caption(connector.image)]] + # fmt: off rows = [[f'{html_bgcolor(connector.bgcolor_title)}{remove_links(connector.name)}' if connector.show_name else None], @@ -202,13 +208,20 @@ def create_graph(self) -> Graph: f'{connector.pincount}-pin' if connector.show_pincount else None, translate_color(connector.color, self.options.color_mode) if connector.color else None, html_colorbar(connector.color)], - '' if connector.style != 'simple' else None, - [html_image(connector.image)], - [html_caption(connector.image)]] + '' if connector.style != 'simple' else None] # fmt: on + imagetable='' + if connector.image: + if connector.image.alignment == 'under' or connector.image.alignment is None: + rows += image_rows + elif connector.image.alignment == 'above': + rows = image_rows + rows + else: + imagetable = ''.join(nested_html_table(image_rows, html_bgcolor_attr(connector.bgcolor))) rows.extend(get_additional_component_table(self, connector)) rows.append([html_line_breaks(connector.notes)]) + html.extend(nested_html_table(rows, html_bgcolor_attr(connector.bgcolor))) if connector.style != "simple": @@ -217,11 +230,16 @@ def create_graph(self) -> Graph: '' ) - for pinindex, (pinname, pinlabel, pincolor) in enumerate( - zip_longest( + firstpin = True + pincount = len(connector.pins) + if len(connector.pinlabels) > pincount: + pincount = len(connector.pinlabels) + if len(connector.pincolors) > pincount: + pincount = len(connector.pincolors) + + for pinindex, (pinname, pinlabel, pincolor) in enumerate(zip_longest( connector.pins, connector.pinlabels, connector.pincolors - ) - ): + )): if ( connector.hide_disconnected_pins and not connector.visible_pins.get(pinname, False) @@ -229,6 +247,11 @@ def create_graph(self) -> Graph: continue pinhtml.append(" ") + + if firstpin and connector.image and connector.image.alignment == 'left': + firstpin = False + pinhtml.append(f'') + if connector.ports_left: pinhtml.append(f' ') if pinlabel: @@ -248,6 +271,11 @@ def create_graph(self) -> Graph: if connector.ports_right: pinhtml.append(f' ') + + if firstpin and connector.image and connector.image.alignment == 'right': + firstpin = False + pinhtml.append( + f'') pinhtml.append(" ") pinhtml.append("
{imagetable}{pinname}{pinname}{imagetable}
") From f19fafb638296232af97bf2b7058e5f0b4542ead Mon Sep 17 00:00:00 2001 From: Miklos Marton Date: Sat, 7 Sep 2024 20:31:00 +0200 Subject: [PATCH 2/2] Fix @kvid's suggestions: - Rename image alignment to position - Rename under to below - Display the above positioned images right above the pins list Fixes #418 --- src/wireviz/DataClasses.py | 4 ++-- src/wireviz/Harness.py | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index 8cce386b..617e59d9 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -23,7 +23,7 @@ PlainText # = Literal['wirecount', 'terminations', 'length', 'total_length'] ) ImageScale = PlainText # = Literal['false', 'true', 'width', 'height', 'both'] -ImageAlignment = PlainText # = Literal['under', 'above', 'left', 'right'] +ImagePosition = PlainText # = Literal['below', 'above', 'left', 'right'] # Type combinations Pin = Union[int, PlainText] # Pin identifier @@ -87,7 +87,7 @@ class Image: height: Optional[int] = None fixedsize: Optional[bool] = None bgcolor: Optional[Color] = None - alignment: Optional[ImageAlignment] = None + position: Optional[ImagePosition] = None # Contents of the text cell just below the image cell: caption: Optional[MultilineHypertext] = None # See also HTML doc at https://graphviz.org/doc/info/shapes.html#html diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 367e2c24..5f826c4e 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -193,7 +193,7 @@ def create_graph(self) -> Graph: html = [] image_rows = [] - if connector.image: + if connector.image and connector.image.position != 'above': image_rows = [[html_image(connector.image)], [html_caption(connector.image)]] @@ -205,17 +205,17 @@ def create_graph(self) -> Graph: html_line_breaks(pn_info_string(HEADER_SPN, connector.supplier, connector.spn))], [html_line_breaks(connector.type), html_line_breaks(connector.subtype), - f'{connector.pincount}-pin' if connector.show_pincount else None, + f'{connector.pincount}-pin' if connector.show_pincount else None], + [html_caption(connector.image) if connector.image and connector.image.position == 'above' else None, + html_image(connector.image) if connector.image and connector.image.position == 'above' else None, translate_color(connector.color, self.options.color_mode) if connector.color else None, html_colorbar(connector.color)], '' if connector.style != 'simple' else None] # fmt: on imagetable='' if connector.image: - if connector.image.alignment == 'under' or connector.image.alignment is None: + if connector.image.position == 'below' or connector.image.position is None: rows += image_rows - elif connector.image.alignment == 'above': - rows = image_rows + rows else: imagetable = ''.join(nested_html_table(image_rows, html_bgcolor_attr(connector.bgcolor))) @@ -248,7 +248,7 @@ def create_graph(self) -> Graph: pinhtml.append(" ") - if firstpin and connector.image and connector.image.alignment == 'left': + if firstpin and connector.image and connector.image.position == 'left': firstpin = False pinhtml.append(f'{imagetable}') @@ -272,7 +272,7 @@ def create_graph(self) -> Graph: if connector.ports_right: pinhtml.append(f' {pinname}') - if firstpin and connector.image and connector.image.alignment == 'right': + if firstpin and connector.image and connector.image.position == 'right': firstpin = False pinhtml.append( f'{imagetable}') @@ -741,3 +741,4 @@ def bom(self): if not self._bom: self._bom = generate_bom(self) return self._bom +