Graphics Reference
In-Depth Information
distSquared,closestPt,beforeVertex = \
geometry.closestSegmentWithContext(layerPt)
distance = math.sqrt(distSquared)
tolerance = self.calcTolerance(event.pos())
if distance > tolerance: return
geometry.insertVertex(closestPt.x(), closestPt.y(),
beforeVertex)
self.layer.changeGeometry(feature.id(), geometry)
self.onTrackEdited()
self.canvas().refresh()
We now have the
moveVertexTo()
method, which moves the clicked-on vertex to the
current mouse location. While the logic is very similar to the method with the same name
in our
EditTool
, we also want to support vertex snapping so that the user can click on
an existing vertex to connect two tracks together. Here is the implementation of this meth-
od:
def moveVertexTo(self, pos):
snappedPt = self.snapToNearestVertex(pos,
self.layer,
self.feature)
geometry = self.feature.geometry()
layerPt = self.toLayerCoordinates(self.layer, pos)
geometry.moveVertex(snappedPt.x(), snappedPt.y(),
self.vertex)
self.layer.changeGeometry(self.feature.id(),
geometry)
self.onTrackEdited()
Notice that our call to
snapToNearestVertex()
makes use of the
ex-
cludeFeature
parameter to exclude the clicked-on feature when finding a vertex to
snap to. This ensures that we don't snap a feature to itself.
Finally, we have the
deleteVertex()
method, which is copied almost verbatim from
the
EditTool
class: