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

« back to all changes in this revision

Viewing changes to doc/html/animation-stickman-stickman-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

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.6: stickman.cpp Example File (animation/stickman/stickman.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://qt.nokia.com/"><img src="images/qt-logo.png" align="left" 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="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td><td class="searchBar" align="right" valign="center"><form action="http://www.google.com/cse" id="cse-search-box"><div><input type="hidden" name="cx" value="000136343326384750312:dhbxnqlakyu" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" /></div></form></td>
 
14
</tr></table><h1 class="title">stickman.cpp Example File<br /><span class="small-subtitle">animation/stickman/stickman.cpp</span>
 
15
</h1>
 
16
<pre><span class="comment"> /****************************************************************************
 
17
 **
 
18
 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
19
 ** All rights reserved.
 
20
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 
21
 **
 
22
 ** This file is part of the QtCore module of the Qt Toolkit.
 
23
 **
 
24
 ** $QT_BEGIN_LICENSE:LGPL$
 
25
 ** No Commercial Usage
 
26
 ** This file contains pre-release code and may not be distributed.
 
27
 ** You may use this file in accordance with the terms and conditions
 
28
 ** contained in the Technology Preview License Agreement accompanying
 
29
 ** this package.
 
30
 **
 
31
 ** GNU Lesser General Public License Usage
 
32
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 
33
 ** General Public License version 2.1 as published by the Free Software
 
34
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 
35
 ** packaging of this file.  Please review the following information to
 
36
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 
37
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
38
 **
 
39
 ** In addition, as a special exception, Nokia gives you certain additional
 
40
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 
41
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
42
 **
 
43
 ** If you have questions regarding the use of this file, please contact
 
44
 ** Nokia at qt-info@nokia.com.
 
45
 **
 
46
 **
 
47
 **
 
48
 **
 
49
 **
 
50
 **
 
51
 **
 
52
 **
 
53
 ** $QT_END_LICENSE$
 
54
 **
 
55
 ****************************************************************************/</span>
 
56
 
 
57
 #include &quot;stickman.h&quot;
 
58
 #include &quot;node.h&quot;
 
59
 
 
60
 #include &lt;QPainter&gt;
 
61
 #include &lt;QTimer&gt;
 
62
 
 
63
 #define _USE_MATH_DEFINES
 
64
 #include &lt;math.h&gt;
 
65
 
 
66
 #ifndef M_PI
 
67
 #define M_PI 3.14159265358979323846
 
68
 #endif
 
69
 
 
70
 static const qreal Coords[NodeCount * 2] = {
 
71
     0.0, -150.0, <span class="comment">// head, #0</span>
 
72
 
 
73
     0.0, -100.0, <span class="comment">// body pentagon, top-&gt;bottom, left-&gt;right, #1 - 5</span>
 
74
     -50.0, -50.0,
 
75
     50.0, -50.0,
 
76
     -25.0, 50.0,
 
77
     25.0, 50.0,
 
78
 
 
79
     -100.0, 0.0, <span class="comment">// right arm, #6 - 7</span>
 
80
     -125.0, 50.0,
 
81
 
 
82
     100.0, 0.0, <span class="comment">// left arm, #8 - 9</span>
 
83
     125.0, 50.0,
 
84
 
 
85
     -35.0, 75.0, <span class="comment">// lower body, #10 - 11</span>
 
86
     35.0, 75.0,
 
87
 
 
88
     -25.0, 200.0, <span class="comment">// right leg, #12 - 13</span>
 
89
     -30.0, 300.0,
 
90
 
 
91
     25.0, 200.0, <span class="comment">// left leg, #14 - 15</span>
 
92
     30.0, 300.0
 
93
 
 
94
 };
 
95
 
 
96
 static const int Bones[BoneCount * 2] = {
 
97
     0, 1, <span class="comment">// neck</span>
 
98
 
 
99
     1, 2, <span class="comment">// body</span>
 
100
     1, 3,
 
101
     1, 4,
 
102
     1, 5,
 
103
     2, 3,
 
104
     2, 4,
 
105
     2, 5,
 
106
     3, 4,
 
107
     3, 5,
 
108
     4, 5,
 
109
 
 
110
     2, 6, <span class="comment">// right arm</span>
 
111
     6, 7,
 
112
 
 
113
     3, 8, <span class="comment">// left arm</span>
 
114
     8, 9,
 
115
 
 
116
     4, 10, <span class="comment">// lower body</span>
 
117
     4, 11,
 
118
     5, 10,
 
119
     5, 11,
 
120
     10, 11,
 
121
 
 
122
     10, 12, <span class="comment">// right leg</span>
 
123
     12, 13,
 
124
 
 
125
     11, 14, <span class="comment">// left leg</span>
 
126
     14, 15
 
127
 
 
128
 };
 
129
 
 
130
 StickMan::StickMan()
 
131
 {
 
132
     m_sticks = true;
 
133
     m_isDead = false;
 
134
     m_pixmap = QPixmap(&quot;images/head.png&quot;);
 
135
     m_penColor = Qt::white;
 
136
     m_fillColor = Qt::black;
 
137
 
 
138
     <span class="comment">// Set up start position of limbs</span>
 
139
     for (int i=0; i&lt;NodeCount; ++i) {
 
140
         m_nodes[i] = new Node(QPointF(Coords[i * 2], Coords[i * 2 + 1]), this);
 
141
         connect(m_nodes[i], SIGNAL(positionChanged()), this, SLOT(childPositionChanged()));
 
142
     }
 
143
 
 
144
     for (int i=0; i&lt;BoneCount; ++i) {
 
145
         int n1 = Bones[i * 2];
 
146
         int n2 = Bones[i * 2 + 1];
 
147
 
 
148
         Node *node1 = m_nodes[n1];
 
149
         Node *node2 = m_nodes[n2];
 
150
 
 
151
         QPointF dist = node1-&gt;pos() - node2-&gt;pos();
 
152
         m_perfectBoneLengths[i] = sqrt(pow(dist.x(),2) + pow(dist.y(),2));
 
153
     }
 
154
 
 
155
     startTimer(10);
 
156
 }
 
157
 
 
158
 StickMan::~StickMan()
 
159
 {
 
160
 }
 
161
 
 
162
 void StickMan::childPositionChanged()
 
163
 {
 
164
     prepareGeometryChange();
 
165
 }
 
166
 
 
167
 void StickMan::setDrawSticks(bool on)
 
168
 {
 
169
     m_sticks = on;
 
170
     for (int i=0;i&lt;nodeCount();++i) {
 
171
         Node *node = m_nodes[i];
 
172
         node-&gt;setVisible(on);
 
173
     }
 
174
 }
 
175
 
 
176
 QRectF StickMan::boundingRect() const
 
177
 {
 
178
     <span class="comment">// account for head radius=50.0 plus pen which is 5.0</span>
 
179
     return childrenBoundingRect().adjusted(-55.0, -55.0, 55.0, 55.0);
 
180
 }
 
181
 
 
182
 int StickMan::nodeCount() const
 
183
 {
 
184
     return NodeCount;
 
185
 }
 
186
 
 
187
 Node *StickMan::node(int idx) const
 
188
 {
 
189
     if (idx &gt;= 0 &amp;&amp; idx &lt; NodeCount)
 
190
         return m_nodes[idx];
 
191
     else
 
192
         return 0;
 
193
 }
 
194
 
 
195
 void StickMan::timerEvent(QTimerEvent *)
 
196
 {
 
197
     update();
 
198
 }
 
199
 
 
200
 void StickMan::stabilize()
 
201
 {
 
202
     static const qreal threshold = 0.001;
 
203
 
 
204
     for (int i=0; i&lt;BoneCount; ++i) {
 
205
         int n1 = Bones[i * 2];
 
206
         int n2 = Bones[i * 2 + 1];
 
207
 
 
208
         Node *node1 = m_nodes[n1];
 
209
         Node *node2 = m_nodes[n2];
 
210
 
 
211
         QPointF pos1 = node1-&gt;pos();
 
212
         QPointF pos2 = node2-&gt;pos();
 
213
 
 
214
         QPointF dist = pos1 - pos2;
 
215
         qreal length = sqrt(pow(dist.x(),2) + pow(dist.y(),2));
 
216
         qreal diff = (length - m_perfectBoneLengths[i]) / length;
 
217
 
 
218
         QPointF p = dist * (0.5 * diff);
 
219
         if (p.x() &gt; threshold &amp;&amp; p.y() &gt; threshold) {
 
220
             pos1 -= p;
 
221
             pos2 += p;
 
222
 
 
223
             node1-&gt;setPos(pos1);
 
224
             node2-&gt;setPos(pos2);
 
225
         }
 
226
     }
 
227
 }
 
228
 
 
229
 QPointF StickMan::posFor(int idx) const
 
230
 {
 
231
     return m_nodes[idx]-&gt;pos();
 
232
 }
 
233
 
 
234
<span class="comment"> //#include &lt;QTime&gt;</span>
 
235
 void StickMan::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
 
236
 {
 
237
   <span class="comment">/*  static int frames = 0;
 
238
     static QTime time;
 
239
     if (frames++ % 100 == 0) {
 
240
         frames = 1;
 
241
         time.restart();
 
242
     }
 
243
 
 
244
     if (time.elapsed() &gt; 0) {
 
245
         painter-&gt;setPen(Qt::white);
 
246
         painter-&gt;drawText(0, 0, QString::number(frames / (time.elapsed() / 1000.0)));
 
247
     }*/</span>
 
248
 
 
249
     stabilize();
 
250
     if (m_sticks) {
 
251
         painter-&gt;setPen(Qt::white);
 
252
         for (int i=0; i&lt;BoneCount; ++i) {
 
253
             int n1 = Bones[i * 2];
 
254
             int n2 = Bones[i * 2 + 1];
 
255
 
 
256
             Node *node1 = m_nodes[n1];
 
257
             Node *node2 = m_nodes[n2];
 
258
 
 
259
             painter-&gt;drawLine(node1-&gt;pos(), node2-&gt;pos());
 
260
         }
 
261
     } else {
 
262
         <span class="comment">// first bone is neck and will be used for head</span>
 
263
 
 
264
         QPainterPath path;
 
265
         path.moveTo(posFor(0));
 
266
         path.lineTo(posFor(1));
 
267
 
 
268
         <span class="comment">// right arm</span>
 
269
         path.lineTo(posFor(2));
 
270
         path.lineTo(posFor(6));
 
271
         path.lineTo(posFor(7));
 
272
 
 
273
         <span class="comment">// left arm</span>
 
274
         path.moveTo(posFor(3));
 
275
         path.lineTo(posFor(8));
 
276
         path.lineTo(posFor(9));
 
277
 
 
278
         <span class="comment">// body</span>
 
279
         path.moveTo(posFor(2));
 
280
         path.lineTo(posFor(4));
 
281
         path.lineTo(posFor(10));
 
282
         path.lineTo(posFor(11));
 
283
         path.lineTo(posFor(5));
 
284
         path.lineTo(posFor(3));
 
285
         path.lineTo(posFor(1));
 
286
 
 
287
         <span class="comment">// right leg</span>
 
288
         path.moveTo(posFor(10));
 
289
         path.lineTo(posFor(12));
 
290
         path.lineTo(posFor(13));
 
291
 
 
292
         <span class="comment">// left leg</span>
 
293
         path.moveTo(posFor(11));
 
294
         path.lineTo(posFor(14));
 
295
         path.lineTo(posFor(15));
 
296
 
 
297
         painter-&gt;setPen(QPen(m_penColor, 5.0, Qt::SolidLine, Qt::RoundCap));
 
298
         painter-&gt;drawPath(path);
 
299
 
 
300
         {
 
301
             int n1 = Bones[0];
 
302
             int n2 = Bones[1];
 
303
             Node *node1 = m_nodes[n1];
 
304
             Node *node2 = m_nodes[n2];
 
305
 
 
306
             QPointF dist = node2-&gt;pos() - node1-&gt;pos();
 
307
 
 
308
             qreal sinAngle = dist.x() / sqrt(pow(dist.x(), 2) + pow(dist.y(), 2));
 
309
             qreal angle = asin(sinAngle) * 180.0 / M_PI;
 
310
 
 
311
             QPointF headPos = node1-&gt;pos();
 
312
             painter-&gt;translate(headPos);
 
313
             painter-&gt;rotate(-angle);
 
314
 
 
315
             painter-&gt;setBrush(m_fillColor);
 
316
             painter-&gt;drawEllipse(QPointF(0,0), 50.0, 50.0);
 
317
 
 
318
             painter-&gt;setBrush(m_penColor);
 
319
             painter-&gt;setPen(QPen(m_penColor, 2.5, Qt::SolidLine, Qt::RoundCap));
 
320
 
 
321
             <span class="comment">// eyes</span>
 
322
             if (m_isDead) {
 
323
                 painter-&gt;drawLine(-30.0, -30.0, -20.0, -20.0);
 
324
                 painter-&gt;drawLine(-20.0, -30.0, -30.0, -20.0);
 
325
 
 
326
                 painter-&gt;drawLine(20.0, -30.0, 30.0, -20.0);
 
327
                 painter-&gt;drawLine(30.0, -30.0, 20.0, -20.0);
 
328
             } else {
 
329
                 painter-&gt;drawChord(QRectF(-30.0, -30.0, 25.0, 70.0), 30.0*16, 120.0*16);
 
330
                 painter-&gt;drawChord(QRectF(5.0, -30.0, 25.0, 70.0), 30.0*16, 120.0*16);
 
331
             }
 
332
 
 
333
             <span class="comment">// mouth</span>
 
334
             if (m_isDead) {
 
335
                 painter-&gt;drawLine(-28.0, 2.0, 29.0, 2.0);
 
336
             } else {
 
337
                 painter-&gt;setBrush(QColor(128, 0, 64 ));
 
338
                 painter-&gt;drawChord(QRectF(-28.0, 2.0-55.0/2.0, 57.0, 55.0), 0.0, -180.0*16);
 
339
             }
 
340
 
 
341
             <span class="comment">// pupils</span>
 
342
             if (!m_isDead) {
 
343
                 painter-&gt;setPen(QPen(m_fillColor, 1.0, Qt::SolidLine, Qt::RoundCap));
 
344
                 painter-&gt;setBrush(m_fillColor);
 
345
                 painter-&gt;drawEllipse(QPointF(-12.0, -25.0), 5.0, 5.0);
 
346
                 painter-&gt;drawEllipse(QPointF(22.0, -25.0), 5.0, 5.0);
 
347
             }
 
348
         }
 
349
     }
 
350
 }</pre>
 
351
<p /><address><hr /><div align="center">
 
352
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
353
<td width="40%" align="left">Copyright &copy; 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
 
354
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
355
<td width="40%" align="right"><div align="right">Qt 4.6.0</div></td>
 
356
<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script><script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script></tr></table></div></address></body>
 
357
</html>