• 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
  • LinkedList
ListStructure.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2011 Wagner Reck <wagner.reck@gmail.com>
4  Copyright 2013 Andreas Cord-Landwehr <cordlandwehr@kde.org>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "ListStructure.h"
21 #include "ListNode.h"
22 #include "Pointer.h"
23 #include <KLocale>
24 #include <KDebug>
25 #include <boost/shared_ptr.hpp>
26 
27 
28 DataStructurePtr Rocs::ListStructure::create(Document *parent)
29 {
30  return DataStructure::create<ListStructure>(parent);
31 }
32 
33 DataStructurePtr Rocs::ListStructure::create(DataStructurePtr other, Document *parent)
34 {
35  boost::shared_ptr<ListStructure> ds = boost::static_pointer_cast<ListStructure>(create(parent));
36  ds->importStructure(other);
37  return ds;
38 }
39 
40 Rocs::ListStructure::ListStructure(Document* parent)
41  : DataStructure(parent)
42  , m_building(true)
43 {
44  m_building = false;
45  init();
46 }
47 
48 void Rocs::ListStructure::importStructure(DataStructurePtr other)
49 {
50  m_building = true;
51  QHash < Data*, DataPtr > dataTodata;
52  foreach (int type, other->document()->dataTypeList()) {
53  foreach (DataPtr n, other->dataList(type)) {
54  DataPtr newdata = createData("", type);
55  newdata->setColor(n->color());
56  newdata->setProperty("value", n->property("value").toString());
57  newdata->setX(n->x());
58  newdata->setY(n->y());
59  newdata->setWidth(n->width());
60  dataTodata.insert(n.get(), newdata);
61  foreach (const QString &property, other->document()->dataType(type)->properties()) {
62  newdata->setProperty(property.toLatin1(), n->property(property.toLatin1()));
63  }
64  }
65  }
66  foreach (int type, other->document()->pointerTypeList()) {
67  foreach (PointerPtr e, other->pointers(type)) {
68  DataPtr from = dataTodata.value(e->from().get());
69  DataPtr to = dataTodata.value(e->to().get());
70 
71  PointerPtr newPointer = createPointer(from, to, type);
72  newPointer->setColor(e->color());
73  newPointer->setProperty("value", e->property("value").toString());
74  foreach (const QString &property, other->document()->pointerType(type)->properties()) {
75  newPointer->setProperty(property.toLatin1(), e->property(property.toLatin1()));
76  }
77  }
78  }
79  m_building = false;
80  init();
81 }
82 
83 
84 void Rocs::ListStructure::init()
85 {
86  connect(this, SIGNAL(changed()), this, SLOT(arrangeNodes()));
87 
88  m_animationGroup = new QParallelAnimationGroup(parent());
89  if (!dataList(0).isEmpty()) {
90  m_begin = boost::static_pointer_cast<ListNode>(dataList(0).first());
91  }
92 }
93 
94 Rocs::ListStructure::~ListStructure()
95 {
96  m_building = true;
97  m_animationGroup->stop();
98 // FIXME this is a memory leak, but fix this later
99 // since delete later guides to crash on deletion
100 // m_animationGroup->deleteLater();
101 }
102 
103 PointerPtr Rocs::ListStructure::createPointer(DataPtr from, DataPtr to, int pointerType)
104 {
105  foreach(PointerPtr e, from->outPointerList()) {
106  e->remove();
107  }
108 
109  PointerPtr e = DataStructure::createPointer(from, to, pointerType);
110  return e;
111 }
112 
113 DataPtr Rocs::ListStructure::createData(const QString& name, int dataType)
114 {
115  boost::shared_ptr<ListNode> n = boost::static_pointer_cast<ListNode>(
116  ListNode::create(getDataStructure(), generateUniqueIdentifier(), dataType)
117  );
118  n->setProperty("name", name);
119  addData(n);
120 
121  // insert node as new head
122  n->pointTo(m_begin);
123  m_begin = n;
124  return n;
125 }
126 
127 void Rocs::ListStructure::remove(DataPtr n)
128 {
129  if (m_begin == n) {
130  m_begin = boost::static_pointer_cast<ListNode>(n)->next();
131  }
132  DataStructure::remove(n);
133 }
134 
135 QScriptValue Rocs::ListStructure::head()
136 {
137  return m_begin->scriptValue();
138 }
139 
140 void Rocs::ListStructure::setHead(Data *headNode)
141 {
142  Q_ASSERT(headNode);
143  if (!headNode) {
144  return;
145  }
146  //FIXME: we need resorting!
147  m_begin = boost::static_pointer_cast<ListNode>(headNode->getData());
148 }
149 
150 QScriptValue Rocs::ListStructure::begin()
151 {
152  emit scriptError(i18n("The method \"%1\" is deprecated, please use \"%2\" instead.",
153  QString("begin()"),
154  QString("head()")));
155  return head();
156 }
157 
158 void Rocs::ListStructure::setBegin(Data* node)
159 {
160  emit scriptError(i18n("The method \"%1\" is deprecated, please use \"%2\" instead.",
161  QString("setBegin(node)"),
162  QString("setHead(node)")));
163  setHead(node);
164 }
165 
166 void Rocs::ListStructure::remove(PointerPtr ptr)
167 {
168  DataStructure::remove(ptr);
169 }
170 
171 QScriptValue Rocs::ListStructure::createNode()
172 {
173  return createNode(0);
174 }
175 
176 QScriptValue Rocs::ListStructure::createNode(int type)
177 {
178  boost::shared_ptr<ListNode> n = boost::static_pointer_cast<ListNode>(
179  DataStructure::addData(ListNode::create(getDataStructure(), generateUniqueIdentifier(), type))
180  );
181  n->setEngine(engine());
182  return n->scriptValue();
183 }
184 
185 void Rocs::ListStructure::arrangeNodes()
186 {
187  if (m_building) {
188  return;
189  }
190 
191  QRectF size = document()->sceneRect();
192  qreal x;
193  qreal y = size.top() + 100;;
194  if (m_animationGroup->state() != QAnimationGroup::Stopped) {
195  m_animationGroup->stop();
196  }
197  //FIXME only default data type considered
198  QScopedArrayPointer<bool>visited(new bool[dataList(0).count()]);
199  for (int i = 0; i < dataList(0).count(); ++i) {
200  visited[i] = false;
201  }
202 
203  QPropertyAnimation * anim;
204  boost::shared_ptr<ListNode> n = m_begin;
205  if (n) {
206  x = size.left() + 50;
207  do {
208  if (visited[dataList(0).indexOf(n)]) {
209  break;
210  }
211  visited[dataList(0).indexOf(n)] = true;
212  if (x > size.right() - 140 + n->width() * 40) {
213  x = size.left() + 50 + n->width() * 40;
214  y += 85;
215  } else {
216  x += 70 + n->width() * 40;
217  }
218  anim = new QPropertyAnimation(n.get(), "x");;
219  anim->setDuration(500);
220  anim->setStartValue(n->x());
221  anim->setEndValue(x);
222  m_animationGroup->addAnimation(anim);
223  anim = new QPropertyAnimation(n.get(), "y");;
224  anim->setDuration(500);
225  anim->setStartValue(n->y());
226  anim->setEndValue(y);
227  m_animationGroup->addAnimation(anim);
228  } while ((n = n->next()));
229  }
230  x = 50 + size.left();
231  y += 100;
232  //FIXME only default data type considered
233  foreach(DataPtr n, dataList(0)) {
234  if (!visited[dataList(0).indexOf(n)]) {
235  anim = new QPropertyAnimation(n.get(), "x");;
236  anim->setDuration(500);
237  anim->setStartValue(n->x());
238  anim->setEndValue(x);
239  m_animationGroup->addAnimation(anim);
240  anim = new QPropertyAnimation(n.get(), "y");;
241  anim->setDuration(500);
242  anim->setStartValue(n->y());
243  anim->setEndValue(y);
244  m_animationGroup->addAnimation(anim);
245  x += 60;
246  if (x > size.right() - 60) {
247  x = 50 + size.left();
248  y += 50;
249  }
250  }
251  }
252  m_animationGroup->start();
253 }
Rocs::ListStructure::createPointer
PointerPtr createPointer(DataPtr from, DataPtr to, int pointerType)
Internal method to create new graph edge.
Definition: ListStructure.cpp:103
Rocs::ListStructure::importStructure
void importStructure(DataStructurePtr other)
overwrites the current DataStructure with all values (Data and Pointer) from the given datastructure ...
Definition: ListStructure.cpp:48
Rocs::ListStructure::begin
Q_INVOKABLE QScriptValue begin()
Use head();.
Definition: ListStructure.cpp:150
ListNode
Definition: ListNode.h:29
DataStructure
Definition: DataStructure.h:43
Data::setEngine
virtual void setEngine(QScriptEngine *_engine)
Set script engine.
Definition: Data.cpp:476
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
GmlParser::document
Document * document
Definition: GmlGrammar.cpp:40
DotParser::createData
void createData(const std::string &str)
Definition: DotGrammar.cpp:372
ListNode.h
ListNode::create
static DataPtr create(DataStructurePtr parent, int uniqueIdentifier, int dataType)
Definition: ListNode.cpp:29
Rocs::ListStructure::setHead
Q_INVOKABLE void setHead(Data *headNode)
Set head node of linked list.
Definition: ListStructure.cpp:140
PointerPtr
boost::shared_ptr< Pointer > PointerPtr
Definition: CoreTypes.h:35
DataStructure::addData
DataPtr addData(DataPtr data)
Definition: DataStructure.cpp:311
Rocs::ListStructure::head
Q_INVOKABLE QScriptValue head()
Definition: ListStructure.cpp:135
Rocs::ListStructure::setBegin
Q_INVOKABLE void setBegin(Data *node)
Use setHead(node);.
Definition: ListStructure.cpp:158
Rocs::ListStructure::~ListStructure
virtual ~ListStructure()
Definition: ListStructure.cpp:94
Rocs::ListStructure::createNode
Q_INVOKABLE QScriptValue createNode()
Creates a new data element and return it.
Definition: ListStructure.cpp:171
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
Data::getData
virtual DataPtr getData() const
Definition: Data.cpp:125
DataStructure::remove
void remove()
if this datastructure shall be deleted, call ONLY this function
Definition: DataStructure.cpp:271
Rocs::ListStructure::createData
DataPtr createData(const QString &name, int dataType)
Internal method to create new graph node.
Definition: ListStructure.cpp:113
Pointer.h
DataPtr
boost::shared_ptr< Data > DataPtr
Definition: CoreTypes.h:34
Rocs::ListStructure
Definition: ListStructure.h:31
Data
Definition: Data.h:40
Rocs::ListStructure::create
static DataStructurePtr create(Document *parent)
Definition: ListStructure.cpp:28
ListStructure.h
Document::sceneRect
QRectF sceneRect() const
Definition: Document.cpp:272
Rocs::ListStructure::ListStructure
ListStructure(Document *parent=0)
Definition: ListStructure.cpp:40
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