Skip to content

Commit

Permalink
UX - Display a message when : base layer has been added, also if QGS …
Browse files Browse the repository at this point in the history
…legend items != CFG legend items
  • Loading branch information
Gustry committed Dec 5, 2023
1 parent 340321a commit f8cfda0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lizmap/dialogs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@ def setup_icons(self):
self.mOptionsListWidget.setIconSize(QSize(20, 20))
i = 0

# If adding a new panel, all mOptionsListWidget.item(X) must be checked
# definitions/online_help.py about mapping as well

# Information
# It must be the first tab, with index 0.
icon = QIcon()
Expand Down
43 changes: 41 additions & 2 deletions lizmap/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
PREVENT_ECW,
PREVENT_OTHER_DRIVE,
PREVENT_SERVICE,
count_legend_items,
duplicated_layer_name_or_group,
duplicated_layer_with_filter,
project_invalid_pk,
Expand Down Expand Up @@ -239,6 +240,7 @@ def __init__(self, iface):
self.current_path = None
# noinspection PyUnresolvedReferences
self.project.fileNameChanged.connect(self.filename_changed)
self.project.projectSaved.connect(self.project_saved)
self.filename_changed()
self.update_plugin = None

Expand Down Expand Up @@ -816,6 +818,34 @@ def write_log_message(message, tag, level):
self.help_action = None
self.help_action_cloud = None

def project_saved(self):
""" When the project is saved. """
if not self.dlg.check_cfg_file_exists():
return

if self.layerList is None:
# The user didn't open the plugin since QGIS has started
# Sorry, we don't know if the user added/removed layers, maybe nothing
return

# Check the number of layers between the project and the Lizmap configuration file.
count_cfg = len(self.layerList.keys())
count_qgs = count_legend_items(self.project.layerTreeRoot(), self.project, 0)
if count_cfg != count_qgs:
self.iface.messageBar().pushMessage(
'Lizmap',
tr(
'The project has {count_qgs} items in the legend, while the Lizmap configuration has {count_cfg} '
'items. Please open the plugin to sync the "{layer_tab}" tab.'
).format(
count_qgs=count_qgs,
count_cfg=count_cfg,
layer_tab=self.dlg.mOptionsListWidget.item(2).text()
),
Qgis.Warning,
duration=DURATION_WARNING_BAR,
)

def filename_changed(self):
""" When the current project has been renamed. """
if os.getenv("QGIS_PLUGIN_AUTO_SAVING"):
Expand Down Expand Up @@ -2543,6 +2573,8 @@ def _add_base_layer(self, source: str, name: str, attribution_url: str = None, a
""" Add a base layer to the "baselayers" group. """
self.add_group_baselayers()
raster = QgsRasterLayer(source, name, 'wms')
self.project.addMapLayer(raster, False) # False to not add it in the legend, only in the project

if attribution_url:
raster.setAttributionUrl(attribution_url)
if attribution_name:
Expand All @@ -2553,9 +2585,16 @@ def _add_base_layer(self, source: str, name: str, attribution_url: str = None, a
for qgis_group in groups:
qgis_group: QgsLayerTreeGroup
if qgis_group.name() == 'baselayers':
self.project.addMapLayer(raster, False) # False is the key
qgis_group.addLayer(raster)
return
break

self.dlg.display_message_bar(
tr('New layer'),
tr('Please close and reopen the dialog to display your layer in the tab "{tab_name}".').format(
tab_name=self.dlg.mOptionsListWidget.item(2).text()
),
Qgis.Warning,
)

@staticmethod
def string_to_list(text):
Expand Down
15 changes: 15 additions & 0 deletions lizmap/project_checker_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,21 @@ def project_trust_layer_metadata(project: QgsProject, fix: bool = False) -> bool
return True


def count_legend_items(layer_tree: QgsLayerTreeNode, project, count: int) -> int:
""" Count all items in the project legend. """
for child in layer_tree.children():
# noinspection PyArgumentList
if QgsLayerTree.isLayer(child):
count += 1
else:
child = cast_to_group(child)
count += 1
# Recursive call
count = count_legend_items(child, project, count)

return count


def trailing_layer_group_name(layer_tree: QgsLayerTreeNode, project, results: List) -> List:
""" Check for a trailing space in layer or group name. """
for child in layer_tree.children():
Expand Down

0 comments on commit f8cfda0

Please sign in to comment.