QtBase  v6.3.1
node.cpp
Go to the documentation of this file.
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 
61 Node::Node(GraphWidget *graphWidget)
62  : graph(graphWidget)
63 {
67  setZValue(-1);
68 }
70 
72 void Node::addEdge(Edge *edge)
73 {
74  edgeList << edge;
75  edge->adjust();
76 }
77 
79 {
80  return edgeList;
81 }
83 
86 {
87  if (!scene() || scene()->mouseGrabberItem() == this) {
88  newPos = pos();
89  return;
90  }
92 
94  // Sum up all forces pushing this item away
95  qreal xvel = 0;
96  qreal yvel = 0;
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  }
113 
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  }
127 
129  if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
130  xvel = yvel = 0;
132 
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 }
140 
143 {
144  if (newPos == pos())
145  return false;
146 
147  setPos(newPos);
148  return true;
149 }
151 
154 {
155  qreal adjust = 2;
156  return QRectF( -10 - adjust, -10 - adjust, 23 + adjust, 23 + adjust);
157 }
159 
162 {
164  path.addEllipse(-10, -10, 20, 20);
165  return path;
166 }
168 
171 {
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 
189  painter->drawEllipse(-10, -10, 20, 20);
190 }
192 
195 {
196  switch (change) {
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 }
209 
212 {
213  update();
215 }
216 
218 {
219  update();
221 }
[0]
Definition: edge.h:60
void adjust()
[1]
Definition: edge.cpp:81
void itemMoved()
[1]
[0]
Definition: node.h:62
QRectF boundingRect() const override
[7]
Definition: node.cpp:153
bool advancePosition()
[6]
Definition: node.cpp:142
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
[9]
Definition: node.cpp:170
Node()
Definition: lalr.h:207
void calculateForces()
[1]
Definition: node.cpp:85
QPainterPath shape() const override
[8]
Definition: node.cpp:161
void addEdge(Edge *edge)
[0]
Definition: node.cpp:72
QList< Edge * > edges() const
Definition: node.cpp:78
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
[11]
Definition: node.cpp:211
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
Definition: node.cpp:217
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
[10]
Definition: node.cpp:194
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
void setColorAt(qreal pos, const QColor &color)
Definition: qbrush.cpp:1596
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:83
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
QPointF mapToItem(const QGraphicsItem *item, const QPointF &point) const
void update(const QRectF &rect=QRectF())
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value)
QGraphicsScene * scene() const
QPointF pos() const
@ ItemSendsGeometryChanges
Definition: qgraphicsitem.h:97
void setZValue(qreal z)
void setPos(const QPointF &pos)
void setCacheMode(CacheMode mode, const QSize &cacheSize=QSize())
void setFlag(GraphicsItemFlag flag, bool enabled=true)
QList< QGraphicsItem * > items(Qt::SortOrder order=Qt::DescendingOrder) const
QRectF sceneRect
the scene rectangle; the bounding rectangle of the scene
The QGraphicsSceneMouseEvent class provides mouse events in the graphics view framework.
qsizetype size() const noexcept
Definition: qlist.h:414
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
void setPen(const QColor &color)
Definition: qpainter.cpp:3640
void drawEllipse(const QRectF &r)
Definition: qpainter.cpp:3986
void setBrush(const QBrush &brush)
Definition: qpainter.cpp:3755
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:65
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:61
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:242
constexpr qreal x() const noexcept
Definition: qpoint.h:361
constexpr qreal y() const noexcept
Definition: qpoint.h:366
constexpr void setY(qreal y) noexcept
Definition: qpoint.h:376
constexpr void setX(qreal x) noexcept
Definition: qpoint.h:371
The QRadialGradient class is used in combination with QBrush to specify a radial gradient brush.
Definition: qbrush.h:448
void setCenter(const QPointF &center)
Definition: qbrush.cpp:2216
void setFocalPoint(const QPointF &focalPoint)
Definition: qbrush.cpp:2338
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
constexpr qreal bottom() const noexcept
Definition: qrect.h:527
constexpr qreal left() const noexcept
Definition: qrect.h:524
constexpr qreal top() const noexcept
Definition: qrect.h:525
constexpr qreal right() const noexcept
Definition: qrect.h:526
@ State_Sunken
Definition: qstyle.h:105
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
Definition: qstyleoption.h:684
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:95
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
FT_Vector * vec
Definition: ftbbox.c:469
QPainterPath node()
Definition: paths.cpp:574
@ yellow
Definition: qnamespace.h:71
@ darkGray
Definition: qnamespace.h:63
@ black
Definition: qnamespace.h:61
@ darkYellow
Definition: qnamespace.h:77
@ NoPen
Definition: qnamespace.h:1112
EGLOutputLayerEXT EGLint EGLAttrib value
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
GLuint GLuint GLfloat weight
struct _cl_event * event
Definition: qopenglext.h:2998
GLsizei const GLchar *const * path
Definition: qopenglext.h:4283
GLuint GLenum option
Definition: qopenglext.h:5929
QPointF qAbs(const QPointF &p)
Definition: qscroller.cpp:119
QGraphicsItem * item
QList< QTreeWidgetItem * > items
QPainter painter(this)
[7]
FT_Pos x
Definition: ftimage.h:77
FT_Pos y
Definition: ftimage.h:78