2025-11-17 12:23:04 +01:00
|
|
|
from qgis.PyQt.QtWidgets import QAction, QMenu, QToolBar, QActionGroup
|
2025-10-09 13:56:13 +02:00
|
|
|
|
|
|
|
|
class Navigation:
|
2025-11-17 10:05:42 +01:00
|
|
|
def __init__(self, iface):
|
|
|
|
|
self.iface = iface
|
|
|
|
|
self.actions = []
|
2025-11-17 12:23:04 +01:00
|
|
|
|
2025-11-17 10:05:42 +01:00
|
|
|
# Menü und Toolbar einmalig anlegen
|
|
|
|
|
self.menu = QMenu("LNO Sachsen", iface.mainWindow())
|
|
|
|
|
iface.mainWindow().menuBar().addMenu(self.menu)
|
|
|
|
|
|
|
|
|
|
self.toolbar = QToolBar("LNO Sachsen")
|
2025-11-18 12:17:15 +01:00
|
|
|
self.toolbar.setObjectName("LnoSachsenToolbar")
|
2025-11-17 10:05:42 +01:00
|
|
|
iface.addToolBar(self.toolbar)
|
|
|
|
|
|
2025-11-17 12:23:04 +01:00
|
|
|
# Gruppe für exklusive Auswahl (nur ein Plugin aktiv)
|
|
|
|
|
self.plugin_group = QActionGroup(iface.mainWindow())
|
|
|
|
|
self.plugin_group.setExclusive(True)
|
|
|
|
|
|
|
|
|
|
def add_action(self, text, callback, tooltip="", priority=100):
|
2025-11-17 11:29:04 +01:00
|
|
|
action = QAction(text, self.iface.mainWindow())
|
2025-11-17 12:23:04 +01:00
|
|
|
action.setToolTip(tooltip)
|
|
|
|
|
action.setCheckable(True) # Button kann aktiv sein
|
2025-11-17 10:05:42 +01:00
|
|
|
action.triggered.connect(callback)
|
|
|
|
|
|
2025-11-17 12:23:04 +01:00
|
|
|
# Action in Gruppe aufnehmen
|
|
|
|
|
self.plugin_group.addAction(action)
|
|
|
|
|
|
|
|
|
|
# Action mit Priority speichern
|
2025-11-17 10:05:42 +01:00
|
|
|
self.actions.append((priority, action))
|
|
|
|
|
return action
|
|
|
|
|
|
|
|
|
|
def finalize_menu_and_toolbar(self):
|
2025-11-17 12:23:04 +01:00
|
|
|
# Sortieren nach Priority
|
|
|
|
|
self.actions.sort(key=lambda x: x[0])
|
2025-11-17 11:29:04 +01:00
|
|
|
|
2025-11-17 12:23:04 +01:00
|
|
|
# Menüeinträge
|
|
|
|
|
self.menu.clear()
|
|
|
|
|
for _, action in self.actions:
|
2025-11-17 11:29:04 +01:00
|
|
|
self.menu.addAction(action)
|
2025-11-17 12:23:04 +01:00
|
|
|
|
|
|
|
|
# Toolbar-Einträge
|
|
|
|
|
self.toolbar.clear()
|
|
|
|
|
for _, action in self.actions:
|
2025-10-09 13:56:13 +02:00
|
|
|
self.toolbar.addAction(action)
|
2025-11-17 12:23:04 +01:00
|
|
|
|
|
|
|
|
def set_active_plugin(self, active_action):
|
|
|
|
|
# Alle zurücksetzen, dann aktives Plugin markieren
|
|
|
|
|
for _, action in self.actions:
|
|
|
|
|
action.setChecked(False)
|
2025-11-17 12:48:16 +01:00
|
|
|
if active_action:
|
|
|
|
|
active_action.setChecked(True)
|
|
|
|
|
|
|
|
|
|
def remove_all(self):
|
|
|
|
|
"""Alles entfernen beim Entladen des Basisplugins"""
|
|
|
|
|
# Menü entfernen
|
|
|
|
|
if self.menu:
|
|
|
|
|
self.iface.mainWindow().menuBar().removeAction(self.menu.menuAction())
|
|
|
|
|
self.menu = None
|
|
|
|
|
|
|
|
|
|
# Toolbar entfernen
|
|
|
|
|
if self.toolbar:
|
|
|
|
|
self.iface.mainWindow().removeToolBar(self.toolbar)
|
|
|
|
|
self.toolbar = None
|
|
|
|
|
|
|
|
|
|
# Actions zurücksetzen
|
|
|
|
|
self.actions.clear()
|
|
|
|
|
|
|
|
|
|
# Gruppe leeren
|
|
|
|
|
self.plugin_group = None
|
|
|
|
|
|
|
|
|
|
def remove_action(self, action):
|
|
|
|
|
"""Entfernt eine einzelne Action aus Menü und Toolbar"""
|
|
|
|
|
if not action:
|
|
|
|
|
return
|
|
|
|
|
# Menüeintrag entfernen
|
|
|
|
|
if self.menu:
|
|
|
|
|
self.menu.removeAction(action)
|
|
|
|
|
# Toolbar-Eintrag entfernen
|
|
|
|
|
if self.toolbar:
|
|
|
|
|
self.toolbar.removeAction(action)
|
|
|
|
|
# Aus der internen Liste löschen
|
|
|
|
|
self.actions = [(p, a) for p, a in self.actions if a != action]
|