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] |
61 | Node::Node(GraphWidget *graphWidget) |
62 | : graph(graphWidget) |
63 | { |
64 | setFlag(ItemIsMovable); |
65 | setFlag(ItemSendsGeometryChanges); |
66 | setCacheMode(DeviceCoordinateCache); |
67 | setZValue(-1); |
68 | } |
69 | //! [0] |
70 | |
71 | //! [1] |
72 | void Node::addEdge(Edge *edge) |
73 | { |
74 | edgeList << edge; |
75 | edge->adjust(); |
76 | } |
77 | |
78 | QList<Edge *> Node::edges() const |
79 | { |
80 | return edgeList; |
81 | } |
82 | //! [1] |
83 | |
84 | //! [2] |
85 | void 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(node, 0, 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(edgeList)) { |
118 | QPointF vec; |
119 | if (edge->sourceNode() == this) |
120 | vec = mapToItem(edge->destNode(), 0, 0); |
121 | else |
122 | vec = mapToItem(edge->sourceNode(), 0, 0); |
123 | xvel -= vec.x() / weight; |
124 | yvel -= vec.y() / weight; |
125 | } |
126 | //! [4] |
127 | |
128 | //! [5] |
129 | if (qAbs(xvel) < 0.1 && qAbs(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(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10)); |
137 | newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10)); |
138 | } |
139 | //! [6] |
140 | |
141 | //! [7] |
142 | bool Node::advancePosition() |
143 | { |
144 | if (newPos == pos()) |
145 | return false; |
146 | |
147 | setPos(newPos); |
148 | return true; |
149 | } |
150 | //! [7] |
151 | |
152 | //! [8] |
153 | QRectF 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] |
161 | QPainterPath Node::shape() const |
162 | { |
163 | QPainterPath path; |
164 | path.addEllipse(-10, -10, 20, 20); |
165 | return path; |
166 | } |
167 | //! [9] |
168 | |
169 | //! [10] |
170 | void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) |
171 | { |
172 | painter->setPen(Qt::NoPen); |
173 | painter->setBrush(Qt::darkGray); |
174 | painter->drawEllipse(-7, -7, 20, 20); |
175 | |
176 | QRadialGradient gradient(-3, -3, 10); |
177 | if (option->state & QStyle::State_Sunken) { |
178 | gradient.setCenter(3, 3); |
179 | gradient.setFocalPoint(3, 3); |
180 | gradient.setColorAt(1, QColor(Qt::yellow).lighter(120)); |
181 | gradient.setColorAt(0, QColor(Qt::darkYellow).lighter(120)); |
182 | } else { |
183 | gradient.setColorAt(0, Qt::yellow); |
184 | gradient.setColorAt(1, Qt::darkYellow); |
185 | } |
186 | painter->setBrush(gradient); |
187 | |
188 | painter->setPen(QPen(Qt::black, 0)); |
189 | painter->drawEllipse(-10, -10, 20, 20); |
190 | } |
191 | //! [10] |
192 | |
193 | //! [11] |
194 | QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) |
195 | { |
196 | switch (change) { |
197 | case ItemPositionHasChanged: |
198 | for (Edge *edge : qAsConst(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] |
211 | void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) |
212 | { |
213 | update(); |
214 | QGraphicsItem::mousePressEvent(event); |
215 | } |
216 | |
217 | void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
218 | { |
219 | update(); |
220 | QGraphicsItem::mouseReleaseEvent(event); |
221 | } |
222 | //! [12] |
223 | |