-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
289 lines (226 loc) · 10.1 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from __future__ import annotations
from typing import TYPE_CHECKING, Callable, cast
from qgis.core import QgsApplication
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.plan_manager import PlanManager
from arho_feature_template.gui.dialogs.plugin_settings import PluginSettings
from arho_feature_template.gui.docks.validation_dock import ValidationDock
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
if TYPE_CHECKING:
from qgis.gui import QgisInterface
iface: QgisInterface = cast("QgisInterface", iface) # type: ignore[no-redef]
class Plugin:
"""QGIS Plugin Implementation."""
name = plugin_name()
def __init__(self) -> None:
setup_logger(Plugin.name)
self.digitizing_tool = None
# 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
self.toolbar = iface.addToolBar("ARHO Toolbar")
self.toolbar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
def add_action(
self,
text: str,
icon: QIcon | None = None,
triggered_callback: Callable | None = None,
*,
toggled_callback: Callable | None = None,
object_name: str | None = None,
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,
checkable: bool = False,
) -> QAction:
"""Add a toolbar icon to the toolbar.
:param icon: Icon for this action.
: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
"""
if not icon:
icon = QIcon("")
action = QAction(icon, text, parent)
# noinspection PyUnresolvedReferences
if triggered_callback:
action.triggered.connect(triggered_callback)
if toggled_callback:
action.toggled.connect(toggled_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 object_name:
action.setObjectName(object_name)
if checkable:
action.setCheckable(True)
if add_to_toolbar:
self.toolbar.addAction(action)
if add_to_menu:
iface.addPluginToMenu(self.menu, action)
self.actions.append(action)
return action
def initGui(self) -> None: # noqa N802
# plan_icon_path = os.path.join(PLUGIN_PATH, "resources/icons/city.png") # A placeholder icon
# load_icon_path = os.path.join(PLUGIN_PATH, "resources/icons/folder.png") # A placeholder icon
self.plan_manager = PlanManager()
iface.addDockWidget(Qt.RightDockWidgetArea, self.plan_manager.new_feature_dock)
self.plan_manager.new_feature_dock.setUserVisible(False)
self.plan_manager.new_feature_dock.visibilityChanged.connect(self.dock_visibility_changed)
iface.mapCanvas().mapToolSet.connect(self.plan_manager.plan_digitize_map_tool.deactivate)
self.validation_dock = ValidationDock()
iface.addDockWidget(Qt.RightDockWidgetArea, self.validation_dock)
self.validation_dock.setUserVisible(False)
self.validation_dock.visibilityChanged.connect(self.validation_dock_visibility_changed)
# icons to consider:
# icon=QgsApplication.getThemeIcon("mActionStreamingDigitize.svg"),
# icon=QgsApplication.getThemeIcon("mIconGeometryCollectionLayer.svg"),
# icon=QgsApplication.getThemeIcon("mActionSharingExport.svg"),
# Add main plugin action to the toolbar
self.new_land_use_plan_action = self.add_action(
text="Luo kaava",
icon=QgsApplication.getThemeIcon("mActionNewMap.svg"),
triggered_callback=self.add_new_plan,
add_to_menu=True,
add_to_toolbar=True,
status_tip="Luo uusi kaava",
)
self.load_land_use_plan_action = self.add_action(
text="Avaa kaava",
icon=QgsApplication.getThemeIcon("mActionFileOpen.svg"),
triggered_callback=self.load_existing_land_use_plan,
parent=iface.mainWindow(),
add_to_menu=True,
add_to_toolbar=True,
status_tip="Lataa/avaa kaava",
)
self.edit_land_use_plan_action = self.add_action(
text="Muokkaa kaavaa",
# icon=QgsApplication.getThemeIcon("mActionFileOpen.svg"),
triggered_callback=self.plan_manager.edit_plan,
parent=iface.mainWindow(),
add_to_menu=True,
add_to_toolbar=True,
status_tip="Muokkaa aktiivisen kaavan tietoja",
)
self.new_feature_dock_action = self.add_action(
text="Luo kaavakohde",
icon=QgsApplication.getThemeIcon("mIconFieldGeometry.svg"),
toggled_callback=self.toggle_new_feature_dock,
checkable=True,
add_to_menu=True,
add_to_toolbar=True,
)
self.validation_dock_action = self.add_action(
text="Validointi",
icon=QgsApplication.getThemeIcon("mActionEditNodesItem.svg"),
toggled_callback=self.toggle_validation_dock,
checkable=True,
add_to_menu=True,
add_to_toolbar=True,
)
self.new_plan_regulation_group = self.add_action(
text="Luo kaavamääräysryhmä",
icon=QgsApplication.getThemeIcon("mActionAddManualTable.svg"),
triggered_callback=self.open_plan_regulation_group_form,
add_to_menu=True,
add_to_toolbar=True,
)
self.serialize_plan_action = self.add_action(
text="Tallenna kaava JSON",
icon=QgsApplication.getThemeIcon("mActionFileSaveAs.svg"),
triggered_callback=self.serialize_plan,
add_to_menu=True,
add_to_toolbar=True,
status_tip="Tallenna aktiivinen kaava JSON muodossa",
)
self.plugin_settings_action = self.add_action(
text="Asetukset",
triggered_callback=self.open_settings,
add_to_menu=True,
add_to_toolbar=False,
status_tip="Muokkaa pluginin asetuksia",
)
self.identify_plan_features_action = self.add_action(
text="Muokkaa kohteita",
toggled_callback=self.plan_manager.toggle_identify_plan_features,
add_to_menu=False,
add_to_toolbar=True,
checkable=True,
)
self.plan_manager.inspect_plan_feature_tool.deactivated.connect(
lambda: self.identify_plan_features_action.setChecked(False)
)
def add_new_plan(self):
self.plan_manager.add_new_plan()
def load_existing_land_use_plan(self):
self.plan_manager.load_land_use_plan()
def serialize_plan(self):
"""Serializes currently active plan."""
self.plan_manager.get_plan_json()
def open_settings(self):
"""Open the plugin settings dialog."""
settings = PluginSettings()
settings.exec_()
def unload(self) -> None:
"""Removes the plugin menu item and icon from QGIS GUI."""
# Handle signals
self.plan_manager.new_feature_dock.visibilityChanged.disconnect()
iface.mapCanvas().mapToolSet.disconnect()
# Handle actions
for action in self.actions:
iface.removePluginMenu(Plugin.name, action)
iface.removeToolBarIcon(action)
action.deleteLater()
self.actions.clear()
# Handle toolbar
iface.mainWindow().removeToolBar(self.toolbar)
self.toolbar = None
# Handle plan manager
self.plan_manager.unload()
# Handle validation dock
iface.removeDockWidget(self.validation_dock)
self.validation_dock.deleteLater()
# Handle logger
teardown_logger(Plugin.name)
def dock_visibility_changed(self, visible: bool) -> None: # noqa: FBT001
self.new_feature_dock_action.setChecked(visible)
def toggle_new_feature_dock(self, show: bool) -> None: # noqa: FBT001
self.plan_manager.new_feature_dock.setUserVisible(show)
def validation_dock_visibility_changed(self, visible: bool) -> None: # noqa: FBT001
self.validation_dock_action.setChecked(visible)
def toggle_validation_dock(self, show: bool) -> None: # noqa: FBT001
self.validation_dock.setUserVisible(show)
def open_plan_regulation_group_form(self):
self.plan_manager.create_new_regulation_group()