Ablauf optimiert, Styles laden implementiert

This commit is contained in:
Michael Otto
2025-11-18 16:10:40 +01:00
parent a12fdae45e
commit 4336a70cff
4 changed files with 388 additions and 0 deletions

0
functions/__init__.py Normal file
View File

44
functions/messages.py Normal file
View File

@@ -0,0 +1,44 @@
# sn_basis/functions/messages.py
from typing import Optional
from qgis.core import Qgis
from qgis.PyQt.QtWidgets import QWidget
from qgis.utils import iface
def push_message(
level: Qgis.MessageLevel,
title: str,
text: str,
duration: Optional[int] = 5,
parent: Optional[QWidget] = None,
):
"""
Zeigt eine Meldung in der QGIS-MessageBar.
- level: Qgis.Success | Qgis.Info | Qgis.Warning | Qgis.Critical
- title: Überschrift links (kurz halten)
- text: eigentliche Nachricht
- duration: Sekunden bis Auto-Ausblendung; None => bleibt sichtbar (mit Close-Button)
- parent: optionales Eltern-Widget (für Kontext), normalerweise nicht nötig
Rückgabe: MessageBarItem-Widget (kann später geschlossen/entfernt werden).
"""
bar = iface.messageBar()
# QGIS akzeptiert None als "sticky" Meldung
return bar.pushMessage(title, text, level=level, duration=duration)
def success(title: str, text: str, duration: int = 5):
return push_message(Qgis.Success, title, text, duration)
def info(title: str, text: str, duration: int = 5):
return push_message(Qgis.Info, title, text, duration)
def warning(title: str, text: str, duration: int = 5):
return push_message(Qgis.Warning, title, text, duration)
def error(title: str, text: str, duration: Optional[int] = 5):
# Fehler evtl. länger sichtbar lassen; setze duration=None falls gewünscht
return push_message(Qgis.Critical, title, text, duration)

28
functions/styles.py Normal file
View File

@@ -0,0 +1,28 @@
# sn_basis/functions/styles.py
import os
from qgis.core import QgsVectorLayer
def apply_style(layer: QgsVectorLayer, style_name: str) -> bool:
"""
Lädt einen QML-Style aus dem styles-Ordner des Plugins und wendet ihn auf den Layer an.
style_name: Dateiname ohne Pfad, z.B. 'verfahrensgebiet.qml'
Rückgabe: True bei Erfolg, False sonst
"""
if not layer or not layer.isValid():
return False
# Basis-Pfad: sn_basis/styles
base_dir = os.path.dirname(os.path.dirname(__file__)) # geht von functions/ eins hoch
style_path = os.path.join(base_dir, "styles", style_name)
if not os.path.exists(style_path):
print(f"Style-Datei nicht gefunden: {style_path}")
return False
ok, error_msg = layer.loadNamedStyle(style_path)
if not ok:
print(f"Style konnte nicht geladen werden: {error_msg}")
return False
layer.triggerRepaint()
return True