PARP Research Group University of Murcia, Spain


src/qvgui/qvdesigner/slate/link.cpp

00001 /*
00002  *      Copyright (C) 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 
00022 #include <QtGui>
00023 
00024 #include "link.h"
00025 #include "node.h"
00026 
00027 Link::Link(Node *fromNode, QString fromProp, Node *toNode, QString toProp, QGraphicsItem * parent, QGraphicsScene * scene): QGraphicsLineItem(parent, scene), line2(this, scene), line3(this, scene), line4(this, scene), line5(this, scene)
00028 {
00029     myFromNode = fromNode;
00030     myToNode = toNode;
00031         myFromProp = fromProp;
00032         myToProp = toProp;
00033 
00034     myFromNode->addOutLink(this);
00035     myToNode->addInLink(this);
00036 
00037     setFlags(QGraphicsItem::ItemIsSelectable);
00038         line2.setFlags(QGraphicsItem::ItemIsSelectable);
00039         line3.setFlags(QGraphicsItem::ItemIsSelectable);
00040         line4.setFlags(QGraphicsItem::ItemIsSelectable);
00041         line5.setFlags(QGraphicsItem::ItemIsSelectable);
00042 
00043         setHandlesChildEvents(true);
00044 
00045     trackNodes();
00046 }
00047 
00048 Link::~Link()
00049 {
00050     myFromNode->removeLink(this);
00051     myToNode->removeLink(this);
00052 }
00053 
00054 Node *Link::fromNode() const
00055 {
00056     return myFromNode;
00057 }
00058 
00059 Node *Link::toNode() const
00060 {
00061     return myToNode;
00062 }
00063 
00064 QString Link::fromProp() const
00065 {
00066         return myFromProp;
00067 }
00068 
00069 QString Link::toProp() const
00070 {
00071         return myToProp;
00072 }
00073 
00074 void Link::setColor(const QColor &color)
00075 {
00076     setPen(QPen(color, 1.0));
00077 }
00078 
00079 QColor Link::color() const
00080 {
00081     return pen().color();
00082 }
00083 
00084 
00085 void Link::trackNodes()
00086 {
00087         // actualiza el zValue al máximo de los dos, o al del mayor grupo al que pertenecen
00088         // reducimos el tamaño del Pen del link en función del número de antecesores (*0.5 por cada uno)
00089         //(solo se mira para uno pq no se permiten enlaces entre grupos)
00090         double zvalue = (myFromNode->zValue() > myToNode->zValue()) ? myFromNode->zValue() + 1 : myToNode->zValue() + 1;
00091 
00092         // obtenemos el pen
00093         QGraphicsItem *ancestor = myFromNode->parentItem();
00094         QPen myPen = pen();
00095     while (ancestor) {
00096                 setZValue(ancestor->zValue() + 1);
00097                 myPen.setWidthF(myPen.width() * 0.4);
00098                 ancestor = ancestor->parentItem();
00099         }
00100 
00101         // obtenemos las posiciones origen y destino
00102         QPointF orig = myFromNode->scenePointPos(myFromProp, FALSE);
00103         QPointF dest = myToNode->scenePointPos(myToProp, TRUE);
00104 
00105         if (orig.x() <= dest.x()) {
00106                 setZValue(zvalue);
00107                 setPen(myPen);
00108                 setLine(QLineF(orig, dest));
00109                 line2.hide();
00110                 line3.hide();
00111                 line4.hide();
00112                 line5.hide();
00113         }
00114         else {
00115                 // obtenemos el desplazamiento lateral para esta propiedad dentro de sus nodos (para que no se solapen verticalmente)
00116                 double origDesp = 10.0 * (fromNode()->numProps() - fromNode()->propPoint(fromProp(), true) + 1);
00117                 double destDesp = 10.0 * (toNode()->numProps() - toNode()->propPoint(toProp(), true) + 1);
00118 
00119                 // obtenemos la coordenada Y de las partes inferior
00120                 double origBottom = /*fromNode()->scenePos().y() +*/ fromNode()->sceneBoundingRect().bottom() + origDesp;
00121                 double destBottom = /*toNode()->scenePos().y() +*/ toNode()->sceneBoundingRect().bottom() + destDesp;
00122                 double bottom = origBottom > destBottom ? origBottom : destBottom;
00123 
00124                 setZValue(zvalue);
00125                 line2.setZValue(zvalue);
00126                 line3.setZValue(zvalue);
00127                 line4.setZValue(zvalue);
00128                 line5.setZValue(zvalue);
00129                 setPen(myPen);
00130                 line2.setPen(myPen);
00131                 line3.setPen(myPen);
00132                 line4.setPen(myPen);
00133                 line5.setPen(myPen);
00134                 setLine(QLineF(orig, orig + QPointF(origDesp, 0.0)));
00135                 line2.setLine(QLineF(orig + QPointF(origDesp, 0.0), QPointF(orig.x() + origDesp, bottom)));
00136                 line3.setLine(QLineF(QPointF(orig.x() + origDesp, bottom), QPointF(dest.x() - destDesp, bottom)));
00137                 line4.setLine(QLineF(QPointF(dest.x() - destDesp, bottom), dest + QPointF(0.0 - destDesp, 0.0)));
00138                 line5.setLine(QLineF(dest + QPointF(0.0 - destDesp, 0.0), dest));
00139                 line2.show();
00140                 line3.show();
00141                 line4.show();
00142                 line5.show();
00143         }
00144 }
00145 
00146 bool Link::sceneEvent(QEvent * event)
00147 {
00148         setSelected(true);
00149         line2.setSelected(true);
00150         line3.setSelected(true);
00151         line4.setSelected(true);
00152         line5.setSelected(true);
00153 
00154         return true;
00155 }
00156 
00157 



QVision framework. PARP research group, copyright 2007, 2008.