1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
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.
|
---|
25 | **
|
---|
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."
|
---|
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 |
|
---|
50 | //! [0]
|
---|
51 | Node::Node(GraphWidget *graphWidget)
|
---|
52 | : graph(graphWidget)
|
---|
53 | {
|
---|
54 | setFlag(ItemIsMovable);
|
---|
55 | setFlag(ItemSendsGeometryChanges);
|
---|
56 | setCacheMode(DeviceCoordinateCache);
|
---|
57 | setZValue(-1);
|
---|
58 | }
|
---|
59 | //! [0]
|
---|
60 |
|
---|
61 | //! [1]
|
---|
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 | }
|
---|
72 | //! [1]
|
---|
73 |
|
---|
74 | //! [2]
|
---|
75 | void Node::calculateForces()
|
---|
76 | {
|
---|
77 | if (!scene() || scene()->mouseGrabberItem() == this) {
|
---|
78 | newPos = pos();
|
---|
79 | return;
|
---|
80 | }
|
---|
81 | //! [2]
|
---|
82 |
|
---|
83 | //! [3]
|
---|
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 |
|
---|
92 | QPointF vec = mapToItem(node, 0, 0);
|
---|
93 | qreal dx = vec.x();
|
---|
94 | qreal dy = vec.y();
|
---|
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 | }
|
---|
101 | //! [3]
|
---|
102 |
|
---|
103 | //! [4]
|
---|
104 | // Now subtract all forces pulling items together
|
---|
105 | double weight = (edgeList.size() + 1) * 10;
|
---|
106 | foreach (Edge *edge, edgeList) {
|
---|
107 | QPointF vec;
|
---|
108 | if (edge->sourceNode() == this)
|
---|
109 | vec = mapToItem(edge->destNode(), 0, 0);
|
---|
110 | else
|
---|
111 | vec = mapToItem(edge->sourceNode(), 0, 0);
|
---|
112 | xvel -= vec.x() / weight;
|
---|
113 | yvel -= vec.y() / weight;
|
---|
114 | }
|
---|
115 | //! [4]
|
---|
116 |
|
---|
117 | //! [5]
|
---|
118 | if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
|
---|
119 | xvel = yvel = 0;
|
---|
120 | //! [5]
|
---|
121 |
|
---|
122 | //! [6]
|
---|
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 | }
|
---|
128 | //! [6]
|
---|
129 |
|
---|
130 | //! [7]
|
---|
131 | bool Node::advance()
|
---|
132 | {
|
---|
133 | if (newPos == pos())
|
---|
134 | return false;
|
---|
135 |
|
---|
136 | setPos(newPos);
|
---|
137 | return true;
|
---|
138 | }
|
---|
139 | //! [7]
|
---|
140 |
|
---|
141 | //! [8]
|
---|
142 | QRectF Node::boundingRect() const
|
---|
143 | {
|
---|
144 | qreal adjust = 2;
|
---|
145 | return QRectF(-10 - adjust, -10 - adjust,
|
---|
146 | 23 + adjust, 23 + adjust);
|
---|
147 | }
|
---|
148 | //! [8]
|
---|
149 |
|
---|
150 | //! [9]
|
---|
151 | QPainterPath Node::shape() const
|
---|
152 | {
|
---|
153 | QPainterPath path;
|
---|
154 | path.addEllipse(-10, -10, 20, 20);
|
---|
155 | return path;
|
---|
156 | }
|
---|
157 | //! [9]
|
---|
158 |
|
---|
159 | //! [10]
|
---|
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 | }
|
---|
180 | //! [10]
|
---|
181 |
|
---|
182 | //! [11]
|
---|
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 | }
|
---|
197 | //! [11]
|
---|
198 |
|
---|
199 | //! [12]
|
---|
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 | }
|
---|
211 | //! [12]
|
---|