Difference between revisions of "Documentation/Nightly/Developers/IO"

From Slicer Wiki
Jump to: navigation, search
(→‎Example: Updating Links)
 
(13 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
<noinclude>{{documentation/versioncheck}}</noinclude>
 
=Read=
 
=Read=
 
[[Image:IOOverview.png|600px|center]]
 
[[Image:IOOverview.png|600px|center]]
Line 5: Line 6:
 
* <code>vtkSlicer<i>XYZ</i>StorageNode</code> is the file reader. It populates the storable node using the method  <code>vtkMRML<i>XYZ</i>StorageNode::ReadData(vtkMRMLStorableNode*,bool)</code>.
 
* <code>vtkSlicer<i>XYZ</i>StorageNode</code> is the file reader. It populates the storable node using the method  <code>vtkMRML<i>XYZ</i>StorageNode::ReadData(vtkMRMLStorableNode*,bool)</code>.
 
* <code>vtkSlicer<i>XYZ</i>sLogic</code> is the MRML logic of the <code>XYZ</code> module. It exposes a convenient method <code>AddXYZ(const char* fileName, const char* nodeName=0);</code> that creates a MRML <i>XYZ</i> node (<code>vtkMRML<i>XYZ</i>Node</code>)and its associated storage node (<code>vtkMRML<i>XYZ</i>StorageNode</code>), add them into the scene, and call <code>vtkMRML<i>XYZ</i>StorageNode::ReadData(vtkMRMLStorableNode*,bool);</code> on the storage node to load the file. If the loading fails, it removes the previously created nodes from the scene.
 
* <code>vtkSlicer<i>XYZ</i>sLogic</code> is the MRML logic of the <code>XYZ</code> module. It exposes a convenient method <code>AddXYZ(const char* fileName, const char* nodeName=0);</code> that creates a MRML <i>XYZ</i> node (<code>vtkMRML<i>XYZ</i>Node</code>)and its associated storage node (<code>vtkMRML<i>XYZ</i>StorageNode</code>), add them into the scene, and call <code>vtkMRML<i>XYZ</i>StorageNode::ReadData(vtkMRMLStorableNode*,bool);</code> on the storage node to load the file. If the loading fails, it removes the previously created nodes from the scene.
* <code>qSlicer<i>XYZ</i>sIO</code> is a plugin that is registered by modules into the <code>qSlicerCoreIOManager</code>. It is the interface between Qt and MRML logics. It internally calls <code>vtkSlicer<i>XYZ</i>sLogic::Add<i>XYZ</i></code>().
+
* <code>qSlicer<i>XYZ</i>sReaderPlugin</code> is a plugin that is registered by modules into the <code>qSlicerCoreIOManager</code>. It is the interface between Qt and MRML logics. It internally calls <code>vtkSlicer<i>XYZ</i>sLogic::Add<i>XYZ</i></code>().
* <code>qSlicer<i>XYZ</i>sIOOptionsWidget</code> is a widget that sets loading options that gets passed to the logic.
+
* <code>qSlicer<i>XYZ</i>sOptionsWidget</code> is a widget that sets loading options that gets passed to the logic.
 
* <code>qSlicerCoreIOManager</code> is the central class where any IO operation must go through. qSlicerIOs can be registered using <code>qSlicerCoreIOManager::registerIO(qSlicerIO*)</code> and nodes can be loaded using <code>qSlicerCoreIOManager::loadNodes(...)</code>. It exposes a set of convenient methods such as "what reader must be used for what file".
 
* <code>qSlicerCoreIOManager</code> is the central class where any IO operation must go through. qSlicerIOs can be registered using <code>qSlicerCoreIOManager::registerIO(qSlicerIO*)</code> and nodes can be loaded using <code>qSlicerCoreIOManager::loadNodes(...)</code>. It exposes a set of convenient methods such as "what reader must be used for what file".
 
* <code>qSlicerDataDialog</code> is the dialog that allows the user to select the files to load.
 
* <code>qSlicerDataDialog</code> is the dialog that allows the user to select the files to load.
Line 13: Line 14:
 
# Write method <code>vtkMRMLXYZStorageNode::ReadDataInternal(vtkMRMLStorableNode*, bool temporary);</code>
 
# Write method <code>vtkMRMLXYZStorageNode::ReadDataInternal(vtkMRMLStorableNode*, bool temporary);</code>
 
# Write method <code>vtkMRMLXYZsLogic::AddXYZ(const char* fileName, const char nodeName =0);</code>
 
# Write method <code>vtkMRMLXYZsLogic::AddXYZ(const char* fileName, const char nodeName =0);</code>
# Write class <code>qSlicerXYZsIO</code>
+
# Write class <code>qSlicerXYZsReaderPlugin</code>
# Optional: Write class <code>qSlicerXYZsIOOptionsWidget</code> if you want the user to optionally specify loading options.
+
# Optional: Write class <code>qSlicerXYZsOptionsWidget</code> if you want the user to optionally specify loading options.
 
# In <code>qSlicerXYZsModule::setup()</code>, instantiate and register <code>qSlicerXYZsIO</code> to <code>qSlicerCoreIOManager</code>
 
# In <code>qSlicerXYZsModule::setup()</code>, instantiate and register <code>qSlicerXYZsIO</code> to <code>qSlicerCoreIOManager</code>
 
# Add file format to [Documentation/{{documentation/version}}/SlicerApplication/SupportedDataFormat|SupportedDataFormat] wiki page
 
# Add file format to [Documentation/{{documentation/version}}/SlicerApplication/SupportedDataFormat|SupportedDataFormat] wiki page
Line 23: Line 24:
 
<code>vtkMRMLStorageNode</code> keeps track of when a file was last read or written (<code>vtkTimeStamp* vtkMRMLStorageNode::StoredTime</code>). <code>vtkMRMLStorableNode</code> keeps track of when the data was last modified (<code>vtkTimeStamp vtkMRMLStorableNode::StorableModifiedTime</code>). Anytime a <code>vtkMRMLStorableNode</code> property that is saved in file is modified, the <code>StorableModifiedTime</code> time stamp must be modified.
 
<code>vtkMRMLStorageNode</code> keeps track of when a file was last read or written (<code>vtkTimeStamp* vtkMRMLStorageNode::StoredTime</code>). <code>vtkMRMLStorableNode</code> keeps track of when the data was last modified (<code>vtkTimeStamp vtkMRMLStorableNode::StorableModifiedTime</code>). Anytime a <code>vtkMRMLStorableNode</code> property that is saved in file is modified, the <code>StorableModifiedTime</code> time stamp must be modified.
  
At exit time (<code>qSlicerMainWindow::closeEvent()</code>), <code>vtkMRMLStorableNode::GetModifiedSinceRead()</code> is called to check if the data in the node is the same as in the file or if it has been modified after the file was last read or written. It internally checks <code>vtkTimeStamp* vtkMRMLStorageNode::StoredTime</code> and <code>vtkTimeStamp vtkMRMLStorableNode::StorableModifiedTime</code>. If the data is more recent, then a message dialog is shown to the user telling him that some data is different from file; leaving without saving will loose the changes.
+
At exit time (<code>qSlicerMainWindow::closeEvent()</code>), <code>vtkMRMLStorableNode::GetModifiedSinceRead()</code> is called to check if the data in the node is the same as in the file or if it has been modified after the file was last read or written. It internally checks <code>vtkTimeStamp* vtkMRMLStorageNode::StoredTime</code> and <code>vtkTimeStamp vtkMRMLStorableNode::StorableModifiedTime</code>. If the data is more recent, then a message dialog is shown to the user telling him that some data is different from file; leaving without saving will lose the changes.
  
 
== Example ==
 
== Example ==
 
* Models  
 
* Models  
** [http://viewvc.slicer.org/viewvc.cgi/Slicer4/trunk/Libs/MRML/Core/vtkMRMLModelNode.h?view=markup Libs/MRML/Core/vtkMRMLModelNode]
+
** [https://github.com/Slicer/Slicer/blob/master/Libs/MRML/Core/vtkMRMLModelNode.h Libs/MRML/Core/vtkMRMLModelNode]
** [http://viewvc.slicer.org/viewvc.cgi/Slicer4/trunk/Libs/MRML/Core/vtkMRMLModelStorageNode.h?view=markup Libs/MRML/Core/vtkMRMLModelStorageNode]
+
** [https://github.com/Slicer/Slicer/blob/master/Libs/MRML/Core/vtkMRMLModelStorageNode.h Libs/MRML/Core/vtkMRMLModelStorageNode]
** [http://viewvc.slicer.org/viewvc.cgi/Slicer4/trunk/Modules/Loadable/Models/Logic/vtkSlicerModelsLogic.h?view=markup Modules/Loadable/Models/Logic/vtkSlicerModelsLogic]
+
** [https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Models/Logic/vtkSlicerModelsLogic.h Modules/Loadable/Models/Logic/vtkSlicerModelsLogic]
** [http://viewvc.slicer.org/viewvc.cgi/Slicer4/trunk/Modules/Loadable/Models/qSlicerModelsIO.h?view=markup Modules/Loadable/Models/qSlicerModelsIO]
+
** [https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Models/qSlicerModelsReader.h Modules/Loadable/Models/qSlicerModelsReader]
** [http://viewvc.slicer.org/viewvc.cgi/Slicer4/trunk/Modules/Loadable/Models/qSlicerModelsModule.h?view=markup Modules/Loadable/Models/qSlicerModelsModule]
+
** [https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Models/qSlicerModelsModule.h Modules/Loadable/Models/qSlicerModelsModule]
 +
* VffFileReader
 +
** [https://github.com/SlicerRt/SlicerRT/tree/master/VffFileReader VffFileReader]
  
 
== How to load files programmatically ==
 
== How to load files programmatically ==
Line 41: Line 44:
 
  [http://slicer.org/doc/html/classqSlicerCoreIOManager.html qSlicerCoreIOManager]* coreIOManager = qSlicerCoreApplication::application()->coreIOManager();
 
  [http://slicer.org/doc/html/classqSlicerCoreIOManager.html qSlicerCoreIOManager]* coreIOManager = qSlicerCoreApplication::application()->coreIOManager();
 
  qSlicerIO::IOProperties fileParameters;
 
  qSlicerIO::IOProperties fileParameters;
  fileParameters["filename"] = "/path/of/file.ext";
+
  fileParameters["fileName"] = "/path/of/file.ext";
  vtkMRMLNode* volumeNode = coreIOManager->[http://slicer.org/doc/html/classqSlicerCoreIOManager.html#a36a78dba553b52f395f997fe11aaff23 loadNodesAndGetFirst]("VolumeFile", parameters);
+
  vtkMRMLNode* volumeNode = coreIOManager->[http://slicer.org/doc/html/classqSlicerCoreIOManager.html#a36a78dba553b52f395f997fe11aaff23 loadNodesAndGetFirst]("VolumeFile", fileParameters);
 
Works also with "ModelFile", "TransformFile", "SceneFile"...
 
Works also with "ModelFile", "TransformFile", "SceneFile"...
 
| valign="top" |
 
| valign="top" |
 
  >>> volumeNode = slicer.util.loadVolume('/path/of/file.ext')
 
  >>> volumeNode = slicer.util.loadVolume('/path/of/file.ext')
>>> ...
 
 
It works also with loadModel, loadTransform, loadScene...
 
It works also with loadModel, loadTransform, loadScene...
 
|}
 
|}
 +
Note that [[#IO_Dialogs|IO dialogs]] should be used as often as possible.
  
 
=Write=
 
=Write=
 
The mechanism is similar to '''Read'''. <code>vtkMRMLXYZStorageNode::WriteDataInternal(...)</code> and <code>qSlicerXYZWriter</code> must implemented.
 
The mechanism is similar to '''Read'''. <code>vtkMRMLXYZStorageNode::WriteDataInternal(...)</code> and <code>qSlicerXYZWriter</code> must implemented.
 +
 +
== How to save files programmatically ==
 +
{|width = "100%"
 +
! style="border-bottom: 1px solid darkgrey;font-size: 75%;"|C++
 +
! style="border-bottom: 1px solid darkgrey;font-size: 75%;"|Python
 +
|-
 +
| valign="top" |
 +
[http://slicer.org/doc/html/classqSlicerCoreIOManager.html qSlicerCoreIOManager]* coreIOManager = qSlicerCoreApplication::application()->coreIOManager();
 +
qSlicerIO::IOProperties fileParameters;
 +
fileParameters["nodeID"] = volumeNode->GetID();
 +
fileParameters["fileName"] = "/path/of/file.ext";
 +
coreIOManager->[http://slicer.org/doc/html/classqSlicerCoreIOManager.html#a1cd29140ded984afdd0f667064d38845 saveNodes]("VolumeFile", fileParameters);
 +
Works also with "ModelFile", "TransformFile", "SceneFile"...
 +
| valign="top" |
 +
>>> slicer.util.saveNode(volumeNode, '/path/of/file.ext')
 +
|}
 +
Note that [[#IO_Dialogs|IO dialogs]] should be used as often as possible.
  
 
=IO Dialogs=
 
=IO Dialogs=
By default, all the IOs must be done through <code>qSlicerDataDialog</code> for reading files and <code>qSlicerSaveDataDialog</code> for writting files. However, due to historical reasons, it is possible to have custom dialogs for each node types.
+
* <code>[http://slicer.org/doc/html/classqSlicerFileDialog.html qSlicerFileDialog]</code> is the base class to override a custom dialog.
 +
* <code>[http://slicer.org/doc/html/classqSlicerStandardFileDialog.html qSlicerStandardFileDialog]</code> is a multi-purpose file dialog.
 +
* <code>[http://slicer.org/doc/html/classqSlicerDataDialog.html qSlicerDataDialog.h]</code> is the default dialog to load files.
 +
* <code>[http://slicer.org/doc/html/classqSlicerSaveDataDialog.html qSlicerSaveDataDialog]</code> is the default dialog to save files.
 +
 
 +
By default, all the IOs must be done through <code>qSlicerDataDialog</code> for reading files and <code>qSlicerSaveDataDialog</code> for writing files. However, due to historical reasons, it is possible to have custom dialogs for each node types.
 +
 
 +
It can be useful to create your own IO dialog to add custom behavior to the Slicer drag&drop default behavior.
 +
== How to register a dialog ? ==
 +
{|width = "100%"
 +
! style="border-bottom: 1px solid darkgrey;font-size: 75%;"|C++
 +
! style="border-bottom: 1px solid darkgrey;font-size: 75%;"|Python
 +
|-
 +
| valign="top" |
 +
Register dialogs in qSlicerXYZModule::[http://slicer.org/doc/html/classqSlicerAbstractCoreModule.html#a9ad37e756338e7226f157b4eb54b9bcd setup()]:
 +
[http://slicer.org/doc/html/classqSlicerIOManager.html qSlicerIOManager]* ioManager = qSlicerApplication::application()->ioManager();
 +
ioManager->[http://slicer.org/doc/html/classqSlicerIOManager.html#ae47e9cf27f230335b865bce84a56e156 registerDialog](new qSlicerXYZDialog(this));
 +
ioManager->[http://slicer.org/doc/html/classqSlicerIOManager.html#ae47e9cf27f230335b865bce84a56e156 registerDialog](new qSlicerSaveXYZDialog(this));
 +
| valign="top" |
 +
Declare an XYZFileDialog in your XYZ.py module file. It will be automatically registered. For example:
 +
class XYZFileDialog:
 +
  def __init__(self, parent):
 +
    self.parent = parent
 +
    parent.fileType = 'XYZFile'
 +
    parent.description = 'XYZ'
 +
    parent.action = slicer.qSlicerFileDialog.Read
 +
  def isMimeDataAccepted(self):
 +
    accept = self.parent.mimeData().hasFormat("text/uri-list")
 +
    self.parent.acceptMimeData(accept)
 +
  def dropEvent(self):
 +
    self.parent.dropEvent().accept()
 +
  def execDialog(self):
 +
    print 'exec'
 +
|}
 +
 
 +
== How to open a registered a dialog ? ==
 +
{|width = "100%"
 +
! style="border-bottom: 1px solid darkgrey;font-size: 75%;"|C++
 +
! style="border-bottom: 1px solid darkgrey;font-size: 75%;"|Python
 +
|-
 +
| valign="top" |
 +
[http://slicer.org/doc/html/classqSlicerIOManager.html qSlicerIOManager]* ioManager = qSlicerApplication::application()->ioManager();
 +
ioManager->openDialog("VolumeFile", qSlicerFileDialog::Read, qSlicerIO::IOProperties());
 +
| valign="top" |
 +
  io = slicer.app.ioManager()
 +
  params = {}
 +
  io.openDialog("VolumeFile", slicer.qSlicerFileDialog.Read, params)
 +
And the special case for standard data file types:
 +
slicer.util.openAddVolumeDialog()
 +
|}
 +
 
 +
== How to order the list of drag&drop dialogs ?==
 +
By controlling the order of module initialization. This is done by adding module dependencies. Make your module dependent of of the "Data" would make it initialize after the Data module that registers the "Any Data" dialogs:
 +
{|width = "100%"
 +
! style="border-bottom: 1px solid darkgrey;font-size: 75%;"|C++
 +
! style="border-bottom: 1px solid darkgrey;font-size: 75%;"|Python
 +
|-
 +
| valign="top" |
 +
QStringList qSlicerXYZModule::dependencies() const
 +
{
 +
  QStringList moduleDependencies;
 +
  moduleDependencies << "Data";
 +
  return moduleDependencies;
 +
}
 +
| valign="top" |
 +
def __init__(self, parent):
 +
  ...
 +
  parent.dependencies = ["Data"] class XYZFileDialog:
 +
  ...
 +
|}

Latest revision as of 13:54, 1 August 2018

Home < Documentation < Nightly < Developers < IO


Read

IOOverview.png

Main classes

  • vtkSlicerXYZNode is a storable node that represents the data from file (e.g. [vtkMRMLModelNode)
  • vtkSlicerXYZStorageNode is the file reader. It populates the storable node using the method vtkMRMLXYZStorageNode::ReadData(vtkMRMLStorableNode*,bool).
  • vtkSlicerXYZsLogic is the MRML logic of the XYZ module. It exposes a convenient method AddXYZ(const char* fileName, const char* nodeName=0); that creates a MRML XYZ node (vtkMRMLXYZNode)and its associated storage node (vtkMRMLXYZStorageNode), add them into the scene, and call vtkMRMLXYZStorageNode::ReadData(vtkMRMLStorableNode*,bool); on the storage node to load the file. If the loading fails, it removes the previously created nodes from the scene.
  • qSlicerXYZsReaderPlugin is a plugin that is registered by modules into the qSlicerCoreIOManager. It is the interface between Qt and MRML logics. It internally calls vtkSlicerXYZsLogic::AddXYZ().
  • qSlicerXYZsOptionsWidget is a widget that sets loading options that gets passed to the logic.
  • qSlicerCoreIOManager is the central class where any IO operation must go through. qSlicerIOs can be registered using qSlicerCoreIOManager::registerIO(qSlicerIO*) and nodes can be loaded using qSlicerCoreIOManager::loadNodes(...). It exposes a set of convenient methods such as "what reader must be used for what file".
  • qSlicerDataDialog is the dialog that allows the user to select the files to load.

How to add support for reading a new file format ?

  1. Write method vtkMRMLXYZStorageNode::ReadDataInternal(vtkMRMLStorableNode*, bool temporary);
  2. Write method vtkMRMLXYZsLogic::AddXYZ(const char* fileName, const char nodeName =0);
  3. Write class qSlicerXYZsReaderPlugin
  4. Optional: Write class qSlicerXYZsOptionsWidget if you want the user to optionally specify loading options.
  5. In qSlicerXYZsModule::setup(), instantiate and register qSlicerXYZsIO to qSlicerCoreIOManager
  6. Add file format to [Documentation/Nightly/SlicerApplication/SupportedDataFormat|SupportedDataFormat] wiki page

ModifiedSinceRead

In order to inform the user what data has been changed since it was last read, a ModifiedSinceRead mechanism is in place to track when was the file last read and when was the data last modified. If a modification happened , the storable node vtkMRMLStorableNode::GetModifiedSinceRead() must return true. If the data in the node is the same as in the file, then vtkMRMLStorableNode::GetModifiedSinceRead() must return false.

vtkMRMLStorageNode keeps track of when a file was last read or written (vtkTimeStamp* vtkMRMLStorageNode::StoredTime). vtkMRMLStorableNode keeps track of when the data was last modified (vtkTimeStamp vtkMRMLStorableNode::StorableModifiedTime). Anytime a vtkMRMLStorableNode property that is saved in file is modified, the StorableModifiedTime time stamp must be modified.

At exit time (qSlicerMainWindow::closeEvent()), vtkMRMLStorableNode::GetModifiedSinceRead() is called to check if the data in the node is the same as in the file or if it has been modified after the file was last read or written. It internally checks vtkTimeStamp* vtkMRMLStorageNode::StoredTime and vtkTimeStamp vtkMRMLStorableNode::StorableModifiedTime. If the data is more recent, then a message dialog is shown to the user telling him that some data is different from file; leaving without saving will lose the changes.

Example

How to load files programmatically

C++ Python
qSlicerCoreIOManager* coreIOManager = qSlicerCoreApplication::application()->coreIOManager();
qSlicerIO::IOProperties fileParameters;
fileParameters["fileName"] = "/path/of/file.ext";
vtkMRMLNode* volumeNode = coreIOManager->loadNodesAndGetFirst("VolumeFile", fileParameters);

Works also with "ModelFile", "TransformFile", "SceneFile"...

>>> volumeNode = slicer.util.loadVolume('/path/of/file.ext')

It works also with loadModel, loadTransform, loadScene...

Note that IO dialogs should be used as often as possible.

Write

The mechanism is similar to Read. vtkMRMLXYZStorageNode::WriteDataInternal(...) and qSlicerXYZWriter must implemented.

How to save files programmatically

C++ Python
qSlicerCoreIOManager* coreIOManager = qSlicerCoreApplication::application()->coreIOManager();
qSlicerIO::IOProperties fileParameters;
fileParameters["nodeID"] = volumeNode->GetID();
fileParameters["fileName"] = "/path/of/file.ext";
coreIOManager->saveNodes("VolumeFile", fileParameters);

Works also with "ModelFile", "TransformFile", "SceneFile"...

>>> slicer.util.saveNode(volumeNode, '/path/of/file.ext')

Note that IO dialogs should be used as often as possible.

IO Dialogs

By default, all the IOs must be done through qSlicerDataDialog for reading files and qSlicerSaveDataDialog for writing files. However, due to historical reasons, it is possible to have custom dialogs for each node types.

It can be useful to create your own IO dialog to add custom behavior to the Slicer drag&drop default behavior.

How to register a dialog ?

C++ Python

Register dialogs in qSlicerXYZModule::setup():

qSlicerIOManager* ioManager = qSlicerApplication::application()->ioManager();
ioManager->registerDialog(new qSlicerXYZDialog(this));
ioManager->registerDialog(new qSlicerSaveXYZDialog(this));

Declare an XYZFileDialog in your XYZ.py module file. It will be automatically registered. For example:

class XYZFileDialog:
  def __init__(self, parent):
    self.parent = parent
    parent.fileType = 'XYZFile'
    parent.description = 'XYZ'
    parent.action = slicer.qSlicerFileDialog.Read
  def isMimeDataAccepted(self):
    accept = self.parent.mimeData().hasFormat("text/uri-list")
    self.parent.acceptMimeData(accept)
  def dropEvent(self):
    self.parent.dropEvent().accept()
  def execDialog(self):
    print 'exec' 

How to open a registered a dialog ?

C++ Python
qSlicerIOManager* ioManager = qSlicerApplication::application()->ioManager();
ioManager->openDialog("VolumeFile", qSlicerFileDialog::Read, qSlicerIO::IOProperties());
 io = slicer.app.ioManager()
 params = {}
 io.openDialog("VolumeFile", slicer.qSlicerFileDialog.Read, params)

And the special case for standard data file types:

slicer.util.openAddVolumeDialog()

How to order the list of drag&drop dialogs ?

By controlling the order of module initialization. This is done by adding module dependencies. Make your module dependent of of the "Data" would make it initialize after the Data module that registers the "Any Data" dialogs:

C++ Python
QStringList qSlicerXYZModule::dependencies() const
{
  QStringList moduleDependencies;
  moduleDependencies << "Data";
  return moduleDependencies;
}
def __init__(self, parent):
  ...
  parent.dependencies = ["Data"] class XYZFileDialog:
  ...