This chapter explains the scripting interface of LabPlot that can help you to automate your work. With the use of the scripting interface you can get very productive and simplify your work when doing the same things often. With the knowledge of this interface you are able to completely control LabPlot remotely .
LabPlot uses Qt™ Script for Applications (QSA) developed by Trolltech, Inc. It is released under two different licenses - one commercial (that costs an arm and a leg) and the other GPL (free for download). The GPL version has some restrictions that are applicable to the case of a commercially developed application.
Of course LabPlot needs to be build with QSA support. For KDE 3 (based on Qt™ 3) you need version 1.1.X of QSA.
Scripts are small files that contain instructions to be executed. Since LabPlot can interpret such scripts it can be automated using this. Scripts can be created and edited with your favorite text editor or by using the QSA Workbench (Can be found in the LabPlot menu under "Script->QSA Workbench..."). If the icons in the Workbench are missing check out the Workbench Chapter.
To execute a script you can call
LabPlot
from the commandline or drag and drop a script on the desktop into LabPlot.
You can also use the dialog "Script->Open Script" in LabPlot to execute a script.
script.qs
LabPlot is divided into a bunch of classes. For most scripting needs, you need to know only a few of them. For every operation you just call the corresponding function on the LabPlot classes. All available functions can be found in the classes reference at http://cvs.sourceforge.net/viewcvs.py/*checkout*/labplot/doc/html/hierarchy.html.
All MainWin functions can be called directly. Let start with
importData("sample.dat");This simply imports the data file "sample.dat" into a Spreadsheet in LabPlot. You can see it in the screenshot.
If you now want to work with the Spreadsheet you have to call the corresponding Spreadsheet function. Let's say we want to make a 2D Plot
importData("sample.dat"); s = activeSpreadsheet(); s.plot2DSimple();The result is
importData("sample.dat"); s = activeSpreadsheet(); s.plot2DSimple(); w = activeWorksheet(); p = w.get2DPlot(w.API()); p.setBackground("green"); w.redraw();With the result that we have a green background
A complete script that imports data and changes some settings before saving the result as EPS would look like this:
importData("sample-data/sin.dat"); s = activeSpreadsheet(); s.plot2DSimple(); w = activeWorksheet(); p = w.get2DPlot(w.API()); p.setBackground("green"); p.setGraphBackground("lightblue"); r = p.ActRange(0); r.setRange(250,750); r = p.ActRange(1); r.setRange(-2,2); l = p.getLegend(); l.setPosition(.5,.4); t = p.Title(); t.setTitle("example title"); t.setRotation(10); a = p.getAxis(0); a.enableMajorGrid(); ll = a.getLabel(); ll.setTitle("different x axis"); font = new Font("SanSerif"); a.setTickLabelFont(font); p.setMarksEnabled(); mark = p.markX(); mark.setRange(450,550); p.setRegionEnabled(); p.setRegion(350,650); // w.redraw(); exportEPS("export.eps"); exit();The used functions should be quite self-explanatory. the resulting EPS then looks like that
This is basically all you need to know about writing scripts. More examples can be found in the directory examples/scripts/ of the source distribution or in the data directory of LabPlot.
For a detailed description of the QSA syntax check out the QSA documentation. All enumerations available in LabPlot can also be used in Scripts, check out the global script labplot.qs.
With QSA it is also possible to use dialogs to enter filenames, etc. . The following example uses a dialog to enter a data file name:
d = new ImportDialog(); var filename = FileDialog.getOpenFileName( "*.dat" ); if (filename) { d.setFilename(filename); d.Apply(); }
Would you like to make a comment or contribute an update to this page?
Send feedback to the KDE Docs Team