~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/graphicsview-elasticnodes-node-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-10-12 23:14:14 UTC
  • mto: (15.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20061012231414-y2oqbom5dy389os0
Tags: upstream-4.2.0
ImportĀ upstreamĀ versionĀ 4.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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">
 
5
<head>
 
6
  <title>Qt 4.2: node.cpp Example File (graphicsview/elasticnodes/node.cpp)</title>
 
7
  <link href="classic.css" rel="stylesheet" type="text/css" />
 
8
</head>
 
9
<body>
 
10
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
11
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <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> /****************************************************************************
 
16
 **
 
17
 ** Copyright (C) 2006-2006 Trolltech ASA. All rights reserved.
 
18
 **
 
19
 ** This file is part of the example classes of the Qt Toolkit.
 
20
 **
 
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>
 
27
 **
 
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.
 
32
 **
 
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.
 
35
 **
 
36
 ****************************************************************************/
 
37
 
 
38
 #include &lt;QGraphicsScene&gt;
 
39
 #include &lt;QGraphicsSceneMouseEvent&gt;
 
40
 #include &lt;QPainter&gt;
 
41
 #include &lt;QStyleOption&gt;
 
42
 
 
43
 #include &quot;edge.h&quot;
 
44
 #include &quot;node.h&quot;
 
45
 #include &quot;graphwidget.h&quot;
 
46
 
 
47
 Node::Node(GraphWidget *graphWidget)
 
48
     : graph(graphWidget)
 
49
 {
 
50
     setFlag(ItemIsMovable);
 
51
     setZValue(1);
 
52
 }
 
53
 
 
54
 void Node::addEdge(Edge *edge)
 
55
 {
 
56
     edgeList &lt;&lt; edge;
 
57
     edge-&gt;adjust();
 
58
 }
 
59
 
 
60
 QList&lt;Edge *&gt; Node::edges() const
 
61
 {
 
62
     return edgeList;
 
63
 }
 
64
 
 
65
 void Node::calculateForces()
 
66
 {
 
67
     if (!scene() || scene()-&gt;mouseGrabberItem() == this) {
 
68
         newPos = pos();
 
69
         return;
 
70
     }
 
71
 
 
72
     <span class="comment">// Sum up all forces pushing this item away</span>
 
73
     qreal xvel = 0;
 
74
     qreal yvel = 0;
 
75
     foreach (QGraphicsItem *item, scene()-&gt;items()) {
 
76
         Node *node = qgraphicsitem_cast&lt;Node *&gt;(item);
 
77
         if (!node)
 
78
             continue;
 
79
 
 
80
         QLineF line(mapFromItem(node, 0, 0), QPointF(0, 0));
 
81
         qreal dx = line.dx();
 
82
         qreal dy = line.dy();
 
83
         double l = 2.0 * (dx * dx + dy * dy);
 
84
         if (l &gt; 0) {
 
85
             xvel += (dx * 150.0) / l;
 
86
             yvel += (dy * 150.0) / l;
 
87
         }
 
88
     }
 
89
 
 
90
     <span class="comment">// Now subtract all forces pulling items together</span>
 
91
     double weight = (edgeList.size() + 1) * 10;
 
92
     foreach (Edge *edge, edgeList) {
 
93
         QPointF pos;
 
94
         if (edge-&gt;sourceNode() == this)
 
95
             pos = mapFromItem(edge-&gt;destNode(), 0, 0);
 
96
         else
 
97
             pos = mapFromItem(edge-&gt;sourceNode(), 0, 0);
 
98
         xvel += pos.x() / weight;
 
99
         yvel += pos.y() / weight;
 
100
     }
 
101
 
 
102
     if (qAbs(xvel) &lt; 0.1 &amp;&amp; qAbs(yvel) &lt; 0.1)
 
103
         xvel = yvel = 0;
 
104
 
 
105
     QRectF sceneRect = scene()-&gt;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));
 
109
 }
 
110
 
 
111
 bool Node::advance()
 
112
 {
 
113
     if (newPos == pos())
 
114
         return false;
 
115
 
 
116
     setPos(newPos);
 
117
     return true;
 
118
 }
 
119
 
 
120
 QRectF Node::boundingRect() const
 
121
 {
 
122
     qreal adjust = 2;
 
123
     return QRectF(-10 - adjust, -10 - adjust,
 
124
                   23 + adjust, 23 + adjust);
 
125
 }
 
126
 
 
127
 QPainterPath Node::shape() const
 
128
 {
 
129
     QPainterPath path;
 
130
     path.addEllipse(-10, -10, 20, 20);
 
131
     return path;
 
132
 }
 
133
 
 
134
 void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
 
135
 {
 
136
     painter-&gt;setPen(Qt::NoPen);
 
137
     painter-&gt;setBrush(Qt::darkGray);
 
138
     painter-&gt;drawEllipse(-7, -7, 20, 20);
 
139
 
 
140
     QRadialGradient gradient(-3, -3, 10);
 
141
     if (option-&gt;state &amp; 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));
 
146
     } else {
 
147
         gradient.setColorAt(0, Qt::yellow);
 
148
         gradient.setColorAt(1, Qt::darkYellow);
 
149
     }
 
150
     painter-&gt;setBrush(gradient);
 
151
     painter-&gt;setPen(QPen(Qt::black, 0));
 
152
     painter-&gt;drawEllipse(-10, -10, 20, 20);
 
153
 }
 
154
 
 
155
 QVariant Node::itemChange(GraphicsItemChange change, const QVariant &amp;value)
 
156
 {
 
157
     switch (change) {
 
158
     case ItemPositionChange:
 
159
         foreach (Edge *edge, edgeList)
 
160
             edge-&gt;adjust();
 
161
         graph-&gt;itemMoved();
 
162
         break;
 
163
     default:
 
164
         break;
 
165
     };
 
166
 
 
167
     return QGraphicsItem::itemChange(change, value);
 
168
 }
 
169
 
 
170
 void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
 
171
 {
 
172
     update();
 
173
     QGraphicsItem::mousePressEvent(event);
 
174
 }
 
175
 
 
176
 void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
 
177
 {
 
178
     update();
 
179
     QGraphicsItem::mouseReleaseEvent(event);
 
180
 }</pre>
 
181
<p /><address><hr /><div align="center">
 
182
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
183
<td width="30%">Copyright &copy; 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>
 
187
</html>