How to add your script to PDFedit (step by step example)
Example 1 - drawing arrows
Step 1: Writing the script
Our aim is to be able to draw simple arrows. We use a simple algorithm which takes 2 coordinates specifying the start and the end of a line and gives us 2
coordinates specifying left and right end point of an arrow. The end point is computed as the 1/10 of the main line width in 45 degree angle.
We use built-in script function
operatorDrawLine which takes an array of lines to draw, line width and line colour.
We obtain the line width and the line colour from default menu selectors using built-in functions.
Input coordinates and output coordinates may differ because page can use arbitrary transformation of coordinates. That is why every
coordinate needs to be converted using either
convertPixmapPosToPdfPos_x or
convertPixmapPosToPdfPos_y function.
An array consists of 3 lines. The main one connecting first two points, then one connecting left end point with
the end of the line, and likewise the right end point with the end of line. We use the input points as the start and end of the main line which is drawn first. Then
the line connecting left end and then the one connecting right end.
Finally reload the page to see the result.
/**
* Draw an arrow.
*/// Get the end points
// Convert main line points
// Convert arrow end points
"linewidth"), getColor("fg"));
// Reload page
go()
}
Step 2: Adding the script
Open
dialogs.qs which should be in your install directory. Add the function
drawArrow to the end (optionally write a comment describing the function in
doxygen-like∞ style to add the function in command line documentation which is automatically generated) and save the file.
Then after either restarting the application or reloading the script file calling
run ("dialogs.qs");
in command line
we can manually call the function from the command line.
Step 3: (optional) Finding new icon
Try e.g.
kde look&feel;∞ to get hicolor icons (32x32).
Add the icon to either
icon or
icon/something directory to match your actual icon set directory.
Step 4, alternative 1: Connecting button and mouse events with our function
In
callback.qs add
/** Callback when selection mode is changed to draw new arrow */"new_object","line","drawArrow(%1,%2,%3,%4,false)");
setModeButton('drawarrow');
}
/** Callback when selection mode is changed to draw new arrow */"new_object","line","drawArrow(%1,%2,%3,%4,true)");
setModeButton('drawarrowe');
}
These two functions are almost identical (only first is really needed the latter one is for functions that can be even more customized and require a dialog). They set a drawing mode (we use an existing one which draws lines) and when it finished (the user releases the mouse) the function
drawArrow is called with two points marking the start and the end
of the input sequence (when the mouse was pressed and when the mouse was released).
In
pdfeditrc add
which means that an item is created which can be referenced from other items. The item is described by a name, action to take when clicked, button icon, what is needed to
make the button enabled, the mode of the button.
And add the button to the toolbar by adding
drawarrowe to
the result can look like this
this shows the button in the
DrawToolbar menu.
Step 4, alternative 2: Creating the script as a plugin script
Take the code from step 1 and append this code to the end of the script.
/** Callback when selection mode is changed to draw new arrow */"new_object","line","drawArrow(%1,%2,%3,%4,false)");
setModeButton('drawarrow');
}
/** Callback when selection mode is changed to draw new arrow */"new_object","line","drawArrow(%1,%2,%3,%4,true)");
setModeButton('drawarrowe''DrawToolbar','drawarrow',tr('Draw arrow'),'onSelectMode_drawArrow()','','draw_arrow.png',['need_page','mode_button''DrawToolbar','drawarrowe',tr('Draw arrow'),'onSelectMode_drawArrowe()','','draw_arrowe.png',['need_page','mode_button']);
Save it as "arrow.qs" and put the script in "scripts" dicrectory
Installation
You can use easier method of installation by downloading the script file and associated icons.
Put the script file in your home directory in subdirectory
.pdfedit/scripts
Put the associated icons into
.pdfedit/icon subdirectory in your home directory
No other installation is necessary, the function will be available next time you start the editor. If you do not want to restart editor, you can load the new functions while the editor is running by typing
run("scripts/name_of_script.qs") in editor command line.
If you have root privileges, you can install this script for everybody - put the script file into
/usr/local/share/pdfedit/scripts and icons into
/usr/local/share/pdfedit/icon directory
(Assuming you have not changed default installation path)
|
Attachment
|
Size
|
Date Added
|
|
arrow.qs
|
1.73 KB
|
15.9.2006 00:00:00
|
|
|
|
Categories
Scripting
Howto
There are no comments on this page. [Add comment]