From f8cfda0fe35c575f7a37a84ed9977338776a062e Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Tue, 5 Dec 2023 12:33:46 +0100 Subject: [PATCH] UX - Display a message when : base layer has been added, also if QGS legend items != CFG legend items --- lizmap/dialogs/main.py | 3 +++ lizmap/plugin.py | 43 +++++++++++++++++++++++++++++++-- lizmap/project_checker_tools.py | 15 ++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lizmap/dialogs/main.py b/lizmap/dialogs/main.py index fa63d801..de07a924 100755 --- a/lizmap/dialogs/main.py +++ b/lizmap/dialogs/main.py @@ -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() diff --git a/lizmap/plugin.py b/lizmap/plugin.py index a02dca98..e96e1cf1 100755 --- a/lizmap/plugin.py +++ b/lizmap/plugin.py @@ -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, @@ -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 @@ -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"): @@ -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: @@ -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): diff --git a/lizmap/project_checker_tools.py b/lizmap/project_checker_tools.py index a2be9638..1ca4195a 100644 --- a/lizmap/project_checker_tools.py +++ b/lizmap/project_checker_tools.py @@ -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():