Skip to content

Commit

Permalink
Fix fields WMS not published when generating the HTML maptip, add opt…
Browse files Browse the repository at this point in the history
…ion empty value
  • Loading branch information
Gustry committed Sep 25, 2023
1 parent 3299f82 commit fb72e6e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Unreleased

* Add the possibility to open the online documentation for the current panel in the plugin
* Do not generate the HTML for a field if the field is excluded from WMS
* Add an option to show empty rows in the HTML map tip to generate another QGIS expression

## 3.17.0 - 2023-09-18

Expand Down
66 changes: 66 additions & 0 deletions lizmap/dialogs/html_maptip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
__copyright__ = 'Copyright 2023, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'


from qgis.core import QgsVectorLayer
from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox

from lizmap.qgis_plugin_tools.tools.i18n import tr
from lizmap.qgis_plugin_tools.tools.resources import load_ui

FORM_CLASS = load_ui('ui_html_maptip.ui')


class HtmlMapTipDialog(QDialog, FORM_CLASS):

def __init__(self, layer: QgsVectorLayer):
QDialog.__init__(self)
self.setupUi(self)
self.layer = layer

accept_button = self.button_box.button(QDialogButtonBox.Ok)
accept_button.clicked.connect(self.accept)
cancel_button = self.button_box.button(QDialogButtonBox.Cancel)
cancel_button.clicked.connect(self.reject)

def map_tip(self) -> str:
table_template = """<table class="table table-condensed table-striped table-bordered lizmapPopupTable">
<thead>
<tr>
<th>{field}</th>
<th>{value}</th>
</tr>
</thead>
<tbody>
{fields_template}
</tbody>
</table>"""

if self.show_null.isChecked():
field_template = """ <tr>
<th>{display}</th>
<td>[% "{name}" %]</td>
</tr>
"""
else:
field_template = """
[% with_variable(
'content',
'<tr><th>{display}</th><td>' || "{name}" || '</td></tr>',
if( "{name}" is not NULL or "{name}" <> '',@content,'')
)
%]
"""

fields = ""
for field in self.layer.fields():
name = field.name()
if name in self.layer.excludeAttributesWms():
fields += "<!-- Field {} excluded from WMS in the layer properties -->\n".format(name)
else:
fields += field_template.format(display=field.displayName(), name=name)

result = table_template.format(field=tr("Field"), value=tr("Value"), fields_template=fields)

return result
28 changes: 7 additions & 21 deletions lizmap/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
from lizmap.definitions.tooltip import ToolTipDefinitions
from lizmap.definitions.warnings import Warnings
from lizmap.dialogs.html_editor import HtmlEditorDialog
from lizmap.dialogs.html_maptip import HtmlMapTipDialog
from lizmap.dialogs.lizmap_popup import LizmapPopupDialog
from lizmap.dialogs.main import LizmapDialog
from lizmap.dialogs.scroll_message_box import ScrollMessageBox
Expand Down Expand Up @@ -2725,27 +2726,12 @@ def html_table_from_layer(self):
if not isinstance(layer, QgsVectorLayer):
return

table_template = """<table class="table table-condensed table-striped table-bordered lizmapPopupTable">
<thead>
<tr>
<th>{field}</th>
<th>{value}</th>
</tr>
</thead>
<tbody>
{fields_template}
</tbody>
</table>"""
field_template = """ <tr>
<th>{name}</th>
<td>[% "{value}" %]</td>
</tr>
"""
fields = ""
for field in layer.fields():
fields += field_template.format(name=field.displayName(), value=field.name())
result = table_template.format(field=tr("Field"), value=tr("Value"), fields_template=fields)
self._set_maptip(layer, result)
html_maptip_dialog = HtmlMapTipDialog(layer)
if not html_maptip_dialog.exec_():
return

result = html_maptip_dialog.map_tip()
self._set_maptip(layer, result, False)

def maptip_from_form(self):
""" Button set popup maptip from DND form in the Lizmap configuration. """
Expand Down
54 changes: 54 additions & 0 deletions lizmap/resources/ui/ui_html_maptip.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>391</width>
<height>128</height>
</rect>
</property>
<property name="windowTitle">
<string>HTML Maptip</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QCheckBox" name="show_null">
<property name="text">
<string>Show a row when the value is empty or NULL</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="button_box">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

0 comments on commit fb72e6e

Please sign in to comment.