• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/RocsCore

  • sources
  • kde-4.12
  • kdeedu
  • rocs
  • RocsCore
  • DataStructures
  • RootedTree
RootedTreeNode.cpp
Go to the documentation of this file.
1 /*
2  This file is part of RootedTree (Rocs Plugin).
3  Copyright 2012 Wagner Reck <wagner.reck@gmail.com>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License as
7  published by the Free Software Foundation; either version 2 of
8  the License, or (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "RootedTreeNode.h"
20 #include "DataStructure.h"
21 #include "Pointer.h"
22 
23 #include <KDebug>
24 #include "RootedTreeStructure.h"
25 
26 DataPtr RootedTreeNode::create(DataStructurePtr parent, int uniqueIdentifier, int dataType)
27 {
28  return Data::create<RootedTreeNode>(parent, uniqueIdentifier, dataType);
29 }
30 
31 RootedTreeNode::RootedTreeNode(DataStructurePtr parent, int uniqueIdentifier, int dataType)
32  : Data(parent, uniqueIdentifier, dataType)
33  , m_nChilds(-1)
34 {
35 }
36 
37 
38 RootedTreeNode::~RootedTreeNode()
39 {
40 }
41 
42 quint32 RootedTreeNode::numberOfChilds() const
43 {
44  if (m_nChilds != -1) {
45  return m_nChilds;
46  }
47  if (dataStructure()->property("ChildCount").isValid()) {
48  return dataStructure()->property("ChildCount").toUInt();
49  }
50  return 2;
51 }
52 
53 void RootedTreeNode::setNumberOfChilds(const qint32 number)
54 {
55  if (number != m_nChilds){
56  for (qint32 i = number; i < m_nChilds; ++i) {
57  setChild(dataStructure()->createData("", 0), i);
58  }
59  m_nChilds = number;
60  }
61 }
62 
63 
64 DataList RootedTreeNode::children() const
65 {
66  DataList list;
67  foreach (PointerPtr ptr, outPointerList())
68  if (ptr->property("TreeEdge").isValid() && ptr->property("TreeEdge") != -1) {
69  list << ptr->to();
70  }
71  return list;
72 }
73 
74 
75 PointerPtr RootedTreeNode::addRigthChild(DataPtr child) const
76 {
77  return setChild(child, numberOfChilds()-1);
78 }
79 
80 DataPtr RootedTreeNode::rightChild() const
81 {
82  return child(numberOfChilds()-1);
83 }
84 
85 
86 PointerPtr RootedTreeNode::addLeftChild(DataPtr child) const
87 {
88  return setChild(child, 0);
89 }
90 
91 
92 DataPtr RootedTreeNode::leftChild() const
93 {
94  return child(0);
95 }
96 
97 
98 PointerPtr RootedTreeNode::setNodeParent(DataPtr parent) const
99 {
100  foreach (PointerPtr p, outPointerList()) {
101  if (p->property("TreeEdge").isValid() && p->property("TreeEdge").toInt() == -1){
102  p->remove();
103  }
104  }
105  if (parent.get()){
106  PointerPtr ptr = dataStructure()->createPointer(this->getData(), parent, 0);
107  ptr->setProperty("TreeEdge", -1);
108  return ptr;
109  }
110  return PointerPtr();
111 }
112 
113 
114 DataPtr RootedTreeNode::nodeParent() const
115 {
116  foreach (PointerPtr ptr, outPointerList()) {
117  if (ptr->property("TreeEdge").isValid() && ptr->property("TreeEdge").toInt() < 0) {
118  return ptr->to();
119  }
120  }
121  return DataPtr();
122 }
123 
124 
125 DataPtr RootedTreeNode::child(const quint32 i) const
126 {
127  if (i >= numberOfChilds()) {
128  return DataPtr();
129  }
130  foreach (PointerPtr ptr, outPointerList()) {
131  if (ptr->property("TreeEdge").isValid() && ptr->property("TreeEdge").toUInt() == i) {
132  return ptr->to();
133  }
134  }
135  return DataPtr();
136 }
137 
138 
139 PointerPtr RootedTreeNode::setChild(DataPtr c, quint32 idx) const{
140 
141  if (idx < numberOfChilds()){
142  foreach (PointerPtr p, pointerList(child(idx))) {
143  p->remove();
144  }
145 // child(idx)->remove();
146  if (c && c.get()){
147  PointerPtr ptr = getData()->createPointer(c);
148  ptr->setProperty("TreeEdge", idx);
149  return ptr;
150  }
151  }
152  return PointerPtr();
153 }
154 
155 
156 QScriptValue RootedTreeNode::add_child(RootedTreeNode* childNode, quint32 idx) const
157 {
158  if (idx < numberOfChilds() && childNode){
159  PointerPtr pointer = setChild(childNode->getData(), idx);
160  if (!pointer) {
161  return pointer->scriptValue();
162  }
163  }
164  setChild(DataPtr(), idx);
165  return QScriptValue();
166 }
167 
168 
169 QScriptValue RootedTreeNode::add_left_child(RootedTreeNode* child) const
170 {
171  if (!child){
172  addLeftChild(DataPtr());
173  } else {
174  PointerPtr pointer = addLeftChild(child->getData());
175  if (!pointer) {
176  return pointer->scriptValue();
177  }
178  }
179  return QScriptValue();
180 }
181 
182 
183 QScriptValue RootedTreeNode::add_right_child(RootedTreeNode* child) const
184 {
185  if (!child){
186  addRigthChild(DataPtr());
187  } else {
188  PointerPtr pointer = addRigthChild(child->getData());
189  if (!pointer) {
190  return pointer->scriptValue();
191  }
192  }
193  return QScriptValue();
194 }
195 
196 
197 QScriptValue RootedTreeNode::add_node_parent(RootedTreeNode* parentNode) const
198 {
199  if (!parentNode){
200  setNodeParent(DataPtr());
201  } else {
202  PointerPtr pointer = setNodeParent(parentNode->getData());
203  if (!pointer) {
204  return pointer->scriptValue();
205  }
206  }
207  return QScriptValue();
208 }
209 
210 
211 QScriptValue RootedTreeNode::child_at(quint32 idx) const
212 {
213  DataPtr data = child(idx);
214  if (!data) {
215  return data->scriptValue();
216  }
217  return QScriptValue();
218 }
219 
220 
221 QScriptValue RootedTreeNode::left_child() const
222 {
223  DataPtr data = leftChild();
224  if (data) {
225  return data->scriptValue();
226  }
227  return QScriptValue();
228 }
229 
230 
231 QScriptValue RootedTreeNode::right_child() const
232 {
233  DataPtr data = rightChild();
234  if (data) {
235  return data->scriptValue();
236  }
237  return QScriptValue();
238 }
239 
240 
241 QScriptValue RootedTreeNode::node_parent() const
242 {
243  DataPtr data = nodeParent();
244  if (data) {
245  return data->scriptValue();
246  }
247  return QScriptValue();
248 }
249 
250 
251 QScriptValue RootedTreeNode::children_list() const
252 {
253  QScriptValue value = dataStructure()->engine()->newArray();
254  foreach (const DataPtr &child, children()) {
255  value.property("push").call(value, QScriptValueList() << child->scriptValue());
256  }
257  return value;
258 }
259 
260 
261 void RootedTreeNode::adjustPosition()
262 {
263  DataPtr parent = nodeParent();
264  const QRectF size = dataStructure()->document()->sceneRect();
265  if (parent){
266  qreal adjust = 0.0;
267  foreach (PointerPtr p, nodeParent()->pointerList(this->getData())){
268  if (p->property("TreeEdge").toInt() >= 0){
269  adjust = p->property("TreeEdge").toReal()/(numberOfChilds() - 1);
270  break;
271  }
272  }
273  const qreal vSize = dataStructure()->property("NodeSize").toReal();
274  const qreal hSize = vSize * qPow(2, height()) * 0.5;
275  qreal posX = parent->x() - hSize * numberOfChilds()/2.0 +
276  hSize * numberOfChilds() * adjust;
277  qreal posY = parent->y() + vSize;
278  setPos(posX, posY);
279  } else if (qobject_cast<RootedTreeStructure*>(dataStructure().get())->rootNode().get() == this) {
280  setPos(size.center().x(), size.top() + 100);
281  }
282 }
283 
284 
285 qint8 RootedTreeNode::height() const
286 {
287  qint8 l = 0, r = 0;
288  if (leftChild().get())
289  l = 1 + qobject_cast<RootedTreeNode*>(leftChild().get())->height();
290  if (rightChild().get())
291  r = 1 + qobject_cast<RootedTreeNode*>(rightChild().get())->height();
292  return l < r?
293  r:
294  l;
295 }
296 
RootedTreeNode::RootedTreeNode
RootedTreeNode(DataStructurePtr parent, int uniqueIdentifier, int dataType)
Definition: RootedTreeNode.cpp:31
RootedTreeNode::create
static DataPtr create(DataStructurePtr parent, int uniqueIdentifier, int dataType)
Definition: RootedTreeNode.cpp:26
RootedTreeNode::height
qint8 height() const
Definition: RootedTreeNode.cpp:285
RootedTreeNode::~RootedTreeNode
~RootedTreeNode()
Definition: RootedTreeNode.cpp:38
RootedTreeStructure.h
RootedTreeNode::nodeParent
DataPtr nodeParent() const
RootedTreeNode::m_nChilds
qint32 m_nChilds
Definition: RootedTreeNode.h:34
RootedTreeNode::add_left_child
QScriptValue add_left_child(RootedTreeNode *child) const
Definition: RootedTreeNode.cpp:169
Data::dataStructure
DataStructurePtr dataStructure() const
Definition: Data.cpp:150
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
DotParser::createData
void createData(const std::string &str)
Definition: DotGrammar.cpp:372
DataList
QList< boost::shared_ptr< Data > > DataList
Definition: CoreTypes.h:30
Data::dataType
int dataType() const
Definition: Data.cpp:387
RootedTreeNode::add_node_parent
QScriptValue add_node_parent(RootedTreeNode *child) const
Definition: RootedTreeNode.cpp:197
RootedTreeNode::addLeftChild
PointerPtr addLeftChild(DataPtr child) const
Definition: RootedTreeNode.cpp:86
RootedTreeNode::add_child
QScriptValue add_child(RootedTreeNode *child, quint32 idx) const
Definition: RootedTreeNode.cpp:156
RootedTreeNode::setNodeParent
PointerPtr setNodeParent(DataPtr parent) const
Definition: RootedTreeNode.cpp:98
PointerPtr
boost::shared_ptr< Pointer > PointerPtr
Definition: CoreTypes.h:35
DataStructure.h
RootedTreeNode::adjustPosition
void adjustPosition()
Definition: RootedTreeNode.cpp:261
RootedTreeNode::addRigthChild
PointerPtr addRigthChild(DataPtr child) const
Definition: RootedTreeNode.cpp:75
RootedTreeNode::child_at
QScriptValue child_at(quint32 idx) const
Definition: RootedTreeNode.cpp:211
RootedTreeNode::right_child
QScriptValue right_child() const
Definition: RootedTreeNode.cpp:231
RootedTreeNode::children_list
QScriptValue children_list() const
return the list of valid children
Definition: RootedTreeNode.cpp:251
RootedTreeNode::leftChild
DataPtr leftChild() const
Data::getData
virtual DataPtr getData() const
Definition: Data.cpp:125
RootedTreeNode::numberOfChilds
quint32 numberOfChilds() const
Data::outPointerList
PointerList & outPointerList() const
Definition: Data.cpp:377
Data::setPos
void setPos(qreal x, qreal y)
Definition: Data.cpp:416
RootedTreeNode::node_parent
QScriptValue node_parent() const
Definition: RootedTreeNode.cpp:241
Pointer.h
DataPtr
boost::shared_ptr< Data > DataPtr
Definition: CoreTypes.h:34
RootedTreeNode::left_child
QScriptValue left_child() const
Definition: RootedTreeNode.cpp:221
Data
Definition: Data.h:40
RootedTreeNode.h
RootedTreeNode::setNumberOfChilds
void setNumberOfChilds(const qint32 number)
Definition: RootedTreeNode.cpp:53
RootedTreeNode::rightChild
DataPtr rightChild() const
RootedTreeNode::children
DataList children() const
Definition: RootedTreeNode.cpp:64
RootedTreeNode::setChild
PointerPtr setChild(DataPtr child, quint32 idx) const
Definition: RootedTreeNode.cpp:139
RootedTreeNode
Definition: RootedTreeNode.h:25
RootedTreeNode::child
DataPtr child(const quint32 i) const
return the i (0 to n-1) child
Definition: RootedTreeNode.cpp:125
Data::pointerList
PointerList pointerList() const
Definition: Data.cpp:218
RootedTreeNode::add_right_child
QScriptValue add_right_child(RootedTreeNode *child) const
Definition: RootedTreeNode.cpp:183
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/RocsCore

Skip menu "rocs/RocsCore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal