forked from AG_QGIS/Plugin_SN_Basis
141 lines
2.9 KiB
Python
141 lines
2.9 KiB
Python
|
|
"""
|
|||
|
|
sn_basis/functions/qgisui_wrapper.py – zentrale QGIS-UI-Abstraktion
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from typing import Any, List
|
|||
|
|
|
|||
|
|
from sn_basis.functions.qt_wrapper import QDockWidget
|
|||
|
|
|
|||
|
|
|
|||
|
|
iface: Any
|
|||
|
|
QGIS_UI_AVAILABLE = False
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
# iface initialisieren (QGIS oder Mock)
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from qgis.utils import iface as _iface
|
|||
|
|
iface = _iface
|
|||
|
|
QGIS_UI_AVAILABLE = True
|
|||
|
|
|
|||
|
|
except Exception:
|
|||
|
|
|
|||
|
|
|
|||
|
|
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()
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
# 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
|