1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.2: node.cpp Example File (graphicsview/elasticnodes/node.cpp)</title>
7
<link href="classic.css" rel="stylesheet" type="text/css" />
10
<table border="0" cellpadding="0" cellspacing="0" width="100%">
12
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
13
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
14
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">node.cpp Example File<br /><sup><sup>graphicsview/elasticnodes/node.cpp</sup></sup></h1>
15
<pre> /****************************************************************************
17
** Copyright (C) 2006-2006 Trolltech ASA. All rights reserved.
19
** This file is part of the example classes of the Qt Toolkit.
21
** This file may be used under the terms of the GNU General Public
22
** License version 2.0 as published by the Free Software Foundation
23
** and appearing in the file LICENSE.GPL included in the packaging of
24
** this file. Please review the following information to ensure GNU
25
** General Public Licensing requirements will be met:
26
** http:<span class="comment">//www.trolltech.com/products/qt/opensource.html</span>
28
** If you are unsure which license is appropriate for your use, please
29
** review the following information:
30
** http:<span class="comment">//www.trolltech.com/products/qt/licensing.html or contact the</span>
31
** sales department at sales@trolltech.com.
33
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
34
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
36
****************************************************************************/
38
#include <QGraphicsScene>
39
#include <QGraphicsSceneMouseEvent>
40
#include <QPainter>
41
#include <QStyleOption>
43
#include "edge.h"
44
#include "node.h"
45
#include "graphwidget.h"
47
Node::Node(GraphWidget *graphWidget)
50
setFlag(ItemIsMovable);
54
void Node::addEdge(Edge *edge)
56
edgeList << edge;
60
QList<Edge *> Node::edges() const
65
void Node::calculateForces()
67
if (!scene() || scene()->mouseGrabberItem() == this) {
72
<span class="comment">// Sum up all forces pushing this item away</span>
75
foreach (QGraphicsItem *item, scene()->items()) {
76
Node *node = qgraphicsitem_cast<Node *>(item);
80
QLineF line(mapFromItem(node, 0, 0), QPointF(0, 0));
83
double l = 2.0 * (dx * dx + dy * dy);
85
xvel += (dx * 150.0) / l;
86
yvel += (dy * 150.0) / l;
90
<span class="comment">// Now subtract all forces pulling items together</span>
91
double weight = (edgeList.size() + 1) * 10;
92
foreach (Edge *edge, edgeList) {
94
if (edge->sourceNode() == this)
95
pos = mapFromItem(edge->destNode(), 0, 0);
97
pos = mapFromItem(edge->sourceNode(), 0, 0);
98
xvel += pos.x() / weight;
99
yvel += pos.y() / weight;
102
if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
105
QRectF sceneRect = scene()->sceneRect();
106
newPos = pos() + QPointF(xvel, yvel);
107
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
108
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
120
QRectF Node::boundingRect() const
123
return QRectF(-10 - adjust, -10 - adjust,
124
23 + adjust, 23 + adjust);
127
QPainterPath Node::shape() const
130
path.addEllipse(-10, -10, 20, 20);
134
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
136
painter->setPen(Qt::NoPen);
137
painter->setBrush(Qt::darkGray);
138
painter->drawEllipse(-7, -7, 20, 20);
140
QRadialGradient gradient(-3, -3, 10);
141
if (option->state & QStyle::State_Sunken) {
142
gradient.setCenter(3, 3);
143
gradient.setFocalPoint(3, 3);
144
gradient.setColorAt(1, QColor(Qt::yellow).light(120));
145
gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
147
gradient.setColorAt(0, Qt::yellow);
148
gradient.setColorAt(1, Qt::darkYellow);
150
painter->setBrush(gradient);
151
painter->setPen(QPen(Qt::black, 0));
152
painter->drawEllipse(-10, -10, 20, 20);
155
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
158
case ItemPositionChange:
159
foreach (Edge *edge, edgeList)
161
graph->itemMoved();
167
return QGraphicsItem::itemChange(change, value);
170
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
173
QGraphicsItem::mousePressEvent(event);
176
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
179
QGraphicsItem::mouseReleaseEvent(event);
181
<p /><address><hr /><div align="center">
182
<table width="100%" cellspacing="0" border="0"><tr class="address">
183
<td width="30%">Copyright © 2006 <a href="trolltech.html">Trolltech</a></td>
184
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
185
<td width="30%" align="right"><div align="right">Qt 4.2.0</div></td>
186
</tr></table></div></address></body>