27 #include "PointerType.h"
28 #include "DataStructure.h"
31 #include <QGraphicsScene>
32 #include <QGraphicsSceneMouseEvent>
35 #include <QPainterPath>
38 #include <QtAlgorithms>
40 #include <QtCore/qmath.h>
41 #include <QGraphicsSimpleTextItem>
45 class PointerItemPrivate
48 PointerItemPrivate(PointerPtr pointer)
50 , _index(pointer->relativeIndex())
51 , _font(QFont(
"Helvetica [Cronyx]", 12))
52 , _item(new QGraphicsItemGroup())
58 qDeleteAll(_propertyValues);
59 _propertyValues.clear();
69 void updatePropertyList(qreal x, qreal y);
74 QGraphicsItemGroup* _item;
75 QMap<QString, QGraphicsSimpleTextItem*> _propertyValues;
78 void PointerItemPrivate::updatePropertyList(qreal x, qreal y) {
80 foreach (
const QString& property, _pointer->properties()) {
81 if (!_propertyValues.contains(property)) {
82 kError() <<
"Cannot update unknown property : " << property;
85 if (_propertyValues[property]->isVisible() ==
false) {
88 _propertyValues[property]->setPos(x + 20, y + offset);
89 _propertyValues[property]->update();
100 , d(new PointerItemPrivate(pointer))
102 connect(pointer.get(), SIGNAL(posChanged()),
this, SLOT(
updatePos()));
103 connect(pointer.get(), SIGNAL(removed()),
this, SLOT(
remove()));
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)));
111 connect(pointer.get(), SIGNAL(directionChanged(PointerType::Direction)),
this, SLOT(
updateAttributes()));
112 connect(pointer.get(), SIGNAL(pointerTypeChanged(
int)),
this, SLOT(
updateAttributes()));
114 setZValue(-1 - d->_index);
115 setFlag(ItemIsSelectable,
true);
118 foreach (
const QString &property, d->_pointer->properties()) {
142 scene()->removeItem(
this);
149 if (!d->_pointer || !d->_pointer->from() || !d->_pointer->to()) {
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());
164 Qt::PenStyle style =
pointer()->style();
166 setPen(QPen(QBrush(QColor(d->_pointer->color())), d->_pointer->width(), style, Qt::RoundCap, Qt::RoundJoin));
168 QPointF middle = path().pointAtPercent(0.5);
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);
175 d->updatePropertyList(middle.x(), middle.y());
176 }
if (d->_pointer->isVisible() ==
true) {
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;
184 d->_propertyValues[property]->setVisible(pointerType->isPropertyVisible(property));
188 foreach (
const QString& property, d->_pointer->properties()) {
189 if (!d->_propertyValues.contains(property)) {
190 kWarning() <<
"No graphics item registered for property : " << property;
193 d->_propertyValues[property]->setVisible(
false);
201 if (d->_propertyValues.contains(name)) {
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]);
217 if (!d->_propertyValues.contains(name)) {
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();
230 if (d->_propertyValues.contains(name)) {
231 kWarning() <<
"Property not removed: not registered at DataItem.";
234 d->_propertyValues[name]->setVisible(
false);
235 d->_item->removeFromGroup(d->_propertyValues[name]);
236 delete d->_propertyValues[name];
237 d->_propertyValues.remove(name);
262 painter->setPen(QPen(Qt::black, d->_pointer->width(), Qt::DotLine));
264 QGraphicsPathItem::paint(painter, option, widget);
virtual ~PointerItem()
Default destructor.
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
when there's a mouse release on the element, this method is invoked
void updateProperty(const QString &name)
PointerItem(PointerPtr pointer, QGraphicsItem *parent=0)
Default constructor.
static GraphicsLayout * self()
void removeProperty(const QString &name)
void registerProperty(const QString &name)
virtual QPainterPath createCurves()=0
QGraphicsItem * propertyListItem() const
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
void mousePressEvent(QGraphicsSceneMouseEvent *event)
when there's a mouse click on the element, this method is invoked
PointerPtr pointer() const