Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 97 additions & 1 deletion qgis-samgeo-plugin/samgeo_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import (
QAction,
QToolBar,
QDockWidget,
QWidget,
QVBoxLayout,
Expand Down Expand Up @@ -41,6 +42,9 @@
from .map_tools import PointPromptTool, BoxPromptTool


TOOLBAR_OBJECT_NAME = "SamGeo"
MENU_TITLE = "&SamGeo"

class SamGeoPlugin:
"""QGIS Plugin for remote sensing image segmentation using SamGeo."""

Expand All @@ -57,8 +61,10 @@ def __init__(self, iface):
# Initialize plugin attributes
self.actions = []
self.menu = "&SamGeo"
self._remove_toolbars_by_object_name()
self._remove_menus_by_title()
self.toolbar = self.iface.addToolBar("SamGeo")
Comment thread
giswqs marked this conversation as resolved.
self.toolbar.setObjectName("SamGeo")
self.toolbar.setObjectName(TOOLBAR_OBJECT_NAME)

# Dock widget
self.dock_widget = None
Expand Down Expand Up @@ -133,6 +139,93 @@ def initGui(self):
status_tip=self.tr("Open SamGeo Segmentation Panel"),
)


def _remove_toolbar(self, toolbar):
"""Detach and schedule deletion of a plugin toolbar widget."""
if toolbar is None:
return

main_window = self.iface.mainWindow()
actions = []
try:
actions = list(toolbar.actions())
except Exception:
pass # nosec B110
try:
toolbar.clear()
except Exception:
pass # nosec B110
for action in actions:
try:
action.deleteLater()
except Exception:
pass # nosec B110
try:
main_window.removeToolBar(toolbar)
except Exception:
pass # nosec B110
try:
toolbar.hide()
except Exception:
pass # nosec B110
try:
toolbar.setParent(None)
except Exception:
pass # nosec B110
try:
toolbar.deleteLater()
except Exception:
pass # nosec B110
Comment thread
giswqs marked this conversation as resolved.

def _remove_toolbars_by_object_name(self):
"""Remove current or stale plugin toolbars from QGIS."""
main_window = self.iface.mainWindow()
for toolbar in main_window.findChildren(QToolBar, TOOLBAR_OBJECT_NAME):
self._remove_toolbar(toolbar)

def _plugin_menu_titles(self):
"""Return possible translated and untranslated plugin menu titles."""
titles = {MENU_TITLE}
translator = getattr(self, "tr", None)
if callable(translator):
try:
titles.add(translator(MENU_TITLE))
except Exception:
pass # nosec B110
return titles

def _remove_menu(self, menu):
"""Detach and schedule deletion of a plugin menu."""
if menu is None:
return

main_window = self.iface.mainWindow()
try:
menu.clear()
except Exception:
pass # nosec B110
try:
main_window.menuBar().removeAction(menu.menuAction())
except Exception:
pass # nosec B110
try:
menu.setParent(None)
except Exception:
pass # nosec B110
try:
menu.deleteLater()
except Exception:
pass # nosec B110

def _remove_menus_by_title(self):
"""Remove current or stale plugin menus from QGIS."""
menu_bar = self.iface.mainWindow().menuBar()
titles = self._plugin_menu_titles()
for action in menu_bar.actions():
menu = action.menu()
if menu is not None and menu.title() in titles:
self._remove_menu(menu)
Comment thread
giswqs marked this conversation as resolved.

def unload(self):
"""Remove the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
Expand All @@ -151,6 +244,9 @@ def unload(self):
del self.sam
self.sam = None

self._remove_toolbars_by_object_name()
self._remove_menus_by_title()

def run(self):
"""Run the plugin - show the dock widget."""
if self.dock_widget is None:
Expand Down
Loading