For simple operations, subclassing IDLitDataOperation is an easy way to quickly add an operation to the iTools system. See previous post "Creating an iTool data operation" for more information about IDLitDataOperation. If you want to do anything more complicated than a numeric operation on the dependent variable of the currently selected visualization, then you want to subclass IDLitOperation. We will examine a fairly simple subclass of IDLitOperation, MGitOpDerivative, which calculates the derivative of data in a plot visualization.
Here are the files for this demo:
- MGitOpDerivative__define.pro (doc)
- A subclass of IDLitOperation which can calculate a derivative for IDLVECTOR data parameters in IDLitVisPlot visualizations.
- MGitOpDerivative_demo.pro (doc)
- Program which creates an iTool, loads a sample data set, and registers the above operation for it.
The old standards: init, cleanup, getProperty, setProperty methods
It is very important that the init method calls IDLitOperation::init with the types keyword and pass along it's own keywords with _EXTRA.
name='Derivative', $
icon='plot', _extra=e)) then return, 0B
The cleanup, getProperty, and setProperty methods do not need to be overridden from those inherited from IDLitOperation.
The doAction method
The doAction method is called when the operation is performed by the user. The doAction method does the following:
- Finds
IDLitVisPlotobjects in the currently selected items of the iTool. - Saves initial y data of the selected visualizations in an
IDLitCommandSetso that the operation can be undone later (done inrecordInitialValues) - Do the derivatives (in
doDerivative). - Saves the final y data of the visualizations in the same
IDLitCommandSetso that the operation can be redone later (done inrecordFinalValues) - Return the
IDLitCommandSetthat the initial and final values are stored in.
The real work is done in mgitopderivative::doDerivative. Here the plot parameter is an IDLitVisPlot object. The X and Y parameters of this object contain the data.
compile_opt strictarr
xdata = plot->getParameter('X')
ydata = plot->getParameter('Y')
result = xdata->getData(x)
result = ydata->getData(y)
yprime = deriv(x, y)
result = ydata->setData(yprime)
end
The same kind of code will be used later to change the y values, using IDLitData::setData method instead of IDLitData::getData.
Undo/Redo
Unlike when subclassing IDLitDataOperation, we are responsible for storing information to enable undo/redo. This information is stored in an IDLitCommandSet object. For each visualization acted on, an IDLitCommand is created, data is added to it, and it is added to the command set. Here is a snippet from recordInitialvalues:
target_identifier=targets[i]->getFullIdentifier())
ydata = targets[i]->getParameter('Y')
result = ydata->getData(y)
result = ocmd->addItem('INITIAL_Y', y)
oCmdSet->add, ocmd
recordFinalValues does the same thing, but names the item 'FINAL_Y'. The undoOperation and redoOperation methods simply use this information to set the data of the Y parameter back to the stored value.
Registering the operation
Creating the iTool and registering the operation is straightforward. It is necessary to register the operation so that the tool knows about it. It is possible to globally register the operation, but we will register it with a single iTool in our example. Use itGetCurrent to find the object reference for the iTool and IDLitTool::registerOperation to register the operation. This is done in mgitopderivative_demo:
id = itGetCurrent(tool=otool)
otool->registerOperation, 'Derivative', 'mgitopderivative', $
identifier='Operations/Derivative'

June 20th, 2006 at 8:32 pm
Thanks so much for writing this up. It was much easier than I had feared.
August 4th, 2006 at 10:05 am
Is there a way to pass parameters to the operation when you register?
August 4th, 2006 at 1:15 pm
You can change the values of properties of an operation after you register it. Find the identifier of the operation using IDLitTool::findIdentifiers, then use IDLitTool::doSetProperty to change the property values.
August 5th, 2006 at 1:52 pm
Say in the menu in iTools, I want to have one of those subdivisive items with an arrow to the right where when you put your mouse over it, a submenu pops up to the right where a list of options are shown. Then when the user clicks on one of the options, I call the operation with a certain parameter. All the options share the same operation. For example, say I have the operation zoom, and to the right are the options of 100%, 200%, 400%, etc. And when the user clicks on one, I call the operation with the size parameter. Before iTools, this could be done in the menu event handler. But I can’t find where the event is handled. I guess my question comes down to, where is the event handled where I can set property of the operation? Thanks very much!
November 24th, 2006 at 11:43 am
I find very useful your tips for itools, this is one of the few pages that I’ve found about work with them from code in a simple and understable way. I perfectly understand about the ways to call an operations and it has worked for me, however I disagree with IDL just in one point, IDL doesn’t suck but the Help does.
Most of the times that I have programmed itools from code I have used more intuition than brain to programme all my Itools. When will be write a proper tutorial for programming with Itools from roots without the just-click-here way that is shown in the 99% of the tutorials for ITOOLS?. Hope you show more very good examples of itools. congrats again