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

rocs/VisualEditor

  • sources
  • kde-4.14
  • kdeedu
  • rocs
  • VisualEditor
  • Scene
PointerItem.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2008-2011 Tomaz Canabrava <tomaz.canabrava@gmail.com>
4  Copyright 2008 Ugo Sangiori <ugorox@gmail.com>
5  Copyright 2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program 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
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "PointerItem.h"
22 
23 #include "DataItem.h"
24 #include "GraphScene.h"
25 #include "Data.h"
26 #include "Pointer.h"
27 #include "PointerType.h"
28 #include "DataStructure.h"
29 #include "GraphicsLayout.h"
30 
31 #include <QGraphicsScene>
32 #include <QGraphicsSceneMouseEvent>
33 #include <QPainter>
34 #include <QPen>
35 #include <QPainterPath>
36 #include <QLine>
37 #include <QPolygonF>
38 #include <QtAlgorithms>
39 #include <KDebug>
40 #include <QtCore/qmath.h>
41 #include <QGraphicsSimpleTextItem>
42 
43 // class PointerItemPrivate
44 
45 class PointerItemPrivate
46 {
47 public:
48  PointerItemPrivate(PointerPtr pointer)
49  : _pointer(pointer)
50  , _index(pointer->relativeIndex())
51  , _font(QFont("Helvetica [Cronyx]", 12))
52  , _item(new QGraphicsItemGroup())
53  {
54  }
55 
56  ~PointerItemPrivate()
57  {
58  qDeleteAll(_propertyValues);
59  _propertyValues.clear();
60  delete _item;
61  }
62 
69  void updatePropertyList(qreal x, qreal y);
70 
71  PointerPtr _pointer;
72  int _index;
73  QFont _font;
74  QGraphicsItemGroup* _item;
75  QMap<QString, QGraphicsSimpleTextItem*> _propertyValues;
76 };
77 
78 void PointerItemPrivate::updatePropertyList(qreal x, qreal y) {
79  qreal offset = 0;
80  foreach (const QString& property, _pointer->properties()) {
81  if (!_propertyValues.contains(property)) {
82  kError() << "Cannot update unknown property : " << property;
83  continue;
84  }
85  if (_propertyValues[property]->isVisible() == false) {
86  continue;
87  }
88  _propertyValues[property]->setPos(x + 20, y + offset);
89  _propertyValues[property]->update();
90  offset += 20;
91  }
92 }
93 
94 
95 // class PointerItem
96 
97 PointerItem::PointerItem(PointerPtr pointer, QGraphicsItem *parent)
98  : QObject(0)
99  , QGraphicsPathItem(parent)
100  , d(new PointerItemPrivate(pointer))
101 {
102  connect(pointer.get(), SIGNAL(posChanged()), this, SLOT(updatePos()));
103  connect(pointer.get(), SIGNAL(removed()), this, SLOT(remove()));
104  connect(pointer.get(), SIGNAL(changed()), this, SLOT(updateAttributes()));
105 
106  connect(pointer.get(), SIGNAL(propertyAdded(QString)), this, SLOT(registerProperty(QString)));
107  connect(pointer.get(), SIGNAL(propertyRemoved(QString)), this, SLOT(removeProperty(QString)));
108  connect(pointer.get(), SIGNAL(propertyChanged(QString)), this, SLOT(updateProperty(QString)));
109  connect(GraphicsLayout::self(), SIGNAL(changed()), this, SLOT(updateAttributes()));
110 
111  connect(pointer.get(), SIGNAL(directionChanged(PointerType::Direction)), this, SLOT(updateAttributes()));
112  connect(pointer.get(), SIGNAL(pointerTypeChanged(int)), this, SLOT(updateAttributes()));
113 
114  setZValue(-1 - d->_index);
115  setFlag(ItemIsSelectable, true);
116 
117  // register properties
118  foreach (const QString &property, d->_pointer->properties()) {
119  registerProperty(property);
120  }
121 
122  updateAttributes();
123  this->show();
124 }
125 
126 PointerItem::~PointerItem()
127 {
128  delete d;
129 }
130 
131 void PointerItem::mousePressEvent(QGraphicsSceneMouseEvent */*event*/)
132 {
133 }
134 
135 void PointerItem::mouseReleaseEvent(QGraphicsSceneMouseEvent */*event*/)
136 {
137 }
138 
139 void PointerItem::remove()
140 {
141  if (scene()) {
142  scene()->removeItem(this);
143  }
144  deleteLater();
145 }
146 
147 void PointerItem::updatePos()
148 {
149  if (!d->_pointer || !d->_pointer->from() || !d->_pointer->to()) {
150  return;
151  }
152  QLine q(d->_pointer->from()->x(), d->_pointer->from()->y(), d->_pointer->to()->x(), d->_pointer->to()->y());
153  qreal size = qSqrt(qPow(q.dx(), 2) + qPow(q.dy(), 2));
154  if (d->_pointer->from() != d->_pointer->to() && size < 20) {
155  setPath(QPainterPath());
156  } else {
157  setPath(createCurves());
158  }
159  updateAttributes();
160 }
161 
162 void PointerItem::updateAttributes()
163 {
164  Qt::PenStyle style = pointer()->style();
165 
166  setPen(QPen(QBrush(QColor(d->_pointer->color())), d->_pointer->width(), style, Qt::RoundCap, Qt::RoundJoin));
167  this->hide();
168  QPointF middle = path().pointAtPercent(0.5);
169 
170  if (d->_pointer->from() == d->_pointer->to()) {
171  qreal x1 = boundingRect().x() + boundingRect().width() + 5;
172  qreal y1 = boundingRect().y() + (boundingRect().height() / 2) - 10;
173  d->updatePropertyList(x1, y1);
174  } else {
175  d->updatePropertyList(middle.x(), middle.y());
176  } if (d->_pointer->isVisible() == true) {
177  this->show();
178  PointerTypePtr pointerType = d->_pointer->dataStructure()->document()->pointerType(d->_pointer->pointerType());
179  foreach (const QString& property, d->_pointer->properties()) {
180  if (!d->_propertyValues.contains(property)) {
181  kWarning() << "No graphics item registered for property : " << property;
182  continue;
183  }
184  d->_propertyValues[property]->setVisible(pointerType->isPropertyVisible(property));
185  }
186  } else {
187  this->hide();
188  foreach (const QString& property, d->_pointer->properties()) {
189  if (!d->_propertyValues.contains(property)) {
190  kWarning() << "No graphics item registered for property : " << property;
191  continue;
192  }
193  d->_propertyValues[property]->setVisible(false);
194  }
195  }
196  update();
197 }
198 
199 void PointerItem::registerProperty(const QString& name)
200 {
201  if (d->_propertyValues.contains(name)) {
202  return;
203  }
204  PointerTypePtr pointerType = d->_pointer->dataStructure()->document()->pointerType(d->_pointer->pointerType());
205  d->_propertyValues.insert(name, new QGraphicsSimpleTextItem(d->_pointer->property(name.toLatin1()).toString()));
206  d->_propertyValues[name]->setFlags(ItemIgnoresTransformations);
207  d->_propertyValues[name]->setVisible(pointerType->isPropertyVisible(name));
208  d->_propertyValues[name]->setFont(d->_font);
209  d->_propertyValues[name]->setZValue(zValue() + 1);
210  d->_item->addToGroup(d->_propertyValues[name]);
211 
212  updateAttributes();
213 }
214 
215 void PointerItem::updateProperty(const QString& name)
216 {
217  if (!d->_propertyValues.contains(name)) {
218  registerProperty(name);
219  return;
220  }
221  PointerTypePtr pointerType = d->_pointer->dataStructure()->document()->pointerType(d->_pointer->pointerType());
222  d->_propertyValues[name]->setText(d->_pointer->property(name.toLatin1()).toString());
223  d->_propertyValues[name]->setVisible(pointerType->isPropertyVisible(name));
224  d->_propertyValues[name]->update();
225  updateAttributes();
226 }
227 
228 void PointerItem::removeProperty(const QString& name)
229 {
230  if (d->_propertyValues.contains(name)) {
231  kWarning() << "Property not removed: not registered at DataItem.";
232  return;
233  }
234  d->_propertyValues[name]->setVisible(false);
235  d->_item->removeFromGroup(d->_propertyValues[name]);
236  delete d->_propertyValues[name];
237  d->_propertyValues.remove(name);
238 
239  updateAttributes();
240 }
241 
242 int PointerItem::type() const
243 {
244  return Type;
245 }
246 
247 QGraphicsItem* PointerItem::propertyListItem() const
248 {
249  return d->_item;
250 }
251 
252 PointerPtr PointerItem::pointer() const {
253  return d->_pointer;
254 }
255 
256 void PointerItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
257 {
258  Q_UNUSED(option);
259  Q_UNUSED(widget);
260 
261  if (isSelected()) {
262  painter->setPen(QPen(Qt::black, d->_pointer->width(), Qt::DotLine));
263  }
264  QGraphicsPathItem::paint(painter, option, widget);
265 }
QWidget
QLine
PointerItem::~PointerItem
virtual ~PointerItem()
Default destructor.
Definition: PointerItem.cpp:126
QGraphicsItem::setFlag
void setFlag(GraphicsItemFlag flag, bool enabled)
QRectF::x
qreal x() const
QRectF::y
qreal y() const
QFont
PointerItem::mouseReleaseEvent
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
when there's a mouse release on the element, this method is invoked
Definition: PointerItem.cpp:135
QMap< QString, QGraphicsSimpleTextItem * >
QGraphicsPathItem::paint
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
PointerItem::updateProperty
void updateProperty(const QString &name)
Definition: PointerItem.cpp:215
QBrush
QGraphicsItem
PointerItem::PointerItem
PointerItem(PointerPtr pointer, QGraphicsItem *parent=0)
Default constructor.
Definition: PointerItem.cpp:97
QGraphicsItem::hide
void hide()
GraphicsLayout::self
static GraphicsLayout * self()
Definition: GraphicsLayout.cpp:39
GraphScene.h
QGraphicsItem::scene
QGraphicsScene * scene() const
PointerItem::type
int type() const
Definition: PointerItem.cpp:242
QGraphicsItemGroup
QGraphicsItem::update
void update(const QRectF &rect)
QPointF
PointerItem::updatePos
void updatePos()
Definition: PointerItem.cpp:147
PointerItem.h
QGraphicsSimpleTextItem
PointerItem::remove
void remove()
Definition: PointerItem.cpp:139
QObject::name
const char * name() const
PointerItem::updateAttributes
void updateAttributes()
Definition: PointerItem.cpp:162
QGraphicsItem::zValue
qreal zValue() const
PointerItem::removeProperty
void removeProperty(const QString &name)
Definition: PointerItem.cpp:228
QPointF::x
qreal x() const
QPointF::y
qreal y() const
QObject::property
QVariant property(const char *name) const
QGraphicsScene::removeItem
void removeItem(QGraphicsItem *item)
GraphicsLayout.h
QObject
QPainter::setPen
void setPen(const QColor &color)
QGraphicsSceneMouseEvent
QGraphicsItem::isSelected
bool isSelected() const
QGraphicsPathItem
PointerItem::registerProperty
void registerProperty(const QString &name)
Definition: PointerItem.cpp:199
QPainter
PointerItem::createCurves
virtual QPainterPath createCurves()=0
QGraphicsPathItem::boundingRect
virtual QRectF boundingRect() const
QObject::deleteLater
void deleteLater()
QString
QPainterPath::pointAtPercent
QPointF pointAtPercent(qreal t) const
QColor
QPainterPath
QString::toLatin1
QByteArray toLatin1() const
QRectF::width
qreal width() const
QAbstractGraphicsShapeItem::setPen
void setPen(const QPen &pen)
PointerItem::propertyListItem
QGraphicsItem * propertyListItem() const
Definition: PointerItem.cpp:247
QGraphicsPathItem::setPath
void setPath(const QPainterPath &path)
DataItem.h
PointerItem::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Definition: PointerItem.cpp:256
QPen
QStyleOptionGraphicsItem
QRectF::height
qreal height() const
QGraphicsItem::show
void show()
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QGraphicsPathItem::path
QPainterPath path() const
QGraphicsItem::setZValue
void setZValue(qreal z)
PointerItem::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *event)
when there's a mouse click on the element, this method is invoked
Definition: PointerItem.cpp:131
PointerItem::Type
Definition: PointerItem.h:63
PointerItem::pointer
PointerPtr pointer() const
Definition: PointerItem.cpp:252
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/VisualEditor

Skip menu "rocs/VisualEditor"
  • Main Page
  • 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
  • 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