76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
|
|
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 UI:
|
||
|
|
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
|