1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
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
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
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."
39 ****************************************************************************/
41 #include <QGraphicsScene>
42 #include <QGraphicsSceneMouseEvent>
44 #include <QStyleOption>
48 #include "GraphWidget.h"
50 Node::Node(GraphWidget *graphWidget, const std::string& label)
54 setFlag(ItemIsMovable);
55 setFlag(ItemSendsGeometryChanges);
56 setCacheMode(DeviceCoordinateCache);
58 this->setToolTip( this->m_Label.c_str( ) );
61 void Node::addEdge(Edge *edge)
67 QList<Edge *> Node::edges() const
72 void Node::calculateForces()
74 if (!scene() || scene()->mouseGrabberItem() == this) {
79 // Sum up all forces pushing this item away
82 foreach (QGraphicsItem *item, scene()->items()) {
83 Node *node = qgraphicsitem_cast<Node *>(item);
87 QPointF vec = mapToItem(node, 0, 0);
90 double l = 2.0 * (dx * dx + dy * dy);
92 xvel += (dx * 150.0) / l;
93 yvel += (dy * 150.0) / l;
97 // Now subtract all forces pulling items together
98 double weight = (edgeList.size() + 1) * 10;
99 foreach (Edge *edge, edgeList) {
101 if (edge->sourceNode() == this)
102 vec = mapToItem(edge->destNode(), 0, 0);
104 vec = mapToItem(edge->sourceNode(), 0, 0);
105 xvel -= vec.x() / weight;
106 yvel -= vec.y() / weight;
109 if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
112 QRectF sceneRect = scene()->sceneRect();
113 newPos = pos() + QPointF(xvel, yvel);
114 newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
115 newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
117 newPos = pos(); // + QPointF(xvel, yvel);
129 QRectF Node::boundingRect() const
131 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
132 // Add some extra space around the circle for easier touching with finger
134 return QRectF( -10 - adjust, -10 - adjust,
135 20 + adjust * 2, 20 + adjust * 2);
138 return QRectF( -10 - adjust, -10 - adjust,
139 23 + adjust, 23 + adjust);
143 QPainterPath Node::shape() const
146 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
147 // Add some extra space around the circle for easier touching with finger
148 path.addEllipse( -40, -40, 80, 80);
150 path.addEllipse(-10, -10, 20, 20);
155 void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
157 painter->setPen(Qt::NoPen);
158 painter->setBrush(Qt::darkGray);
159 painter->drawEllipse(-7, -7, 20, 20);
161 QPointF text_point = this->boundingRect( ).bottomRight( ) + this->pos( );
162 painter->drawText( text_point, this->m_Label.c_str( ) );
163 std::cout << text_point.x( ) << " " << text_point.y( ) << std::endl;
165 QRadialGradient gradient(-3, -3, 10);
166 if (option->state & QStyle::State_Sunken) {
167 gradient.setCenter(3, 3);
168 gradient.setFocalPoint(3, 3);
169 gradient.setColorAt(1, QColor(Qt::yellow).light(120));
170 gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
172 gradient.setColorAt(0, Qt::yellow);
173 gradient.setColorAt(1, Qt::darkYellow);
175 painter->setBrush(gradient);
177 painter->setPen(QPen(Qt::black, 0));
178 painter->drawEllipse(-10, -10, 20, 20);
181 QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
184 case ItemPositionHasChanged:
185 foreach (Edge *edge, edgeList)
193 return QGraphicsItem::itemChange(change, value);
196 void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
199 QGraphicsItem::mousePressEvent(event);
202 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
205 QGraphicsItem::mouseReleaseEvent(event);