5. CallbacksΒΆ

Similarly to interests, callbacks is a notification mechanism used to keep the whole application coherent. It works by specifying a function to be called when a specific event occurs.

These callbacks are set/removed by calling the methods defined in OrsEvent.eventCallback.EventCallback.

Note

When callbacks associated to a model object instance should not be triggered, call the method ORSModel.ors.Managed.setCallbacksEnabled() on that instance. This is used when such a model object instance is created to perform a given task and is then deleted in the same method.

Syntax for adding a callback (for example, using an instance callback):

# Callback function to be called
# Using a weak reference of the plugin so that it can be deleted
weakSelf = self.getWeakRef()  # "self" is the plugin instance

def aCallbackFunction(eventData):
    mySelf = weakSelf()
    if mySelf is None:
        return
    mySelf.aPluginMethod()  # "aPluginMethod" is an instance method of the plugin

# Creating a unique ID for this callback
callbackId = 'DemoPlugin_{}_{}_{}'.format('ObjectDeleted', anObject.getGUID(), repr(id(self)))

# Setting the callback
# The returned object of EventCallback should be kept in order to keep the callback alive
self._objectDeletedEventCallback = EventCallback.addCallbackObjectDeletedEvent(anObject, callbackId,
                                                                               aCallbackFunction)

The callback will exist as long as the object returned by the method of EventCallback is referenced. To delete the callback, remove all references of that callback object. For example:

# Removing the callback
self._objectDeletedEventCallback = None

Instance callbacks

These callbacks are set on specific instances of the model. For example, it can be to react to a specific Channel instance.

The available methods are those of OrsEvent.eventCallback.EventCallback starting with addCallback.

Source code example:

  1. Download the compressed file;
  2. Extract these files into a plugin extension folder;
  3. Start the application;
  4. Open the top level menu Demos to see the menu item named Demo: instance callbacks;
  5. By clicking on the menu item, an instance of the plugin will be created and his mainform will be displayed. When initialized, this plugin instance will create and publish a ROI named ROI with callback set on delete, for which a callback is set on the deletion of this object that will close the plugin instance. At the plugin initialization, another ROI named A common ROI is also created and published, for which no specific callback is set;
  6. Select the ROI A common ROI. Delete this ROI (use the button Delete next to the list of objects). Note that the plugin is still open and responsive;
  7. Select the ROI ROI with callback set on delete. Delete this ROI. The plugin is closed due to the callback on the deletion of that specific object.

Class callbacks

These callbacks are set for any instances of the model. For example, it can be to react to the changes to any Channel instance.

The available methods are those of OrsEvent.eventCallback.EventCallback starting with addClassCallback.

Source code example:

  1. Download the compressed file;
  2. Extract these files into a plugin extension folder;
  3. Start the application;
  4. Open the top level menu Demos to see the menu item named Demo: class callbacks;
  5. By clicking on the menu item, an instance of the plugin will be created and his mainform will be displayed. When initialized, this plugin instance will set 2 class callbacks on the class StructuredGrid: one for the publish and one for the deletion. With this, any model object instance of that class that is getting published or deleted will trigger this callback, that will be used to update the plugin UI;
  6. Create/import/delete some datasets, ROIs and MultiROIs to see the plugin UI being updated with the count of these objects.

Global callbacks

These callbacks are not specific to any model object, but to more general events.

The available methods are those of OrsEvent.eventCallback.EventCallback starting with addGlobalCallback.

Source code example:

  1. Download the compressed file;
  2. Extract these files into a plugin extension folder;
  3. Start the application;
  4. Open the top level menu Demos to see the menu item named Demo: global callbacks;
  5. By clicking on the menu item, an instance of the plugin will be created and his mainform will be displayed. When initialized, this plugin instance will set a global callback on the view mode change;
  6. Set the view mode of the current view to be in 3D. The pushbutton of the plugin UI will be disabled;
  7. Set the view mode of the current view to be in any 2D view. The pushbutton of the plugin is re-enabled.