29 #include "graphicscomponentitem.h"
30 #include "graphicslinkitem.h"
31 #include "graphicsportitem.h"
32 #include "sheetview.h"
36 #include <arcs/arcsabstractcomponent.h>
39 #include <QGraphicsScene>
40 #include <QGraphicsSceneMoveEvent>
42 #include <QStyleOptionGraphicsItem>
45 #define MAX(A,B) (((A)>(B))?(A):(B))
49 #define MIN(A,B) (((A)<(B))?(A):(B))
53 GraphicsLinkItem::GraphicsLinkItem(QGraphicsItem *parent,
ARCSConnection* c) : QGraphicsPathItem(parent),connection(c)
56 setAcceptHoverEvents(
true);
57 setFlag(ItemIsSelectable,
true);
58 markedForDeletion =
false;
62 finalized = connection;
71 grips[i]->setPrevious(grips[i-1]);
72 grips[i-1]->setNext(grips[i]);
79 QList<QPointF>& coords = connection->getCoordinates();
80 if (coords.count() >= 4)
83 grips[i]->setPos(coords[i]);
96 destination->removeLinkItem(
this);
98 source->removeLinkItem(
this);
104 if ( markedForDeletion )
113 view->getSheet().removeConnection(*connection);
118 void GraphicsLinkItem::redraw(
bool isSource)
123 if ( ! (source && destination) )
163 if (isSource || connection->getCoordinates().count() < 4)
164 computePath(mapFromScene(source->scenePos()), mapFromScene(destination->scenePos()));
170 void GraphicsLinkItem::setCurrentPoint(QPointF p)
172 if ( ! (source || destination) ) return ;
176 computePath(QPointF(0,0),p);
178 computePath(p,QPoint(0,0));
184 if (dest.x() - orig.x() > 40)
187 if (destination && source)
189 QRectF dstRect = mapFromScene(destination->parentItem()->sceneBoundingRect()).boundingRect();
190 QRectF srcRect = mapFromScene(source->parentItem()->sceneBoundingRect()).boundingRect();
192 if ( (srcRect.y() < dstRect.y()) && (dstRect.y() < srcRect.y() + srcRect.height()) )
194 if ( (dstRect.y() < srcRect.y()) && (srcRect.y() < dstRect.y() + dstRect.height()) )
199 QRectF parentRect = mapFromScene(parentItem()->parentItem()->sceneBoundingRect()).boundingRect();
201 if ( (parentRect.y() < orig.y()) && (orig.y() < parentRect.y() + parentRect.height()) )
208 void GraphicsLinkItem::computePath(QPointF orig, QPointF dest)
212 grips[0]->setPos(orig + QPoint(20,0));
213 grips[3]->setPos(dest + QPointF(-20,0));
218 grips[1]->setPos((grips[0]->x()+grips[3]->x())/2, grips[0]->y());
219 grips[2]->setPos(grips[1]->x(), grips[3]->y());
222 grips[1]->setPos(grips[0]->x(),(grips[0]->y()+grips[3]->y())/2);
223 grips[2]->setPos(grips[3]->x(),grips[1]->y());
228 if (destination && source)
230 QRectF dstRect = mapFromScene(destination->parentItem()->sceneBoundingRect()).boundingRect();
231 QRectF srcRect = mapFromScene(source->parentItem()->sceneBoundingRect()).boundingRect();
232 loopHeight = MAX(srcRect.y(),dstRect.y()) + MAX(srcRect.height(),dstRect.height()) - orig.y() + 20 ;
236 QRectF parentRect = mapFromScene(parentItem()->parentItem()->sceneBoundingRect()).boundingRect();
237 loopHeight = parentRect.y() + parentRect.height() - orig.y() + 20 ;
239 grips[1]->setPos(grips[0]->x(),grips[0]->y()+loopHeight);
240 grips[2]->setPos(grips[3]->x(),grips[1]->y());
243 configuration = config;
244 connectGrips(orig,dest);
248 void GraphicsLinkItem::hoverEnterEvent(QGraphicsSceneHoverEvent *)
251 destination->showName();
253 for (
int i=0; i < 4; i++)
257 void GraphicsLinkItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
260 destination->hideName();
261 for (
int i=0; i < 4; i++)
267 void GraphicsLinkItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
269 QStyleOptionGraphicsItem myOptions(*option);
270 if (myOptions.state & (QStyle::State_Selected))
272 QPen myPen(Qt::darkRed);
275 myOptions.state = myOptions.state ^ QStyle::State_Selected ;
281 QGraphicsPathItem::paint(painter,& myOptions, widget );
286 QPainterPath GraphicsLinkItem::shape()
const
288 if ((! source) || (! destination) )
289 return QGraphicsPathItem::shape();
291 QPointF orig = mapFromScene(source->scenePos());
292 QPointF dest = mapFromScene(destination->scenePos());
296 QRectF rectLine(QPointF(MIN(orig.x(),grips[0]->x()),MIN(orig.y(),grips[0]->y())),
297 QPointF(MAX(orig.x(),grips[0]->x()),MAX(orig.y(),grips[0]->y())));
299 pp.addRect(rectLine.adjusted(-2,-2,2,2));
301 for (
int i=0; i < 3;i++ )
303 rectLine = QRectF(QPointF(MIN(grips[i]->x(),grips[i+1]->x()), MIN(grips[i]->y(),grips[i+1]->y())),
304 QPointF(MAX(grips[i]->x(),grips[i+1]->x()), MAX(grips[i]->y(),grips[i+1]->y())));
305 pp.addRect(rectLine.adjusted(-3,-3,3,3));
308 rectLine = QRectF(QPointF(MIN(dest.x(),grips[3]->x()),MIN(dest.y(),grips[3]->y())),
309 QPointF(MAX(dest.x(),grips[3]->x()),MAX(dest.y(),grips[3]->y())));
310 pp.addRect(rectLine.adjusted(-2,-2,2,2));
315 void GraphicsLinkItem::connectGrips(QPointF orig, QPointF dest)
319 pp.lineTo(grips[0]->pos());
320 pp.lineTo(grips[1]->pos());
321 pp.lineTo(grips[2]->pos());
322 pp.lineTo(grips[3]->pos());
329 QList<QPointF>& coords = connection->getCoordinates();
331 for (
int i=0; i< 4; i++)
332 coords.append(grips[i]->pos());
338 void GraphicsLinkItem::connectGrips()
341 QPointF orig = mapFromScene(source->scenePos());
342 QPointF dest = mapFromScene(destination->scenePos());
343 connectGrips(orig,dest);
347 void GraphicsLinkItem::finalize()
349 if (! (destination && source))
359 if (! (sourceItem && destinationItem))
365 destinationItem->getComponent()->
getProperty(
"id").toString(),
366 destination->getName()));
380 GraphicsGripLinkItem::GraphicsGripLinkItem(QGraphicsItem *parent):
381 QGraphicsRectItem(-5,-5,10,10, parent)
384 setFlag(QGraphicsItem::ItemIsMovable,
true);
385 setAcceptHoverEvents(
true);
387 br.setStyle(Qt::SolidPattern);
388 br.setColor(Qt::magenta);
397 void GraphicsGripLinkItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
406 QPointF deltaMove = parentItem()->mapFromScene(event->scenePos()) - pos();
410 if (previous && next)
412 bool yMove = ! ((pos().y() == next->pos().y() && ! next->next) || (pos().y() == previous->pos().y() && ! previous->previous));
414 if (pos().x() == next->pos().x() )
415 next->setPos(next->pos() + QPointF(deltaMove.x(),0) );
418 next->setPos(next->pos() + QPointF(0,deltaMove.y()) );
421 if (pos().x() == previous->pos().x() )
422 previous->setPos(previous->pos() + QPointF(deltaMove.x(),0) );
425 previous->setPos(previous->pos() + QPointF(0,deltaMove.y()) );
428 setPos(parentItem()->mapFromScene(event->scenePos()));
430 setPos(pos() + QPointF(deltaMove.x(),0));
435 if (previous->pos().y() != pos().y())
436 previous->setPos(previous->pos() + QPointF(deltaMove.x(),0) );
438 if (next->pos().y() != pos().y())
439 next->setPos(next->pos() + QPointF(deltaMove.x(),0) );
441 setPos(pos() + QPointF(deltaMove.x(),0));
QVariant getProperty(QString name)
Gets a meta-property from this component.
This class is a loop sending as much iterations as needed.
ARCSConnection & addConnection(QString src, QString sgn, QString dst, QString slt, bool q=false, bool head=false)
Adds a connection to the sheet structure.
Maintains connections between objects.