summaryrefslogtreecommitdiffstats
path: root/examples/graphicsview/elasticnodes/node.cpp
diff options
context:
space:
mode:
authorAndreas Aardal Hanssen <andreas.aardal.hanssen@nokia.com>2010-04-29 10:39:13 (GMT)
committerAndreas Aardal Hanssen <andreas.aardal.hanssen@nokia.com>2010-04-29 10:40:01 (GMT)
commit4d312a6e635ce0cde0ae5c439541f315d7fddff3 (patch)
treec2934e360a72d231ac7dd7005d81334292f58cb7 /examples/graphicsview/elasticnodes/node.cpp
parent71165896c2867091abe6df98ea18ac0536028a0b (diff)
downloadQt-4d312a6e635ce0cde0ae5c439541f315d7fddff3.zip
Qt-4d312a6e635ce0cde0ae5c439541f315d7fddff3.tar.gz
Qt-4d312a6e635ce0cde0ae5c439541f315d7fddff3.tar.bz2
Documentation for the Elastic Nodes example.
Checked for qdoc errors.
Diffstat (limited to 'examples/graphicsview/elasticnodes/node.cpp')
-rw-r--r--examples/graphicsview/elasticnodes/node.cpp46
1 files changed, 36 insertions, 10 deletions
diff --git a/examples/graphicsview/elasticnodes/node.cpp b/examples/graphicsview/elasticnodes/node.cpp
index 774046c..495aa89 100644
--- a/examples/graphicsview/elasticnodes/node.cpp
+++ b/examples/graphicsview/elasticnodes/node.cpp
@@ -48,6 +48,7 @@
#include "node.h"
#include "graphwidget.h"
+//! [0]
Node::Node(GraphWidget *graphWidget)
: graph(graphWidget)
{
@@ -56,7 +57,9 @@ Node::Node(GraphWidget *graphWidget)
setCacheMode(DeviceCoordinateCache);
setZValue(-1);
}
+//! [0]
+//! [1]
void Node::addEdge(Edge *edge)
{
edgeList << edge;
@@ -67,14 +70,18 @@ QList<Edge *> Node::edges() const
{
return edgeList;
}
+//! [1]
+//! [2]
void Node::calculateForces()
{
if (!scene() || scene()->mouseGrabberItem() == this) {
newPos = pos();
return;
}
-
+//! [2]
+
+//! [3]
// Sum up all forces pushing this item away
qreal xvel = 0;
qreal yvel = 0;
@@ -83,37 +90,45 @@ void Node::calculateForces()
if (!node)
continue;
- QLineF line(mapFromItem(node, 0, 0), QPointF(0, 0));
- qreal dx = line.dx();
- qreal dy = line.dy();
+ QPointF vec = mapToItem(node, 0, 0);
+ qreal dx = vec.x();
+ qreal dy = vec.y();
double l = 2.0 * (dx * dx + dy * dy);
if (l > 0) {
xvel += (dx * 150.0) / l;
yvel += (dy * 150.0) / l;
}
}
+//! [3]
+//! [4]
// Now subtract all forces pulling items together
double weight = (edgeList.size() + 1) * 10;
foreach (Edge *edge, edgeList) {
- QPointF pos;
+ QPointF vec;
if (edge->sourceNode() == this)
- pos = mapFromItem(edge->destNode(), 0, 0);
+ vec = mapToItem(edge->destNode(), 0, 0);
else
- pos = mapFromItem(edge->sourceNode(), 0, 0);
- xvel += pos.x() / weight;
- yvel += pos.y() / weight;
+ vec = mapToItem(edge->sourceNode(), 0, 0);
+ xvel -= vec.x() / weight;
+ yvel -= vec.y() / weight;
}
-
+//! [4]
+
+//! [5]
if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
xvel = yvel = 0;
+//! [5]
+//! [6]
QRectF sceneRect = scene()->sceneRect();
newPos = pos() + QPointF(xvel, yvel);
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
}
+//! [6]
+//! [7]
bool Node::advance()
{
if (newPos == pos())
@@ -122,21 +137,27 @@ bool Node::advance()
setPos(newPos);
return true;
}
+//! [7]
+//! [8]
QRectF Node::boundingRect() const
{
qreal adjust = 2;
return QRectF(-10 - adjust, -10 - adjust,
23 + adjust, 23 + adjust);
}
+//! [8]
+//! [9]
QPainterPath Node::shape() const
{
QPainterPath path;
path.addEllipse(-10, -10, 20, 20);
return path;
}
+//! [9]
+//! [10]
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
painter->setPen(Qt::NoPen);
@@ -157,7 +178,9 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
painter->setPen(QPen(Qt::black, 0));
painter->drawEllipse(-10, -10, 20, 20);
}
+//! [10]
+//! [11]
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
switch (change) {
@@ -172,7 +195,9 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
return QGraphicsItem::itemChange(change, value);
}
+//! [11]
+//! [12]
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
update();
@@ -184,3 +209,4 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
update();
QGraphicsItem::mouseReleaseEvent(event);
}
+//! [12]