-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.py
192 lines (150 loc) · 7.16 KB
/
canvas.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
# coding=utf-8
from qgis.core import * # unable to import QgsWKBTypes otherwize (quid?)
from qgis.gui import *
from PyQt4.QtCore import Qt, pyqtSignal
from PyQt4.QtGui import QMenu, QColor, QWidget, QMessageBox
from .layer import Layer
from .toolbar import Toolbar, LineSelectTool
from .section_tools import SelectionTool, MoveFeatureTool
from math import sqrt
class Canvas(QgsMapCanvas):
def __init__(self, section, iface, parent=None):
QgsMapCanvas.__init__(self, parent)
self.setWheelAction(QgsMapCanvas.WheelZoomToMouseCursor)
self.setCrsTransformEnabled(False)
self.__iface = iface
self.__highlighter = None
self.__section = section
section.changed.connect(self.__define_section_line)
self.extentsChanged.connect(self.__extents_changed)
iface.mapCanvas().extentsChanged.connect(self.__extents_changed)
self.currentLayerChanged.connect(self.__update_layer_action_states)
def unload(self):
self.__cleanup()
self.section_actions = []
def build_default_section_actions(self):
return [
{ 'icon': QgsApplication.getThemeIcon('/mActionPan.svg'), 'label': 'pan', 'tool': QgsMapToolPan(self) },
{ 'icon': QgsApplication.getThemeIcon('/mActionZoomIn.svg'), 'label': 'zoom in', 'tool': QgsMapToolZoom(self, False) },
{ 'icon': QgsApplication.getThemeIcon('/mActionZoomOut.svg'), 'label': 'zoom out', 'tool': QgsMapToolZoom(self, True) },
None,
{ 'icon': QgsApplication.getThemeIcon('/mActionSelect.svg'), 'label': 'select', 'tool': SelectionTool(self) },
{ 'icon': QgsApplication.getThemeIcon('/mActionToggleEditing.svg'), 'label': 'toggle edit', 'clicked': self.__toggle_edit, 'layer_state': lambda l: l.isEditable()},
{ 'icon': QgsApplication.getThemeIcon('/mActionMoveFeature.svg'), 'label': 'move feature', 'tool': MoveFeatureTool(self) },
]
def add_section_actions_to_toolbar(self, actions, toolbar):
self.section_actions = []
for action in actions:
if action is None:
toolbar.addSeparator()
continue
act = toolbar.addAction(action['icon'], action['label'])
if 'tool' in action:
act.setCheckable(True)
act.setData(action['tool'])
act.triggered.connect(self._setSectionCanvasTool)
elif 'clicked' in action:
act.setCheckable('layer_state' in action)
act.triggered.connect(action['clicked'])
action['action'] = act
self.section_actions += [ action ]
def _setSectionCanvasTool(self, checked):
if not checked:
return
tool = self.sender().data()
self.setMapTool(tool)
for action in self.section_actions:
if 'tool' in action:
action['action'].setChecked(tool == action['tool'])
def __cleanup(self):
if self.__highlighter is not None:
self.__iface.mapCanvas().scene().removeItem(self.__highlighter)
self.__highlighter = None
self.__iface.mapCanvas().refresh()
def __define_section_line(self, line_wkt, width):
self.__cleanup()
if not line_wkt:
return
self.__highlighter = QgsRubberBand(self.__iface.mapCanvas(), QGis.Line)
self.__highlighter.addGeometry(QgsGeometry.fromWkt(line_wkt), None) # todo use section.line
self.__highlighter.setWidth(width/self.__iface.mapCanvas().getCoordinateTransform().mapUnitsPerPixel())
color = QColor(255, 0, 0, 128)
self.__highlighter.setColor(color)
if not len(self.layers()):
return
min_z = min((layer.extent().yMinimum() for layer in self.layers()))
max_z = max((layer.extent().yMaximum() for layer in self.layers()))
z_range = max_z - min_z
self.setExtent(QgsRectangle(0, min_z - z_range * 0.1, self.__section.line.length, max_z + z_range * 0.1))
self.refresh()
def z_autoscale(self, flag=None):
ext = self.extent()
smin, smax = ext.xMinimum(), ext.xMaximum()
ztmin, ztmax = self.__section.z_range(smin, smax)
print "z range", ztmin, ztmax
if ztmin == ztmax:
return
dzt = ztmax - ztmin
zmin, zmax = ext.yMinimum(), ext.yMaximum()
print "ext", smin, zmin, smax, zmax
dz = zmax - zmin
fct = dz/dzt
self.__section.set_z_scale(fct)
print "new ext", smin, ztmin, smax, ztmax
print "scaling by", fct
self.setExtent(QgsRectangle(smin, ztmin*fct, smax, ztmax*fct))
self.refresh()
def __extents_changed(self):
if not self.__section.is_valid:
return
ext = self.extent()
line = QgsGeometry.fromWkt(self.__section.line.wkt)
# section visibility bounds
start = max(0, ext.xMinimum())
end = start + min(line.length(), ext.width())
vertices = [line.interpolate(start).asPoint()]
vertex_count = len(line.asPolyline())
distance = 0
for i in range(1, vertex_count):
vertex_i = line.vertexAt(i)
distance += sqrt(line.sqrDistToVertexAt(vertex_i, i-1))
# 2.16 distance = line.distanceToVertex(i)
if distance <= start:
pass
elif distance < end:
vertices += [vertex_i]
else:
break
vertices += [line.interpolate(end).asPoint()]
if self.__highlighter is not None:
self.__highlighter.reset()
self.__highlighter.addGeometry(QgsGeometry.fromPolyline(vertices), None)
self.__highlighter.setWidth(self.__section.width/self.__iface.mapCanvas().getCoordinateTransform().mapUnitsPerPixel())
def __toggle_edit(self, checked):
#TODO: simplistic implementation. Would be nice to be able to use QgisApp::toggleEditing( QgsMapLayer *layer, bool allowCancel)
currentLayer = self.currentLayer()
if currentLayer is None:
self.__update_layer_action_states()
else:
print checked
if checked:
if not currentLayer.isReadOnly():
currentLayer.startEditing()
else:
if currentLayer.isModified():
res = QMessageBox.information(None, "Stop editing", "Do you want to save the changes to layer {}?".format(currentLayer.name()), QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
print res
if res == QMessageBox.Cancel:
return
elif res == QMessageBox.Discard:
currentLayer.rollBack()
currentLayer.triggerRepaint()
else:
currentLayer.commitChanges()
else:
currentLayer.rollBack()
def __update_layer_action_states(self):
currentLayer = self.currentLayer()
for action in self.section_actions:
if 'layer_state' in action:
action['action'].setChecked(False if currentLayer is None else action['layer_state'](currentLayer))