[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 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.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QGraphicsScene>
|
---|
| 42 | #include <QGraphicsSceneMouseEvent>
|
---|
| 43 | #include <QPainter>
|
---|
| 44 | #include <QStyleOption>
|
---|
| 45 |
|
---|
| 46 | #include "edge.h"
|
---|
| 47 | #include "node.h"
|
---|
| 48 | #include "graphwidget.h"
|
---|
| 49 |
|
---|
[846] | 50 | //! [0]
|
---|
[2] | 51 | Node::Node(GraphWidget *graphWidget)
|
---|
| 52 | : graph(graphWidget)
|
---|
| 53 | {
|
---|
| 54 | setFlag(ItemIsMovable);
|
---|
[561] | 55 | setFlag(ItemSendsGeometryChanges);
|
---|
[2] | 56 | setCacheMode(DeviceCoordinateCache);
|
---|
[561] | 57 | setZValue(-1);
|
---|
[2] | 58 | }
|
---|
[846] | 59 | //! [0]
|
---|
[2] | 60 |
|
---|
[846] | 61 | //! [1]
|
---|
[2] | 62 | void Node::addEdge(Edge *edge)
|
---|
| 63 | {
|
---|
| 64 | edgeList << edge;
|
---|
| 65 | edge->adjust();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | QList<Edge *> Node::edges() const
|
---|
| 69 | {
|
---|
| 70 | return edgeList;
|
---|
| 71 | }
|
---|
[846] | 72 | //! [1]
|
---|
[2] | 73 |
|
---|
[846] | 74 | //! [2]
|
---|
[2] | 75 | void Node::calculateForces()
|
---|
| 76 | {
|
---|
| 77 | if (!scene() || scene()->mouseGrabberItem() == this) {
|
---|
| 78 | newPos = pos();
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
[846] | 81 | //! [2]
|
---|
| 82 |
|
---|
| 83 | //! [3]
|
---|
[2] | 84 | // Sum up all forces pushing this item away
|
---|
| 85 | qreal xvel = 0;
|
---|
| 86 | qreal yvel = 0;
|
---|
| 87 | foreach (QGraphicsItem *item, scene()->items()) {
|
---|
| 88 | Node *node = qgraphicsitem_cast<Node *>(item);
|
---|
| 89 | if (!node)
|
---|
| 90 | continue;
|
---|
| 91 |
|
---|
[846] | 92 | QPointF vec = mapToItem(node, 0, 0);
|
---|
| 93 | qreal dx = vec.x();
|
---|
| 94 | qreal dy = vec.y();
|
---|
[2] | 95 | double l = 2.0 * (dx * dx + dy * dy);
|
---|
| 96 | if (l > 0) {
|
---|
| 97 | xvel += (dx * 150.0) / l;
|
---|
| 98 | yvel += (dy * 150.0) / l;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
[846] | 101 | //! [3]
|
---|
[2] | 102 |
|
---|
[846] | 103 | //! [4]
|
---|
[2] | 104 | // Now subtract all forces pulling items together
|
---|
| 105 | double weight = (edgeList.size() + 1) * 10;
|
---|
| 106 | foreach (Edge *edge, edgeList) {
|
---|
[846] | 107 | QPointF vec;
|
---|
[2] | 108 | if (edge->sourceNode() == this)
|
---|
[846] | 109 | vec = mapToItem(edge->destNode(), 0, 0);
|
---|
[2] | 110 | else
|
---|
[846] | 111 | vec = mapToItem(edge->sourceNode(), 0, 0);
|
---|
| 112 | xvel -= vec.x() / weight;
|
---|
| 113 | yvel -= vec.y() / weight;
|
---|
[2] | 114 | }
|
---|
[846] | 115 | //! [4]
|
---|
| 116 |
|
---|
| 117 | //! [5]
|
---|
[2] | 118 | if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
|
---|
| 119 | xvel = yvel = 0;
|
---|
[846] | 120 | //! [5]
|
---|
[2] | 121 |
|
---|
[846] | 122 | //! [6]
|
---|
[2] | 123 | QRectF sceneRect = scene()->sceneRect();
|
---|
| 124 | newPos = pos() + QPointF(xvel, yvel);
|
---|
| 125 | newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
|
---|
| 126 | newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
|
---|
| 127 | }
|
---|
[846] | 128 | //! [6]
|
---|
[2] | 129 |
|
---|
[846] | 130 | //! [7]
|
---|
[2] | 131 | bool Node::advance()
|
---|
| 132 | {
|
---|
| 133 | if (newPos == pos())
|
---|
| 134 | return false;
|
---|
| 135 |
|
---|
| 136 | setPos(newPos);
|
---|
| 137 | return true;
|
---|
| 138 | }
|
---|
[846] | 139 | //! [7]
|
---|
[2] | 140 |
|
---|
[846] | 141 | //! [8]
|
---|
[2] | 142 | QRectF Node::boundingRect() const
|
---|
| 143 | {
|
---|
| 144 | qreal adjust = 2;
|
---|
| 145 | return QRectF(-10 - adjust, -10 - adjust,
|
---|
| 146 | 23 + adjust, 23 + adjust);
|
---|
| 147 | }
|
---|
[846] | 148 | //! [8]
|
---|
[2] | 149 |
|
---|
[846] | 150 | //! [9]
|
---|
[2] | 151 | QPainterPath Node::shape() const
|
---|
| 152 | {
|
---|
| 153 | QPainterPath path;
|
---|
| 154 | path.addEllipse(-10, -10, 20, 20);
|
---|
| 155 | return path;
|
---|
| 156 | }
|
---|
[846] | 157 | //! [9]
|
---|
[2] | 158 |
|
---|
[846] | 159 | //! [10]
|
---|
[2] | 160 | void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
|
---|
| 161 | {
|
---|
| 162 | painter->setPen(Qt::NoPen);
|
---|
| 163 | painter->setBrush(Qt::darkGray);
|
---|
| 164 | painter->drawEllipse(-7, -7, 20, 20);
|
---|
| 165 |
|
---|
| 166 | QRadialGradient gradient(-3, -3, 10);
|
---|
| 167 | if (option->state & QStyle::State_Sunken) {
|
---|
| 168 | gradient.setCenter(3, 3);
|
---|
| 169 | gradient.setFocalPoint(3, 3);
|
---|
| 170 | gradient.setColorAt(1, QColor(Qt::yellow).light(120));
|
---|
| 171 | gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
|
---|
| 172 | } else {
|
---|
| 173 | gradient.setColorAt(0, Qt::yellow);
|
---|
| 174 | gradient.setColorAt(1, Qt::darkYellow);
|
---|
| 175 | }
|
---|
| 176 | painter->setBrush(gradient);
|
---|
| 177 | painter->setPen(QPen(Qt::black, 0));
|
---|
| 178 | painter->drawEllipse(-10, -10, 20, 20);
|
---|
| 179 | }
|
---|
[846] | 180 | //! [10]
|
---|
[2] | 181 |
|
---|
[846] | 182 | //! [11]
|
---|
[2] | 183 | QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
|
---|
| 184 | {
|
---|
| 185 | switch (change) {
|
---|
| 186 | case ItemPositionHasChanged:
|
---|
| 187 | foreach (Edge *edge, edgeList)
|
---|
| 188 | edge->adjust();
|
---|
| 189 | graph->itemMoved();
|
---|
| 190 | break;
|
---|
| 191 | default:
|
---|
| 192 | break;
|
---|
| 193 | };
|
---|
| 194 |
|
---|
| 195 | return QGraphicsItem::itemChange(change, value);
|
---|
| 196 | }
|
---|
[846] | 197 | //! [11]
|
---|
[2] | 198 |
|
---|
[846] | 199 | //! [12]
|
---|
[2] | 200 | void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
---|
| 201 | {
|
---|
| 202 | update();
|
---|
| 203 | QGraphicsItem::mousePressEvent(event);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
---|
| 207 | {
|
---|
| 208 | update();
|
---|
| 209 | QGraphicsItem::mouseReleaseEvent(event);
|
---|
| 210 | }
|
---|
[846] | 211 | //! [12]
|
---|