• 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
Pointer.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2004-2011 Tomaz Canabrava <tomaz.canabrava@gmail.com>
4  Copyright 2010-2011 Wagner Reck <wagner.reck@gmail.com>
5  Copyright 2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "Pointer.h"
22 #include "PointerType.h"
23 #include "Data.h"
24 #include "DataStructure.h"
25 #include "QtScriptBackend.h"
26 
27 #include <KDebug>
28 #include <KLocale>
29 #include <QColor>
30 #include <boost/weak_ptr.hpp>
31 
32 class PointerPrivate
33 {
34 public:
35  PointerPrivate() {}
36  boost::weak_ptr<Pointer> q; // self pointer
37 
38  DataPtr from;
39  DataPtr to;
40  int relativeIndex;
41 
42  QColor color;
43  qreal width;
44  PointerTypePtr pointerType;
45  bool visible;
46 
47  DataStructurePtr dataStructure;
48 
49  QScriptValue scriptvalue;
50  QScriptEngine *engine;
51 };
52 
53 PointerPtr Pointer::create(DataStructurePtr dataStructure, DataPtr from, DataPtr to, int pointerType)
54 {
55  return create<Pointer>(dataStructure, from, to, pointerType);
56 }
57 
58 void Pointer::setQpointer(PointerPtr q)
59 {
60  d->q = q;
61 }
62 
63 void Pointer::initialize()
64 {
65  // register pointer at endpoints and connect to changes
66  d->from->addPointer(getPointer());
67  d->to->addPointer(getPointer());
68  updateRelativeIndex();
69  connect(d->to.get(), SIGNAL(posChanged(QPointF)), this, SIGNAL(posChanged()));
70  connect(d->from.get(), SIGNAL(pointerListChanged()), this, SLOT(updateRelativeIndex()));
71  connect(d->pointerType.get(), SIGNAL(directionChanged(PointerType::Direction)), this, SIGNAL(changed()));
72  connect(d->from.get(), SIGNAL(posChanged(QPointF)), this, SIGNAL(posChanged()));
73 
74  // register properties and connect to changes
75  installEventFilter(this);
76  foreach(const QString& property, d->pointerType->properties()) {
77  addDynamicProperty(property, d->pointerType->propertyDefaultValue(property));
78  }
79  connect(d->pointerType.get(), SIGNAL(propertyAdded(QString,QVariant)),
80  this, SLOT(addDynamicProperty(QString,QVariant)));
81  connect(d->pointerType.get(), SIGNAL(propertyRemoved(QString)),
82  this, SLOT(removeDynamicProperty(QString)));
83  connect(d->pointerType.get(), SIGNAL(propertyRenamed(QString,QString)),
84  this, SLOT(renameDynamicProperty(QString,QString)));
85  connect(d->pointerType.get(), SIGNAL(propertyDefaultValueChanged(QString)),
86  this, SLOT(updateDynamicProperty(QString)));
87  connect(d->pointerType.get(), SIGNAL(visibilityChanged(bool)),
88  this, SLOT(setVisible(bool)));
89  connect(d->pointerType.get(), SIGNAL(propertyVisibilityChanged(QString)),
90  this, SLOT(updateDynamicProperty(QString)));
91 }
92 
93 PointerPtr Pointer::getPointer() const
94 {
95  PointerPtr px(d->q);
96  return px;
97 }
98 
99 Pointer::Pointer(DataStructurePtr parent, DataPtr from, DataPtr to, int pointerType) :
100  d(new PointerPrivate())
101 {
102  d->from = from;
103  d->to = to;
104  d->visible = true;
105  d->dataStructure = parent;
106  d->color = d->dataStructure->document()->pointerType(pointerType)->defaultColor();
107  d->width = 1;
108  d->pointerType = d->dataStructure->document()->pointerType(pointerType);
109 
110  connect(d->pointerType.get(), SIGNAL(directionChanged(PointerType::Direction)),
111  this, SIGNAL(directionChanged(PointerType::Direction)));
112  connect(d->pointerType.get(), SIGNAL(styleChanged()),
113  this, SIGNAL(changed()));
114  connect(d->pointerType.get(), SIGNAL(removed()), this, SLOT(remove()));
115 }
116 
117 Pointer::~Pointer()
118 {
119 }
120 
121 bool Pointer::eventFilter(QObject *obj, QEvent *event){
122  if (event->type() == QEvent::DynamicPropertyChange) {
123  if (QDynamicPropertyChangeEvent* const dynEvent = dynamic_cast<QDynamicPropertyChangeEvent*>(event)) {
124  event->accept();
125  emit(propertyChanged(dynEvent->propertyName()));
126  }
127  }
128  return QObject::eventFilter(obj, event);
129 }
130 
131 int Pointer::relativeIndex() const
132 {
133  return d->relativeIndex;
134 }
135 
136 void Pointer::updateRelativeIndex() {
137  d->relativeIndex = d->to->pointerList(d->from).indexOf(getPointer());
138 }
139 
140 DataStructurePtr Pointer::dataStructure() const
141 {
142  return d->dataStructure;
143 }
144 
145 DataPtr Pointer::from() const
146 {
147  return d->from;
148 }
149 
150 DataPtr Pointer::to() const
151 {
152  return d->to;
153 }
154 
155 int Pointer::pointerType() const
156 {
157  return d->pointerType->identifier();
158 }
159 
160 
161 void Pointer::setPointerType(int typeIdentifier)
162 {
163  Q_ASSERT(d->dataStructure->document()->pointerTypeList().contains(typeIdentifier));
164 
165  // disconnect from old type
166  d->pointerType.get()->disconnect(this);
167 
168  // change type
169  d->pointerType = d->dataStructure->document()->pointerType(typeIdentifier);
170  d->dataStructure->updatePointer(getPointer());
171 
172  // connect to new pointer type and emit information about
173  connect(d->pointerType.get(), SIGNAL(directionChanged(PointerType::Direction)),
174  this, SIGNAL(directionChanged(PointerType::Direction)));
175  connect(d->pointerType.get(), SIGNAL(removed()), this, SLOT(remove()));
176  emit pointerTypeChanged(typeIdentifier);
177 }
178 
179 QList<QString> Pointer::properties() const
180 {
181  return d->pointerType->properties();
182 }
183 
184 void Pointer::remove()
185 {
186  d->pointerType->disconnect(this);
187  d->from->disconnect(this);
188  d->to->disconnect(this);
189 
190  // create self reference to ensure pointer to live up to the end of this method call
191  // creating the reference increases the shared pointer count by 1
192  PointerPtr pointer = getPointer();
193  if (d->from) {
194  d->from->remove(pointer);
195  }
196  if (d->to) {
197  d->to->remove(pointer);
198  }
199  d->dataStructure->remove(pointer);
200  emit removed();
201 }
202 
203 void Pointer::self_remove()
204 {
205  kWarning() << "self_remove() is a deprecated function, please use remove()";
206  remove();
207 }
208 
209 
210 PointerType::Direction Pointer::direction() const
211 {
212  return d->pointerType->direction();
213 }
214 
215 bool Pointer::isDirected() const
216 {
217  return (d->pointerType->direction() == PointerType::Unidirectional);
218 }
219 
220 void Pointer::setVisible(bool visible)
221 {
222  d->visible = visible;
223  emit changed();
224 }
225 
226 bool Pointer::isVisible() const
227 {
228  return d->visible;
229 }
230 
231 void Pointer::setColor(const QColor& color)
232 {
233  if (d->color != color) {
234  d->color = color;
235  emit changed();
236  }
237 }
238 
239 QColor Pointer::color() const
240 {
241  return d->color;
242 }
243 
244 qreal Pointer::width() const
245 {
246  return d->width;
247 }
248 
249 void Pointer::setWidth(qreal width)
250 {
251  d->width = width;
252  emit changed();
253 }
254 
255 Qt::PenStyle Pointer::style() const
256 {
257  return d->pointerType->lineStyle();
258 }
259 
260 void Pointer::addDynamicProperty(const QString& property, const QVariant& value)
261 {
262  if (!Document::isValidIdentifier(property)) {
263  kWarning() << "Property identifier \"" << property << "\" is not valid: aborting";
264  return;
265  }
266  setProperty(property.toAscii(), value);
267  emit(propertyAdded(property));
268 }
269 
270 void Pointer::removeDynamicProperty(const QString& property)
271 {
272  setProperty(property.toAscii(), QVariant::Invalid);
273  emit(propertyRemoved(property));
274 }
275 
276 void Pointer::updateDynamicProperty(const QString& property)
277 {
278  if (this->property(property.toLatin1()) == QVariant::Invalid
279  || this->property(property.toLatin1()).toString().isEmpty()
280  ) {
281  this->setProperty(property.toLatin1(), d->pointerType->propertyDefaultValue(property));
282  }
283  emit propertyChanged(property);
284 }
285 
286 void Pointer::renameDynamicProperty(const QString& oldName, const QString& newName)
287 {
288  if (!Document::isValidIdentifier(newName)) {
289  kWarning() << "Property identifier \"" << newName << "\" is not valid: aborting";
290  return;
291  }
292  setProperty(newName.toLatin1(), property(oldName.toLatin1()));
293  setProperty(oldName.toLatin1(), QVariant::Invalid);
294 }
295 
296 QScriptValue Pointer::set_type(int pointerType)
297 {
298  if (!d->dataStructure->document()->pointerTypeList().contains(pointerType)) {
299  dataStructure()->document()->engineBackend()->debug(
300  i18n("Could not set pointer type: pointer type does not exist."));
301  return d->dataStructure->engine()->newVariant(false);
302  }
303  setPointerType(pointerType);
304  return d->dataStructure->engine()->newVariant(true);
305 }
306 
307 QScriptValue Pointer::type() const
308 {
309  return d->dataStructure->engine()->newVariant(d->pointerType->identifier());
310 }
311 
312 void Pointer::remove_property(const QString& name)
313 {
314  removeDynamicProperty(name);
315 }
316 
317 void Pointer::add_property(const QString& name, const QString& value)
318 {
319  addDynamicProperty(name, value);
320 }
321 
322 QScriptValue Pointer::start() const
323 {
324  // from==0 possible if this pointer is deleted
325  if (!d->from) {
326  return QScriptValue();
327  }
328  return d->from->scriptValue();
329 }
330 
331 QScriptValue Pointer::end() const
332 {
333  // to==0 possible if this pointer is deleted
334  if (!d->to) {
335  return QScriptValue();
336  }
337  return d->to->scriptValue();
338 }
339 
340 void Pointer::setEngine(QScriptEngine *engine)
341 {
342  d->engine = engine;
343  d->scriptvalue = d->engine->newQObject(this);
344 }
345 
346 QScriptValue Pointer::scriptValue() const
347 {
348  return d->scriptvalue;
349 }
Pointer::renameDynamicProperty
void renameDynamicProperty(const QString &oldName, const QString &newName)
Definition: Pointer.cpp:286
Pointer::pointerTypeChanged
void pointerTypeChanged(int pointerType)
Emitted when the pointer type of this pointer changes.
Pointer::to
DataPtr to() const
Pointer::add_property
Q_INVOKABLE void add_property(const QString &name, const QString &value)
Add new property to pointer.
Definition: Pointer.cpp:317
PointerTypePtr
boost::shared_ptr< PointerType > PointerTypePtr
Definition: CoreTypes.h:37
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
Pointer::set_type
Q_INVOKABLE QScriptValue set_type(int type)
Set pointer type from script engine.
Definition: Pointer.cpp:296
Pointer::properties
QList< QString > properties() const
Definition: Pointer.cpp:179
Pointer::setPointerType
void setPointerType(int pointerType)
Change pointer type of the pointer.
Definition: Pointer.cpp:161
QtScriptBackend.h
QObject
Data.h
Pointer::dataStructure
DataStructurePtr dataStructure() const
Definition: Pointer.cpp:140
Pointer::relativeIndex
int relativeIndex() const
The relative index is the index that this pointer has relative to the pair of data elements to which ...
Definition: Pointer.cpp:131
PointerType::Unidirectional
Definition: PointerType.h:47
Pointer::directionChanged
void directionChanged(PointerType::Direction direction)
Emitted when direction was changed.
Pointer::removed
void removed()
Emitted when this pointer is removed.
Pointer::isVisible
bool isVisible() const
Definition: Pointer.cpp:226
Pointer::pointerType
int pointerType() const
Definition: Pointer.cpp:155
PointerPtr
boost::shared_ptr< Pointer > PointerPtr
Definition: CoreTypes.h:35
Pointer::setWidth
void setWidth(qreal width)
Set the width of this pointer.
Definition: Pointer.cpp:249
Pointer::color
QColor color() const
Pointer::removeDynamicProperty
void removeDynamicProperty(const QString &property)
Remove dynamic property with identifier property from data element.
Definition: Pointer.cpp:270
DataStructure.h
Pointer::propertyRemoved
void propertyRemoved(const QString &name)
Pointer::setColor
void setColor(const QColor &color)
Set color attribute of this pointer.
Definition: Pointer.cpp:231
Pointer::style
Qt::PenStyle style() const
Definition: Pointer.cpp:255
Pointer::remove
Q_INVOKABLE void remove()
Remove pointer.
Definition: Pointer.cpp:184
Pointer::width
qreal width() const
Pointer::end
Q_INVOKABLE QScriptValue end() const
Definition: Pointer.cpp:331
Pointer::changed
void changed()
Emitted when a connected data element or the pointer is changed.
Pointer::propertyChanged
void propertyChanged(const QString &name)
Pointer::create
static PointerPtr create(DataStructurePtr dataStructure, DataPtr from, DataPtr to, int pointerType)
Create pointer objects.
Definition: Pointer.cpp:53
Pointer::eventFilter
bool eventFilter(QObject *obj, QEvent *event)
Definition: Pointer.cpp:121
Pointer::type
QScriptValue type() const
Pointer::direction
PointerType::Direction direction() const
Definition: Pointer.cpp:210
Pointer::remove_property
Q_INVOKABLE void remove_property(const QString &name)
Remove a property named name from this pointer.
Definition: Pointer.cpp:312
Pointer::setVisible
void setVisible(bool visible)
Definition: Pointer.cpp:220
Pointer::scriptValue
QScriptValue scriptValue() const
Definition: Pointer.cpp:346
Pointer::addDynamicProperty
void addDynamicProperty(const QString &property, const QVariant &value)
Add new dynamic property with identifier property to this data element and sets it to value...
Definition: Pointer.cpp:260
PointerType::Direction
Direction
Definition: PointerType.h:46
Pointer.h
Pointer::self_remove
Q_INVOKABLE void self_remove()
Definition: Pointer.cpp:203
DataPtr
boost::shared_ptr< Data > DataPtr
Definition: CoreTypes.h:34
Pointer::start
Q_INVOKABLE QScriptValue start() const
Definition: Pointer.cpp:322
Pointer::Pointer
Pointer(DataStructurePtr parent, DataPtr from, DataPtr to, int pointerType)
Protected constructor.
Definition: Pointer.cpp:99
Pointer::from
DataPtr from() const
PointerType.h
Pointer::updateDynamicProperty
void updateDynamicProperty(const QString &property)
Definition: Pointer.cpp:276
Pointer::getPointer
PointerPtr getPointer() const
Definition: Pointer.cpp:93
Pointer::propertyAdded
void propertyAdded(const QString &name)
Document::isValidIdentifier
static bool isValidIdentifier(const QString &identifier)
Evaluates given string and returns true if identifier is valid, otherwise returns false...
Definition: Document.cpp:232
Pointer::setEngine
void setEngine(QScriptEngine *engine)
Definition: Pointer.cpp:340
Pointer::~Pointer
virtual ~Pointer()
Default destructor.
Definition: Pointer.cpp:117
Pointer::updateRelativeIndex
void updateRelativeIndex()
Update relative index.
Definition: Pointer.cpp:136
Pointer::posChanged
void posChanged()
Emitted when the position of a connected data element changes.
Pointer::isDirected
bool isDirected() const
Definition: Pointer.cpp:215
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