1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <Standard_WarningsDisable.hxx>
42 #include <QGraphicsScene>
43 #include <QGraphicsSceneMouseEvent>
44 #include <QPainter>
45 #include <QStyleOption>
46 #include <Standard_WarningsRestore.hxx>
47
48 #include "edge.h"
49 #include "node.h"
50 #include "graphwidget.h"
51
52 #include <TFunction_IFunction.hxx>
53 #include <TFunction_GraphNode.hxx>
54 #include <TDataStd_Name.hxx>
55 #include <TDataStd_Tick.hxx>
56
57 //! [0]
Node(GraphWidget * graphWidget)58 Node::Node(GraphWidget *graphWidget)
59 : graph(graphWidget)
60 {
61 setFlag(ItemIsMovable);
62 setFlag(ItemSendsGeometryChanges);
63 setCacheMode(DeviceCoordinateCache);
64 setZValue(-1);
65 }
66 //! [0]
67
setFunction(const TDF_Label & func)68 void Node::setFunction(const TDF_Label& func)
69 {
70 myFunction = func;
71 }
72
getFunction() const73 const TDF_Label& Node::getFunction() const
74 {
75 return myFunction;
76 }
77
78 //! [1]
addEdge(Edge * edge)79 void Node::addEdge(Edge *edge)
80 {
81 edgeList << edge;
82 edge->adjust();
83 }
84
edges() const85 QList<Edge *> Node::edges() const
86 {
87 return edgeList;
88 }
89 //! [1]
90
91 //! [2]
calculateForces()92 void Node::calculateForces()
93 {
94 if (!scene() || scene()->mouseGrabberItem() == this) {
95 newPos = pos();
96 return;
97 }
98 //! [2]
99
100 //! [3]
101 // Sum up all forces pushing this item away
102 qreal xvel = 0;
103 qreal yvel = 0;
104 foreach (QGraphicsItem *item, scene()->items()) {
105 Node *node = qgraphicsitem_cast<Node *>(item);
106 if (!node)
107 continue;
108
109 QPointF vec = mapToItem(node, 0, 0);
110 qreal dx = vec.x();
111 qreal dy = vec.y();
112 double l = 2.0 * (dx * dx + dy * dy);
113 if (l > 0) {
114 xvel += (dx * 150.0) / l;
115 yvel += (dy * 150.0) / l;
116 }
117 }
118 //! [3]
119
120 //! [4]
121 // Now subtract all forces pulling items together
122 double weight = (edgeList.size() + 1) * 10;
123 foreach (Edge *edge, edgeList) {
124 QPointF vec;
125 if (edge->sourceNode() == this)
126 vec = mapToItem(edge->destNode(), 0, 0);
127 else
128 vec = mapToItem(edge->sourceNode(), 0, 0);
129 xvel -= vec.x() / weight;
130 yvel -= vec.y() / weight;
131 }
132 //! [4]
133
134 //! [5]
135 if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
136 xvel = yvel = 0;
137 //! [5]
138
139 //! [6]
140 QRectF sceneRect = scene()->sceneRect();
141 newPos = pos() + QPointF(xvel, yvel);
142 newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
143 newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
144 }
145 //! [6]
146
147 //! [7]
advance()148 bool Node::advance()
149 {
150 if (newPos == pos())
151 return false;
152
153 setPos(newPos);
154 return true;
155 }
156 //! [7]
157
158 //! [8]
boundingRect() const159 QRectF Node::boundingRect() const
160 {
161 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
162 // Add some extra space around the circle for easier touching with finger
163 qreal adjust = 30;
164 return QRectF( -10 - adjust, -10 - adjust,
165 20 + adjust * 2, 20 + adjust * 2);
166 #else
167 qreal adjust = 2;
168 return QRectF( -10 - adjust, -10 - adjust,
169 23 + adjust, 23 + adjust);
170 #endif
171 }
172 //! [8]
173
174 //! [9]
shape() const175 QPainterPath Node::shape() const
176 {
177 QPainterPath path;
178 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
179 // Add some extra space around the circle for easier touching with finger
180 path.addEllipse( -40, -40, 80, 80);
181 #else
182 path.addEllipse(-10, -10, 20, 20);
183 #endif
184 return path;
185 }
186 //! [9]
187
188 //! [10]
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget *)189 void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
190 {
191 painter->setPen(Qt::NoPen);
192 painter->setBrush(Qt::darkGray);
193 painter->drawEllipse(-7, -7, 20, 20);
194
195 QColor light_color(Qt::yellow);
196 TFunction_IFunction iFunc(myFunction);
197 Handle(TFunction_GraphNode) graphNode = iFunc.GetGraphNode();
198 TFunction_ExecutionStatus status = graphNode->GetStatus();
199 switch (status)
200 {
201 case TFunction_ES_WrongDefinition:
202 case TFunction_ES_Failed:
203 light_color = Qt::red;
204 break;
205 case TFunction_ES_NotExecuted:
206 light_color = Qt::green;
207 break;
208 case TFunction_ES_Executing:
209 light_color = Qt::yellow;
210 break;
211 case TFunction_ES_Succeeded:
212 light_color = Qt::blue;
213 break;
214 }
215 if (myFunction.IsAttribute(TDataStd_Tick::GetID()))
216 light_color = Qt::white;
217 QColor dark_color = light_color.dark(150);
218
219 QRadialGradient gradient(-3, -3, 10);
220 if (option->state & QStyle::State_Sunken) {
221 gradient.setCenter(3, 3);
222 gradient.setFocalPoint(3, 3);
223 gradient.setColorAt(1, light_color.light(120));
224 gradient.setColorAt(0, dark_color.light(120));
225 } else {
226 gradient.setColorAt(0, light_color);
227 gradient.setColorAt(1, dark_color);
228 }
229 painter->setBrush(gradient);
230
231 painter->setPen(QPen(Qt::black, 0));
232 painter->drawEllipse(-10, -10, 20, 20);
233
234 QString s;
235 Handle(TDataStd_Name) n;
236 if (myFunction.FindAttribute(TDataStd_Name::GetID(), n))
237 s = TCollection_AsciiString(n->Get()).ToCString();
238 painter->drawText(-7, 3, s);
239 }
240 //! [10]
241
242 //! [11]
itemChange(GraphicsItemChange change,const QVariant & value)243 QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
244 {
245 switch (change) {
246 case ItemPositionHasChanged:
247 foreach (Edge *edge, edgeList)
248 edge->adjust();
249 //graph->itemMoved();
250 break;
251 default:
252 break;
253 };
254
255 return QGraphicsItem::itemChange(change, value);
256 }
257 //! [11]
258
259 //! [12]
mousePressEvent(QGraphicsSceneMouseEvent * event)260 void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
261 {
262 update();
263 QGraphicsItem::mousePressEvent(event);
264 }
265
mouseReleaseEvent(QGraphicsSceneMouseEvent * event)266 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
267 {
268 update();
269 QGraphicsItem::mouseReleaseEvent(event);
270 }
271 //! [12]
272