Skip to content

Commit

Permalink
[FEATURE] Text widget in tooltip (#518)
Browse files Browse the repository at this point in the history
* [FEATURE] text widget in tooltip
  • Loading branch information
ghtmtt authored Sep 27, 2023
1 parent fb72e6e commit 0574978
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lizmap/test/test_tooltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ def test_attribute_editor_relation(self):
</div>'''
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 = '''
<p><strong>a label</strong>
<div class="field">a text widget</div>
</p>
'''
self.assertEqual(expected, expression)

def test_external_resource_image(self):
"""Test we can generate external resource for an image."""
widget_config = {
Expand Down
24 changes: 24 additions & 0 deletions lizmap/tooltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
QgsVectorLayer,
)
from qgis.gui import QgsExternalResourceWidget
from qgis.PyQt.QtXml import QDomDocument

LOGGER = logging.getLogger('Lizmap')
SPACES = ' '
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = '''
<p><strong>{0}</strong>
<div class="field">{1}</div>
</p>
'''.format(
label,
expression
)
return text

@staticmethod
def css() -> str:
css = '''<style>
Expand Down

0 comments on commit 0574978

Please sign in to comment.