examples/graphicsview/elasticnodes/node.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.
    11 **
    12 ** You may use this file in accordance with the terms and conditions
    12 ** "Redistribution and use in source and binary forms, with or without
    13 ** contained in the Technology Preview License Agreement accompanying
    13 ** modification, are permitted provided that the following conditions are
    14 ** this package.
    14 ** met:
    15 **
    15 **   * Redistributions of source code must retain the above copyright
    16 ** GNU Lesser General Public License Usage
    16 **     notice, this list of conditions and the following disclaimer.
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
    17 **   * Redistributions in binary form must reproduce the above copyright
    18 ** General Public License version 2.1 as published by the Free Software
    18 **     notice, this list of conditions and the following disclaimer in
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
    19 **     the documentation and/or other materials provided with the
    20 ** packaging of this file.  Please review the following information to
    20 **     distribution.
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    22 **     the names of its contributors may be used to endorse or promote
    23 **
    23 **     products derived from this software without specific prior written
    24 ** In addition, as a special exception, Nokia gives you certain additional
    24 **     permission.
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    25 **
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    27 **
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    28 ** If you have questions regarding the use of this file, please contact
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    29 ** Nokia at qt-info@nokia.com.
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    30 **
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    31 **
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    32 **
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    33 **
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    34 **
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    35 **
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    36 **
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
    37 **
       
    38 ** $QT_END_LICENSE$
    37 ** $QT_END_LICENSE$
    39 **
    38 **
    40 ****************************************************************************/
    39 ****************************************************************************/
    41 
    40 
    42 #include <QGraphicsScene>
    41 #include <QGraphicsScene>
    46 
    45 
    47 #include "edge.h"
    46 #include "edge.h"
    48 #include "node.h"
    47 #include "node.h"
    49 #include "graphwidget.h"
    48 #include "graphwidget.h"
    50 
    49 
       
    50 //! [0]
    51 Node::Node(GraphWidget *graphWidget)
    51 Node::Node(GraphWidget *graphWidget)
    52     : graph(graphWidget)
    52     : graph(graphWidget)
    53 {
    53 {
    54     setFlag(ItemIsMovable);
    54     setFlag(ItemIsMovable);
    55     setFlag(ItemSendsGeometryChanges);
    55     setFlag(ItemSendsGeometryChanges);
    56     setCacheMode(DeviceCoordinateCache);
    56     setCacheMode(DeviceCoordinateCache);
    57     setZValue(-1);
    57     setZValue(-1);
    58 }
    58 }
    59 
    59 //! [0]
       
    60 
       
    61 //! [1]
    60 void Node::addEdge(Edge *edge)
    62 void Node::addEdge(Edge *edge)
    61 {
    63 {
    62     edgeList << edge;
    64     edgeList << edge;
    63     edge->adjust();
    65     edge->adjust();
    64 }
    66 }
    65 
    67 
    66 QList<Edge *> Node::edges() const
    68 QList<Edge *> Node::edges() const
    67 {
    69 {
    68     return edgeList;
    70     return edgeList;
    69 }
    71 }
    70 
    72 //! [1]
       
    73 
       
    74 //! [2]
    71 void Node::calculateForces()
    75 void Node::calculateForces()
    72 {
    76 {
    73     if (!scene() || scene()->mouseGrabberItem() == this) {
    77     if (!scene() || scene()->mouseGrabberItem() == this) {
    74         newPos = pos();
    78         newPos = pos();
    75         return;
    79         return;
    76     }
    80     }
    77     
    81 //! [2]
       
    82 
       
    83 //! [3]
    78     // Sum up all forces pushing this item away
    84     // Sum up all forces pushing this item away
    79     qreal xvel = 0;
    85     qreal xvel = 0;
    80     qreal yvel = 0;
    86     qreal yvel = 0;
    81     foreach (QGraphicsItem *item, scene()->items()) {
    87     foreach (QGraphicsItem *item, scene()->items()) {
    82         Node *node = qgraphicsitem_cast<Node *>(item);
    88         Node *node = qgraphicsitem_cast<Node *>(item);
    83         if (!node)
    89         if (!node)
    84             continue;
    90             continue;
    85 
    91 
    86         QLineF line(mapFromItem(node, 0, 0), QPointF(0, 0));
    92         QPointF vec = mapToItem(node, 0, 0);
    87         qreal dx = line.dx();
    93         qreal dx = vec.x();
    88         qreal dy = line.dy();
    94         qreal dy = vec.y();
    89         double l = 2.0 * (dx * dx + dy * dy);
    95         double l = 2.0 * (dx * dx + dy * dy);
    90         if (l > 0) {
    96         if (l > 0) {
    91             xvel += (dx * 150.0) / l;
    97             xvel += (dx * 150.0) / l;
    92             yvel += (dy * 150.0) / l;
    98             yvel += (dy * 150.0) / l;
    93         }
    99         }
    94     }
   100     }
    95 
   101 //! [3]
       
   102 
       
   103 //! [4]
    96     // Now subtract all forces pulling items together
   104     // Now subtract all forces pulling items together
    97     double weight = (edgeList.size() + 1) * 10;
   105     double weight = (edgeList.size() + 1) * 10;
    98     foreach (Edge *edge, edgeList) {
   106     foreach (Edge *edge, edgeList) {
    99         QPointF pos;
   107         QPointF vec;
   100         if (edge->sourceNode() == this)
   108         if (edge->sourceNode() == this)
   101             pos = mapFromItem(edge->destNode(), 0, 0);
   109             vec = mapToItem(edge->destNode(), 0, 0);
   102         else
   110         else
   103             pos = mapFromItem(edge->sourceNode(), 0, 0);
   111             vec = mapToItem(edge->sourceNode(), 0, 0);
   104         xvel += pos.x() / weight;
   112         xvel -= vec.x() / weight;
   105         yvel += pos.y() / weight;
   113         yvel -= vec.y() / weight;
   106     }
   114     }
   107     
   115 //! [4]
       
   116 
       
   117 //! [5]
   108     if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
   118     if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
   109         xvel = yvel = 0;
   119         xvel = yvel = 0;
   110 
   120 //! [5]
       
   121 
       
   122 //! [6]
   111     QRectF sceneRect = scene()->sceneRect();
   123     QRectF sceneRect = scene()->sceneRect();
   112     newPos = pos() + QPointF(xvel, yvel);
   124     newPos = pos() + QPointF(xvel, yvel);
   113     newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
   125     newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
   114     newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
   126     newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
   115 }
   127 }
   116 
   128 //! [6]
       
   129 
       
   130 //! [7]
   117 bool Node::advance()
   131 bool Node::advance()
   118 {
   132 {
   119     if (newPos == pos())
   133     if (newPos == pos())
   120         return false;
   134         return false;
   121 
   135 
   122     setPos(newPos);
   136     setPos(newPos);
   123     return true;
   137     return true;
   124 }
   138 }
   125 
   139 //! [7]
       
   140 
       
   141 //! [8]
   126 QRectF Node::boundingRect() const
   142 QRectF Node::boundingRect() const
   127 {
   143 {
   128     qreal adjust = 2;
   144     qreal adjust = 2;
   129     return QRectF(-10 - adjust, -10 - adjust,
   145     return QRectF(-10 - adjust, -10 - adjust,
   130                   23 + adjust, 23 + adjust);
   146                   23 + adjust, 23 + adjust);
   131 }
   147 }
   132 
   148 //! [8]
       
   149 
       
   150 //! [9]
   133 QPainterPath Node::shape() const
   151 QPainterPath Node::shape() const
   134 {
   152 {
   135     QPainterPath path;
   153     QPainterPath path;
   136     path.addEllipse(-10, -10, 20, 20);
   154     path.addEllipse(-10, -10, 20, 20);
   137     return path;
   155     return path;
   138 }
   156 }
   139 
   157 //! [9]
       
   158 
       
   159 //! [10]
   140 void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
   160 void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
   141 {
   161 {
   142     painter->setPen(Qt::NoPen);
   162     painter->setPen(Qt::NoPen);
   143     painter->setBrush(Qt::darkGray);
   163     painter->setBrush(Qt::darkGray);
   144     painter->drawEllipse(-7, -7, 20, 20);
   164     painter->drawEllipse(-7, -7, 20, 20);
   155     }
   175     }
   156     painter->setBrush(gradient);
   176     painter->setBrush(gradient);
   157     painter->setPen(QPen(Qt::black, 0));
   177     painter->setPen(QPen(Qt::black, 0));
   158     painter->drawEllipse(-10, -10, 20, 20);
   178     painter->drawEllipse(-10, -10, 20, 20);
   159 }
   179 }
   160 
   180 //! [10]
       
   181 
       
   182 //! [11]
   161 QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
   183 QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
   162 {
   184 {
   163     switch (change) {
   185     switch (change) {
   164     case ItemPositionHasChanged:
   186     case ItemPositionHasChanged:
   165         foreach (Edge *edge, edgeList)
   187         foreach (Edge *edge, edgeList)
   170         break;
   192         break;
   171     };
   193     };
   172 
   194 
   173     return QGraphicsItem::itemChange(change, value);
   195     return QGraphicsItem::itemChange(change, value);
   174 }
   196 }
   175 
   197 //! [11]
       
   198 
       
   199 //! [12]
   176 void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
   200 void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
   177 {
   201 {
   178     update();
   202     update();
   179     QGraphicsItem::mousePressEvent(event);
   203     QGraphicsItem::mousePressEvent(event);
   180 }
   204 }
   182 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
   206 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
   183 {
   207 {
   184     update();
   208     update();
   185     QGraphicsItem::mouseReleaseEvent(event);
   209     QGraphicsItem::mouseReleaseEvent(event);
   186 }
   210 }
       
   211 //! [12]