Graphics Reference
In-Depth Information
We're now ready to implement our map tool. Earlier, we looked at how you can use the
QgsMapTool class to respond to mouse clicks within the map canvas. In this case, we'll
use a subclass of QgsMapTool , called QgsMapToolIdentify . This class makes it
easy to find the feature at a given point. When the user clicks on the map canvas, we'll use
the QgsMapToolIdentify.identify() method to find the first clicked-on feature,
and then calculate and display various statistics about that feature's geometry.
Add the following code to the end of your geometryInfo.py module:
class GeometryInfoMapTool(QgsMapToolIdentify):
def __init__(self, iface):
QgsMapToolIdentify.__init__(self, iface.mapCanvas())
self.iface = iface
def canvasReleaseEvent(self, event):
QMessageBox.information(self.iface.mainWindow(),
"debug",
"Canvas Click")
This defines our QgsMapToolIdentify subclass. It doesn't do anything useful yet, but
it will respond with a simple "Canvas Click" message when the user clicks on the map
canvas. Now, let's finish writing our plugin's onClick() method to activate and deactiv-
ate our map tool as the user clicks on our toolbar icon. This is what the onClick()
method should look like:
def onClick(self):
if not self.action.isChecked():
self.iface.mapCanvas().unsetMapTool(self.mapTool)
self.mapTool = None
return
self.action.setChecked(True)
self.mapTool = GeometryInfoMapTool(self.iface)
self.mapTool.setAction(self.action)
self.iface.mapCanvas().setMapTool(self.mapTool)
You should now be able to run your plugin by typing make deploy , and then reload it
in QGIS to see how it works. If all goes well, the toolbar icon will be highlighted when
you click on it, and the "Canvas Click" message should appear when you click on the map
canvas.
Search WWH ::




Custom Search