PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Libs/MRML/vtkMRMLStorableNode.cxx

https://github.com/pieper/Slicer4
C++ | 489 lines | 366 code | 59 blank | 64 comment | 99 complexity | 35a2057c539dc5833804b112eabfd983 MD5 | raw file
  1. /*=auto=========================================================================
  2. Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH) All Rights Reserved.
  3. See Doc/copyright/copyright.txt
  4. or http://www.slicer.org/copyright/copyright.txt for details.
  5. Program: 3D Slicer
  6. Module: $RCSfile: vtkMRMLStorableNode.cxx,v $
  7. Date: $Date: 2006/03/03 22:26:39 $
  8. Version: $Revision: 1.3 $
  9. =========================================================================auto=*/
  10. #include <string>
  11. #include <iostream>
  12. #include <sstream>
  13. #include "vtkObjectFactory.h"
  14. #include "vtkCallbackCommand.h"
  15. #include "vtkMRMLStorableNode.h"
  16. #include "vtkMRMLScene.h"
  17. #include "vtkMRMLStorageNode.h"
  18. //----------------------------------------------------------------------------
  19. vtkMRMLStorableNode::vtkMRMLStorableNode()
  20. {
  21. this->UserTagTable = vtkTagTable::New();
  22. this->SlicerDataType = "";
  23. }
  24. //----------------------------------------------------------------------------
  25. vtkMRMLStorableNode::~vtkMRMLStorableNode()
  26. {
  27. if ( this->UserTagTable )
  28. {
  29. this->UserTagTable->Delete();
  30. this->UserTagTable = NULL;
  31. }
  32. this->SlicerDataType.clear();
  33. this->SetAndObserveStorageNodeID( NULL);
  34. }
  35. //----------------------------------------------------------------------------
  36. void vtkMRMLStorableNode::SetSlicerDataType ( const char *type )
  37. {
  38. this->SlicerDataType.clear();
  39. this->SlicerDataType = type;
  40. if (this->Scene)
  41. {
  42. this->Scene->InvokeEvent ( vtkMRMLScene::MetadataAddedEvent );
  43. }
  44. }
  45. //----------------------------------------------------------------------------
  46. const char* vtkMRMLStorableNode::GetSlicerDataType ()
  47. {
  48. return ( this->SlicerDataType.c_str() );
  49. }
  50. //----------------------------------------------------------------------------
  51. void vtkMRMLStorableNode::WriteXML(ostream& of, int nIndent)
  52. {
  53. // Write all attributes not equal to their defaults
  54. Superclass::WriteXML(of, nIndent);
  55. vtkIndent indent(nIndent);
  56. std::stringstream ss;
  57. unsigned int n;
  58. for (n=0; n < this->StorageNodeIDs.size(); n++)
  59. {
  60. ss << this->StorageNodeIDs[n];
  61. if (n < StorageNodeIDs.size()-1)
  62. {
  63. ss << " ";
  64. }
  65. }
  66. if (this->StorageNodeIDs.size() > 0)
  67. {
  68. of << indent << " storageNodeRef=\"" << ss.str().c_str() << "\"";
  69. }
  70. //---write any user tags.
  71. if ( this->GetUserTagTable() != NULL )
  72. {
  73. ss.clear();
  74. ss.str ( "" );
  75. int numc = this->GetUserTagTable()->GetNumberOfTags();
  76. const char *kwd, *val;
  77. for (int i=0; i < numc; i++ )
  78. {
  79. kwd = this->GetUserTagTable()->GetTagAttribute(i);
  80. val = this->GetUserTagTable()->GetTagValue (i);
  81. if (kwd != NULL && val != NULL)
  82. {
  83. ss << kwd << "=" << val;
  84. if ( i < (numc-1) )
  85. {
  86. ss << " ";
  87. }
  88. }
  89. }
  90. if ( ss.str().c_str()!= NULL )
  91. {
  92. of << indent << " userTags=\"" << ss.str().c_str() << "\"";
  93. }
  94. }
  95. }
  96. //----------------------------------------------------------------------------
  97. void vtkMRMLStorableNode::ReadXMLAttributes(const char** atts)
  98. {
  99. int disabledModify = this->StartModify();
  100. Superclass::ReadXMLAttributes(atts);
  101. const char* attName;
  102. const char* attValue;
  103. while (*atts != NULL)
  104. {
  105. attName = *(atts++);
  106. attValue = *(atts++);
  107. if (!strcmp(attName, "storageNodeRef"))
  108. {
  109. std::stringstream ss(attValue);
  110. while (!ss.eof())
  111. {
  112. std::string id;
  113. ss >> id;
  114. this->AddStorageNodeID(id.c_str());
  115. }
  116. }
  117. //---Read any user tags
  118. else if (!strcmp (attName, "userTags"))
  119. {
  120. if ( this->GetUserTagTable() == NULL )
  121. {
  122. this->UserTagTable = vtkTagTable::New();
  123. }
  124. std::stringstream ss(attValue);
  125. std::string kwd = "";
  126. std::string val = "";
  127. std::string::size_type i;
  128. while (!ss.eof())
  129. {
  130. std::string tags;
  131. ss >> tags;
  132. //--- now pull apart individual tags
  133. if ( tags.c_str() != NULL )
  134. {
  135. i = tags.find("=");
  136. if ( i != std::string::npos)
  137. {
  138. kwd = tags.substr(0, i);
  139. val = tags.substr(i+1, std::string::npos );
  140. if ( kwd.c_str() != NULL && val.c_str() != NULL )
  141. {
  142. this->GetUserTagTable()->AddOrUpdateTag ( kwd.c_str(), val.c_str(), 0 );
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. this->EndModify(disabledModify);
  150. }
  151. //----------------------------------------------------------------------------
  152. void vtkMRMLStorableNode::UpdateReferenceID(const char *oldID, const char *newID)
  153. {
  154. Superclass::UpdateReferenceID(oldID, newID);
  155. for (unsigned int i=0; i<this->StorageNodeIDs.size(); i++)
  156. {
  157. if ( std::string(oldID) == this->StorageNodeIDs[i])
  158. {
  159. this->SetNthStorageNodeID(i, newID);
  160. }
  161. }
  162. }
  163. //----------------------------------------------------------------------------
  164. // Copy the node's attributes to this object.
  165. // Does NOT copy: ID, FilePrefix, Name, ID
  166. void vtkMRMLStorableNode::Copy(vtkMRMLNode *anode)
  167. {
  168. int disabledModify = this->StartModify();
  169. Superclass::Copy(anode);
  170. vtkMRMLStorableNode *node = (vtkMRMLStorableNode *) anode;
  171. if (!node)
  172. {
  173. return;
  174. }
  175. //---
  176. //--- Copy any user tags
  177. //---
  178. if ( node->GetUserTagTable() != NULL )
  179. {
  180. //--- make sure the destination node has a TagTable.
  181. if ( this->GetUserTagTable() == NULL )
  182. {
  183. this->UserTagTable = vtkTagTable::New();
  184. }
  185. //--- copy.
  186. int numc = node->GetUserTagTable()->GetNumberOfTags();
  187. const char *kwd, *val;
  188. int sel;
  189. for ( int j=0; j < numc; j++ )
  190. {
  191. kwd = node->GetUserTagTable()->GetTagAttribute(j);
  192. val = node->GetUserTagTable()->GetTagValue (j);
  193. sel = node->GetUserTagTable()->IsTagSelected ( kwd );
  194. if (kwd != NULL && val != NULL && sel >= 0 )
  195. {
  196. this->UserTagTable->AddOrUpdateTag ( kwd, val, sel );
  197. }
  198. }
  199. }
  200. this->SetAndObserveStorageNodeID(NULL);
  201. int ndnodes = node->GetNumberOfStorageNodes();
  202. for (int i=0; i<ndnodes; i++)
  203. {
  204. this->SetAndObserveNthStorageNodeID(i, node->StorageNodeIDs[i].c_str());
  205. }
  206. this->EndModify(disabledModify);
  207. }
  208. //----------------------------------------------------------------------------
  209. void vtkMRMLStorableNode::PrintSelf(ostream& os, vtkIndent indent)
  210. {
  211. Superclass::PrintSelf(os,indent);
  212. this->UserTagTable->PrintSelf(os, indent);
  213. for (unsigned int i=0; i<this->StorageNodeIDs.size(); i++)
  214. {
  215. os << indent << "StorageNodeIDs[" << i << "]: " <<
  216. this->StorageNodeIDs[i] << "\n";
  217. }
  218. }
  219. //-----------------------------------------------------------
  220. void vtkMRMLStorableNode::UpdateScene(vtkMRMLScene *scene)
  221. {
  222. Superclass::UpdateScene(scene);
  223. if (!this->AddToScene)
  224. {
  225. return;
  226. }
  227. vtkDebugMacro("UpdateScene: going through the storage node ids: " << this->StorageNodeIDs.size());
  228. for (unsigned int i=0; i < this->StorageNodeIDs.size(); i++)
  229. {
  230. vtkDebugMacro("UpdateScene: getting storage node at i = " << i);
  231. vtkMRMLStorageNode *pnode = this->GetNthStorageNode(i);
  232. std::string fname = std::string("(null)");
  233. if (pnode)
  234. {
  235. if (pnode->GetFileName() != NULL)
  236. {
  237. fname = std::string(pnode->GetFileName());
  238. }
  239. else if (pnode->GetURI() != NULL)
  240. {
  241. fname = std::string(pnode->GetURI());
  242. }
  243. vtkDebugMacro("UpdateScene: calling ReadData, fname = " << fname.c_str());
  244. if (pnode->ReadData(this) == 0)
  245. {
  246. scene->SetErrorCode(1);
  247. std::string msg = std::string("Error reading file ") + fname;
  248. scene->SetErrorMessage(msg);
  249. }
  250. else
  251. {
  252. vtkDebugMacro("UpdateScene: read data called and succeeded reading " << fname.c_str());
  253. }
  254. }
  255. else
  256. {
  257. vtkErrorMacro("UpdateScene: error getting " << i << "th storage node, id = " << (this->GetNthStorageNodeID(i) == NULL ? "null" : this->GetNthStorageNodeID(i)));
  258. }
  259. }
  260. }
  261. //-----------------------------------------------------------
  262. void vtkMRMLStorableNode::UpdateReferences()
  263. {
  264. Superclass::UpdateReferences();
  265. for (unsigned int i=0; i<this->StorageNodeIDs.size(); i++)
  266. {
  267. if (this->Scene->GetNodeByID(this->StorageNodeIDs[i]) == NULL)
  268. {
  269. this->SetAndObserveNthStorageNodeID(i, NULL);
  270. }
  271. }
  272. }
  273. //----------------------------------------------------------------------------
  274. vtkMRMLStorageNode* vtkMRMLStorableNode::GetNthStorageNode(int n)
  275. {
  276. vtkMRMLStorageNode* node = NULL;
  277. if (this->GetScene() && this->GetNthStorageNodeID(n) )
  278. {
  279. vtkMRMLNode* snode = this->GetScene()->GetNodeByID(this->GetNthStorageNodeID(n));
  280. node = vtkMRMLStorageNode::SafeDownCast(snode);
  281. }
  282. return node;
  283. }
  284. //----------------------------------------------------------------------------
  285. void vtkMRMLStorableNode::SetStorageNodeID(const char *storageNodeID)
  286. {
  287. if (this->StorageNodeIDs.empty() && storageNodeID == NULL)
  288. {
  289. return;
  290. }
  291. if (this->StorageNodeIDs.size() == 1 && storageNodeID != NULL && this->StorageNodeIDs[0] == std::string(storageNodeID) )
  292. {
  293. return;
  294. }
  295. this->StorageNodeIDs.clear();
  296. if (storageNodeID != NULL)
  297. {
  298. this->StorageNodeIDs.push_back(std::string(storageNodeID));
  299. }
  300. if (storageNodeID)
  301. {
  302. this->Scene->AddReferencedNodeID(storageNodeID, this);
  303. }
  304. }
  305. //----------------------------------------------------------------------------
  306. void vtkMRMLStorableNode::SetNthStorageNodeID(int n, const char *storageNodeID)
  307. {
  308. if (this->StorageNodeIDs.empty() && storageNodeID == NULL)
  309. {
  310. return;
  311. }
  312. if ((int)this->StorageNodeIDs.size() <= n)
  313. {
  314. return;
  315. }
  316. if (storageNodeID != NULL && this->StorageNodeIDs[n] == std::string(storageNodeID) )
  317. {
  318. return;
  319. }
  320. if (storageNodeID != NULL)
  321. {
  322. this->StorageNodeIDs[n] = std::string(storageNodeID);
  323. }
  324. if (storageNodeID)
  325. {
  326. this->Scene->AddReferencedNodeID(storageNodeID, this);
  327. }
  328. }
  329. //----------------------------------------------------------------------------
  330. void vtkMRMLStorableNode::AddStorageNodeID(const char *storageNodeID)
  331. {
  332. if (storageNodeID == NULL)
  333. {
  334. return;
  335. }
  336. this->StorageNodeIDs.push_back(std::string(storageNodeID));
  337. this->Scene->AddReferencedNodeID(storageNodeID, this);
  338. }
  339. //----------------------------------------------------------------------------
  340. void vtkMRMLStorableNode::SetAndObserveStorageNodeID(const char *storageNodeID)
  341. {
  342. for (unsigned int i=0; i<this->StorageNodes.size(); i++)
  343. {
  344. if (this->StorageNodes[i])
  345. {
  346. vtkSetAndObserveMRMLObjectMacro(this->StorageNodes[i], NULL);
  347. //vtkSetMRMLObjectMacro(this->StorageNodes[i], NULL);
  348. }
  349. }
  350. this->StorageNodes.clear();
  351. this->SetStorageNodeID(storageNodeID);
  352. vtkMRMLStorageNode *dnode = this->GetStorageNode();
  353. this->AddAndObserveStorageNode(dnode);
  354. this->Modified();
  355. }
  356. //----------------------------------------------------------------------------
  357. void vtkMRMLStorableNode::SetAndObserveNthStorageNodeID(int n, const char *storageNodeID)
  358. {
  359. if (n >= (int)this->StorageNodes.size())
  360. {
  361. this->AddAndObserveStorageNodeID(storageNodeID);
  362. return;
  363. }
  364. vtkSetAndObserveMRMLObjectMacro(this->StorageNodes[n], NULL);
  365. //vtkSetMRMLObjectMacro(this->StorageNodes[n], NULL);
  366. this->SetNthStorageNodeID(n, storageNodeID);
  367. vtkMRMLStorageNode *dnode = this->GetNthStorageNode(n);
  368. if (dnode)
  369. {
  370. vtkSetAndObserveMRMLObjectMacro(this->StorageNodes[n], dnode);
  371. //vtkSetMRMLObjectMacro(this->StorageNodes[n], dnode);
  372. }
  373. this->Modified();
  374. }
  375. //----------------------------------------------------------------------------
  376. void vtkMRMLStorableNode::AddAndObserveStorageNodeID(const char *storageNodeID)
  377. {
  378. this->AddStorageNodeID(storageNodeID);
  379. vtkMRMLStorageNode *dnode = vtkMRMLStorageNode::SafeDownCast(this->GetScene()->GetNodeByID(storageNodeID));
  380. this->AddAndObserveStorageNode(dnode);
  381. this->Modified();
  382. }
  383. //----------------------------------------------------------------------------
  384. void vtkMRMLStorableNode::AddAndObserveStorageNode(vtkMRMLStorageNode *dnode)
  385. {
  386. if (dnode)
  387. {
  388. vtkMRMLStorageNode *pnode = vtkMRMLStorageNode::New();
  389. vtkSetAndObserveMRMLObjectMacro(pnode, dnode);
  390. //vtkSetMRMLObjectMacro(pnode, dnode);
  391. this->StorageNodes.push_back(pnode);
  392. //pnode->Delete();
  393. }
  394. }
  395. /*
  396. //----------------------------------------------------------------------------
  397. vtkMRMLStorageNode* vtkMRMLStorableNode::GetStorageNode()
  398. {
  399. vtkMRMLStorageNode* node = NULL;
  400. if (this->GetScene() && this->GetStorageNodeID() )
  401. {
  402. vtkMRMLNode* snode = this->GetScene()->GetNodeByID(this->StorageNodeID);
  403. node = vtkMRMLStorageNode::SafeDownCast(snode);
  404. }
  405. return node;
  406. }
  407. */
  408. //---------------------------------------------------------------------------
  409. void vtkMRMLStorableNode::ProcessMRMLEvents ( vtkObject *caller,
  410. unsigned long event,
  411. void *callData )
  412. {
  413. Superclass::ProcessMRMLEvents(caller, event, callData);
  414. for (unsigned int i=0; i<this->StorageNodes.size(); i++)
  415. {
  416. vtkMRMLStorageNode *dnode = this->GetNthStorageNode(i);
  417. if (dnode != NULL && dnode == vtkMRMLStorageNode::SafeDownCast(caller) &&
  418. event == vtkCommand::ModifiedEvent)
  419. {
  420. vtkDebugMacro("Got a modified event on a storage node, id = " << dnode->GetID());
  421. // read?
  422. }
  423. }
  424. return;
  425. }