-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
105 lines (79 loc) · 2.76 KB
/
plugin.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
"""
Plugin class
"""
from pathlib import Path
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction
# Initialize Qt resources from file resources.py
from .resources import * # noqa: F401, F403
# Import the code for the dialog
from .view.main_dialog import MainDialog
class Plugin:
"""QGIS Plugin Implementation."""
def __init__(self, iface, plugin_name, plugin_menu):
"""Constructor."""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = Path(__file__).parent
# Declare instance attributes
self.actions = []
self.name = plugin_name
self.menu = plugin_menu
# Check if plugin was started the first time in current QGIS session
# Must be set in initGui() to survive plugin reloads
self.first_start = None
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None,
):
"""Add a toolbar icon to the toolbar."""
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip:
action.setStatusTip(status_tip)
if whats_this:
action.setWhatsThis(whats_this)
if add_to_toolbar:
self.iface.addToolBarIcon(action)
if add_to_menu:
self.iface.addPluginToMenu(self.menu, action)
self.actions.append(action)
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
self.add_action(
":/assets/icon.png",
text=self.name,
callback=self.run,
parent=self.iface.mainWindow(),
)
# will be set False in run()
self.first_start = True
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(self.menu, action)
self.iface.removeToolBarIcon(action)
def run(self):
"""Run method that performs all the real work"""
# Activate map tips
self.iface.actionMapTips().setChecked(True)
# Create the dialog with elements (after translation) and keep reference
# Only create GUI ONCE in callback, so that it will only load when the plugin is started
if self.first_start:
self.first_start = False
self.main_dialog = MainDialog(self.iface, self.name)
# show the dialog
self.main_dialog.show()