Files
Plugin_SN_Basis/functions/settings_logic.py
2025-11-20 12:18:38 +01:00

38 lines
1.3 KiB
Python

from qgis.core import QgsProject, QgsExpressionContextUtils
class SettingsLogic:
def __init__(self):
self.project = QgsProject.instance()
# Definition der Variablen-Namen
self.global_vars = ["amt", "behoerde", "landkreis_user", "sachgebiet"]
self.project_vars = ["bezeichnung", "verfahrensnummer", "gemeinden", "landkreise_proj"]
def save(self, fields: dict):
"""Speichert Felder als globale und projektbezogene Ausdrucksvariablen."""
# Globale Variablen
for key in self.global_vars:
QgsExpressionContextUtils.setGlobalVariable(f"sn_{key}", fields.get(key, ""))
# Projektvariablen
for key in self.project_vars:
QgsExpressionContextUtils.setProjectVariable(self.project, f"sn_{key}", fields.get(key, ""))
print("✅ Ausdrucksvariablen gespeichert.")
def load(self) -> dict:
"""Lädt Werte ausschließlich aus Ausdrucksvariablen (global + projektbezogen)."""
data = {}
# Globale Variablen
for key in self.global_vars:
data[key] = QgsExpressionContextUtils.globalScope().variable(f"sn_{key}") or ""
# Projektvariablen
for key in self.project_vars:
data[key] = QgsExpressionContextUtils.projectScope(self.project).variable(f"sn_{key}") or ""
return data