Graphics Reference
In-Depth Information
The Add Track map tool
Our first task is to let the user add a new track while in the track editing mode. This in-
volves defining a new map tool, which we will call AddTrackTool . Before we start im-
plementing the AddTrackTool class, however, we're going to create a mixin class that
provides various helper methods for our map tools. We'll call this mixin class
MapToolMixin .
Here is our initial implementation of the MapToolMixin class, which should be placed
near the top of your mapTools.py module:
class MapToolMixin
def setLayer(self, layer):
self.layer = layer
def transformCoordinates(self, screenPt):
return (self.toMapCoordinates(screenPt),
self.toLayerCoordinates(self.layer,
screenPt))
def calcTolerance(self, pos):
pt1 = QPoint(pos.x(), pos.y())
pt2 = QPoint(pos.x() + 10, pos.y())
mapPt1,layerPt1 = self.transformCoordinates(pt1)
mapPt2,layerPt2 = self.transformCoordinates(pt2)
tolerance = layerPt2.x() - layerPt1.x()
return tolerance
We've seen both the transformCoordinates() and calcTolerance() methods
before when we created the geometry editing map tools in Chapter 7 , Selecting and Editing
Features in a PyQGIS Application . The only difference is that we're storing a reference to
the edited map layer so that we don't have to supply it as a parameter each time we want to
calculate the tolerance or transform coordinates.
We can now start implementing the AddTrackTool class. This is very similar to Cap-
tureTool we defined in Chapter 7 , Selecting and Editing Features in a PyQGIS Applica-
Search WWH ::




Custom Search