Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21 kuvakkeellisen esitystavan tuki templaateille #22

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion arho_feature_template/core/feature_template_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from qgis.core import QgsFeature, QgsProject, QgsVectorLayer
from qgis.gui import QgsMapToolDigitizeFeature
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QStandardItem, QStandardItemModel
from qgis.PyQt.QtGui import QIcon, QStandardItem, QStandardItemModel
from qgis.utils import iface

from arho_feature_template.core.template_library_config import (
Expand All @@ -18,10 +18,16 @@
from arho_feature_template.gui.feature_attribute_form import FeatureAttributeForm
from arho_feature_template.gui.template_dock import TemplateLibraryDock
from arho_feature_template.resources.template_libraries import library_config_files
from arho_feature_template.utils import get_icon_file_path

logger = logging.getLogger(__name__)


class IconFileNotFoundError(Exception):
def __init__(self, icon_file: str):
super().__init__(f"Icon file {icon_file} not found")


class LayerNotFoundError(Exception):
def __init__(self, layer_name: str):
super().__init__(f"Layer {layer_name} not found")
Expand Down Expand Up @@ -58,6 +64,17 @@ def __init__(self, template_config: FeatureTemplate) -> None:

self.setCheckable(True)

if template_config.icon_file:
self.set_icon_from_config()

def set_icon_from_config(self):
icon_file = self.config.icon_file
icon_fp = get_icon_file_path(icon_file)
if icon_fp:
self.setIcon(QIcon(str(icon_fp)))
else:
raise IconFileNotFoundError(icon_file)

def is_valid(self) -> bool:
"""Check if the template is valid agains current QGIS project

Expand Down
2 changes: 2 additions & 0 deletions arho_feature_template/core/template_library_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ class FeatureTemplate:
name: str
description: str | None
feature: Feature
icon_file: str | None

@classmethod
def from_dict(cls, data: dict) -> FeatureTemplate:
return cls(
name=data["name"],
description=data.get("description"),
feature=Feature.from_dict(data["feature"]),
icon_file=data.get("icon_file"),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ templates:
default: 1
- name: Asuin-, liike- ja toimistorakennusten alue
description: Aluella kuvataan ...
icon_file: ak.jpg
feature:
layer: land_use_area
attributes:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions arho_feature_template/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import annotations

from pathlib import Path


def get_icon_file_path(
icon_file: str, icons_directory: Path = Path(__file__).parent / "resources/template_libraries/icons"
) -> Path | None:
icon_full_fp = icons_directory / icon_file
if icon_full_fp.exists():
return icon_full_fp.resolve()
return None
Loading