forked from AG_QGIS/Plugin_SN_Basis
Funktionen von ligic nach functions verschoben
This commit is contained in:
37
functions/settings_logic.py
Normal file
37
functions/settings_logic.py
Normal file
@@ -0,0 +1,37 @@
|
||||
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
|
||||
35
functions/variable_utils.py
Normal file
35
functions/variable_utils.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from qgis.core import QgsProject, QgsExpressionContextUtils
|
||||
|
||||
def get_variable(key: str, scope: str = "project") -> str:
|
||||
"""
|
||||
Liefert den Wert einer sn_* Variable zurück.
|
||||
key: Name ohne Präfix, z.B. "verfahrensnummer"
|
||||
scope: 'project' oder 'global'
|
||||
"""
|
||||
projekt = QgsProject.instance()
|
||||
var_name = f"sn_{key}"
|
||||
|
||||
if scope == "project":
|
||||
return QgsExpressionContextUtils.projectScope(projekt).variable(var_name) or ""
|
||||
elif scope == "global":
|
||||
return QgsExpressionContextUtils.globalScope().variable(var_name) or ""
|
||||
else:
|
||||
raise ValueError("Scope muss 'project' oder 'global' sein.")
|
||||
|
||||
|
||||
def set_variable(key: str, value: str, scope: str = "project"):
|
||||
"""
|
||||
Schreibt den Wert einer sn_* Variable.
|
||||
key: Name ohne Präfix, z.B. "verfahrensnummer"
|
||||
value: Wert, der gespeichert werden soll
|
||||
scope: 'project' oder 'global'
|
||||
"""
|
||||
projekt = QgsProject.instance()
|
||||
var_name = f"sn_{key}"
|
||||
|
||||
if scope == "project":
|
||||
QgsExpressionContextUtils.setProjectVariable(projekt, var_name, value)
|
||||
elif scope == "global":
|
||||
QgsExpressionContextUtils.setGlobalVariable(var_name, value)
|
||||
else:
|
||||
raise ValueError("Scope muss 'project' oder 'global' sein.")
|
||||
Reference in New Issue
Block a user