From 4336a70cff6c73b0103c74a739eebdf716d6605f Mon Sep 17 00:00:00 2001 From: Michael Otto Date: Tue, 18 Nov 2025 16:10:40 +0100 Subject: [PATCH] Ablauf optimiert, Styles laden implementiert --- functions/__init__.py | 0 functions/messages.py | 44 +++++ functions/styles.py | 28 ++++ styles/verfahrensgebiet.qml | 316 ++++++++++++++++++++++++++++++++++++ 4 files changed, 388 insertions(+) create mode 100644 functions/__init__.py create mode 100644 functions/messages.py create mode 100644 functions/styles.py create mode 100644 styles/verfahrensgebiet.qml diff --git a/functions/__init__.py b/functions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/functions/messages.py b/functions/messages.py new file mode 100644 index 0000000..fadc330 --- /dev/null +++ b/functions/messages.py @@ -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) diff --git a/functions/styles.py b/functions/styles.py new file mode 100644 index 0000000..0723717 --- /dev/null +++ b/functions/styles.py @@ -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 diff --git a/styles/verfahrensgebiet.qml b/styles/verfahrensgebiet.qml new file mode 100644 index 0000000..5504107 --- /dev/null +++ b/styles/verfahrensgebiet.qml @@ -0,0 +1,316 @@ + + + + 1 + 1 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 0 + 0 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + . + + 0 + . + + 0 + generatedlayout + + + + + + + + + + + + + + + + + + + + + + COALESCE( "name", '<NULL>' ) + + 2 +