Graphics Reference
In-Depth Information
Implementing the pan tool
Panning (that is, clicking and dragging on the map to move around) is another area where
the QGIS default behavior isn't quite what we want. QGIS includes a classQg-
sMapToolPan class, which implements panning; however, it also includes some features
that could be quite confusing for users coming from Google Maps. In particular, if the user
clicks without dragging, the map is re-centered over the clicked-on point. Instead of using
classQgsMapToolPan , we will implement our own panning map tool. Fortunately, this
is simple to do: simply add the following class definition to your lex.py module after the
end of your MapExplorer class definition:
class PanTool(QgsMapTool):
def __init__(self, mapCanvas):
QgsMapTool.__init__(self, mapCanvas)
self.setCursor(Qt.OpenHandCursor)
self.dragging = False
def canvasMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.dragging = True
self.canvas().panAction(event)
def canvasReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self.dragging:
self.canvas().panActionEnd(event.pos())
self.dragging = False
We then need to add the following to the end of our main window's __init__() method
to create an instance of our panning tool:
self.panTool = PanTool(self.mapCanvas)
self.panTool.setAction(self.actionPan)
We can now implement our setPanMode() method to use this map tool:
def setPanMode(self):
self.actionPan.setChecked(True)
self.mapCanvas.setMapTool(self.panTool)
Search WWH ::




Custom Search