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

source code of qtbase/examples/widgets/graphicsview/elasticnodes/node.cpp