Graphics Reference
In-Depth Information
The interesting part here is the call to
tempRubberBand.movePoint()
. The
Qg-
sRubberBand
class works in map coordinates, so we first have to convert from the cur-
rent mouse position, which is in pixels, to map coordinates. We then call
movePoint()
,
which moves the current vertex in the rubber band to the new position.
There is one more event handling method to define:
onKeyEvent()
. This responds to
the user pressing the
Backspace
or
Delete
keys by removing the last added vertex, and to
the user pressing
Return
or
Enter
by closing and saving the current geometry. Here is the
code for this method:
def keyPressEvent(self, event):
if event.key() == Qt.Key_Backspace or \
event.key() == Qt.Key_Delete:
self.removeLastVertex()
event.ignore()
if event.key() == Qt.Key_Return or event.key() ==
Qt.Key_Enter:
points = self.getCapturedGeometry()
self.stopCapturing()
if points != None:
self.geometryCaptured(points)
Now that we've defined our event handling methods, let's now define the various helper
methods that these event handlers rely on. We'll start with the
transformCoordin-
ates()
method, which converts from a mouse position, which is in canvas coordinates,
to map and layer coordinates:
def transformCoordinates(self, canvasPt):
return (self.toMapCoordinates(canvasPt),
self.toLayerCoordinates(self.layer,
canvasPt))
If, for example, the mouse is currently at position
(17,53)
on the canvas, this may
translate to a map and layer coordinate of
lat=37.234
and
long=-112.472
. As the
map and layer might use different coordinate reference systems, we calculate and return
the coordinates for both.