Graphics Reference
In-Depth Information
As you can see, we ignore the mouse click if it is too far away from the vertex, that is, if
the distance is greater than the tolerance. Now that we know that the user did click near
the vertex, we want to respond to that mouse click. How we do this depends on whether
the user pressed the left or the right mouse button:
if event.button() == Qt.LeftButton:
# Left click -> move vertex.
self.dragging = True
self.feature = feature
self.vertex = vertex
self.moveVertexTo(event.pos())
self.canvas().refresh()
elif event.button() == Qt.RightButton:
# Right click -> delete vertex.
self.deleteVertex(feature, vertex)
self.canvas().refresh()
As you can see, we're relying on a number of helper methods to do most of the work.
We'll define these methods shortly, but first, let's finish implementing our event handling
methods, starting with canvasMoveEvent() . This method responds as the user moves
the mouse over the canvas. It does this by moving the dragged vertex (if any) to the cur-
rent mouse position:
def canvasMoveEvent(self, event):
if self.dragging:
self.moveVertexTo(event.pos())
self.canvas().refresh()
Next, we have canvasReleaseEvent() , which moves the vertex to its final position,
refreshes the map canvas, and updates our instance variables to reflect the fact that we are
no longer dragging a vertex:
def canvasReleaseEvent(self, event):
if self.dragging:
self.moveVertexTo(event.pos())
self.layer.updateExtents()
self.canvas().refresh()
self.dragging = False
Search WWH ::




Custom Search