Files
Plugin_SN_Plan41/ui/tab_b_ui.py

159 lines
5.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
sn_plan41/ui/tab_b_ui.py UI für Tab B (Druck)
"""
from __future__ import annotations
from typing import Optional
from sn_basis.functions.qt_wrapper import (
QWidget,
QVBoxLayout,
QLabel,
QComboBox,
)
from sn_basis.functions.qgisui_wrapper import iface
from sn_basis.functions.variable_wrapper import get_variable, set_variable
# Services (werden von DockWidget injiziert)
from sn_basis.modules.Pruefmanager import Pruefmanager
from sn_basis.modules.DataGrabber import DataGrabber
from sn_plan41.ui.tab_b_logic import (
TabBLogic,
MASSSTAB_WIE_KARTENFENSTER,
PLOTMASSSTAB_BY_AUSWAHL,
)
THEMA_VAR = "tab_b_thema"
THEMA_PLACEHOLDER = "Thema wählen"
THEMA_38 = "§38"
THEMA_41 = "§41"
MASSSTAB_VAR = "tab_b_massstab"
class TabB(QWidget):
"""
UI-Klasse für Tab B (Druck) des Plan41-Plugins.
Zuständig für:
- Auswahl des Druckthemas
- Auswahl der Druckparameter
- Start der Vorlagenanlage (Druck über QGIS-Druckfunktion)
Services (Pruefmanager, DataGrabber) werden zur Laufzeit vom DockWidget injiziert.
Alle fachlichen Prüfungen laufen über den zentralen Pruefmanager.
"""
tab_title = "Druck" #: Tab-Titel für BaseDockWidget
def __init__(self, parent: Optional[QWidget] = None):
"""
Initialisiert die UI-Struktur.
Services werden später über :meth:`set_services` injiziert.
:param parent: Parent-Widget (typischerweise DockWidget)
"""
super().__init__(parent)
# Services (werden von DockWidget gesetzt)
self.pruefmanager: Optional[Pruefmanager] = None
self.logic: Optional[TabBLogic] = None
self._thema_combo: Optional[QComboBox] = None
self._massstab_combo: Optional[QComboBox] = None
self._build_ui()
self._restore_state()
def set_services(self, pruefmanager: Pruefmanager, data_grabber: DataGrabber) -> None:
"""Injiziert Services vom übergeordneten DockWidget."""
_ = data_grabber
self.pruefmanager = pruefmanager
self.logic = TabBLogic(pruefmanager=self.pruefmanager)
if self._thema_combo:
self.logic.set_kartenname_for_thema(self._thema_combo.currentText())
if self._massstab_combo:
self.logic.set_plotmassstab_for_auswahl(
self._massstab_combo.currentText(),
self._get_current_canvas_scale(),
)
def _build_ui(self) -> None:
"""Erstellt die reduzierte UI für die Themenauswahl."""
main_layout = QVBoxLayout()
main_layout.setSpacing(4)
main_layout.setContentsMargins(4, 4, 4, 4)
thema_label = QLabel("Thema")
thema_label.setStyleSheet("font-weight: bold; margin-top: 6px;")
main_layout.addWidget(thema_label)
self._thema_combo = QComboBox(self)
self._thema_combo.addItem(THEMA_PLACEHOLDER)
self._thema_combo.addItem(THEMA_38)
self._thema_combo.addItem(THEMA_41)
self._thema_combo.currentTextChanged.connect(self._on_thema_changed)
main_layout.addWidget(self._thema_combo)
massstab_label = QLabel("Massstab")
massstab_label.setStyleSheet("font-weight: bold; margin-top: 6px;")
main_layout.addWidget(massstab_label)
self._massstab_combo = QComboBox(self)
self._massstab_combo.addItem(MASSSTAB_WIE_KARTENFENSTER)
self._massstab_combo.addItems(list(PLOTMASSSTAB_BY_AUSWAHL.keys()))
self._massstab_combo.currentTextChanged.connect(self._on_massstab_changed)
main_layout.addWidget(self._massstab_combo)
main_layout.addStretch(1)
self.setLayout(main_layout)
def _restore_state(self) -> None:
"""Stellt die gespeicherten Combobox-Zustände wieder her."""
if not self._thema_combo or not self._massstab_combo:
return
saved_thema = get_variable(THEMA_VAR, scope="project")
if saved_thema in (THEMA_38, THEMA_41):
self._thema_combo.setCurrentText(saved_thema)
else:
self._thema_combo.setCurrentText(THEMA_PLACEHOLDER)
saved_massstab = get_variable(MASSSTAB_VAR, scope="project")
valid_massstaebe = [MASSSTAB_WIE_KARTENFENSTER, *PLOTMASSSTAB_BY_AUSWAHL.keys()]
if saved_massstab in valid_massstaebe:
self._massstab_combo.setCurrentText(saved_massstab)
else:
self._massstab_combo.setCurrentText(MASSSTAB_WIE_KARTENFENSTER)
def _on_thema_changed(self, value: str) -> None:
"""Persistiert die Themenauswahl und setzt den Kartennamen."""
if value in (THEMA_38, THEMA_41):
set_variable(THEMA_VAR, value, scope="project")
else:
set_variable(THEMA_VAR, "", scope="project")
if self.logic:
self.logic.set_kartenname_for_thema(value)
def _on_massstab_changed(self, value: str) -> None:
"""Persistiert Maßstabsauswahl und setzt ``sn_plotmassstab``."""
set_variable(MASSSTAB_VAR, value, scope="project")
if self.logic:
self.logic.set_plotmassstab_for_auswahl(value, self._get_current_canvas_scale())
def _get_current_canvas_scale(self) -> float | None:
"""Liest den aktuellen Maßstab aus der Kartensicht."""
try:
canvas = iface.mapCanvas()
if canvas is None:
return None
scale = canvas.scale()
return float(scale) if scale else None
except Exception:
return None