From fe1b1850b40327aa879cd5d4e8dde78a07a2a952 Mon Sep 17 00:00:00 2001 From: Olli-Pekka Kahilakoski Date: Mon, 11 Apr 2022 22:19:52 +0300 Subject: [PATCH] ADD: Callbacks to NeuronavigationAPI, allowing controlling InVesalius (#441) --- invesalius/net/neuronavigation_api.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/invesalius/net/neuronavigation_api.py b/invesalius/net/neuronavigation_api.py index 31e49dd..1d12081 100644 --- a/invesalius/net/neuronavigation_api.py +++ b/invesalius/net/neuronavigation_api.py @@ -28,20 +28,26 @@ class NeuronavigationApi(metaclass=Singleton): An API used internally in InVesalius to communicate with the outside world. - When something noteworthy happens when running InVesalius, e.g., - the coil is moved during neuronavigation, an object created from - this class can be used to update that information. + When an event of one of several types happens while running InVesalius, e.g., + the coil is moved during neuronavigation, this class is used to update the + information conveyed by the event. When created for the first time, takes a connection object obtained from outside InVesalius (see the main entrypoint in app.py). - If connection object is not given or it is None, skip doing the updates. + The owner of the connection object can update the state of InVesalius by implementing + functions to set callbacks, used then to communicate the new state to InVesalius (see, + e.g., set_callback__set_markers below). + + If connection object is not given or it is None, do not do the updates. """ N_VERTICES_IN_POLYGON = 3 def __init__(self, connection=None): if connection is not None: - assert self._hasmethod(connection, 'update_coil_pose') + self.assert_valid(connection) + + self.__set_callbacks(connection) self.__bind_events() self.connection = connection @@ -49,9 +55,16 @@ class NeuronavigationApi(metaclass=Singleton): def _hasmethod(self, obj, name): return hasattr(obj, name) and callable(getattr(obj, name)) + def assert_valid(self, connection): + assert self._hasmethod(connection, 'update_coil_pose') + assert self._hasmethod(connection, 'update_focus') + assert self._hasmethod(connection, 'set_callback__set_markers') + def __bind_events(self): Publisher.subscribe(self.update_focus, 'Set cross focal point') + # Functions for InVesalius to send updates. + # TODO: Not the cleanest API; for an example of a better API, see update_coil_pose # below, for which position and orientation are sent separately. Changing this # would require changing 'Set cross focal point' publishers and subscribers @@ -96,3 +109,11 @@ class NeuronavigationApi(metaclass=Singleton): points=points, polygons=polygons, ) + + # Functions for InVesalius to receive updates via callbacks. + + def __set_callbacks(self, connection): + connection.set_callback__set_markers(self.set_markers) + + def set_markers(self, markers): + Publisher.sendMessage('Set markers', markers=markers) -- libgit2 0.21.2