Fraxinus  17.12-rc2
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxFrameForest.h"
34 
35 #include "cxData.h"
36 
37 namespace cx
38 {
39 
43 FrameForest::FrameForest(const std::map<QString, DataPtr> &source) : mSource(source)
44 {
45 // std::cout << "Forrest doc2:" << std::endl;
46 // DataManager::DataMap allData = mDataManager->getData();
47  mDocument.appendChild(mDocument.createElement("root"));
48 
49  for (std::map<QString, DataPtr>::const_iterator iter = source.begin(); iter != source.end(); ++iter)
50  {
51  this->insertFrame(iter->second);
52  }
53 
54 // std::cout << "Forrest doc:" << std::endl;
55 // std::cout << mDocument.toString(4) << std::endl;
56 }
57 
59 {
60  return mDocument;
61 }
62 
65 void FrameForest::insertFrame(DataPtr data)
66 {
67  QString parentFrame = data->getParentSpace();
68  QString currentFrame = data->getSpace();
69 
70 // if (parentFrame.isEmpty() || currentFrame.isEmpty())
71 // return;
72 
73  QDomNode parentNode = this->getNodeAnyway(parentFrame);
74  QDomNode currentNode = this->getNodeAnyway(currentFrame);
75 
76  if (parentNode.isNull())
77  return;
78 
79  // move currentNode to child of parentNode
80  currentNode = currentNode.parentNode().removeChild(currentNode);
81  parentNode.appendChild(currentNode);
82 }
83 
87 QDomNode FrameForest::getNode(QString frame)
88 {
89  QDomNodeList list = mDocument.elementsByTagName(frame);
90  if (list.isEmpty())
91  return QDomNode();
92  return list.item(0);
93 }
94 
97 QDomNode FrameForest::getNodeAnyway(QString frame)
98 {
99  QDomNode retval = this->getNode(frame);
100  if (retval.isNull())
101  {
102  retval = mDocument.createElement(frame);
103  mDocument.documentElement().appendChild(retval);
104  }
105  return retval;
106 }
107 
110 bool FrameForest::isAncestorOf(QDomNode node, QDomNode ancestor)
111 {
112  //std::cout << "isAncestorOf : " << node.toElement().tagName() << "---" << ancestor.toElement().tagName() << std::endl;
113 
114  while (!this->isRootNode(node))
115  {
116  if (node == ancestor)
117  {
118  //std::cout << "return true" << std::endl;;
119  return true;
120  }
121  node = node.parentNode();
122  }
123  //std::cout << "return false" << std::endl;;
124  return false;
125 }
126 
127 bool FrameForest::isRootNode(QDomNode node)
128 {
129  return node == mDocument.documentElement();
130 }
131 
134 QDomNode FrameForest::getOldestAncestor(QDomNode node)
135 {
136  if (this->isRootNode(node))
137  return node;
138  while (!this->isRootNode(node.parentNode()))
139  node = node.parentNode();
140  return node;
141 }
142 
145 QDomNode FrameForest::getOldestAncestorNotCommonToRef(QDomNode node, QDomNode ref)
146 {
147  //std::cout << "getOldestAncestorNotCommonToRef " << node.toElement().tagName() << " - " << ref.toElement().tagName() << std::endl;
148  if (this->isAncestorOf(ref, node))
149  return QDomNode();
150 
151  while (!this->isRootNode(node.parentNode()))
152  {
153  //std::cout << "getOldestAncestorNotCommonToRef iterate start: " << node.toElement().tagName() << std::endl;
154  if (this->isAncestorOf(ref, node.parentNode()))
155  break;
156  node = node.parentNode();
157  //std::cout << "getOldestAncestorNotCommonToRef iterate stop: " << node.toElement().tagName() << std::endl;
158  }
159  //std::cout << std::endl;
160  return node;
161 }
162 
165 std::vector<QDomNode> FrameForest::getDescendantsAndSelf(QDomNode node)
166 {
167  std::vector<QDomNode> retval;
168  retval.push_back(node);
169 
170  for (QDomNode child = node.firstChild(); !child.isNull(); child = child.nextSibling())
171  {
172  std::vector<QDomNode> subnodes = this->getDescendantsAndSelf(child);
173  std::copy(subnodes.begin(), subnodes.end(), back_inserter(retval));
174  }
175  return retval;
176 }
177 
181 std::vector<DataPtr> FrameForest::getDataFromDescendantsAndSelf(QDomNode node)
182 {
183  std::vector<QDomNode> nodes = this->getDescendantsAndSelf(node);
184  std::vector<DataPtr> retval;
185 
186  for (unsigned i = 0; i < nodes.size(); ++i)
187  {
188  DataPtr data = mSource[nodes[i].toElement().tagName()];
189 // DataPtr data = mDataManager->getData(nodes[i].toElement().tagName());
190  if (data)
191  retval.push_back(data);
192  }
193  return retval;
194 }
195 
196 }
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.