Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxFrameForest.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxFrameForest.h"
13 
14 #include "cxData.h"
15 
16 namespace cx
17 {
18 
22 FrameForest::FrameForest(const std::map<QString, DataPtr> &source) : mSource(source)
23 {
24 // std::cout << "Forrest doc2:" << std::endl;
25 // DataManager::DataMap allData = mDataManager->getData();
26  mDocument.appendChild(mDocument.createElement("root"));
27 
28  for (std::map<QString, DataPtr>::const_iterator iter = source.begin(); iter != source.end(); ++iter)
29  {
30  this->insertFrame(iter->second);
31  }
32 
33 // std::cout << "Forrest doc:" << std::endl;
34 // std::cout << mDocument.toString(4) << std::endl;
35 }
36 
38 {
39  return mDocument;
40 }
41 
44 void FrameForest::insertFrame(DataPtr data)
45 {
46  QString parentFrame = data->getParentSpace();
47  QString currentFrame = data->getSpace();
48 
49 // if (parentFrame.isEmpty() || currentFrame.isEmpty())
50 // return;
51 
52  QDomNode parentNode = this->getNodeAnyway(parentFrame);
53  QDomNode currentNode = this->getNodeAnyway(currentFrame);
54 
55  if (parentNode.isNull())
56  return;
57 
58  // move currentNode to child of parentNode
59  currentNode = currentNode.parentNode().removeChild(currentNode);
60  parentNode.appendChild(currentNode);
61 }
62 
66 QDomNode FrameForest::getNode(QString frame)
67 {
68  QDomNodeList list = mDocument.elementsByTagName(frame);
69  if (list.isEmpty())
70  return QDomNode();
71  return list.item(0);
72 }
73 
76 QDomNode FrameForest::getNodeAnyway(QString frame)
77 {
78  QDomNode retval = this->getNode(frame);
79  if (retval.isNull())
80  {
81  retval = mDocument.createElement(frame);
82  mDocument.documentElement().appendChild(retval);
83  }
84  return retval;
85 }
86 
89 bool FrameForest::isAncestorOf(QDomNode node, QDomNode ancestor)
90 {
91  //std::cout << "isAncestorOf : " << node.toElement().tagName() << "---" << ancestor.toElement().tagName() << std::endl;
92 
93  while (!this->isRootNode(node))
94  {
95  if (node == ancestor)
96  {
97  //std::cout << "return true" << std::endl;;
98  return true;
99  }
100  node = node.parentNode();
101  }
102  //std::cout << "return false" << std::endl;;
103  return false;
104 }
105 
106 bool FrameForest::isRootNode(QDomNode node)
107 {
108  return node == mDocument.documentElement();
109 }
110 
113 QDomNode FrameForest::getOldestAncestor(QDomNode node)
114 {
115  if (this->isRootNode(node))
116  return node;
117  while (!this->isRootNode(node.parentNode()))
118  node = node.parentNode();
119  return node;
120 }
121 
124 QDomNode FrameForest::getOldestAncestorNotCommonToRef(QDomNode node, QDomNode ref)
125 {
126  //std::cout << "getOldestAncestorNotCommonToRef " << node.toElement().tagName() << " - " << ref.toElement().tagName() << std::endl;
127  if (this->isAncestorOf(ref, node))
128  return QDomNode();
129 
130  while (!this->isRootNode(node.parentNode()))
131  {
132  //std::cout << "getOldestAncestorNotCommonToRef iterate start: " << node.toElement().tagName() << std::endl;
133  if (this->isAncestorOf(ref, node.parentNode()))
134  break;
135  node = node.parentNode();
136  //std::cout << "getOldestAncestorNotCommonToRef iterate stop: " << node.toElement().tagName() << std::endl;
137  }
138  //std::cout << std::endl;
139  return node;
140 }
141 
144 std::vector<QDomNode> FrameForest::getDescendantsAndSelf(QDomNode node)
145 {
146  std::vector<QDomNode> retval;
147  retval.push_back(node);
148 
149  for (QDomNode child = node.firstChild(); !child.isNull(); child = child.nextSibling())
150  {
151  std::vector<QDomNode> subnodes = this->getDescendantsAndSelf(child);
152  std::copy(subnodes.begin(), subnodes.end(), back_inserter(retval));
153  }
154  return retval;
155 }
156 
160 std::vector<DataPtr> FrameForest::getDataFromDescendantsAndSelf(QDomNode node)
161 {
162  std::vector<QDomNode> nodes = this->getDescendantsAndSelf(node);
163  std::vector<DataPtr> retval;
164 
165  for (unsigned i = 0; i < nodes.size(); ++i)
166  {
167  DataPtr data = mSource[nodes[i].toElement().tagName()];
168 // DataPtr data = mDataManager->getData(nodes[i].toElement().tagName());
169  if (data)
170  retval.push_back(data);
171  }
172  return retval;
173 }
174 
175 }
QDomNode getOldestAncestor(QDomNode node)
std::vector< DataPtr > getDataFromDescendantsAndSelf(QDomNode node)
boost::shared_ptr< class Data > DataPtr
std::vector< QDomNode > getDescendantsAndSelf(QDomNode node)
QDomNode getNode(QString frame)
FrameForest(const std::map< QString, DataPtr > &source)
QDomDocument getDocument()
QDomNode getOldestAncestorNotCommonToRef(QDomNode child, QDomNode ref)
Namespace for all CustusX production code.