diff --git a/ui/__init__.py b/ui/__init__.py index 17c0c6d..55a1077 100644 --- a/ui/__init__.py +++ b/ui/__init__.py @@ -1,2 +1,3 @@ from .tab_projekt import TabProjektWidget -from .dockmanager import DockManager \ No newline at end of file +from .dockmanager import DockManager +from .navigation import Navigation \ No newline at end of file diff --git a/ui/navigation.py b/ui/navigation.py new file mode 100644 index 0000000..6fc80f6 --- /dev/null +++ b/ui/navigation.py @@ -0,0 +1,75 @@ +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 + +class Navigation: + TITLE = "LNO Sachsen" + + def __init__(self): + self.menu = self._get_or_create_menu() + self.toolbar = self._get_or_create_toolbar() + + def _get_or_create_menu(self): + global _shared_menu + if _shared_menu: + return _shared_menu + + menubar = iface.mainWindow().menuBar() + for action in menubar.actions(): + if action.menu() and action.text() == self.TITLE: + _shared_menu = action.menu() + return _shared_menu + + menu = QMenu(self.TITLE, iface.mainWindow()) + menu.setObjectName(self.TITLE) + menubar.addMenu(menu) + _shared_menu = menu + return menu + + 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) + 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) + 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