• 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
RootedTreeStructure.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 
20 
21 #include "RootedTreeStructure.h"
22 #include "Data.h"
23 #include "Pointer.h"
24 #include "Document.h"
25 #include "RootedTreeNode.h"
26 
27 
28 #include <boost/shared_ptr.hpp>
29 
30 #include <QScriptEngine>
31 
32 #include <KMessageBox>
33 #include "KDebug"
34 
35 
36 QScriptValue rootedTreeNodeToScriptValue(QScriptEngine */*engine*/, RootedTreeNode* const &in)
37 { return in->scriptValue(); }
38 
39 void rootedTreeNodeFromScriptValue(const QScriptValue &object, RootedTreeNode* &out)
40 { out = qobject_cast<RootedTreeNode*>(object.toQObject()); }
41 
42 
43 namespace boost { void throw_exception( std::exception const & ) {} }
44 
45 DataStructurePtr RootedTreeStructure::create(Document *parent)
46 {
47  return DataStructure::create<RootedTreeStructure>(parent);
48 }
49 
50 
51 DataStructurePtr RootedTreeStructure::create(DataStructurePtr other, Document *parent)
52 {
53  boost::shared_ptr<RootedTreeStructure> ds = boost::static_pointer_cast<RootedTreeStructure>(create(parent));
54  ds->importStructure(other);
55  return ds;
56 }
57 
58 
59 RootedTreeStructure::RootedTreeStructure ( Document* parent ) :
60  DataStructure ( parent ), m_rootNode(0)
61 {
62  addDynamicProperty("NodeSize", 100);
63  addDynamicProperty("ChildCount", 2);
64  addDynamicProperty("PointersRegion", 0.3);
65  m_showPointers = false;
66 }
67 
68 
69 void RootedTreeStructure::importStructure(DataStructurePtr other)
70 {
71  QSet <Data*> visited; // used to track which data elements were already processed
72  QQueue<DataPtr> queue; // list of unprocessed data elements
73  QHash <Data*, DataPtr> fromOtherToNew; // mapping from old to new data elements
74 
75  //FIXME only default data type considered
76  foreach(DataPtr dataOther, other->dataList(0)){
77  if (visited.contains(dataOther.get())) {
78  continue;
79  }
80  queue.enqueue(dataOther);
81 
82  // add data element to tree and register mapping
83  DataPtr dataTree = createData(dataOther->property("name").toString(), 0);
84  dataTree->setColor(dataOther->color());
85  dataTree->setProperty("value", dataOther->property("value").toString());
86  dataTree->setX(dataOther->x());
87  dataTree->setY(dataOther->y());
88  dataTree->setWidth(dataOther->width());
89  fromOtherToNew.insert(dataOther.get(), dataTree);
90 
91  // if no tree root is present, set this data element as root
92  if (!rootNode()) {
93  set_root_node(qobject_cast<RootedTreeNode*>(dataTree.get()));
94  }
95 
96  // process all already met unprocessed data elements
97  while(!queue.isEmpty()) {
98  DataPtr n = queue.dequeue();
99  if (!visited.contains(n.get())) {
100  visited.insert(n.get());
101  }
102  int childCount = 0;
103  RootedTreeNode* newdataRootedNode = qobject_cast< RootedTreeNode* >(fromOtherToNew.value(n.get()).get());
104  newdataRootedNode->setNumberOfChilds(n->adjacentDataList().count()); //FIXME here it goes wrong!
105 
106  // iterate all neighbors and process them if not already elements in tree
107  foreach (DataPtr adjacentData, n->adjacentDataList()) {
108  if (!visited.contains(adjacentData.get())){
109  visited.insert(adjacentData.get());
110  queue.enqueue(adjacentData);
111  DataPtr childdata = createData(adjacentData->property("name").toString(), 0);
112  childdata->setColor(adjacentData->color());
113  childdata->setProperty("value", adjacentData->property("value").toString());
114  childdata->setX(adjacentData->x());
115  childdata->setY(adjacentData->y());
116  childdata->setWidth(adjacentData->width());
117  fromOtherToNew.insert(adjacentData.get(), childdata);
118 
119  //set the child at childCount position
120  newdataRootedNode->setChild(childdata, childCount++);
121  //Set the parent
122  RootedTreeNode* rooted = qobject_cast< RootedTreeNode* >(childdata.get());
123  rooted->setNodeParent(newdataRootedNode->getData());
124  }
125  }
126  // set the correct number of childrem (2 if there is no child)
127  newdataRootedNode->setNumberOfChilds(childCount == 0 ? 2:childCount);
128  }
129  }
130 }
131 
132 
133 RootedTreeStructure::~RootedTreeStructure()
134 {
135 }
136 
137 
138 QScriptValue RootedTreeStructure::add_data(const QString& name)
139 {
140  DataPtr n = createData(name, 0);
141  n->setEngine(engine());
142  return n->scriptValue();
143 }
144 
145 
147 PointerPtr RootedTreeStructure::createPointer(DataPtr from, DataPtr to, int dataType)
148 {
149  PointerPtr ptr = DataStructure::createPointer(from, to, dataType);
150  if (ptr && from->property("ClickPosition").isValid()){
151  QPointF pos = from->property("ClickPosition").toPointF();
152  const qreal pointersSize = property("PointersRegion").toReal();
153  const qreal size = property("NodeSize").toReal();
154  if (isShowingAllPointers()){
155  qint8 treeEdge = -1;
156  if (pos.y() > size * pointersSize ){
157  RootedTreeNode * fromNode = qobject_cast<RootedTreeNode*>(from.get());
158 
159  const qint16 childCount = fromNode->numberOfChilds();
160  const qreal division = ((childCount * pointersSize) > 1 ?
161  childCount * pointersSize * size:
162  size) / childCount;
163  treeEdge = static_cast<qint32> (pos.x()/division);
164  }
165  foreach (const PointerPtr &p, from->outPointerList()) {
166  if (p != ptr && p->property("TreeEdge").isValid() && p->property("TreeEdge").toInt() == treeEdge){
167  p->remove();
168  }
169  }
170  ptr->setProperty("TreeEdge", treeEdge);//Add as parent;
171  }else{
172  //Need a fix to this click position
173  to->setProperty("ClickPosition", QVariant());
174  from->setProperty("ClickPosition", QVariant());
175  //add the data as one of the children (first null child)
176  bool found = false;
177  RootedTreeNode * fromNode = qobject_cast<RootedTreeNode*>(from.get());
178  for (quint32 i = 0; i < fromNode->numberOfChilds(); ++i){
179  if (!fromNode->child(i)){
180  found = true;
181  ptr->setProperty("TreeEdge", i);
182  break;
183  }
184  }
185  //If from data is full of children, increase number of children
186  if (!found){
187  fromNode->setNumberOfChilds(fromNode->numberOfChilds() + 1);
188  ptr->setProperty("TreeEdge", fromNode->numberOfChilds()-1);
189  }
190 
191  // Set from as parent of to
192  RootedTreeNode * toNode = qobject_cast<RootedTreeNode*>(to.get());
193  toNode->setNodeParent(from);
194  }
195  }
196  return ptr;
197 }
198 
199 DataPtr RootedTreeStructure::createData(const QString& name, int dataType)
200 {
201  Q_ASSERT(document()->dataTypeList().contains(dataType));
202  boost::shared_ptr<RootedTreeNode> n = boost::static_pointer_cast<RootedTreeNode>(
203  RootedTreeNode::create(getDataStructure(), generateUniqueIdentifier(), dataType) );
204  n->setProperty("name", name);
205  addData(n);
206  return n;
207 }
208 
209 
210 QScriptValue RootedTreeStructure::root_node() const
211 {
212  if (m_rootNode) {
213  return m_rootNode->scriptValue();
214  }
215  return QScriptValue();
216 }
217 
218 
219 void RootedTreeStructure::set_root_node(RootedTreeNode* node)
220 {
221  m_rootNode = node;
222 }
223 
224 
225 DataPtr RootedTreeStructure::rootNode() const
226 {
227  if (m_rootNode) {
228  return m_rootNode->getData();
229  }
230  return DataPtr();
231 }
232 
233 void RootedTreeStructure::setEngine(QScriptEngine* engine)
234 {
235  DataStructure::setEngine(engine);
236  qScriptRegisterMetaType<RootedTreeNode*>(engine, rootedTreeNodeToScriptValue, rootedTreeNodeFromScriptValue);
237 }
238 
239 
240 bool RootedTreeStructure::isShowingAllPointers() const{
241  return m_showPointers;
242 }
243 
244 
245 void RootedTreeStructure::setShowAllPointers(const bool v)
246 {
247  if (v != m_showPointers){
248  qDebug() << "Changing!";
249  m_showPointers = v;
250  emit showPointersChanged(m_showPointers);
251  emit changed();
252  }
253 }
254 
DataStructure::engine
QScriptEngine * engine() const
Definition: DataStructure.cpp:565
RootedTreeNode::create
static DataPtr create(DataStructurePtr parent, int uniqueIdentifier, int dataType)
Definition: RootedTreeNode.cpp:26
RootedTreeStructure.h
DataStructure
Definition: DataStructure.h:43
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
RootedTreeStructure::importStructure
void importStructure(DataStructurePtr other)
overwrites the current DataStructure with all values (Data and Pointer) from the given datastructure ...
Definition: RootedTreeStructure.cpp:69
DataStructure::getDataStructure
virtual DataStructurePtr getDataStructure() const
Definition: DataStructure.cpp:103
RootedTreeStructure::RootedTreeStructure
RootedTreeStructure(Document *parent=0)
Definition: RootedTreeStructure.cpp:59
RootedTreeStructure::isShowingAllPointers
bool isShowingAllPointers() const
return true if all the pointers need to be draw.
Definition: RootedTreeStructure.cpp:240
rootedTreeNodeToScriptValue
QScriptValue rootedTreeNodeToScriptValue(QScriptEngine *, RootedTreeNode *const &in)
Definition: RootedTreeStructure.cpp:36
DataStructure::changed
void changed()
RootedTreeStructure::showPointersChanged
void showPointersChanged(const bool)
signal emitted on change of pointers visibility
Data::scriptValue
QScriptValue scriptValue() const
Definition: Data.cpp:471
RootedTreeStructure::setShowAllPointers
void setShowAllPointers(const bool v)
set if all the pointers should be draw or not.
Definition: RootedTreeStructure.cpp:245
Data.h
RootedTreeStructure::createPointer
PointerPtr createPointer(DataPtr from, DataPtr to, int dataType=0)
FIXME addeding multiple pointers.
Definition: RootedTreeStructure.cpp:147
Document.h
RootedTreeNode::setNodeParent
PointerPtr setNodeParent(DataPtr parent) const
Definition: RootedTreeNode.cpp:98
RootedTreeStructure
Definition: RootedTreeStructure.h:27
RootedTreeStructure::rootNode
DataPtr rootNode() const
Definition: RootedTreeStructure.cpp:225
PointerPtr
boost::shared_ptr< Pointer > PointerPtr
Definition: CoreTypes.h:35
DataStructure::addData
DataPtr addData(DataPtr data)
Definition: DataStructure.cpp:311
DataStructure::setEngine
virtual void setEngine(QScriptEngine *engine)
Definition: DataStructure.cpp:515
RootedTreeStructure::~RootedTreeStructure
~RootedTreeStructure()
Definition: RootedTreeStructure.cpp:133
RootedTreeStructure::root_node
QScriptValue root_node() const
Definition: RootedTreeStructure.cpp:210
RootedTreeNode::numberOfChilds
quint32 numberOfChilds
Definition: RootedTreeNode.h:29
RootedTreeStructure::setEngine
void setEngine(QScriptEngine *engine)
Definition: RootedTreeStructure.cpp:233
DataStructure::document
Document * document() const
Definition: DataStructure.cpp:570
rootedTreeNodeFromScriptValue
void rootedTreeNodeFromScriptValue(const QScriptValue &object, RootedTreeNode *&out)
Definition: RootedTreeStructure.cpp:39
DataStructure::createPointer
virtual PointerPtr createPointer(DataPtr from, DataPtr to, int pointerType)
Creates new pointer from data element "from" to data element "to" of given type "pointerType".
Definition: DataStructure.cpp:393
Document
Definition: Document.h:41
DataStructure::generateUniqueIdentifier
int generateUniqueIdentifier()
Definition: DataStructure.cpp:295
Data::getData
virtual DataPtr getData() const
Definition: Data.cpp:125
RootedTreeStructure::add_data
QScriptValue add_data(const QString &name)
Create a new data (tree node) Note that it only create but not insert it into the tree...
Definition: RootedTreeStructure.cpp:138
boost::throw_exception
void throw_exception(std::exception const &)
Definition: RootedTreeStructure.cpp:43
Pointer.h
RootedTreeStructure::m_rootNode
RootedTreeNode * m_rootNode
Definition: RootedTreeStructure.h:33
DataPtr
boost::shared_ptr< Data > DataPtr
Definition: CoreTypes.h:34
RootedTreeStructure::createData
DataPtr createData(const QString &name, int dataType=0)
Definition: RootedTreeStructure.cpp:199
RootedTreeNode.h
RootedTreeNode::setNumberOfChilds
void setNumberOfChilds(const qint32 number)
Definition: RootedTreeNode.cpp:53
RootedTreeNode::setChild
PointerPtr setChild(DataPtr child, quint32 idx) const
Definition: RootedTreeNode.cpp:139
RootedTreeStructure::set_root_node
void set_root_node(RootedTreeNode *node)
Definition: RootedTreeStructure.cpp:219
RootedTreeStructure::create
static DataStructurePtr create(Document *parent)
Definition: RootedTreeStructure.cpp:45
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
RootedTreeStructure::m_showPointers
bool m_showPointers
Definition: RootedTreeStructure.h:35
DataStructure::addDynamicProperty
void addDynamicProperty(const QString &property, const QVariant &value=QVariant(0))
Definition: DataStructure.cpp:481
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