Montag, 9. August 2010

Install a listener for the global data storage

Wouldn't it be nice if your plug-in if your plugin would be notified on changed in the data manager like when a file is opened? Yes it definitly would be nice. So this is how you do it:

mitk::DataStorage::Pointer storage = GetDefaultDataStorage();

storage->AddNodeEvent.AddListener(
   mitk::MessageDelegate1<YOURPLUGIN, const mitk::DataNode*>(
   this, &YOURPLUGIN::YOURPLUGINFUNCTION ));

First get a pointer to the default data storage. It's member variable AddNodeEvent, itself a typedef, offers the AddListener function. Use a mitk::MessageDelegate1 as parameter. MessageDelegate1 takes the type of your plug-in and a return value, in this case a mitk::DataNode pointer to effectivly create a delegate. Delegates are not part of C++, so this object is effectivly a delegate implementation taking a this pointer (google can help you if you don't know what this means). At last give the constructor of the delegate a this pointer and a function pointer to your listerner function.

That's it. If a node is added to the data storage the given listener function is called with a pointer to the added node. Further steps can be taken from here.

There are additional listener functions like DeleteNodeEvent, RemoveNodeEvent and ChangeNodeEvent. Implement them all for fame and glory.

How to access the central data storage

One key concept of MITK is a central data storage. All plug-in may access and get data from or post data to it. This enables a basic inter-plug-in communication. This is how to access the data storage (curiously it's called data manager in the running mitk app):

mitk::DataStorage::Pointer storage = GetDefaultDataStorage();

To get a data object from the storage just use the getter taking a name:

mitk::DataNode* = storage->GetNamedNode("node name");

There is also a way to iterate over all items in the data storage (typedefd for convinience):
typedef mitk::DataStorage::SetOfObjects soo;
soo::ConstPointer list = storage->GetAll();
for (see::const_iterator it = list->begin(); it != list->end(); ++it)
MITK_INFO << (*it); // write all object information to standard out

So, this is how one gets data from the central data storage. Enjoy