From 0574978c9a7e54abffbf8a39537e34c6d58f0a28 Mon Sep 17 00:00:00 2001 From: Matteo Ghetta Date: Wed, 27 Sep 2023 12:14:39 +0200 Subject: [PATCH] [FEATURE] Text widget in tooltip (#518) * [FEATURE] text widget in tooltip --- lizmap/test/test_tooltip.py | 10 ++++++++++ lizmap/tooltip.py | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lizmap/test/test_tooltip.py b/lizmap/test/test_tooltip.py index 15a753d4..d03e068d 100755 --- a/lizmap/test/test_tooltip.py +++ b/lizmap/test/test_tooltip.py @@ -268,6 +268,16 @@ def test_attribute_editor_relation(self): ''' self.assertEqual(expected, expression) + def test_text_widget(self): + """Test to check the text widget.""" + expression = Tooltip._generate_text_label('a label', 'a text widget') + expected = ''' +

a label +

a text widget
+

+ ''' + self.assertEqual(expected, expression) + def test_external_resource_image(self): """Test we can generate external resource for an image.""" widget_config = { diff --git a/lizmap/tooltip.py b/lizmap/tooltip.py index 4e0d397c..939fee2a 100755 --- a/lizmap/tooltip.py +++ b/lizmap/tooltip.py @@ -21,6 +21,7 @@ QgsVectorLayer, ) from qgis.gui import QgsExternalResourceWidget +from qgis.PyQt.QtXml import QDomDocument LOGGER = logging.getLogger('Lizmap') SPACES = ' ' @@ -51,6 +52,17 @@ def create_popup_node_item_from_form( regex = re.compile(r"[^a-zA-Z0-9_]", re.IGNORECASE) a = '' h = '' + + if isinstance(node, QgsAttributeEditorElement): + # for text widgets + # TODO QGIS_VERSION_INT 3.32 change to "Qgis.AttributeEditorType.TextElement" + if node.type() == 6: + label = node.name() + expression = node.toDomElement(QDomDocument()).text() + + a += '\n' + SPACES * level + a += Tooltip._generate_text_label(label, expression) + if isinstance(node, QgsAttributeEditorField): if node.idx() < 0: # The form might have been imported from QML with some not existing fields @@ -379,6 +391,18 @@ def _generate_value_relation(widget_config: dict, name: str): ) return field_view + @staticmethod + def _generate_text_label(label: str, expression: str): + text = ''' +

{0} +

{1}
+

+ '''.format( + label, + expression + ) + return text + @staticmethod def css() -> str: css = '''