23 #include "DataStructure.h"
25 #include "DocumentManager.h"
29 #include <QGraphicsColorizeEffect>
30 #include <QGraphicsScene>
31 #include <QGraphicsItem>
32 #include <QSvgRenderer>
33 #include <boost/concept_check.hpp>
39 explicit DataItemPrivate(DataPtr n)
42 , _font(QFont(
"Helvetica [Cronyx]", 12))
44 , _item(new QGraphicsItemGroup())
45 , _originalWidth(n->width())
47 , _oldDataType(n->dataStructure()->document()->dataType(n->dataType()))
53 qDeleteAll(_propertyValues);
54 _propertyValues.clear();
59 QMap<QString, QGraphicsSimpleTextItem*> _propertyValues;
60 QGraphicsColorizeEffect *_colorizer;
61 QGraphicsRectItem *_boundingRect;
64 QGraphicsItemGroup* _item;
68 DataTypePtr _oldDataType;
73 , d(new DataItemPrivate(n))
76 connect(n.get(), SIGNAL(removed()),
this, SLOT(deleteLater()));
77 connect(d->_oldDataType.get(), SIGNAL(iconChanged(QString)),
this, SLOT(updateIcon()));
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)));
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()));
91 setCacheMode(QGraphicsItem::DeviceCoordinateCache);
93 setFlag(ItemIsSelectable,
true);
107 void DataItem::setupNode()
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()));
116 foreach (
const QString &property, d->_data->properties()) {
117 registerProperty(property);
128 void DataItem::updatePos()
130 int fixPos = boundingRect().width() / 2;
131 setPos(d->_data->x() - fixPos, d->_data->y() - fixPos);
132 updatePropertyList();
135 void DataItem::updateSize()
137 if (d->_data->width() == d->_width) {
141 d->_width = d->_data->width();
142 setScale(d->_data->width());
145 void DataItem::updateRenderer()
147 QString iconPackage = d->_data->dataStructure()->document()->iconPackage();
148 setSharedRenderer(DocumentManager::self().sharedRenderer(iconPackage));
151 void DataItem::updateIcon()
153 QString icon = d->_data->dataStructure()->document()->dataType(d->_data->dataType())->iconName();
154 if (elementId().isEmpty() || elementId() != icon) {
156 setTransformOriginPoint(boundingRect().width() / 2, boundingRect().width() / 2);
160 void DataItem::updateVisibility(
bool visible)
162 if (visible ==
true) {
164 QMap<QString, QGraphicsSimpleTextItem*>::const_iterator iter = d->_propertyValues.constBegin();
165 while (iter != d->_propertyValues.constEnd()) {
166 (*iter)->setVisible(
true);
171 QMap<QString, QGraphicsSimpleTextItem*>::const_iterator iter = d->_propertyValues.constBegin();
172 while (iter != d->_propertyValues.constEnd()) {
173 (*iter)->setVisible(
false);
179 void DataItem::updateColor()
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);
188 void DataItem::updateProperty(
const QString& name)
190 if (!d->_propertyValues.contains(name)) {
191 registerProperty(name);
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();
206 void DataItem::updatePropertyList()
209 foreach (
const QString& property,
data()->properties()) {
210 if (!d->_propertyValues.contains(property)) {
211 kError() <<
"Cannot update unknown property : " << property;
214 if (d->_propertyValues[property]->isVisible() ==
false) {
217 d->_propertyValues[property]->setPos(
data()->x()+20,
data()->y() + offset);
218 d->_propertyValues[property]->update();
223 void DataItem::registerProperty(
const QString& name)
225 if (d->_propertyValues.contains(name)) {
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]);
236 updatePropertyList();
239 void DataItem::removeProperty(
const QString& name)
241 if (d->_propertyValues.contains(name)) {
242 kWarning() <<
"Property not removed: not registered at DataItem.";
245 d->_propertyValues[name]->setVisible(
false);
246 d->_item->removeFromGroup(d->_propertyValues[name]);
247 delete d->_propertyValues[name];
248 d->_propertyValues.remove(name);
250 updatePropertyList();
static GraphicsLayout * self()
QGraphicsItem * propertyListItem() const