Slicer 4.2
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
vtkMRMLNode.h
Go to the documentation of this file.
00001 /*=auto=========================================================================
00002 
00003   Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH) All Rights Reserved.
00004 
00005   See COPYRIGHT.txt
00006   or http://www.slicer.org/copyright/copyright.txt for details.
00007 
00008   Program:   3D Slicer
00009   Module:    $RCSfile: vtkMRMLNode.h,v $
00010   Date:      $Date: 2006/03/19 17:12:29 $
00011   Version:   $Revision: 1.18 $
00012 
00013 =========================================================================auto=*/
00014 
00015 #ifndef __vtkMRMLNode_h
00016 #define __vtkMRMLNode_h
00017 
00018 // MRML includes
00019 #include "vtkMRML.h"
00020 #include "vtkObserverManager.h"
00021 #include "vtkIdTypeArray.h"
00022 #include "vtkIntArray.h"
00023 
00024 class vtkMRMLScene;
00025 
00026 
00027 // VTK includes
00028 #include <vtkObject.h>
00029 class vtkCallbackCommand;
00030 
00031 // STD includes
00032 #include <string>
00033 #include <map>
00034 #include <vector>
00035 
00036 #ifndef vtkSetMRMLObjectMacro
00037 #define vtkSetMRMLObjectMacro(node,value)  {this->MRMLObserverManager->SetObject ( vtkObjectPointer( &(node)), (value) );};
00038 #endif
00039 
00041 #ifndef vtkSetAndObserveMRMLObjectMacro
00042 #define vtkSetAndObserveMRMLObjectMacro(node,value)  {this->MRMLObserverManager->SetAndObserveObject ( vtkObjectPointer( &(node)), (value) );};
00043 #endif
00044 
00045 #ifndef vtkSetAndObserveMRMLObjectEventsMacro
00046 #define vtkSetAndObserveMRMLObjectEventsMacro(node,value,events)  {this->MRMLObserverManager->SetAndObserveObjectEvents ( vtkObjectPointer( &(node)), (value), (events));};
00047 #endif
00048 
00049 #ifndef vtkObserveMRMLObjectMacro
00050 #define vtkObserveMRMLObjectMacro(node)  {this->MRMLObserverManager->ObserveObject ( (node) );};
00051 #endif
00052 
00053 #ifndef vtkObserveMRMLObjectEventsMacro
00054 #define vtkObserveMRMLObjectEventsMacro(node, events)  {this->MRMLObserverManager->AddObjectEvents ( (node), (events) );};
00055 #endif
00056 
00057 #ifndef vtkUnObserveMRMLObjectMacro
00058 #define vtkUnObserveMRMLObjectMacro(node)  {this->MRMLObserverManager->RemoveObjectEvents ( (node) );};
00059 #endif
00060 
00061 #ifndef vtkSetReferenceStringBodyMacro
00062 #define vtkSetReferenceStringBodyMacro(name) \
00063   vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting " << #name " to " << (_arg?_arg:"(null)") ); \
00064   if ( this->name == NULL && _arg == NULL) { return;} \
00065   if ( this->name && _arg && (!strcmp(this->name,_arg))) { return;} \
00066   std::string oldValue; \
00067   if (this->name) { oldValue = this->name; delete [] this->name;  } \
00068   if (_arg) \
00069     { \
00070     size_t n = strlen(_arg) + 1; \
00071     char *cp1 =  new char[n]; \
00072     const char *cp2 = (_arg); \
00073     this->name = cp1; \
00074     do { *cp1++ = *cp2++; } while ( --n ); \
00075     } \
00076    else \
00077     { \
00078     this->name = NULL; \
00079     } \
00080   this->Modified(); \
00081   if (this->Scene && this->name) \
00082     { \
00083     if (oldValue.size() > 0) \
00084       { \
00085       this->Scene->RemoveReferencedNodeID(oldValue.c_str(), this); \
00086       } \
00087     this->Scene->AddReferencedNodeID(this->name, this); \
00088     }
00089 #endif
00090 
00091 #ifndef vtkSetReferenceStringMacro
00092 #define vtkSetReferenceStringMacro(name) \
00093 virtual void Set##name (const char* _arg) \
00094   { \
00095   vtkSetReferenceStringBodyMacro(name)\
00096   }
00097 #endif
00098 
00099 #ifndef vtkCxxSetReferenceStringMacro
00100 #define vtkCxxSetReferenceStringMacro(class,name)   \
00101 void class::Set##name (const char* _arg)            \
00102   {                                                 \
00103   vtkSetReferenceStringBodyMacro(name);             \
00104   }
00105 #endif
00106 
00107 #ifndef vtkMRMLNodeNewMacro
00108 #define vtkMRMLNodeNewMacro(newClass) \
00109   vtkStandardNewMacro(newClass); \
00110   vtkMRMLNode* newClass::CreateNodeInstance() \
00111   { \
00112     return newClass::New(); \
00113   }
00114 #endif
00115 
00116 
00122 class VTK_MRML_EXPORT vtkMRMLNode : public vtkObject
00123 {
00127     friend class vtkMRMLScene;
00128     friend class vtkMRMLSceneViewNode;
00129 
00130 public:
00131   vtkTypeMacro(vtkMRMLNode,vtkObject);
00132   void PrintSelf(ostream& os, vtkIndent indent);
00133   
00137   virtual vtkMRMLNode* CreateNodeInstance() = 0;
00138 
00143   virtual void ReadXMLAttributes(const char** atts);
00144 
00148   virtual void UpdateReferences();
00149 
00153   virtual void ProcessChildNode(vtkMRMLNode *){};
00154   
00159   virtual void UpdateScene(vtkMRMLScene *) 
00160   {
00161     this->UpdateNodeReferences();
00162   };
00163 
00167   virtual void OnNodeAddedToScene() 
00168   {
00169     this->UpdateNodeReferences();
00170   };
00171 
00176   virtual void WriteXML(ostream& of, int indent);
00177 
00180   virtual void WriteNodeBodyXML(ostream& of, int indent);
00181   
00186   virtual void Copy(vtkMRMLNode *node);
00187 
00193   void CopyWithScene(vtkMRMLNode *node);
00194   
00200   virtual void Reset();
00201 
00206   virtual int StartModify() 
00207     {
00208     int disabledModify = this->GetDisableModifiedEvent();
00209     this->DisableModifiedEventOn();
00210     return disabledModify;
00211     };
00212 
00218   virtual int EndModify(int previousDisableModifiedEventState) 
00219     {
00220     this->SetDisableModifiedEvent(previousDisableModifiedEventState);
00221     if (!previousDisableModifiedEventState)
00222       {
00223       return this->InvokePendingModifiedEvent();
00224       }
00225     return this->ModifiedEventPending;
00226     };
00227 
00228 
00232   virtual const char* GetNodeTagName() = 0;
00233 
00240   void SetAttribute(const char* name, const char* value);
00241 
00244   void RemoveAttribute(const char* name);
00245 
00249   const char* GetAttribute(const char* name);
00250 
00253   std::vector< std::string > GetAttributeNames();
00254 
00257   vtkGetMacro(HideFromEditors, int);
00258   vtkSetMacro(HideFromEditors, int);
00259   vtkBooleanMacro(HideFromEditors, int);
00260 
00263   vtkGetMacro(Selectable, int);
00264   vtkSetMacro(Selectable, int);
00265   vtkBooleanMacro(Selectable, int);
00266 
00267   
00270   virtual void ProcessMRMLEvents ( vtkObject *caller, unsigned long event, void *callData );
00271 
00275   vtkGetMacro(InMRMLCallbackFlag, int);
00276   void SetInMRMLCallbackFlag (int flag) {
00277     this->InMRMLCallbackFlag = flag;
00278   }
00279 
00282   vtkSetStringMacro(Description);
00283   vtkGetStringMacro(Description);
00284 
00287   vtkSetStringMacro(SceneRootDir);
00288   vtkGetStringMacro(SceneRootDir);
00289   
00292   vtkSetStringMacro(Name);
00293   vtkGetStringMacro(Name);
00294   
00295   
00299   vtkGetMacro(Indent, int);
00300   
00303   //vtkSetStringMacro(ID);
00304   vtkGetStringMacro(ID);
00305 
00312   vtkSetStringMacro(SingletonTag);
00313   vtkGetStringMacro(SingletonTag);
00314 
00317   vtkGetMacro(SaveWithScene, int);
00318   vtkSetMacro(SaveWithScene, int);
00319   vtkBooleanMacro(SaveWithScene, int);
00320 
00323   vtkGetMacro(AddToScene, int);
00324   vtkSetMacro(AddToScene, int);
00325   vtkBooleanMacro(AddToScene, int);
00326   void SetAddToSceneNoModify(int value);
00327 
00330   vtkGetMacro(DisableModifiedEvent, int);
00331   void SetDisableModifiedEvent(int onOff)
00332     {
00333     this->DisableModifiedEvent = onOff;
00334     }
00335   void DisableModifiedEventOn()
00336     {
00337     this->SetDisableModifiedEvent(1);
00338     }
00339   void DisableModifiedEventOff()
00340     {
00341     this->SetDisableModifiedEvent(0);
00342     }
00343 
00345   vtkGetMacro(ModifiedEventPending, int);
00346 
00353   virtual void Modified() 
00354     {
00355     if (!this->GetDisableModifiedEvent())
00356       {
00357       Superclass::Modified();
00358       }
00359     else
00360       {
00361       ++this->ModifiedEventPending;
00362       }
00363     }
00364 
00369   virtual int InvokePendingModifiedEvent ()
00370     {
00371     if ( this->ModifiedEventPending )
00372       {
00373       int oldModifiedEventPending = this->ModifiedEventPending;
00374       this->ModifiedEventPending = 0;
00375       Superclass::Modified();
00376       return oldModifiedEventPending;
00377       }
00378     return this->ModifiedEventPending;
00379     }
00380 
00381   void CopyWithSingleModifiedEvent (vtkMRMLNode *node)
00382     {
00383     int oldMode = this->GetDisableModifiedEvent();
00384     this->DisableModifiedEventOn();
00385     this->Copy(node);
00386     this->InvokePendingModifiedEvent();
00387     this->SetDisableModifiedEvent(oldMode);
00388     }
00389     
00390   void CopyWithoutModifiedEvent (vtkMRMLNode *node)
00391     {
00392     int oldMode = this->GetDisableModifiedEvent();
00393     this->DisableModifiedEventOn();
00394     this->Copy(node);
00395     this->SetDisableModifiedEvent(oldMode);
00396     }
00397 
00398   void CopyWithSceneWithSingleModifiedEvent (vtkMRMLNode *node)
00399     {
00400     int oldMode = this->GetDisableModifiedEvent();
00401     this->DisableModifiedEventOn();
00402     this->CopyWithScene(node);
00403     this->InvokePendingModifiedEvent();
00404     this->SetDisableModifiedEvent(oldMode);
00405     }
00406 
00407   void CopyWithSceneWithoutModifiedEvent (vtkMRMLNode *node)
00408     {
00409     int oldMode = this->GetDisableModifiedEvent();
00410     this->DisableModifiedEventOn();
00411     this->CopyWithScene(node);
00412     this->SetDisableModifiedEvent(oldMode);
00413     }
00414 
00419   vtkGetObjectMacro(Scene, vtkMRMLScene);
00420   virtual void SetScene(vtkMRMLScene* scene);
00421 
00425   virtual void SetSceneReferences();
00426 
00429   virtual void UpdateReferenceID(const char *oldID, const char *newID);
00430 
00435   const char *URLEncodeString(const char *inString);
00440   const char *URLDecodeString(const char *inString);
00441 
00443   vtkGetMacro(Selected, int);
00444   vtkSetMacro(Selected, int);
00445   vtkBooleanMacro(Selected, int);
00446 
00453   void AddNodeReferenceRole(const char *referenceRole, const char *mrmlAttributeName=0);
00454   
00457   vtkMRMLNode* SetNodeReferenceID(const char* referenceRole, const char* referencedNodeID);
00458 
00460   vtkMRMLNode* AddNodeReferenceID(const char* referenceRole , const char* referencedNodeID);
00461 
00464   vtkMRMLNode* SetNthNodeReferenceID(const char* referenceRole, int n, const char* referencedNodeID);
00465 
00469   vtkMRMLNode* SetAndObserveNodeReferenceID(const char* referenceRole , const char* referencedNodeID, vtkIntArray *events=0);
00470 
00474   vtkMRMLNode* AddAndObserveNodeReferenceID(const char* referenceRole , const char* referencedNodeID, vtkIntArray *events=0);
00475   
00490   vtkMRMLNode* SetAndObserveNthNodeReferenceID(const char* referenceRole, int n, const char *nodeReferenceID, vtkIntArray *events=0);
00491 
00495   void RemoveNthNodeReferenceID(const char* referenceRole, int n);
00496 
00500   void RemoveAllNodeReferenceIDs(const char* referenceRole);
00501 
00504   bool HasNodeReferenceID(const char* referenceRole, const char* NodeReferenceID);
00505 
00509   int GetNumberOfNodeReferences(const char* referenceRole);
00515   const char *GetNthNodeReferenceID(const char* referenceRole, int n);
00516 
00520   const char *GetNodeReferenceID(const char* referenceRole);
00521 
00531   vtkMRMLNode* GetNthNodeReference(const char* referenceRole, int n);
00532 
00536   vtkMRMLNode* GetNodeReference(const char* referenceRole);
00537 
00544   void GetNodeReferences(const char* referenceRole, std::vector<vtkMRMLNode*> &nodes);
00545 
00548   enum
00549     {
00550       HierarchyModifiedEvent = 16000,
00551       IDChangedEvent = 16001,
00552       ReferenceAddedEvent,
00553       ReferenceModifiedEvent,
00554       ReferenceRemovedEvent,
00555       ReferencedNodeModifiedEvent
00556     };
00557 protected: 
00560   class VTK_MRML_EXPORT vtkMRMLNodeReference : public vtkObject
00561   {
00562   public: 
00563     vtkTypeMacro(vtkMRMLNodeReference,vtkObject);
00564     static vtkMRMLNodeReference *New();
00565     void PrintSelf(ostream& vtkNotUsed(os), vtkIndent vtkNotUsed(indent)){};
00566 
00567   public:
00568     vtkSetStringMacro(ReferenceRole);
00569     vtkGetStringMacro(ReferenceRole);
00570 
00571     vtkSetStringMacro(ReferencedNodeID);
00572     vtkGetStringMacro(ReferencedNodeID);
00573 
00574     vtkMRMLNode*    ReferencingNode;
00575     vtkMRMLNode*    ReferencedNode;
00576     vtkIntArray*    Events;
00577 
00578   protected:
00579 
00580     vtkMRMLNodeReference()
00581       {
00582       this->ReferencedNode = 0;
00583       this->ReferencingNode = 0;
00584       this->Events = 0;
00585       this->ReferencedNodeID = 0;
00586       this->ReferenceRole = 0;
00587       }
00588 
00589     ~vtkMRMLNodeReference()
00590       {
00591       if (this->Events)
00592         {
00593         this->Events->Delete();
00594         this->Events = 0;
00595         }
00596       };
00597     vtkMRMLNodeReference(const vtkMRMLNodeReference&);
00598     void operator=(const vtkMRMLNodeReference&);
00599 
00600     char*     ReferenceRole;
00601     char*     ReferencedNodeID;
00602 
00603   };
00604 
00608   typedef std::map< std::string, std::vector< vtkMRMLNodeReference *> > NodeReferencesType;
00609   NodeReferencesType NodeReferences;
00610 
00611   std::map< std::string, std::string> NodeReferenceMRMLAttributeNames;
00612 
00613 protected:
00614   
00615   vtkMRMLNode();
00617   virtual ~vtkMRMLNode();
00618   vtkMRMLNode(const vtkMRMLNode&);
00619   void operator=(const vtkMRMLNode&);
00620   
00621   vtkSetMacro(Indent, int);
00622 
00625   static void MRMLCallback( vtkObject *caller,
00626                             unsigned long eid, void *clientData, void *callData );
00627 
00630   vtkCallbackCommand *MRMLCallbackCommand;
00631 
00634   int InMRMLCallbackFlag;
00635 
00636   char *Description;
00637   char *SceneRootDir;
00638   char *Name;
00639   char *ID;
00640   int Indent;
00641   int HideFromEditors;
00642   int Selectable;
00643   int Selected;
00644   int AddToScene;
00645 
00646   int  SaveWithScene;
00647 
00648   vtkMRMLScene *Scene;
00649 
00650   typedef std::map< std::string, std::string > AttributesType;
00651   AttributesType Attributes;
00652 
00653   vtkObserverManager *MRMLObserverManager;
00654 
00657   vtkSetStringMacro( TempURLString );
00658   vtkGetStringMacro( TempURLString );
00659 
00660 
00663   void UpdateNodeReferences(const char* referenceRole);
00664 
00667   void UpdateNodeReferences();
00668 
00671   void UpdateNthNodeReference(const char* referenceRole, int n);
00672 
00673   void UpdateNthNodeReference(vtkMRMLNodeReference *reference, int n);
00674 
00675   void SetAndObserveNthNodeReference(const char* referenceRole, int n, vtkMRMLNode *referencedNode, vtkIntArray *events=0);
00676 
00678   void DeleteAllReferences(bool callOnNodeReferenceRemoved=true);
00679 
00681   void RemoveAllReferencedNodes();
00682 
00685   virtual void OnNodeReferenceAdded(vtkMRMLNodeReference *reference)
00686   {
00687     this->InvokeEvent(vtkMRMLNode::ReferenceAddedEvent, reference);
00688   }
00689 
00692   virtual void OnNodeReferenceModified(vtkMRMLNodeReference *reference)
00693   {
00694     this->InvokeEvent(vtkMRMLNode::ReferenceModifiedEvent, reference);
00695   }
00696 
00699   virtual void OnNodeReferenceRemoved(vtkMRMLNodeReference *reference)
00700   {
00701     this->InvokeEvent(vtkMRMLNode::ReferenceRemovedEvent, reference);
00702   }
00703 
00704 
00705 
00706 private:
00710   void SetID(const char* newID);
00711 
00714   char * TempURLString;
00715 
00716   char *SingletonTag;
00717 
00718   int DisableModifiedEvent;
00719   int ModifiedEventPending;
00720 
00721 };
00722 
00723 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines