From 216af58f5d342ce93110679df042bdb20994a5fb Mon Sep 17 00:00:00 2001 From: Ismail Sunni Date: Wed, 11 Sep 2024 14:07:58 +0200 Subject: [PATCH] Add Layer Definition directly to QGIS. --- qgis_hub_plugin/gui/constants.py | 2 +- qgis_hub_plugin/gui/resource_browser.py | 55 +++++++++++++++++++++++-- qgis_hub_plugin/utilities/common.py | 5 ++- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/qgis_hub_plugin/gui/constants.py b/qgis_hub_plugin/gui/constants.py index 5b3cb9e..d3f6d4d 100644 --- a/qgis_hub_plugin/gui/constants.py +++ b/qgis_hub_plugin/gui/constants.py @@ -13,4 +13,4 @@ class ResoureType: Style = "Style" Geopackage = "Geopackage" Model3D = "3DModel" - QGISLayer = "LayerDefinition" + LayerDefinition = "LayerDefinition" diff --git a/qgis_hub_plugin/gui/resource_browser.py b/qgis_hub_plugin/gui/resource_browser.py index 89aad67..9d4ee62 100644 --- a/qgis_hub_plugin/gui/resource_browser.py +++ b/qgis_hub_plugin/gui/resource_browser.py @@ -5,7 +5,14 @@ from functools import partial from pathlib import Path -from qgis.core import Qgis, QgsApplication, QgsProject, QgsStyle, QgsVectorLayer +from qgis.core import ( + Qgis, + QgsApplication, + QgsLayerDefinition, + QgsProject, + QgsStyle, + QgsVectorLayer, +) from qgis.gui import QgsMessageBar from qgis.PyQt import uic from qgis.PyQt.QtCore import ( @@ -38,7 +45,11 @@ ) from qgis_hub_plugin.gui.resource_item import AttributeSortingItem, ResourceItem from qgis_hub_plugin.toolbelt import PlgLogger, PlgOptionsManager -from qgis_hub_plugin.utilities.common import download_file, download_resource_thumbnail +from qgis_hub_plugin.utilities.common import ( + QGIS_HUB_DIR, + download_file, + download_resource_thumbnail, +) from qgis_hub_plugin.utilities.qgis_util import show_busy_cursor UI_CLASS = uic.loadUiType( @@ -249,7 +260,7 @@ def update_checkbox_states(self): ResoureType.Style: style_checked, ResoureType.Model: model_checked, ResoureType.Model3D: model3d_checked, - ResoureType.QGISLayer: layer_definition_checked, + ResoureType.LayerDefinition: layer_definition_checked, } def update_resource_filter(self): @@ -314,6 +325,11 @@ def update_custom_button(self): self.addQGISPushButton.setToolTip( self.tr("Download and load the layers to QGIS") ) + elif self.selected_resource.resource_type == ResoureType.LayerDefinition: + self.addQGISPushButton.setText(self.tr("Add Layer to QGIS")) + self.addQGISPushButton.setToolTip( + self.tr("Load the layer definition to QGIS") + ) else: self.addQGISPushButton.setVisible(False) @@ -399,6 +415,8 @@ def add_resource_to_qgis(self): self.add_style_to_qgis() elif self.selected_resource.resource_type == ResoureType.Geopackage: self.add_geopackage_to_qgis() + elif self.selected_resource.resource_type == ResoureType.LayerDefinition: + self.add_layer_definition_to_qgis() @show_busy_cursor def add_model_to_qgis(self): @@ -547,6 +565,37 @@ def add_style_to_qgis(self): self.tr(f"Style {resource.name} is not added to QGIS") ) + @show_busy_cursor + def add_layer_definition_to_qgis(self): + resource = self.selected_resource + + layer_definition_dir = QGIS_HUB_DIR / "layer_definitions" + file_path = layer_definition_dir / f"{resource.name}.qlr" + + if not layer_definition_dir.exists(): + layer_definition_dir.mkdir(parents=True, exist_ok=True) + + if not download_resource_file(resource.file, file_path): + self.show_error_message(self.tr(f"Download failed for {resource.name}")) + return + + current_project = QgsProject.instance() + + self.load_layer_definition(current_project, resource.name, file_path) + + def load_layer_definition(self, project, layer_name, layer_definition_file_path): + success, message = QgsLayerDefinition.loadLayerDefinition( + str(layer_definition_file_path), project, project.layerTreeRoot() + ) + if success: + self.show_success_message( + self.tr(f"Successfully load layer definition: {layer_name}") + ) + else: + self.show_error_message( + self.tr(f"Failed to load layer definition: {layer_name}: {message}") + ) + def update_title_bar(self): num_total_resources = len(self.resources) num_selected_resources = self.proxy_model.rowCount() diff --git a/qgis_hub_plugin/utilities/common.py b/qgis_hub_plugin/utilities/common.py index e4335ad..8eee7a1 100644 --- a/qgis_hub_plugin/utilities/common.py +++ b/qgis_hub_plugin/utilities/common.py @@ -8,6 +8,8 @@ from qgis_hub_plugin.toolbelt import PlgLogger from qgis_hub_plugin.utilities.file_downloader import FileDownloader +QGIS_HUB_DIR = Path(QgsApplication.qgisSettingsDirPath(), "qgis_hub") + def get_icon(icon_name: str) -> QIcon: full_path = get_icon_path(icon_name) @@ -36,7 +38,6 @@ def download_resource_thumbnail(url: str, uuid: str) -> Path: PlgLogger.log(f"UUID: {uuid} has URL == None: {url}") return Path(get_icon_path("QGIS_Hub_icon.svg")) - qgis_user_dir = QgsApplication.qgisSettingsDirPath() # Assume it as jpg extension = ".jpg" try: @@ -44,7 +45,7 @@ def download_resource_thumbnail(url: str, uuid: str) -> Path: except IndexError as e: PlgLogger.log(f"UUID: {uuid} on URL: {url} get index error: {e}") - thumbnail_dir = Path(qgis_user_dir, "qgis_hub", "thumbnails") + thumbnail_dir = Path(QGIS_HUB_DIR, "thumbnails") thumbnail_path = Path(thumbnail_dir, f"{uuid}.{extension}") if not thumbnail_dir.exists(): thumbnail_dir.mkdir(parents=True, exist_ok=True)