forked from AG_QGIS/Plugin_SN_Basis
Aufgeräumt und Widget fixiert.
This commit is contained in:
@@ -1,40 +1,37 @@
|
|||||||
from qgis.core import QgsProject, QgsExpressionContextUtils
|
from qgis.core import QgsProject, QgsExpressionContextUtils
|
||||||
|
|
||||||
|
|
||||||
class SettingsLogic:
|
class SettingsLogic:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.project = QgsProject.instance()
|
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):
|
def save(self, fields: dict):
|
||||||
"""Speichert Felder als globale und projektbezogene Ausdrucksvariablen."""
|
"""Speichert Felder als globale und projektbezogene Ausdrucksvariablen."""
|
||||||
|
|
||||||
# 🟦 Globale Ausdrucksvariablen (nutzerspezifisch, Sitzungsspeicher)
|
# Globale Variablen
|
||||||
QgsExpressionContextUtils.setGlobalVariable("sn_amt", fields["amt"])
|
for key in self.global_vars:
|
||||||
QgsExpressionContextUtils.setGlobalVariable("sn_behoerde", fields["behoerde"])
|
QgsExpressionContextUtils.setGlobalVariable(f"sn_{key}", fields.get(key, ""))
|
||||||
QgsExpressionContextUtils.setGlobalVariable("sn_landkreis_user", fields["landkreis_user"])
|
|
||||||
QgsExpressionContextUtils.setGlobalVariable("sn_sachgebiet", fields["sachgebiet"])
|
|
||||||
|
|
||||||
# 🟨 Projektvariablen (sichtbar in Projekt → Eigenschaften → Variablen)
|
# Projektvariablen
|
||||||
QgsExpressionContextUtils.setProjectVariable(self.project, "sn_bezeichnung", fields["bezeichnung"])
|
for key in self.project_vars:
|
||||||
QgsExpressionContextUtils.setProjectVariable(self.project, "sn_verfahrensnummer", fields["verfahrensnummer"])
|
QgsExpressionContextUtils.setProjectVariable(self.project, f"sn_{key}", fields.get(key, ""))
|
||||||
QgsExpressionContextUtils.setProjectVariable(self.project, "sn_gemeinden", fields["gemeinden"])
|
|
||||||
QgsExpressionContextUtils.setProjectVariable(self.project, "sn_landkreise_proj", fields["landkreise_proj"])
|
|
||||||
|
|
||||||
print("✅ Ausdrucksvariablen gespeichert.")
|
print("✅ Ausdrucksvariablen gespeichert.")
|
||||||
|
|
||||||
def load(self) -> dict:
|
def load(self) -> dict:
|
||||||
"""Lädt Werte ausschließlich aus Ausdrucksvariablen (global + projektbezogen)."""
|
"""Lädt Werte ausschließlich aus Ausdrucksvariablen (global + projektbezogen)."""
|
||||||
|
|
||||||
return {
|
data = {}
|
||||||
# Globale Variablen (nutzerspezifisch)
|
|
||||||
"amt": QgsExpressionContextUtils.globalScope().variable("sn_amt") or "",
|
|
||||||
"behoerde": QgsExpressionContextUtils.globalScope().variable("sn_behoerde") or "",
|
|
||||||
"landkreis_user": QgsExpressionContextUtils.globalScope().variable("sn_landkreis_user") or "",
|
|
||||||
"sachgebiet": QgsExpressionContextUtils.globalScope().variable("sn_sachgebiet") or "",
|
|
||||||
|
|
||||||
# Projektvariablen
|
# Globale Variablen
|
||||||
"bezeichnung": QgsExpressionContextUtils.projectScope(self.project).variable("sn_bezeichnung") or "",
|
for key in self.global_vars:
|
||||||
"verfahrensnummer": QgsExpressionContextUtils.projectScope(self.project).variable("sn_verfahrensnummer") or "",
|
data[key] = QgsExpressionContextUtils.globalScope().variable(f"sn_{key}") or ""
|
||||||
"gemeinden": QgsExpressionContextUtils.projectScope(self.project).variable("sn_gemeinden") or "",
|
|
||||||
"landkreise_proj": QgsExpressionContextUtils.projectScope(self.project).variable("sn_landkreise_proj") or ""
|
# Projektvariablen
|
||||||
}
|
for key in self.project_vars:
|
||||||
|
data[key] = QgsExpressionContextUtils.projectScope(self.project).variable(f"sn_{key}") or ""
|
||||||
|
|
||||||
|
return data
|
||||||
|
|||||||
10
main.py
10
main.py
@@ -1,21 +1,15 @@
|
|||||||
from qgis.PyQt.QtCore import QCoreApplication
|
from qgis.PyQt.QtCore import QCoreApplication
|
||||||
from qgis.PyQt.QtGui import QIcon
|
|
||||||
from sn_basis.ui.navigation import Navigation
|
from sn_basis.ui.navigation import Navigation
|
||||||
from sn_basis.ui.dockmanager import DockManager
|
|
||||||
|
|
||||||
class BasisPlugin:
|
class BasisPlugin:
|
||||||
def __init__(self, iface):
|
def __init__(self, iface):
|
||||||
self.iface = iface
|
self.iface = iface
|
||||||
self.ui = None
|
self.ui = None
|
||||||
self.quitting = False
|
QCoreApplication.instance().aboutToQuit.connect(self.unload)
|
||||||
QCoreApplication.instance().aboutToQuit.connect(self._on_quit)
|
|
||||||
|
|
||||||
def _on_quit(self):
|
|
||||||
self.quitting = True
|
|
||||||
|
|
||||||
def initGui(self):
|
def initGui(self):
|
||||||
self.ui = Navigation(self.iface)
|
self.ui = Navigation(self.iface)
|
||||||
|
|
||||||
def unload(self):
|
def unload(self):
|
||||||
if not self.quitting and self.ui:
|
if self.ui:
|
||||||
self.ui.remove_all()
|
self.ui.remove_all()
|
||||||
|
|||||||
21
ui/base_dockwidget.py
Normal file
21
ui/base_dockwidget.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
from qgis.PyQt.QtWidgets import QDockWidget, QTabWidget
|
||||||
|
|
||||||
|
class BaseDockWidget(QDockWidget):
|
||||||
|
base_title = "LNO Sachsen"
|
||||||
|
tabs = []
|
||||||
|
|
||||||
|
def __init__(self, parent=None, subtitle=""):
|
||||||
|
super().__init__(parent)
|
||||||
|
|
||||||
|
# Titel zusammensetzen
|
||||||
|
title = self.base_title if not subtitle else f"{self.base_title} | {subtitle}"
|
||||||
|
self.setWindowTitle(title)
|
||||||
|
|
||||||
|
# Dock fixieren (Nur schließen erlaubt)
|
||||||
|
self.setFeatures(QDockWidget.DockWidgetFeature.DockWidgetClosable)
|
||||||
|
|
||||||
|
|
||||||
|
tab_widget = QTabWidget()
|
||||||
|
for tab_class in self.tabs:
|
||||||
|
tab_widget.addTab(tab_class(), getattr(tab_class, "tab_title", tab_class.__name__))
|
||||||
|
self.setWidget(tab_widget)
|
||||||
@@ -2,17 +2,17 @@ from qgis.PyQt.QtCore import Qt
|
|||||||
from qgis.PyQt.QtWidgets import QDockWidget
|
from qgis.PyQt.QtWidgets import QDockWidget
|
||||||
from qgis.utils import iface
|
from qgis.utils import iface
|
||||||
|
|
||||||
|
|
||||||
class DockManager:
|
class DockManager:
|
||||||
default_area = Qt.DockWidgetArea.RightDockWidgetArea
|
default_area = Qt.DockWidgetArea.RightDockWidgetArea
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def show(cls, dock_widget, area=None):
|
def show(cls, dock_widget, area=None):
|
||||||
if area is None:
|
area = area or cls.default_area
|
||||||
area = cls.default_area
|
|
||||||
|
|
||||||
# Alle vorhandenen Plugin-Docks schließen
|
# Bestehende Plugin-Docks mit Präfix schließen
|
||||||
for widget in iface.mainWindow().findChildren(QDockWidget):
|
for widget in iface.mainWindow().findChildren(QDockWidget):
|
||||||
if widget != dock_widget and widget.objectName().startswith("sn_dock_"):
|
if widget is not dock_widget and widget.objectName().startswith("sn_dock_"):
|
||||||
iface.removeDockWidget(widget)
|
iface.removeDockWidget(widget)
|
||||||
widget.deleteLater()
|
widget.deleteLater()
|
||||||
|
|
||||||
|
|||||||
@@ -13,25 +13,22 @@ class Navigation:
|
|||||||
iface.addToolBar(self.toolbar)
|
iface.addToolBar(self.toolbar)
|
||||||
|
|
||||||
def add_action(self, text, callback, tooltip="", priority=100, icon=None):
|
def add_action(self, text, callback, tooltip="", priority=100, icon=None):
|
||||||
action = QAction(icon, text, self.iface.mainWindow()) if icon else QAction(text, self.iface.mainWindow())
|
action = QAction(text, self.iface.mainWindow())
|
||||||
|
if icon:
|
||||||
|
action.setIcon(icon)
|
||||||
if tooltip:
|
if tooltip:
|
||||||
action.setToolTip(tooltip)
|
action.setToolTip(tooltip)
|
||||||
action.triggered.connect(callback)
|
action.triggered.connect(callback)
|
||||||
|
|
||||||
# Action mit Priority speichern
|
|
||||||
self.actions.append((priority, action))
|
self.actions.append((priority, action))
|
||||||
return action
|
return action
|
||||||
|
|
||||||
def finalize_menu_and_toolbar(self):
|
def finalize_menu_and_toolbar(self):
|
||||||
# Sortieren nach Priority
|
# Menü und Toolbar leeren
|
||||||
self.actions.sort(key=lambda x: x[0])
|
|
||||||
|
|
||||||
# Menüeinträge
|
|
||||||
self.menu.clear()
|
self.menu.clear()
|
||||||
for _, action in self.actions:
|
|
||||||
self.menu.addAction(action)
|
|
||||||
|
|
||||||
# Toolbar-Einträge
|
|
||||||
self.toolbar.clear()
|
self.toolbar.clear()
|
||||||
for _, action in self.actions:
|
|
||||||
|
# Sortiert nach Priority einfügen
|
||||||
|
for _, action in sorted(self.actions, key=lambda x: x[0]):
|
||||||
|
self.menu.addAction(action)
|
||||||
self.toolbar.addAction(action)
|
self.toolbar.addAction(action)
|
||||||
|
|||||||
@@ -6,44 +6,46 @@ from sn_basis.logic.settings_logic import SettingsLogic
|
|||||||
|
|
||||||
|
|
||||||
class SettingsTab(QWidget):
|
class SettingsTab(QWidget):
|
||||||
|
tab_title = "Projekteigenschaften" # Titel für den Tab
|
||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.logic = SettingsLogic()
|
self.logic = SettingsLogic()
|
||||||
|
|
||||||
main_layout = QVBoxLayout()
|
main_layout = QVBoxLayout()
|
||||||
|
|
||||||
|
# Definition der Felder
|
||||||
|
self.user_fields = {
|
||||||
|
"amt": "Amt:",
|
||||||
|
"behoerde": "Behörde:",
|
||||||
|
"landkreis_user": "Landkreis:",
|
||||||
|
"sachgebiet": "Sachgebiet:"
|
||||||
|
}
|
||||||
|
self.project_fields = {
|
||||||
|
"bezeichnung": "Bezeichnung:",
|
||||||
|
"verfahrensnummer": "Verfahrensnummer:",
|
||||||
|
"gemeinden": "Gemeinde(n):",
|
||||||
|
"landkreise_proj": "Landkreis(e):"
|
||||||
|
}
|
||||||
|
|
||||||
# 🟦 Benutzerspezifische Festlegungen
|
# 🟦 Benutzerspezifische Festlegungen
|
||||||
user_group = QGroupBox("Benutzerspezifische Festlegungen")
|
user_group = QGroupBox("Benutzerspezifische Festlegungen")
|
||||||
user_layout = QGridLayout()
|
user_layout = QGridLayout()
|
||||||
self.amt = QLineEdit()
|
self.user_inputs = {}
|
||||||
self.behoerde = QLineEdit()
|
for row, (key, label) in enumerate(self.user_fields.items()):
|
||||||
self.landkreis_user = QLineEdit()
|
self.user_inputs[key] = QLineEdit()
|
||||||
self.sachgebiet = QLineEdit()
|
user_layout.addWidget(QLabel(label), row, 0)
|
||||||
user_layout.addWidget(QLabel("Amt:"), 0, 0)
|
user_layout.addWidget(self.user_inputs[key], row, 1)
|
||||||
user_layout.addWidget(self.amt, 0, 1)
|
|
||||||
user_layout.addWidget(QLabel("Behörde:"), 1, 0)
|
|
||||||
user_layout.addWidget(self.behoerde, 1, 1)
|
|
||||||
user_layout.addWidget(QLabel("Landkreis:"), 2, 0)
|
|
||||||
user_layout.addWidget(self.landkreis_user, 2, 1)
|
|
||||||
user_layout.addWidget(QLabel("Sachgebiet:"), 3, 0)
|
|
||||||
user_layout.addWidget(self.sachgebiet, 3, 1)
|
|
||||||
user_group.setLayout(user_layout)
|
user_group.setLayout(user_layout)
|
||||||
|
|
||||||
# 🟨 Projektspezifische Festlegungen
|
# 🟨 Projektspezifische Festlegungen
|
||||||
project_group = QGroupBox("Projektspezifische Festlegungen")
|
project_group = QGroupBox("Projektspezifische Festlegungen")
|
||||||
project_layout = QGridLayout()
|
project_layout = QGridLayout()
|
||||||
self.bezeichnung = QLineEdit()
|
self.project_inputs = {}
|
||||||
self.verfahrensnummer = QLineEdit()
|
for row, (key, label) in enumerate(self.project_fields.items()):
|
||||||
self.gemeinden = QLineEdit()
|
self.project_inputs[key] = QLineEdit()
|
||||||
self.landkreise_proj = QLineEdit()
|
project_layout.addWidget(QLabel(label), row, 0)
|
||||||
project_layout.addWidget(QLabel("Bezeichnung:"), 0, 0)
|
project_layout.addWidget(self.project_inputs[key], row, 1)
|
||||||
project_layout.addWidget(self.bezeichnung, 0, 1)
|
|
||||||
project_layout.addWidget(QLabel("Verfahrensnummer:"), 1, 0)
|
|
||||||
project_layout.addWidget(self.verfahrensnummer, 1, 1)
|
|
||||||
project_layout.addWidget(QLabel("Gemeinde(n):"), 2, 0)
|
|
||||||
project_layout.addWidget(self.gemeinden, 2, 1)
|
|
||||||
project_layout.addWidget(QLabel("Landkreis(e):"), 3, 0)
|
|
||||||
project_layout.addWidget(self.landkreise_proj, 3, 1)
|
|
||||||
project_group.setLayout(project_layout)
|
project_group.setLayout(project_layout)
|
||||||
|
|
||||||
# 🟩 Speichern-Button
|
# 🟩 Speichern-Button
|
||||||
@@ -60,25 +62,11 @@ class SettingsTab(QWidget):
|
|||||||
self.load_data()
|
self.load_data()
|
||||||
|
|
||||||
def save_data(self):
|
def save_data(self):
|
||||||
fields = {
|
# Alle Felder zusammenführen
|
||||||
"amt": self.amt.text(),
|
fields = {key: widget.text() for key, widget in {**self.user_inputs, **self.project_inputs}.items()}
|
||||||
"behoerde": self.behoerde.text(),
|
|
||||||
"landkreis_user": self.landkreis_user.text(),
|
|
||||||
"sachgebiet": self.sachgebiet.text(),
|
|
||||||
"bezeichnung": self.bezeichnung.text(),
|
|
||||||
"verfahrensnummer": self.verfahrensnummer.text(),
|
|
||||||
"gemeinden": self.gemeinden.text(),
|
|
||||||
"landkreise_proj": self.landkreise_proj.text()
|
|
||||||
}
|
|
||||||
self.logic.save(fields)
|
self.logic.save(fields)
|
||||||
|
|
||||||
def load_data(self):
|
def load_data(self):
|
||||||
data = self.logic.load()
|
data = self.logic.load()
|
||||||
self.amt.setText(data["amt"])
|
for key, widget in {**self.user_inputs, **self.project_inputs}.items():
|
||||||
self.behoerde.setText(data["behoerde"])
|
widget.setText(data.get(key, ""))
|
||||||
self.landkreis_user.setText(data["landkreis_user"])
|
|
||||||
self.sachgebiet.setText(data["sachgebiet"])
|
|
||||||
self.bezeichnung.setText(data["bezeichnung"])
|
|
||||||
self.verfahrensnummer.setText(data["verfahrensnummer"])
|
|
||||||
self.gemeinden.setText(data["gemeinden"])
|
|
||||||
self.landkreise_proj.setText(data["landkreise_proj"])
|
|
||||||
|
|||||||
Reference in New Issue
Block a user