Graphics Reference
In-Depth Information
tion , except that it only captures LineString geometries and it creates a new track feature
with default attributes when the user finishes defining the track. Here is the class defini-
tion with the __init__() method for our new map tool, which should be placed in the
mapTools.py module:
class AddTrackTool(QgsMapTool, MapToolMixin):
def __init__(self, canvas, layer, onTrackAdded):
QgsMapTool.__init__(self, canvas)
self.canvas = canvas
self.onTrackAdded = onTrackAdded
self.rubberBand = None
self.tempRubberBand = None
self.capturedPoints = []
self.capturing = False
self.setLayer(layer)
self.setCursor(Qt.CrossCursor)
As you can see, our class inherits from both QgsMapTool and MapToolMixin . We
also call the setLayer() method so that our mixin knows which layer to work with.
This also makes the currently edited layer available via self.layer .
We next define the various event handling methods for our map tool:
def canvasReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
if not self.capturing:
self.startCapturing()
self.addVertex(event.pos())
elif event.button() == Qt.RightButton:
points = self.getCapturedPoints()
self.stopCapturing()
if points != None:
self.pointsCaptured(points)
def canvasMoveEvent(self, event):
if self.tempRubberBand != None and self.capturing:
mapPt,layerPt =
self.transformCoordinates(event.pos())
self.tempRubberBand.movePoint(mapPt)
Search WWH ::




Custom Search