forked from AG_QGIS/Plugin_SN_Basis
116 lines
2.8 KiB
Python
116 lines
2.8 KiB
Python
"""
|
|
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):
|
|
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")
|
|
add_toolbar(self.toolbar)
|
|
|
|
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):
|
|
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):
|
|
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
|
|
|