Repository navigation
Expand file tree
/
Copy pathplugin.py
More file actions
79 lines (66 loc) · 3.05 KB
/
Copy pathplugin.py
File metadata and controls
79 lines (66 loc) · 3.05 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
74
75
76
77
78
79
# -*- coding: utf-8 -*-
"""Main plugin class: registers the Processing provider and the map tool."""
import os
from qgis.core import QgsApplication
from qgis.PyQt.QtWidgets import QAction
from qgis.PyQt.QtGui import QIcon
from .provider import ItineraProvider
from .gui.point_pick_tool import LcpMapTool
from .gui.settings_dialog import LcpSettingsDialog
class ItineraPlugin:
def __init__(self, iface):
self.iface = iface
self.provider = None
self.action = None
self.settings_action = None
self.map_tool = None
# --- Processing provider -------------------------------------------
def initProcessing(self):
self.provider = ItineraProvider()
QgsApplication.processingRegistry().addProvider(self.provider)
def initGui(self):
self.initProcessing()
here = os.path.dirname(__file__)
icon_path = os.path.join(here, "icon.png")
icon = QIcon(icon_path) if os.path.exists(icon_path) else QIcon()
settings_icon_path = os.path.join(here, "icon_settings.png")
settings_icon = (QIcon(settings_icon_path)
if os.path.exists(settings_icon_path) else icon)
self.action = QAction(icon, "Interactive LCP (two clicks)",
self.iface.mainWindow())
self.action.setCheckable(True)
self.action.triggered.connect(self.toggle_map_tool)
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu("Itinera", self.action)
self.map_tool = LcpMapTool(self.iface.mapCanvas(), self.iface)
self.map_tool.setAction(self.action)
self.settings_action = QAction(
settings_icon, "Interactive LCP settings…",
self.iface.mainWindow())
self.settings_action.triggered.connect(self.open_settings)
self.iface.addToolBarIcon(self.settings_action)
self.iface.addPluginToMenu("Itinera", self.settings_action)
def toggle_map_tool(self, checked):
if checked:
self.iface.mapCanvas().setMapTool(self.map_tool)
else:
self.map_tool.reset()
self.iface.mapCanvas().unsetMapTool(self.map_tool)
def open_settings(self):
dlg = LcpSettingsDialog(
self.map_tool.cost_key, self.map_tool.neighbours,
self.iface.mainWindow())
if dlg.exec(): # exec() works on both PyQt5 and PyQt6 (QGIS 3 & 4)
self.map_tool.set_settings(
dlg.selected_cost_key(), dlg.selected_neighbours())
def unload(self):
if self.provider is not None:
QgsApplication.processingRegistry().removeProvider(self.provider)
if self.action is not None:
self.iface.removeToolBarIcon(self.action)
self.iface.removePluginMenu("Itinera", self.action)
if self.settings_action is not None:
self.iface.removeToolBarIcon(self.settings_action)
self.iface.removePluginMenu("Itinera", self.settings_action)
if self.map_tool is not None:
self.iface.mapCanvas().unsetMapTool(self.map_tool)