-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
137 lines (103 loc) · 4.66 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from __future__ import annotations
from typing import Callable
from qgis.gui import QgsDockWidget
from qgis.PyQt.QtCore import QCoreApplication, Qt, QTranslator
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QWidget
from qgis.utils import iface
from arho_feature_template.core.feature_template_library import FeatureTemplateLibrary
from arho_feature_template.gui.panels.template_library_panel import TemplateLibraryPanel
from arho_feature_template.qgis_plugin_tools.tools.custom_logging import setup_logger, teardown_logger
from arho_feature_template.qgis_plugin_tools.tools.i18n import setup_translation
from arho_feature_template.qgis_plugin_tools.tools.resources import plugin_name, resources_path
LIBRARY_JSON = resources_path("asemakaava-template-library-test.json")
class Plugin:
"""QGIS Plugin Implementation."""
name = plugin_name()
def __init__(self) -> None:
setup_logger(Plugin.name)
# initialize locale
locale, file_path = setup_translation()
if file_path:
self.translator = QTranslator()
self.translator.load(file_path)
# noinspection PyCallByClass
QCoreApplication.installTranslator(self.translator)
else:
pass
self.actions: list[QAction] = []
self.menu = Plugin.name
# Create and initialize default feature template library
self.active_library = FeatureTemplateLibrary(LIBRARY_JSON)
def add_action(
self,
icon_path: str,
text: str,
callback: Callable,
*,
enabled_flag: bool = True,
add_to_menu: bool = True,
add_to_toolbar: bool = True,
status_tip: str | None = None,
whats_this: str | None = None,
parent: QWidget | None = None,
) -> QAction:
"""Add a toolbar icon to the toolbar.
:param icon_path: Path to the icon for this action. Can be a resource
path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
:param text: Text that should be shown in menu items for this action.
:param callback: Function to be called when the action is triggered.
:param enabled_flag: A flag indicating if the action should be enabled
by default. Defaults to True.
:param add_to_menu: Flag indicating whether the action should also
be added to the menu. Defaults to True.
:param add_to_toolbar: Flag indicating whether the action should also
be added to the toolbar. Defaults to True.
:param status_tip: Optional text to show in a popup when mouse pointer
hovers over the action.
:param parent: Parent widget for the new action. Defaults None.
:param whats_this: Optional text to show in the status bar when the
mouse pointer hovers over the action.
:returns: The action that was created. Note that the action is also
added to self.actions list.
:rtype: QAction
"""
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
# noinspection PyUnresolvedReferences
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
# Adds plugin icon to Plugins toolbar
iface.addToolBarIcon(action)
if add_to_menu:
iface.addPluginToMenu(self.menu, action)
self.actions.append(action)
return action
def initGui(self) -> None: # noqa N802
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
self.add_action(
"",
text=Plugin.name,
callback=self.run,
parent=iface.mainWindow(),
add_to_toolbar=True,
)
def onClosePlugin(self) -> None: # noqa N802
"""Cleanup necessary items here when plugin dockwidget is closed"""
def unload(self) -> None:
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
iface.removePluginMenu(Plugin.name, action)
iface.removeToolBarIcon(action)
teardown_logger(Plugin.name)
def run(self) -> None:
self.feature_template_dock = QgsDockWidget()
self.add_feature_panel = TemplateLibraryPanel(self.active_library)
self.feature_template_dock.setWidget(self.add_feature_panel)
self.feature_template_dock.setWindowTitle("ARHO") # NOTE: Placeholder name
iface.addDockWidget(Qt.RightDockWidgetArea, self.feature_template_dock)