Files
Plugin_SN_Basis/ui/navigation.py
T
DanielandClaude Sonnet 5 bbda2cd61b Navigation.remove_all() räumt Toolbar/Menü beim Unload tatsächlich auf (#72)
remove_toolbar()/remove_menu() nahmen Toolbar und Menü bisher nur aus der sichtbaren Leiste,
das Qt-Objekt blieb als Kind von main_window bestehen. Beim Neuladen mit dem Plugin Reloader
meldete QGIS deshalb "removing duplicated widget(s) not cleaned up by the plugin during
unload: LnoSachsenToolbar" - der defensive Workaround in init_ui() (Suche nach übrig
gebliebenen Toolbars) kaschierte das nur beim nächsten Laden, nicht beim Unload selbst.

remove_all() ruft jetzt deleteLater() auf beide Objekte, bevor die Referenzen verworfen
werden - dieselbe Behandlung, die init_ui() schon für Altlasten nutzt. deleteLater() ist in
PyQt5 und PyQt6 identisch, keine QGIS3/4-Fallunterscheidung nötig.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-21 10:22:29 +02:00

134 lines
3.8 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_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):
self.actions = []
self.menu = None
self.toolbar = None
self.plugin_group = None
def init_ui(self):
"""von Test abgesehen, weil vernachlässigbar; HD 18.09.2026"""
print(">>> Navigation.init_ui() CALLED")
main_window = get_main_window()
if not main_window:
return
# -----------------------------------------
# Vorherige Toolbars entfernen
# -----------------------------------------
for tb in main_window.findChildren(QToolBar):
if tb.objectName() == "LnoSachsenToolbar":
remove_toolbar(tb)
tb.deleteLater()
# -----------------------------------------
# Menü und Toolbar neu erzeugen
# -----------------------------------------
self.menu = QMenu("LNO Sachsen", main_window)
add_menu(self.menu)
self.toolbar = QToolBar("LNO Sachsen", main_window)
self.toolbar.setObjectName("LnoSachsenToolbar")
add_toolbar(self.toolbar)
test_action = QAction("TEST ACTION", main_window)
self.menu.addAction(test_action)
self.toolbar.addAction(test_action)
self.plugin_group = QActionGroup(main_window)
self.plugin_group.setExclusive(True)
# -----------------------------------------------------
# Actions
# -----------------------------------------------------
def add_action(self, text, callback, tooltip="", priority=100):
if not self.plugin_group:
return None
action = QAction(text, get_main_window())
action.setToolTip(tooltip)
action.setCheckable(True)
action.triggered.connect(callback)
self.plugin_group.addAction(action)
self.actions.append((priority, action))
return action
def finalize_menu_and_toolbar(self):
if not self.menu or not self.toolbar:
return
self.actions.sort(key=lambda x: x[0])
self.menu.clear()
self.toolbar.clear()
for _, action in self.actions:
self.menu.addAction(action)
self.toolbar.addAction(action)
def set_active_plugin(self, active_action):
for _, action in self.actions:
action.setChecked(False)
if active_action:
active_action.setChecked(True)
# -----------------------------------------------------
# Cleanup
# -----------------------------------------------------
def remove_action(self, action):
if not action:
return
if self.menu:
self.menu.removeAction(action)
if self.toolbar:
self.toolbar.removeAction(action)
self.actions = [(p, a) for p, a in self.actions if a != action]
def remove_all(self):
# remove_menu()/remove_toolbar() nehmen Menü/Toolbar nur aus der sichtbaren Leiste –
# das Qt-Objekt bleibt sonst als Kind von main_window bestehen (Issue #72, vom Plugin
# Reloader als "duplicated widget(s) not cleaned up" gemeldet). deleteLater() räumt es
# tatsächlich auf, analog zum bestehenden Workaround in init_ui().
if self.menu:
remove_menu(self.menu)
self.menu.deleteLater()
self.menu = None
if self.toolbar:
remove_toolbar(self.toolbar)
self.toolbar.deleteLater()
self.toolbar = None
self.actions.clear()
self.plugin_group = None