• 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
DataItem.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2011 Tomaz Canabrava <tomaz.canabrava@gmail.com>
4  Copyright 2011-2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
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 "DataItem.h"
21 
22 #include "Data.h"
23 #include "DataStructure.h"
24 #include "GraphicsLayout.h"
25 #include "DocumentManager.h"
26 
27 #include <KDebug>
28 #include <QFont>
29 #include <QGraphicsColorizeEffect>
30 #include <QGraphicsScene>
31 #include <QGraphicsItem>
32 #include <QSvgRenderer>
33 #include <boost/concept_check.hpp>
34 
35 
36 class DataItemPrivate
37 {
38 public:
39  explicit DataItemPrivate(DataPtr n)
40  : _data(n)
41  , _colorizer(0)
42  , _font(QFont("Helvetica [Cronyx]", 12))
43  , _oldStyle(GraphicsLayout::self()->viewStyleDataNode())
44  , _item(new QGraphicsItemGroup())
45  , _originalWidth(n->width())
46  , _width(-1)
47  , _oldDataType(n->dataStructure()->document()->dataType(n->dataType()))
48  {
49  }
50 
51  ~DataItemPrivate()
52  {
53  qDeleteAll(_propertyValues);
54  _propertyValues.clear();
55  delete _item;
56  }
57 
58  DataPtr _data;
59  QMap<QString, QGraphicsSimpleTextItem*> _propertyValues;
60  QGraphicsColorizeEffect *_colorizer;
61  QGraphicsRectItem *_boundingRect;
62  QFont _font;
63  int _oldStyle;
64  QGraphicsItemGroup* _item;
65 
66  qreal _originalWidth;
67  qreal _width;
68  DataTypePtr _oldDataType;
69 };
70 
71 DataItem::DataItem(DataPtr n)
72  : QGraphicsSvgItem(0)
73  , d(new DataItemPrivate(n))
74 {
75 
76  connect(n.get(), SIGNAL(removed()), this, SLOT(deleteLater()));
77  connect(d->_oldDataType.get(), SIGNAL(iconChanged(QString)), this, SLOT(updateIcon()));
78 
79  connect(n.get(), SIGNAL(propertyAdded(QString)), this, SLOT(registerProperty(QString)));
80  connect(n.get(), SIGNAL(propertyRemoved(QString)), this, SLOT(removeProperty(QString)));
81  connect(n.get(), SIGNAL(propertyChanged(QString)), this, SLOT(updateProperty(QString)));
82  connect(GraphicsLayout::self(), SIGNAL(changed()), this, SLOT(updatePropertyList()));
83 
84  connect(n.get(), SIGNAL(colorChanged(QColor)), this, SLOT(updateColor()));
85  connect(n.get(), SIGNAL(posChanged(QPointF)), this, SLOT(updatePos()));
86  connect(n.get(), SIGNAL(visibilityChanged(bool)), this, SLOT(updateVisibility(bool)));
87  connect(n.get(), SIGNAL(useColorChanged(bool)), this, SLOT(updateColor()));
88  connect(n.get(), SIGNAL(widthChanged(double)), this, SLOT(updateSize()));
89  connect(n.get(), SIGNAL(dataTypeChanged(int)), this, SLOT(setupNode()));
90 
91  setCacheMode(QGraphicsItem::DeviceCoordinateCache);
92  setZValue(1);
93  setFlag(ItemIsSelectable, true);
94  setupNode();
95 }
96 
97 DataItem::~DataItem()
98 {
99  delete d;
100 }
101 
102 DataPtr DataItem::data() const
103 {
104  return d->_data;
105 }
106 
107 void DataItem::setupNode()
108 {
109  if (d->_data.get()->dataStructure()->document()->dataType(d->_data.get()->dataType()) != d->_oldDataType) {
110  disconnect(d->_oldDataType.get(), SIGNAL(iconChanged(QString)), this, SLOT(updateIcon()));
111  d->_oldDataType = d->_data.get()->dataStructure()->document()->dataType(d->_data.get()->dataType());
112  connect(d->_oldDataType.get(), SIGNAL(iconChanged(QString)), this, SLOT(updateIcon()));
113  }
114 
115  // register properties
116  foreach (const QString &property, d->_data->properties()) {
117  registerProperty(property);
118  }
119 
120  updateRenderer();
121  updateIcon();
122  updateColor();
123  updateSize();
124  updatePos();
125  update();
126 }
127 
128 void DataItem::updatePos()
129 {
130  int fixPos = boundingRect().width() / 2;
131  setPos(d->_data->x() - fixPos, d->_data->y() - fixPos);
132  updatePropertyList();
133 }
134 
135 void DataItem::updateSize()
136 {
137  if (d->_data->width() == d->_width) {
138  return;
139  }
140  resetTransform();
141  d->_width = d->_data->width();
142  setScale(d->_data->width());
143 }
144 
145 void DataItem::updateRenderer()
146 {
147  QString iconPackage = d->_data->dataStructure()->document()->iconPackage();
148  setSharedRenderer(DocumentManager::self().sharedRenderer(iconPackage));
149 }
150 
151 void DataItem::updateIcon()
152 {
153  QString icon = d->_data->dataStructure()->document()->dataType(d->_data->dataType())->iconName();
154  if (elementId().isEmpty() || elementId() != icon) {
155  setElementId(icon);
156  setTransformOriginPoint(boundingRect().width() / 2, boundingRect().width() / 2);
157  }
158 }
159 
160 void DataItem::updateVisibility(bool visible)
161 {
162  if (visible == true) {
163  this->show();
164  QMap<QString, QGraphicsSimpleTextItem*>::const_iterator iter = d->_propertyValues.constBegin();
165  while (iter != d->_propertyValues.constEnd()) {
166  (*iter)->setVisible(true);
167  ++iter;
168  }
169  } else {
170  this->hide();
171  QMap<QString, QGraphicsSimpleTextItem*>::const_iterator iter = d->_propertyValues.constBegin();
172  while (iter != d->_propertyValues.constEnd()) {
173  (*iter)->setVisible(false);
174  ++iter;
175  }
176  }
177 }
178 
179 void DataItem::updateColor()
180 {
181  QColor c(d->_data->color().value<QColor>());
182  delete d->_colorizer;
183  d->_colorizer = new QGraphicsColorizeEffect();
184  d->_colorizer->setColor(c);
185  setGraphicsEffect(d->_colorizer);
186 }
187 
188 void DataItem::updateProperty(const QString& name)
189 {
190  if (!d->_propertyValues.contains(name)) {
191  registerProperty(name);
192  return;
193  }
194  DataTypePtr dataType = data()->dataStructure()->document()->dataType(data()->dataType());
195  d->_propertyValues[name]->setText(data()->property(name.toLatin1()).toString());
196  d->_propertyValues[name]->setVisible(dataType->isPropertyVisible(name));
197  d->_propertyValues[name]->update();
198  updatePropertyList();
199 }
200 
201 QGraphicsItem* DataItem::propertyListItem() const
202 {
203  return d->_item;
204 }
205 
206 void DataItem::updatePropertyList()
207 {
208  qreal offset = 0;
209  foreach (const QString& property, data()->properties()) {
210  if (!d->_propertyValues.contains(property)) {
211  kError() << "Cannot update unknown property : " << property;
212  continue;
213  }
214  if (d->_propertyValues[property]->isVisible() == false) {
215  continue;
216  }
217  d->_propertyValues[property]->setPos(data()->x()+20, data()->y() + offset);
218  d->_propertyValues[property]->update();
219  offset += 20;
220  }
221 }
222 
223 void DataItem::registerProperty(const QString& name)
224 {
225  if (d->_propertyValues.contains(name)) {
226  return;
227  }
228  DataTypePtr dataType = data()->dataStructure()->document()->dataType(data()->dataType());
229  d->_propertyValues.insert(name, new QGraphicsSimpleTextItem(data()->property(name.toLatin1()).toString()));
230  d->_propertyValues[name]->setFlags(ItemIgnoresTransformations);
231  d->_propertyValues[name]->setFont(d->_font);
232  d->_propertyValues[name]->setVisible(dataType->isPropertyVisible(name));
233  d->_propertyValues[name]->setZValue(zValue() + 1);
234  d->_item->addToGroup(d->_propertyValues[name]);
235 
236  updatePropertyList();
237 }
238 
239 void DataItem::removeProperty(const QString& name)
240 {
241  if (d->_propertyValues.contains(name)) {
242  kWarning() << "Property not removed: not registered at DataItem.";
243  return;
244  }
245  d->_propertyValues[name]->setVisible(false);
246  d->_item->removeFromGroup(d->_propertyValues[name]);
247  delete d->_propertyValues[name];
248  d->_propertyValues.remove(name);
249 
250  updatePropertyList();
251 }
QGraphicsItem::x
qreal x() const
QGraphicsItem::y
qreal y() const
QGraphicsItem::setScale
void setScale(qreal factor)
QGraphicsItem::setGraphicsEffect
void setGraphicsEffect(QGraphicsEffect *effect)
QGraphicsItem::setFlag
void setFlag(GraphicsItemFlag flag, bool enabled)
DataItem::data
DataPtr data() const
Definition: DataItem.cpp:102
QFont
QMap< QString, QGraphicsSimpleTextItem * >
GraphicsLayout
Definition: GraphicsLayout.h:25
QGraphicsItem
QGraphicsItem::hide
void hide()
GraphicsLayout::self
static GraphicsLayout * self()
Definition: GraphicsLayout.cpp:39
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QGraphicsItemGroup
QGraphicsItem::update
void update(const QRectF &rect)
QPointF
QGraphicsSimpleTextItem
DataItem::~DataItem
virtual ~DataItem()
Definition: DataItem.cpp:97
QObject::name
const char * name() const
QGraphicsItem::zValue
qreal zValue() const
QObject::property
QVariant property(const char *name) const
GraphicsLayout.h
QGraphicsSvgItem::boundingRect
virtual QRectF boundingRect() const
QGraphicsItem::setPos
void setPos(const QPointF &pos)
QString::isEmpty
bool isEmpty() const
QObject::deleteLater
void deleteLater()
QString
QColor
QGraphicsColorizeEffect
DataItem::DataItem
DataItem(DataPtr n)
Definition: DataItem.cpp:71
QGraphicsItem::setTransformOriginPoint
void setTransformOriginPoint(const QPointF &origin)
QGraphicsSvgItem
QString::toLatin1
QByteArray toLatin1() const
QRectF::width
qreal width() const
QGraphicsItem::setCacheMode
void setCacheMode(CacheMode mode, const QSize &logicalCacheSize)
QTest::toString
char * toString(const T &value)
QGraphicsRectItem
QGraphicsItem::resetTransform
void resetTransform()
QGraphicsSvgItem::elementId
QString elementId() const
DataItem.h
QGraphicsItem::show
void show()
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
DataItem::propertyListItem
QGraphicsItem * propertyListItem() const
Definition: DataItem.cpp:201
QGraphicsItem::setZValue
void setZValue(qreal z)
QGraphicsSvgItem::setSharedRenderer
void setSharedRenderer(QSvgRenderer *renderer)
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