-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.py
More file actions
73 lines (59 loc) · 2.58 KB
/
plugin.py
File metadata and controls
73 lines (59 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
from qgis.gui import QgsDockWidget
from .qt_compat import QAction, QIcon, QObject, Qt
class CopernicusConnectPlugin(QObject):
def __init__(self, iface):
super().__init__()
self.iface = iface
self.action = None
self.dock = None # holds reference to the dock panel
def initGui(self):
icon_path = os.path.join(os.path.dirname(__file__), "resources", "icon.png")
icon = QIcon(icon_path)
# Checkable action toggles dock visibility
self.action = QAction(icon, "Copernicus Connect", self.iface.mainWindow())
self.action.setToolTip("Copernicus Connect")
self.action.setCheckable(True)
self.action.toggled.connect(self.toggle_dock)
# Add to toolbar and Plugins menu
self.iface.addToolBarIcon(self.action)
self.iface.pluginMenu().addAction(self.action)
def unload(self):
# Remove dock if it exists
if self.dock:
try:
self.iface.removeDockWidget(self.dock)
except Exception:
pass
self.dock.deleteLater()
self.dock = None
# Remove actions
if self.action:
self.iface.removeToolBarIcon(self.action)
self.iface.pluginMenu().removeAction(self.action)
self.action = None
def toggle_dock(self, checked: bool):
if checked:
# Create the dock the first time
if not self.dock:
from .main import launch_form # must return a QWidget as content
content_widget = launch_form(self.iface.mainWindow())
self.dock = QgsDockWidget("Copernicus Connect", self.iface.mainWindow())
self.dock.setObjectName("CopernicusConnectDock") # required so QGIS remembers placement
self.dock.setAllowedAreas(
Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea |
Qt.TopDockWidgetArea | Qt.BottomDockWidgetArea
)
self.dock.setWidget(content_widget)
# Keep toolbar button state in sync if user hides the panel manually
self.dock.visibilityChanged.connect(self.action.setChecked)
# Register dock with QGIS (appears under View → Panels)
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dock)
self.dock.show()
self.dock.raise_()
else:
if self.dock:
self.dock.hide()
# Keep run for legacy shortcuts; internally just toggles the panel
def run(self):
self.action.toggle()