2025-11-17 10:05:42 +01:00
|
|
|
from qgis.core import QgsProject, QgsExpressionContextUtils
|
|
|
|
|
|
|
|
|
|
class SettingsLogic:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.project = QgsProject.instance()
|
|
|
|
|
|
2025-11-17 11:29:04 +01:00
|
|
|
# Definition der Variablen-Namen
|
|
|
|
|
self.global_vars = ["amt", "behoerde", "landkreis_user", "sachgebiet"]
|
|
|
|
|
self.project_vars = ["bezeichnung", "verfahrensnummer", "gemeinden", "landkreise_proj"]
|
|
|
|
|
|
2025-11-17 10:05:42 +01:00
|
|
|
def save(self, fields: dict):
|
|
|
|
|
"""Speichert Felder als globale und projektbezogene Ausdrucksvariablen."""
|
|
|
|
|
|
2025-11-17 11:29:04 +01:00
|
|
|
# Globale Variablen
|
|
|
|
|
for key in self.global_vars:
|
|
|
|
|
QgsExpressionContextUtils.setGlobalVariable(f"sn_{key}", fields.get(key, ""))
|
2025-11-17 10:05:42 +01:00
|
|
|
|
2025-11-17 11:29:04 +01:00
|
|
|
# Projektvariablen
|
|
|
|
|
for key in self.project_vars:
|
|
|
|
|
QgsExpressionContextUtils.setProjectVariable(self.project, f"sn_{key}", fields.get(key, ""))
|
2025-11-17 10:05:42 +01:00
|
|
|
|
|
|
|
|
print("✅ Ausdrucksvariablen gespeichert.")
|
|
|
|
|
|
|
|
|
|
def load(self) -> dict:
|
|
|
|
|
"""Lädt Werte ausschließlich aus Ausdrucksvariablen (global + projektbezogen)."""
|
|
|
|
|
|
2025-11-17 11:29:04 +01:00
|
|
|
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
|