Graphics Reference
In-Depth Information
self.feature = None
self.vertex = None
Our final event-handling method is canvasDoubleClickEvent() , which responds
to a double-click by adding a new vertex to the feature. This method is similar to the
canvasPressEvent() method; we have to identify the clicked-on feature, and then
identify which line segment the user double-clicked on:
def canvasDoubleClickEvent(self, event):
feature = self.findFeatureAt(event.pos())
if feature == None:
return
mapPt,layerPt =
self.transformCoordinates(event.pos())
geometry = feature.geometry()
distSquared,closestPt,beforeVertex = \
geometry.closestSegmentWithContext(layerPt)
distance = math.sqrt(distSquared)
tolerance = self.calcTolerance(event.pos())
if distance > tolerance: return
As you can see, we ignore the double-click if the mouse position is too far away from the
line segment. Next, we want to add the new vertex to the geometry, and update the map
layer and the map canvas to reflect this change:
geometry.insertVertex(closestPt.x(), closestPt.y(),
beforeVertex)
self.layer.changeGeometry(feature.id(), geometry)
self.canvas().refresh()
This completes all of the event-handling methods for our EditTool . Let's now imple-
ment our various helper methods, starting with the findFeatureAt() method that
identifies the clicked-on feature:
def findFeatureAt(self, pos):
mapPt,layerPt = self.transformCoordinates(pos)
Search WWH ::




Custom Search