Graphics Reference
In-Depth Information
run() : This method is called when the plugin is activated, that is, when the user
clicks on the plugin's icon in the toolbar, or selects the plugin from the Plugins
menu.
Don't worry too much about all the details here; we'll look at the process of initializing
and unloading a plugin in a later chapter. For now, take a closer look at the run() meth-
od. This method essentially looks like the following:
def run(self):
dlg = ZoomToPointDialog()
...
dlg.show()
result = dlg.exec_()
if result == 1:
x = dlg.ui.xCoord.text()
y = dlg.ui.yCoord.text()
scale = dlg.ui.spinBoxScale.value()
rect = QgsRectangle(float(x) - scale,
float(y) - scale,
float(x) + scale,
float(y) + scale)
mc=self.iface.mapCanvas()
mc.setExtent(rect)
mc.refresh()
...
We've excluded the code that remembers the values the user entered previously, and cop-
ies those values back into the dialog when the plugin is run. Looking at the previous code,
the logic seems to be fairly straightforward and is explained as follows:
• Create a ZoomToPointDialog object.
• Display the dialog box to the user.
• If the user clicks on the OK button, extract the entered values, use them to create
a new bounding rectangle, and set the extent of the map to this rectangle.
While this plugin is quite straightforward and the actual code doesn't do all that much, it is
a useful example of what a Python plugin should look like, as well as the various files that
are needed by a Python plugin. In particular, you should note that:
Search WWH ::




Custom Search