forked from AG_QGIS/Plugin_SN_Basis
Wrappe modular aufgebaut, Tests erfolgreich, Menüleiste und Werzeugleiste werden eingetragen (QT6 und QT5)- (Es fehlen noch Fachplugins, um zu prüfen, ob es auch wirklich in QGIS geht)
This commit is contained in:
125
ui/navigation.py
125
ui/navigation.py
@@ -1,84 +1,115 @@
|
||||
#sn_basis/ui/navigation.py
|
||||
from qgis.PyQt.QtWidgets import QAction, QMenu, QToolBar, QActionGroup
|
||||
"""
|
||||
sn_basis/ui/navigation.py
|
||||
|
||||
Zentrale Navigation (Menü + Toolbar) für sn_basis.
|
||||
"""
|
||||
|
||||
from typing import Any, List, Tuple
|
||||
|
||||
from sn_basis.functions.qt_wrapper import (
|
||||
QAction,
|
||||
QMenu,
|
||||
QToolBar,
|
||||
QActionGroup,
|
||||
)
|
||||
from sn_basis.functions import (
|
||||
get_main_window,
|
||||
add_toolbar,
|
||||
remove_toolbar,
|
||||
add_menu,
|
||||
remove_menu,
|
||||
)
|
||||
|
||||
|
||||
class Navigation:
|
||||
def __init__(self, iface):
|
||||
self.iface = iface
|
||||
def __init__(self):
|
||||
self.actions = []
|
||||
|
||||
# Menü und Toolbar einmalig anlegen
|
||||
self.menu = QMenu("LNO Sachsen", iface.mainWindow())
|
||||
iface.mainWindow().menuBar().addMenu(self.menu)
|
||||
self.menu = None
|
||||
self.toolbar = None
|
||||
self.plugin_group = None
|
||||
|
||||
|
||||
self.toolbar = QToolBar("LNO Sachsen")
|
||||
|
||||
def init_ui(self):
|
||||
print(">>> Navigation.init_ui() CALLED")
|
||||
|
||||
main_window = get_main_window()
|
||||
if not main_window:
|
||||
return
|
||||
|
||||
self.menu = QMenu("LNO Sachsen", main_window)
|
||||
add_menu(self.menu)
|
||||
|
||||
self.toolbar = QToolBar("LNO Sachsen", main_window)
|
||||
self.toolbar.setObjectName("LnoSachsenToolbar")
|
||||
iface.addToolBar(self.toolbar)
|
||||
add_toolbar(self.toolbar)
|
||||
|
||||
# Gruppe für exklusive Auswahl (nur ein Plugin aktiv)
|
||||
self.plugin_group = QActionGroup(iface.mainWindow())
|
||||
self.plugin_group.setExclusive(True)
|
||||
test_action = QAction("TEST ACTION", main_window)
|
||||
self.menu.addAction(test_action)
|
||||
self.toolbar.addAction(test_action)
|
||||
|
||||
|
||||
|
||||
# -----------------------------------------------------
|
||||
# Actions
|
||||
# -----------------------------------------------------
|
||||
|
||||
def add_action(self, text, callback, tooltip="", priority=100):
|
||||
action = QAction(text, self.iface.mainWindow())
|
||||
if not self.plugin_group:
|
||||
return None
|
||||
|
||||
action = QAction(text, get_main_window())
|
||||
action.setToolTip(tooltip)
|
||||
action.setCheckable(True) # Button kann aktiv sein
|
||||
action.setCheckable(True)
|
||||
action.triggered.connect(callback)
|
||||
|
||||
# Action in Gruppe aufnehmen
|
||||
self.plugin_group.addAction(action)
|
||||
|
||||
# Action mit Priority speichern
|
||||
self.actions.append((priority, action))
|
||||
return action
|
||||
|
||||
|
||||
def finalize_menu_and_toolbar(self):
|
||||
# Sortieren nach Priority
|
||||
if not self.menu or not self.toolbar:
|
||||
return
|
||||
|
||||
self.actions.sort(key=lambda x: x[0])
|
||||
|
||||
# Menüeinträge
|
||||
self.menu.clear()
|
||||
self.toolbar.clear()
|
||||
|
||||
for _, action in self.actions:
|
||||
self.menu.addAction(action)
|
||||
|
||||
# Toolbar-Einträge
|
||||
self.toolbar.clear()
|
||||
for _, action in self.actions:
|
||||
self.toolbar.addAction(action)
|
||||
|
||||
def set_active_plugin(self, active_action):
|
||||
# Alle zurücksetzen, dann aktives Plugin markieren
|
||||
for _, action in self.actions:
|
||||
action.setChecked(False)
|
||||
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
|
||||
# -----------------------------------------------------
|
||||
# Cleanup
|
||||
# -----------------------------------------------------
|
||||
|
||||
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]
|
||||
|
||||
def remove_all(self):
|
||||
if self.menu:
|
||||
remove_menu(self.menu)
|
||||
self.menu = None
|
||||
|
||||
if self.toolbar:
|
||||
remove_toolbar(self.toolbar)
|
||||
self.toolbar = None
|
||||
|
||||
self.actions.clear()
|
||||
self.plugin_group = None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user