Refactoring Aufgrund Fehler beim Beenden.

This commit is contained in:
Michael Otto
2025-11-17 10:05:42 +01:00
parent c36dc8cae9
commit a302ce2228
12 changed files with 179 additions and 437 deletions

View File

@@ -1,75 +1,37 @@
from qgis.PyQt.QtWidgets import QMenu, QToolBar, QAction
from qgis.PyQt.QtGui import QIcon
from qgis.utils import iface
_shared_toolbar = None # globale Toolbar-Instanz
_shared_menu = None # globale Menü-Instanz
from qgis.PyQt.QtWidgets import QAction, QMenu, QToolBar
class Navigation:
TITLE = "LNO Sachsen"
def __init__(self, iface):
self.iface = iface
self.actions = []
def __init__(self):
self.menu = self._get_or_create_menu()
self.toolbar = self._get_or_create_toolbar()
# Menü und Toolbar einmalig anlegen
self.menu = QMenu("LNO Sachsen", iface.mainWindow())
iface.mainWindow().menuBar().addMenu(self.menu)
def _get_or_create_menu(self):
global _shared_menu
if _shared_menu:
return _shared_menu
self.toolbar = QToolBar("LNO Sachsen")
iface.addToolBar(self.toolbar)
menubar = iface.mainWindow().menuBar()
for action in menubar.actions():
if action.menu() and action.text() == self.TITLE:
_shared_menu = action.menu()
return _shared_menu
def add_action(self, text, callback, tooltip="", priority=100, icon=None):
action = QAction(icon, text, self.iface.mainWindow()) if icon else QAction(text, self.iface.mainWindow())
if tooltip:
action.setToolTip(tooltip)
action.triggered.connect(callback)
menu = QMenu(self.TITLE, iface.mainWindow())
menu.setObjectName(self.TITLE)
menubar.addMenu(menu)
_shared_menu = menu
return menu
# Action mit Priority speichern
self.actions.append((priority, action))
return action
def finalize_menu_and_toolbar(self):
# Sortieren nach Priority
self.actions.sort(key=lambda x: x[0])
def _get_or_create_toolbar(self):
global _shared_toolbar
if _shared_toolbar:
return _shared_toolbar
main_window = iface.mainWindow()
toolbar = main_window.findChild(QToolBar, self.TITLE)
if not toolbar:
toolbar = QToolBar(self.TITLE, main_window)
toolbar.setObjectName(self.TITLE)
main_window.addToolBar(toolbar)
_shared_toolbar = toolbar
return toolbar
def add_action(self, text, callback, icon=None, tooltip=None):
# Menüeintrag
if not any(a.text() == text for a in self.menu.actions()):
action = QAction(icon, text, iface.mainWindow()) if icon else QAction(text, iface.mainWindow())
if tooltip:
action.setToolTip(tooltip)
action.triggered.connect(callback)
# Menüeinträge
self.menu.clear()
for _, action in self.actions:
self.menu.addAction(action)
# Symbolleistenaktion
if not any(a.text() == text for a in self.toolbar.actions()):
action = QAction(icon, text, iface.mainWindow()) if icon else QAction(text, iface.mainWindow())
if tooltip:
action.setToolTip(tooltip)
action.triggered.connect(callback)
# Toolbar-Einträge
self.toolbar.clear()
for _, action in self.actions:
self.toolbar.addAction(action)
def remove_action(self, text):
# Menüeintrag entfernen
for act in self.menu.actions():
if act.text() == text:
self.menu.removeAction(act)
break
# Symbolleistenaktion entfernen
for act in self.toolbar.actions():
if act.text() == text:
self.toolbar.removeAction(act)
break