examples/graphicsview/elasticnodes/edge.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QPainter>
       
    43 
       
    44 #include "edge.h"
       
    45 #include "node.h"
       
    46 
       
    47 #include <math.h>
       
    48 
       
    49 static const double Pi = 3.14159265358979323846264338327950288419717;
       
    50 static double TwoPi = 2.0 * Pi;
       
    51 
       
    52 Edge::Edge(Node *sourceNode, Node *destNode)
       
    53     : arrowSize(10)
       
    54 {
       
    55     setAcceptedMouseButtons(0);
       
    56     source = sourceNode;
       
    57     dest = destNode;
       
    58     source->addEdge(this);
       
    59     dest->addEdge(this);
       
    60     adjust();
       
    61 }
       
    62 
       
    63 Edge::~Edge()
       
    64 {
       
    65 }
       
    66 
       
    67 Node *Edge::sourceNode() const
       
    68 {
       
    69     return source;
       
    70 }
       
    71 
       
    72 void Edge::setSourceNode(Node *node)
       
    73 {
       
    74     source = node;
       
    75     adjust();
       
    76 }
       
    77 
       
    78 Node *Edge::destNode() const
       
    79 {
       
    80     return dest;
       
    81 }
       
    82 
       
    83 void Edge::setDestNode(Node *node)
       
    84 {
       
    85     dest = node;
       
    86     adjust();
       
    87 }
       
    88 
       
    89 void Edge::adjust()
       
    90 {
       
    91     if (!source || !dest)
       
    92         return;
       
    93 
       
    94     QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
       
    95     qreal length = line.length();
       
    96 
       
    97     prepareGeometryChange();
       
    98 
       
    99     if (length > qreal(20.)) {
       
   100         QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
       
   101         sourcePoint = line.p1() + edgeOffset;
       
   102         destPoint = line.p2() - edgeOffset;
       
   103     } else {
       
   104         sourcePoint = destPoint = line.p1();
       
   105     }
       
   106 }
       
   107 
       
   108 QRectF Edge::boundingRect() const
       
   109 {
       
   110     if (!source || !dest)
       
   111         return QRectF();
       
   112 
       
   113     qreal penWidth = 1;
       
   114     qreal extra = (penWidth + arrowSize) / 2.0;
       
   115 
       
   116     return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
       
   117                                       destPoint.y() - sourcePoint.y()))
       
   118         .normalized()
       
   119         .adjusted(-extra, -extra, extra, extra);
       
   120 }
       
   121 
       
   122 void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
       
   123 {
       
   124     if (!source || !dest)
       
   125         return;
       
   126 
       
   127     QLineF line(sourcePoint, destPoint);
       
   128     if (qFuzzyCompare(line.length(), qreal(0.)))
       
   129         return;
       
   130 
       
   131     // Draw the line itself
       
   132     painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
       
   133     painter->drawLine(line);
       
   134 
       
   135     // Draw the arrows
       
   136     double angle = ::acos(line.dx() / line.length());
       
   137     if (line.dy() >= 0)
       
   138         angle = TwoPi - angle;
       
   139 
       
   140     QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize,
       
   141                                                   cos(angle + Pi / 3) * arrowSize);
       
   142     QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
       
   143                                                   cos(angle + Pi - Pi / 3) * arrowSize);   
       
   144     QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize,
       
   145                                               cos(angle - Pi / 3) * arrowSize);
       
   146     QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize,
       
   147                                               cos(angle - Pi + Pi / 3) * arrowSize);
       
   148 
       
   149     painter->setBrush(Qt::black);
       
   150     painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
       
   151     painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);        
       
   152 }