examples/qvdesigner0.2/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)
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     setZValue((myFromNode->zValue() > myToNode->zValue()) ? myFromNode->zValue()+1 : myToNode->zValue()+1);
00039 
00040     trackNodes();
00041 }
00042 
00043 Link::~Link()
00044 {
00045     myFromNode->removeLink(this);
00046     myToNode->removeLink(this);
00047 }
00048 
00049 Node *Link::fromNode() const
00050 {
00051     return myFromNode;
00052 }
00053 
00054 Node *Link::toNode() const
00055 {
00056     return myToNode;
00057 }
00058 
00059 QString Link::fromProp() const
00060 {
00061         return myFromProp;
00062 }
00063 
00064 QString Link::toProp() const
00065 {
00066         return myToProp;
00067 }
00068 
00069 void Link::setColor(const QColor &color)
00070 {
00071     setPen(QPen(color, 1.0));
00072 }
00073 
00074 QColor Link::color() const
00075 {
00076     return pen().color();
00077 }
00078 
00079 
00080 void Link::trackNodes()
00081 {
00082         // actualiza el zValue al máximo de los dos, o al del mayor grupo al que pertenecen
00083         // reducimos el tamaño del Pen del link en función del número de antecesores (*0.5 por cada uno)
00084         //(solo se mira para uno pq no se permiten enlaces entre grupos)
00085         setZValue((myFromNode->zValue() > myToNode->zValue()) ? myFromNode->zValue() + 1 : myToNode->zValue() + 1);
00086 
00087         QGraphicsItem *ancestor = myFromNode->parentItem();
00088         QPen myPen = pen();
00089     while (ancestor) {
00090                 setZValue(ancestor->zValue() + 1);
00091                 myPen.setWidthF(myPen.width() * 0.4);
00092                 ancestor = ancestor->parentItem();
00093         }
00094         setPen(myPen);
00095 
00096 
00097         setLine( QLineF(myFromNode->scenePointPos(myFromProp, FALSE), myToNode->scenePointPos(myToProp, TRUE)) );
00098 }
00099