| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QGraphicsScene>
|
|---|
| 43 | #include <QGraphicsSceneMouseEvent>
|
|---|
| 44 | #include <QPainter>
|
|---|
| 45 | #include <QStyleOption>
|
|---|
| 46 |
|
|---|
| 47 | #include "edge.h"
|
|---|
| 48 | #include "node.h"
|
|---|
| 49 | #include "graphwidget.h"
|
|---|
| 50 |
|
|---|
| 51 | Node::Node(GraphWidget *graphWidget)
|
|---|
| 52 | : graph(graphWidget)
|
|---|
| 53 | {
|
|---|
| 54 | setFlag(ItemIsMovable);
|
|---|
| 55 | setCacheMode(DeviceCoordinateCache);
|
|---|
| 56 | setZValue(1);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void Node::addEdge(Edge *edge)
|
|---|
| 60 | {
|
|---|
| 61 | edgeList << edge;
|
|---|
| 62 | edge->adjust();
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | QList<Edge *> Node::edges() const
|
|---|
| 66 | {
|
|---|
| 67 | return edgeList;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | void Node::calculateForces()
|
|---|
| 71 | {
|
|---|
| 72 | if (!scene() || scene()->mouseGrabberItem() == this) {
|
|---|
| 73 | newPos = pos();
|
|---|
| 74 | return;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | // Sum up all forces pushing this item away
|
|---|
| 78 | qreal xvel = 0;
|
|---|
| 79 | qreal yvel = 0;
|
|---|
| 80 | foreach (QGraphicsItem *item, scene()->items()) {
|
|---|
| 81 | Node *node = qgraphicsitem_cast<Node *>(item);
|
|---|
| 82 | if (!node)
|
|---|
| 83 | continue;
|
|---|
| 84 |
|
|---|
| 85 | QLineF line(mapFromItem(node, 0, 0), QPointF(0, 0));
|
|---|
| 86 | qreal dx = line.dx();
|
|---|
| 87 | qreal dy = line.dy();
|
|---|
| 88 | double l = 2.0 * (dx * dx + dy * dy);
|
|---|
| 89 | if (l > 0) {
|
|---|
| 90 | xvel += (dx * 150.0) / l;
|
|---|
| 91 | yvel += (dy * 150.0) / l;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | // Now subtract all forces pulling items together
|
|---|
| 96 | double weight = (edgeList.size() + 1) * 10;
|
|---|
| 97 | foreach (Edge *edge, edgeList) {
|
|---|
| 98 | QPointF pos;
|
|---|
| 99 | if (edge->sourceNode() == this)
|
|---|
| 100 | pos = mapFromItem(edge->destNode(), 0, 0);
|
|---|
| 101 | else
|
|---|
| 102 | pos = mapFromItem(edge->sourceNode(), 0, 0);
|
|---|
| 103 | xvel += pos.x() / weight;
|
|---|
| 104 | yvel += pos.y() / weight;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
|
|---|
| 108 | xvel = yvel = 0;
|
|---|
| 109 |
|
|---|
| 110 | QRectF sceneRect = scene()->sceneRect();
|
|---|
| 111 | newPos = pos() + QPointF(xvel, yvel);
|
|---|
| 112 | newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
|
|---|
| 113 | newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | bool Node::advance()
|
|---|
| 117 | {
|
|---|
| 118 | if (newPos == pos())
|
|---|
| 119 | return false;
|
|---|
| 120 |
|
|---|
| 121 | setPos(newPos);
|
|---|
| 122 | return true;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | QRectF Node::boundingRect() const
|
|---|
| 126 | {
|
|---|
| 127 | qreal adjust = 2;
|
|---|
| 128 | return QRectF(-10 - adjust, -10 - adjust,
|
|---|
| 129 | 23 + adjust, 23 + adjust);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | QPainterPath Node::shape() const
|
|---|
| 133 | {
|
|---|
| 134 | QPainterPath path;
|
|---|
| 135 | path.addEllipse(-10, -10, 20, 20);
|
|---|
| 136 | return path;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
|
|---|
| 140 | {
|
|---|
| 141 | painter->setPen(Qt::NoPen);
|
|---|
| 142 | painter->setBrush(Qt::darkGray);
|
|---|
| 143 | painter->drawEllipse(-7, -7, 20, 20);
|
|---|
| 144 |
|
|---|
| 145 | QRadialGradient gradient(-3, -3, 10);
|
|---|
| 146 | if (option->state & QStyle::State_Sunken) {
|
|---|
| 147 | gradient.setCenter(3, 3);
|
|---|
| 148 | gradient.setFocalPoint(3, 3);
|
|---|
| 149 | gradient.setColorAt(1, QColor(Qt::yellow).light(120));
|
|---|
| 150 | gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
|
|---|
| 151 | } else {
|
|---|
| 152 | gradient.setColorAt(0, Qt::yellow);
|
|---|
| 153 | gradient.setColorAt(1, Qt::darkYellow);
|
|---|
| 154 | }
|
|---|
| 155 | painter->setBrush(gradient);
|
|---|
| 156 | painter->setPen(QPen(Qt::black, 0));
|
|---|
| 157 | painter->drawEllipse(-10, -10, 20, 20);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
|
|---|
| 161 | {
|
|---|
| 162 | switch (change) {
|
|---|
| 163 | case ItemPositionHasChanged:
|
|---|
| 164 | foreach (Edge *edge, edgeList)
|
|---|
| 165 | edge->adjust();
|
|---|
| 166 | graph->itemMoved();
|
|---|
| 167 | break;
|
|---|
| 168 | default:
|
|---|
| 169 | break;
|
|---|
| 170 | };
|
|---|
| 171 |
|
|---|
| 172 | return QGraphicsItem::itemChange(change, value);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|---|
| 176 | {
|
|---|
| 177 | update();
|
|---|
| 178 | QGraphicsItem::mousePressEvent(event);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|---|
| 182 | {
|
|---|
| 183 | update();
|
|---|
| 184 | QGraphicsItem::mouseReleaseEvent(event);
|
|---|
| 185 | }
|
|---|