forked from gusthiot/VDLTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vdl_tools.py
161 lines (141 loc) · 6.54 KB
/
vdl_tools.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
VDLTools
A QGIS plugin for the Ville de Lausanne
-------------------
begin : 2016-04-05
git sha : $Format:%H$
copyright : (C) 2016 Ville de Lausanne
author : Christophe Gusthiot
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from PyQt4.QtCore import (QSettings,
QTranslator,
qVersion,
QCoreApplication)
from PyQt4.QtGui import (QAction,
QIcon)
from tools.duplicate_tool import DuplicateTool
from tools.intersect_tool import IntersectTool
from tools.profile_tool import ProfileTool
from tools.interpolate_tool import InterpolateTool
from tools.extrapolate_tool import ExtrapolateTool
from tools.move_tool import MoveTool
from tools.show_settings import ShowSettings
from tools.import_measures import ImportMeasures
# Initialize Qt resources from file resources.py
import resources
import os.path
class VDLTools:
def __init__(self, iface):
"""Constructor
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
self.mapCanvas = iface.mapCanvas()
self.duplicateTool = None
self.intersectTool = None
self.profileTool = None
self.moveTool = None
self.showSettings = None
self.importMeasures = None
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'VDLTools_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Declare instance attributes
self.actions = []
self.menu = QCoreApplication.translate("VDLTools", "&VDL Tools")
self.toolbar = self.iface.addToolBar("VDLTools")
self.toolbar.setObjectName("VDLTools")
def add_action(self, tool, parent, enable=True, isMapTool=True, inToolBar=True):
"""To add an available action.
:param tool: the tool linked to the action
:param parent: the interface main window
:param enable: if the action is enabled at the beginning
:param isMapTool: if the action is a map tool or not
:param inToolBar: if the action has to be in the menu with an icon
:return: the added action
"""
icon = QIcon(tool.icon_path())
action = QAction(icon, tool.text(), parent)
if isMapTool:
tool.setAction(action)
action.triggered.connect(tool.setTool)
action.setEnabled(enable)
else:
action.triggered.connect(tool.start)
if inToolBar:
self.toolbar.addAction(action)
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.showSettings = ShowSettings(self.iface)
self.add_action(self.showSettings, self.iface.mainWindow(), True, False, False)
self.duplicateTool = DuplicateTool(self.iface)
self.add_action(self.duplicateTool, self.iface.mainWindow(), False)
self.intersectTool = IntersectTool(self.iface)
self.add_action(self.intersectTool, self.iface.mainWindow())
self.profileTool = ProfileTool(self.iface)
self.add_action(self.profileTool, self.iface.mainWindow(), False)
self.interpolateTool = InterpolateTool(self.iface)
self.add_action(self.interpolateTool, self.iface.mainWindow(), False)
self.extrapolateTool = ExtrapolateTool(self.iface)
self.add_action(self.extrapolateTool, self.iface.mainWindow(), False)
self.moveTool = MoveTool(self.iface)
self.add_action(self.moveTool, self.iface.mainWindow(), False)
self.importMeasures = ImportMeasures(self.iface)
self.add_action(self.importMeasures, self.iface.mainWindow(), isMapTool=False)
self.profileTool.setEnable(self.iface.activeLayer())
self.iface.currentLayerChanged.connect(self.profileTool.setEnable)
self.interpolateTool.setEnable(self.iface.activeLayer())
self.iface.currentLayerChanged.connect(self.interpolateTool.setEnable)
self.extrapolateTool.setEnable(self.iface.activeLayer())
self.iface.currentLayerChanged.connect(self.extrapolateTool.setEnable)
self.duplicateTool.setEnable(self.iface.activeLayer())
self.iface.currentLayerChanged.connect(self.duplicateTool.setEnable)
self.moveTool.setEnable(self.iface.activeLayer())
self.iface.currentLayerChanged.connect(self.moveTool.setEnable)
self.intersectTool.setOwnSettings(self.showSettings)
self.interpolateTool.setOwnSettings(self.showSettings)
self.importMeasures.setOwnSettings(self.showSettings)
def unload(self):
"""
Removes the plugin menu item and icon from QGIS GUI
"""
for action in self.actions:
self.iface.removePluginMenu(
QCoreApplication.translate("VDLTools", "&VDL Tools"),
action)
self.iface.removeToolBarIcon(action)
# remove the toolbar
del self.toolbar