Tools Plugins
Tools plugins are introduced in Veusz 1.9 to allow the user to add code to Veusz to automate the manipulation of documents. The user can do anything, including
- Adding, changing or manipulating widgets
- Change settings inside widgets
- Import data
- Save files and export plots
The Python code for any plugins can be added in the preferences dialog box.
A tools plugin uses the standard Veusz embedding interface to manipulate the document. The standard Veusz commands can be accessed with the interface object (e.g. interface.Export('page.png')). The object-based API can be accessed from the interface.Root object, as described in EmbeddingPython. Here is an example tools plugin which searches and replaces markers in xy widgets (point plotters):
1 import veusz.plugins as plugins
2
3 class ReplaceMarkerPlugin(plugins.ToolsPlugin):
4 """Replace marker with another marker in XY widgets."""
5
6 # a tuple of strings building up menu to place plugin on
7 menu = ('Replace marker',)
8 # unique name for plugin
9 name = 'Replace marker'
10
11 # name to appear on status tool bar
12 description_short = 'Replace a marker with a different marker'
13 # text to appear in dialog box
14 description_full = 'Search and replace a marker with a different marker.'
15
16 def __init__(self):
17 """Make list of fields."""
18 self.fields = [
19 plugins.FieldWidget("widget", descr="Start from widget",
20 default="/"),
21 plugins.FieldMarker("markersearch", descr="Search for marker"),
22 plugins.FieldMarker("markerreplace", descr="Replace with marker"),
23 ]
24
25 def apply(self, interface, fields):
26 """Do the work of the plugin.
27 interface: veusz command line interface object (exporting commands)
28 fields: dict mapping field names to values
29 """
30
31 # get the Node corresponding to the widget path given
32 fromwidget = interface.Root.fromPath(fields['widget'])
33 search = fields['markersearch']
34 replace = fields['markerreplace']
35
36 # loop over every xy widget including and below fromwidget
37 for node in fromwidget.WalkWidgets(widgettype='xy'):
38 # if marker is value given, replace
39 if node.marker.val == search:
40 node.marker.val = replace
41
42 plugins.toolspluginregistry.append(ReplaceMarkerPlugin)
A tools plugin should inherit ToolsPlugin.
