summaryrefslogtreecommitdiffstats
path: root/examples/graphicsview
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
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')
-rw-r--r--examples/graphicsview/elasticnodes/edge.cpp30
-rw-r--r--examples/graphicsview/elasticnodes/edge.h6
-rw-r--r--examples/graphicsview/elasticnodes/graphwidget.cpp34
-rw-r--r--examples/graphicsview/elasticnodes/graphwidget.h4
-rw-r--r--examples/graphicsview/elasticnodes/node.cpp46
-rw-r--r--examples/graphicsview/elasticnodes/node.h2
6 files changed, 80 insertions, 42 deletions
diff --git a/examples/graphicsview/elasticnodes/edge.cpp b/examples/graphicsview/elasticnodes/edge.cpp
index 25c769b..634fbee 100644
--- a/examples/graphicsview/elasticnodes/edge.cpp
+++ b/examples/graphicsview/elasticnodes/edge.cpp
@@ -49,6 +49,7 @@
static const double Pi = 3.14159265358979323846264338327950288419717;
static double TwoPi = 2.0 * Pi;
+//! [0]
Edge::Edge(Node *sourceNode, Node *destNode)
: arrowSize(10)
{
@@ -59,33 +60,21 @@ Edge::Edge(Node *sourceNode, Node *destNode)
dest->addEdge(this);
adjust();
}
+//! [0]
-Edge::~Edge()
-{
-}
-
+//! [1]
Node *Edge::sourceNode() const
{
return source;
}
-void Edge::setSourceNode(Node *node)
-{
- source = node;
- adjust();
-}
-
Node *Edge::destNode() const
{
return dest;
}
+//! [1]
-void Edge::setDestNode(Node *node)
-{
- dest = node;
- adjust();
-}
-
+//! [2]
void Edge::adjust()
{
if (!source || !dest)
@@ -104,7 +93,9 @@ void Edge::adjust()
sourcePoint = destPoint = line.p1();
}
}
+//! [2]
+//! [3]
QRectF Edge::boundingRect() const
{
if (!source || !dest)
@@ -118,7 +109,9 @@ QRectF Edge::boundingRect() const
.normalized()
.adjusted(-extra, -extra, extra, extra);
}
+//! [3]
+//! [4]
void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
if (!source || !dest)
@@ -127,11 +120,15 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
QLineF line(sourcePoint, destPoint);
if (qFuzzyCompare(line.length(), qreal(0.)))
return;
+//! [4]
+//! [5]
// Draw the line itself
painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter->drawLine(line);
+//! [5]
+//! [6]
// Draw the arrows
double angle = ::acos(line.dx() / line.length());
if (line.dy() >= 0)
@@ -150,3 +147,4 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
}
+//! [6]
diff --git a/examples/graphicsview/elasticnodes/edge.h b/examples/graphicsview/elasticnodes/edge.h
index b6b951f..58cc89c 100644
--- a/examples/graphicsview/elasticnodes/edge.h
+++ b/examples/graphicsview/elasticnodes/edge.h
@@ -46,17 +46,14 @@
class Node;
+//! [0]
class Edge : public QGraphicsItem
{
public:
Edge(Node *sourceNode, Node *destNode);
- ~Edge();
Node *sourceNode() const;
- void setSourceNode(Node *node);
-
Node *destNode() const;
- void setDestNode(Node *node);
void adjust();
@@ -74,5 +71,6 @@ private:
QPointF destPoint;
qreal arrowSize;
};
+//! [0]
#endif
diff --git a/examples/graphicsview/elasticnodes/graphwidget.cpp b/examples/graphicsview/elasticnodes/graphwidget.cpp
index 7c244be..8b419b8 100644
--- a/examples/graphicsview/elasticnodes/graphwidget.cpp
+++ b/examples/graphicsview/elasticnodes/graphwidget.cpp
@@ -43,14 +43,13 @@
#include "edge.h"
#include "node.h"
-#include <QDebug>
-#include <QGraphicsScene>
-#include <QWheelEvent>
+#include <QtGui>
#include <math.h>
-GraphWidget::GraphWidget()
- : timerId(0)
+//! [0]
+GraphWidget::GraphWidget(QWidget *parent)
+ : QGraphicsView(parent), timerId(0)
{
QGraphicsScene *scene = new QGraphicsScene(this);
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
@@ -60,8 +59,12 @@ GraphWidget::GraphWidget()
setViewportUpdateMode(BoundingRectViewportUpdate);
setRenderHint(QPainter::Antialiasing);
setTransformationAnchor(AnchorUnderMouse);
- setResizeAnchor(AnchorViewCenter);
+ scale(qreal(0.8), qreal(0.8));
+ setMinimumSize(400, 400);
+ setWindowTitle(tr("Elastic Nodes"));
+//! [0]
+//! [1]
Node *node1 = new Node(this);
Node *node2 = new Node(this);
Node *node3 = new Node(this);
@@ -102,18 +105,18 @@ GraphWidget::GraphWidget()
node7->setPos(-50, 50);
node8->setPos(0, 50);
node9->setPos(50, 50);
-
- scale(qreal(0.8), qreal(0.8));
- setMinimumSize(400, 400);
- setWindowTitle(tr("Elastic Nodes"));
}
+//! [1]
+//! [2]
void GraphWidget::itemMoved()
{
if (!timerId)
timerId = startTimer(1000 / 25);
}
+//! [2]
+//! [3]
void GraphWidget::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
@@ -146,7 +149,9 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
QGraphicsView::keyPressEvent(event);
}
}
+//! [3]
+//! [4]
void GraphWidget::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
@@ -171,12 +176,16 @@ void GraphWidget::timerEvent(QTimerEvent *event)
timerId = 0;
}
}
+//! [4]
+//! [5]
void GraphWidget::wheelEvent(QWheelEvent *event)
{
scaleView(pow((double)2, -event->delta() / 240.0));
}
+//! [5]
+//! [6]
void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_UNUSED(rect);
@@ -213,12 +222,15 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
painter->setPen(Qt::black);
painter->drawText(textRect, message);
}
+//! [6]
+//! [7]
void GraphWidget::scaleView(qreal scaleFactor)
{
- qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
+ qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
if (factor < 0.07 || factor > 100)
return;
scale(scaleFactor, scaleFactor);
}
+//! [7]
diff --git a/examples/graphicsview/elasticnodes/graphwidget.h b/examples/graphicsview/elasticnodes/graphwidget.h
index 6f67b4e..3f78a5f 100644
--- a/examples/graphicsview/elasticnodes/graphwidget.h
+++ b/examples/graphicsview/elasticnodes/graphwidget.h
@@ -46,12 +46,13 @@
class Node;
+//! [0]
class GraphWidget : public QGraphicsView
{
Q_OBJECT
public:
- GraphWidget();
+ GraphWidget(QWidget *parent = 0);
void itemMoved();
@@ -67,5 +68,6 @@ private:
int timerId;
Node *centerNode;
};
+//! [0]
#endif
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]
diff --git a/examples/graphicsview/elasticnodes/node.h b/examples/graphicsview/elasticnodes/node.h
index 0df579d..990346e 100644
--- a/examples/graphicsview/elasticnodes/node.h
+++ b/examples/graphicsview/elasticnodes/node.h
@@ -51,6 +51,7 @@ QT_BEGIN_NAMESPACE
class QGraphicsSceneMouseEvent;
QT_END_NAMESPACE
+//! [0]
class Node : public QGraphicsItem
{
public:
@@ -80,5 +81,6 @@ private:
QPointF newPos;
GraphWidget *graph;
};
+//! [0]
#endif