Skip to content

Commit

Permalink
Add Layer Definition directly to QGIS.
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailsunni committed Sep 11, 2024
1 parent 8710ecf commit 216af58
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion qgis_hub_plugin/gui/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class ResoureType:
Style = "Style"
Geopackage = "Geopackage"
Model3D = "3DModel"
QGISLayer = "LayerDefinition"
LayerDefinition = "LayerDefinition"
55 changes: 52 additions & 3 deletions qgis_hub_plugin/gui/resource_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions qgis_hub_plugin/utilities/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -36,15 +38,14 @@ 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:
extension = url.split(".")[-1]
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)
Expand Down

0 comments on commit 216af58

Please sign in to comment.