2025-12-19 14:29:52 +01:00
|
|
|
|
"""
|
|
|
|
|
|
sn_basis/functions/qgisui_wrapper.py – zentrale QGIS-UI-Abstraktion
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
2026-01-08 17:13:51 +01:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
from typing import Any, List, Type
|
|
|
|
|
|
|
2025-12-19 14:29:52 +01:00
|
|
|
|
|
|
|
|
|
|
from sn_basis.functions.qt_wrapper import QDockWidget
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iface: Any
|
|
|
|
|
|
QGIS_UI_AVAILABLE = False
|
|
|
|
|
|
|
2026-01-08 17:13:51 +01:00
|
|
|
|
QgsFileWidget: Type[Any]
|
|
|
|
|
|
QgsMapLayerComboBox: Type[Any]
|
|
|
|
|
|
|
2025-12-19 14:29:52 +01:00
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
2026-01-08 17:13:51 +01:00
|
|
|
|
# iface + QGIS-Widgets initialisieren (QGIS oder Mock)
|
2025-12-19 14:29:52 +01:00
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
from qgis.utils import iface as _iface
|
2026-01-08 17:13:51 +01:00
|
|
|
|
from qgis.gui import (
|
|
|
|
|
|
QgsFileWidget as _QgsFileWidget,
|
|
|
|
|
|
QgsMapLayerComboBox as _QgsMapLayerComboBox,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-19 14:29:52 +01:00
|
|
|
|
iface = _iface
|
2026-01-08 17:13:51 +01:00
|
|
|
|
QgsFileWidget = _QgsFileWidget
|
|
|
|
|
|
QgsMapLayerComboBox = _QgsMapLayerComboBox
|
2025-12-19 14:29:52 +01:00
|
|
|
|
QGIS_UI_AVAILABLE = True
|
|
|
|
|
|
|
|
|
|
|
|
except Exception:
|
2026-01-08 17:13:51 +01:00
|
|
|
|
QGIS_UI_AVAILABLE = False
|
|
|
|
|
|
|
|
|
|
|
|
class _MockSignal:
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
self._callbacks: list[Any] = []
|
|
|
|
|
|
|
|
|
|
|
|
def connect(self, callback):
|
|
|
|
|
|
self._callbacks.append(callback)
|
|
|
|
|
|
|
|
|
|
|
|
def emit(self, *args, **kwargs):
|
|
|
|
|
|
for cb in list(self._callbacks):
|
|
|
|
|
|
cb(*args, **kwargs)
|
2025-12-19 14:29:52 +01:00
|
|
|
|
|
|
|
|
|
|
class _MockMessageBar:
|
|
|
|
|
|
def pushMessage(self, title, text, level=0, duration=5):
|
|
|
|
|
|
return {
|
|
|
|
|
|
"title": title,
|
|
|
|
|
|
"text": text,
|
|
|
|
|
|
"level": level,
|
|
|
|
|
|
"duration": duration,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class _MockIface:
|
|
|
|
|
|
def messageBar(self):
|
|
|
|
|
|
return _MockMessageBar()
|
|
|
|
|
|
|
|
|
|
|
|
def mainWindow(self):
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def addDockWidget(self, *args, **kwargs):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def removeDockWidget(self, *args, **kwargs):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def addToolBar(self, *args, **kwargs):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def removeToolBar(self, *args, **kwargs):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
iface = _MockIface()
|
|
|
|
|
|
|
2026-01-08 17:13:51 +01:00
|
|
|
|
class _MockQgsFileWidget:
|
|
|
|
|
|
GetFile = 0
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
self._path = ""
|
|
|
|
|
|
self.fileChanged = _MockSignal()
|
|
|
|
|
|
|
|
|
|
|
|
def setStorageMode(self, *args, **kwargs):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def setFilter(self, *args, **kwargs):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def setFilePath(self, path: str):
|
|
|
|
|
|
self._path = path
|
|
|
|
|
|
self.fileChanged.emit(path)
|
|
|
|
|
|
|
|
|
|
|
|
def filePath(self) -> str:
|
|
|
|
|
|
return self._path
|
|
|
|
|
|
|
|
|
|
|
|
class _MockQgsMapLayerComboBox:
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
self.layerChanged = _MockSignal()
|
|
|
|
|
|
self._layer = None
|
|
|
|
|
|
self._count = 0
|
|
|
|
|
|
|
|
|
|
|
|
def setFilters(self, *args, **kwargs):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def setLayer(self, layer):
|
|
|
|
|
|
self._layer = layer
|
|
|
|
|
|
self.layerChanged.emit(layer)
|
|
|
|
|
|
|
|
|
|
|
|
def count(self) -> int:
|
|
|
|
|
|
return self._count
|
|
|
|
|
|
|
|
|
|
|
|
def setCurrentIndex(self, idx: int):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
QgsFileWidget = _MockQgsFileWidget
|
|
|
|
|
|
QgsMapLayerComboBox = _MockQgsMapLayerComboBox
|
|
|
|
|
|
|
2025-12-19 14:29:52 +01:00
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
# Main Window
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
def get_main_window():
|
|
|
|
|
|
try:
|
|
|
|
|
|
return iface.mainWindow()
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
# Dock-Handling
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
def add_dock_widget(area, dock: Any) -> None:
|
|
|
|
|
|
try:
|
|
|
|
|
|
iface.addDockWidget(area, dock)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_dock_widget(dock: Any) -> None:
|
|
|
|
|
|
try:
|
|
|
|
|
|
iface.removeDockWidget(dock)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_dock_widgets() -> List[Any]:
|
|
|
|
|
|
main_window = get_main_window()
|
|
|
|
|
|
if not main_window:
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
return main_window.findChildren(QDockWidget)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
# Menü-Handling
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
def add_menu(menu):
|
|
|
|
|
|
main_window = iface.mainWindow()
|
|
|
|
|
|
if not main_window:
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# Nur echte Qt-Menüs an Qt übergeben
|
|
|
|
|
|
if hasattr(menu, "menuAction"):
|
|
|
|
|
|
main_window.menuBar().addMenu(menu)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_menu(menu):
|
|
|
|
|
|
main_window = iface.mainWindow()
|
|
|
|
|
|
if not main_window:
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
if hasattr(menu, "menuAction"):
|
|
|
|
|
|
main_window.menuBar().removeAction(menu.menuAction())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
# Toolbar-Handling
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
def add_toolbar(toolbar: Any) -> None:
|
|
|
|
|
|
try:
|
|
|
|
|
|
iface.addToolBar(toolbar)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_toolbar(toolbar: Any) -> None:
|
|
|
|
|
|
try:
|
|
|
|
|
|
iface.removeToolBar(toolbar)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
pass
|