examples/graphicsview/elasticnodes/edge.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the examples of the Qt Toolkit.
     7 ** This file is part of the examples of the Qt Toolkit.
     8 **
     8 **
     9 ** $QT_BEGIN_LICENSE:LGPL$
     9 ** $QT_BEGIN_LICENSE:BSD$
    10 ** No Commercial Usage
    10 ** You may use this file under the terms of the BSD license as follows:
    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 **
    11 **
    16 ** GNU Lesser General Public License Usage
    12 ** "Redistribution and use in source and binary forms, with or without
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
    13 ** modification, are permitted provided that the following conditions are
    18 ** General Public License version 2.1 as published by the Free Software
    14 ** met:
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
    15 **   * Redistributions of source code must retain the above copyright
    20 ** packaging of this file.  Please review the following information to
    16 **     notice, this list of conditions and the following disclaimer.
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
    17 **   * Redistributions in binary form must reproduce the above copyright
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    18 **     notice, this list of conditions and the following disclaimer in
       
    19 **     the documentation and/or other materials provided with the
       
    20 **     distribution.
       
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
       
    22 **     the names of its contributors may be used to endorse or promote
       
    23 **     products derived from this software without specific prior written
       
    24 **     permission.
    23 **
    25 **
    24 ** In addition, as a special exception, Nokia gives you certain additional
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    27 **
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    28 ** If you have questions regarding the use of this file, please contact
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    29 ** Nokia at qt-info@nokia.com.
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    30 **
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    31 **
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    32 **
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    33 **
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    34 **
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
    37 ** $QT_END_LICENSE$
    39 **
    38 **
    40 ****************************************************************************/
    39 ****************************************************************************/
    41 
    40 
    42 #include <QPainter>
    41 #include <QPainter>
    47 #include <math.h>
    46 #include <math.h>
    48 
    47 
    49 static const double Pi = 3.14159265358979323846264338327950288419717;
    48 static const double Pi = 3.14159265358979323846264338327950288419717;
    50 static double TwoPi = 2.0 * Pi;
    49 static double TwoPi = 2.0 * Pi;
    51 
    50 
       
    51 //! [0]
    52 Edge::Edge(Node *sourceNode, Node *destNode)
    52 Edge::Edge(Node *sourceNode, Node *destNode)
    53     : arrowSize(10)
    53     : arrowSize(10)
    54 {
    54 {
    55     setAcceptedMouseButtons(0);
    55     setAcceptedMouseButtons(0);
    56     source = sourceNode;
    56     source = sourceNode;
    57     dest = destNode;
    57     dest = destNode;
    58     source->addEdge(this);
    58     source->addEdge(this);
    59     dest->addEdge(this);
    59     dest->addEdge(this);
    60     adjust();
    60     adjust();
    61 }
    61 }
       
    62 //! [0]
    62 
    63 
    63 Edge::~Edge()
    64 //! [1]
    64 {
       
    65 }
       
    66 
       
    67 Node *Edge::sourceNode() const
    65 Node *Edge::sourceNode() const
    68 {
    66 {
    69     return source;
    67     return source;
    70 }
       
    71 
       
    72 void Edge::setSourceNode(Node *node)
       
    73 {
       
    74     source = node;
       
    75     adjust();
       
    76 }
    68 }
    77 
    69 
    78 Node *Edge::destNode() const
    70 Node *Edge::destNode() const
    79 {
    71 {
    80     return dest;
    72     return dest;
    81 }
    73 }
       
    74 //! [1]
    82 
    75 
    83 void Edge::setDestNode(Node *node)
    76 //! [2]
    84 {
       
    85     dest = node;
       
    86     adjust();
       
    87 }
       
    88 
       
    89 void Edge::adjust()
    77 void Edge::adjust()
    90 {
    78 {
    91     if (!source || !dest)
    79     if (!source || !dest)
    92         return;
    80         return;
    93 
    81 
   102         destPoint = line.p2() - edgeOffset;
    90         destPoint = line.p2() - edgeOffset;
   103     } else {
    91     } else {
   104         sourcePoint = destPoint = line.p1();
    92         sourcePoint = destPoint = line.p1();
   105     }
    93     }
   106 }
    94 }
       
    95 //! [2]
   107 
    96 
       
    97 //! [3]
   108 QRectF Edge::boundingRect() const
    98 QRectF Edge::boundingRect() const
   109 {
    99 {
   110     if (!source || !dest)
   100     if (!source || !dest)
   111         return QRectF();
   101         return QRectF();
   112 
   102 
   116     return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
   106     return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
   117                                       destPoint.y() - sourcePoint.y()))
   107                                       destPoint.y() - sourcePoint.y()))
   118         .normalized()
   108         .normalized()
   119         .adjusted(-extra, -extra, extra, extra);
   109         .adjusted(-extra, -extra, extra, extra);
   120 }
   110 }
       
   111 //! [3]
   121 
   112 
       
   113 //! [4]
   122 void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
   114 void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
   123 {
   115 {
   124     if (!source || !dest)
   116     if (!source || !dest)
   125         return;
   117         return;
   126 
   118 
   127     QLineF line(sourcePoint, destPoint);
   119     QLineF line(sourcePoint, destPoint);
   128     if (qFuzzyCompare(line.length(), qreal(0.)))
   120     if (qFuzzyCompare(line.length(), qreal(0.)))
   129         return;
   121         return;
       
   122 //! [4]
   130 
   123 
       
   124 //! [5]
   131     // Draw the line itself
   125     // Draw the line itself
   132     painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
   126     painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
   133     painter->drawLine(line);
   127     painter->drawLine(line);
       
   128 //! [5]
   134 
   129 
       
   130 //! [6]
   135     // Draw the arrows
   131     // Draw the arrows
   136     double angle = ::acos(line.dx() / line.length());
   132     double angle = ::acos(line.dx() / line.length());
   137     if (line.dy() >= 0)
   133     if (line.dy() >= 0)
   138         angle = TwoPi - angle;
   134         angle = TwoPi - angle;
   139 
   135 
   148 
   144 
   149     painter->setBrush(Qt::black);
   145     painter->setBrush(Qt::black);
   150     painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
   146     painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
   151     painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);        
   147     painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);        
   152 }
   148 }
       
   149 //! [6]