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.6: stickman.cpp Example File (animation/stickman/stickman.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://qt.nokia.com/"><img src="images/qt-logo.png" align="left" 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="functions.html"><font color="#004faf">All Functions</font></a> · <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>
16
<pre><span class="comment"> /****************************************************************************
18
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
19
** All rights reserved.
20
** Contact: Nokia Corporation (qt-info@nokia.com)
22
** This file is part of the QtCore module of the Qt Toolkit.
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
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.
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.
43
** If you have questions regarding the use of this file, please contact
44
** Nokia at qt-info@nokia.com.
55
****************************************************************************/</span>
57
#include "stickman.h"
58
#include "node.h"
60
#include <QPainter>
61
#include <QTimer>
63
#define _USE_MATH_DEFINES
64
#include <math.h>
67
#define M_PI 3.14159265358979323846
70
static const qreal Coords[NodeCount * 2] = {
71
0.0, -150.0, <span class="comment">// head, #0</span>
73
0.0, -100.0, <span class="comment">// body pentagon, top->bottom, left->right, #1 - 5</span>
79
-100.0, 0.0, <span class="comment">// right arm, #6 - 7</span>
82
100.0, 0.0, <span class="comment">// left arm, #8 - 9</span>
85
-35.0, 75.0, <span class="comment">// lower body, #10 - 11</span>
88
-25.0, 200.0, <span class="comment">// right leg, #12 - 13</span>
91
25.0, 200.0, <span class="comment">// left leg, #14 - 15</span>
96
static const int Bones[BoneCount * 2] = {
97
0, 1, <span class="comment">// neck</span>
99
1, 2, <span class="comment">// body</span>
110
2, 6, <span class="comment">// right arm</span>
113
3, 8, <span class="comment">// left arm</span>
116
4, 10, <span class="comment">// lower body</span>
122
10, 12, <span class="comment">// right leg</span>
125
11, 14, <span class="comment">// left leg</span>
134
m_pixmap = QPixmap("images/head.png");
135
m_penColor = Qt::white;
136
m_fillColor = Qt::black;
138
<span class="comment">// Set up start position of limbs</span>
139
for (int i=0; i<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()));
144
for (int i=0; i<BoneCount; ++i) {
145
int n1 = Bones[i * 2];
146
int n2 = Bones[i * 2 + 1];
148
Node *node1 = m_nodes[n1];
149
Node *node2 = m_nodes[n2];
151
QPointF dist = node1->pos() - node2->pos();
152
m_perfectBoneLengths[i] = sqrt(pow(dist.x(),2) + pow(dist.y(),2));
158
StickMan::~StickMan()
162
void StickMan::childPositionChanged()
164
prepareGeometryChange();
167
void StickMan::setDrawSticks(bool on)
170
for (int i=0;i<nodeCount();++i) {
171
Node *node = m_nodes[i];
172
node->setVisible(on);
176
QRectF StickMan::boundingRect() const
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);
182
int StickMan::nodeCount() const
187
Node *StickMan::node(int idx) const
189
if (idx >= 0 && idx < NodeCount)
195
void StickMan::timerEvent(QTimerEvent *)
200
void StickMan::stabilize()
202
static const qreal threshold = 0.001;
204
for (int i=0; i<BoneCount; ++i) {
205
int n1 = Bones[i * 2];
206
int n2 = Bones[i * 2 + 1];
208
Node *node1 = m_nodes[n1];
209
Node *node2 = m_nodes[n2];
211
QPointF pos1 = node1->pos();
212
QPointF pos2 = node2->pos();
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;
218
QPointF p = dist * (0.5 * diff);
219
if (p.x() > threshold && p.y() > threshold) {
223
node1->setPos(pos1);
224
node2->setPos(pos2);
229
QPointF StickMan::posFor(int idx) const
231
return m_nodes[idx]->pos();
234
<span class="comment"> //#include <QTime></span>
235
void StickMan::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
237
<span class="comment">/* static int frames = 0;
239
if (frames++ % 100 == 0) {
244
if (time.elapsed() > 0) {
245
painter->setPen(Qt::white);
246
painter->drawText(0, 0, QString::number(frames / (time.elapsed() / 1000.0)));
251
painter->setPen(Qt::white);
252
for (int i=0; i<BoneCount; ++i) {
253
int n1 = Bones[i * 2];
254
int n2 = Bones[i * 2 + 1];
256
Node *node1 = m_nodes[n1];
257
Node *node2 = m_nodes[n2];
259
painter->drawLine(node1->pos(), node2->pos());
262
<span class="comment">// first bone is neck and will be used for head</span>
265
path.moveTo(posFor(0));
266
path.lineTo(posFor(1));
268
<span class="comment">// right arm</span>
269
path.lineTo(posFor(2));
270
path.lineTo(posFor(6));
271
path.lineTo(posFor(7));
273
<span class="comment">// left arm</span>
274
path.moveTo(posFor(3));
275
path.lineTo(posFor(8));
276
path.lineTo(posFor(9));
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));
287
<span class="comment">// right leg</span>
288
path.moveTo(posFor(10));
289
path.lineTo(posFor(12));
290
path.lineTo(posFor(13));
292
<span class="comment">// left leg</span>
293
path.moveTo(posFor(11));
294
path.lineTo(posFor(14));
295
path.lineTo(posFor(15));
297
painter->setPen(QPen(m_penColor, 5.0, Qt::SolidLine, Qt::RoundCap));
298
painter->drawPath(path);
303
Node *node1 = m_nodes[n1];
304
Node *node2 = m_nodes[n2];
306
QPointF dist = node2->pos() - node1->pos();
308
qreal sinAngle = dist.x() / sqrt(pow(dist.x(), 2) + pow(dist.y(), 2));
309
qreal angle = asin(sinAngle) * 180.0 / M_PI;
311
QPointF headPos = node1->pos();
312
painter->translate(headPos);
313
painter->rotate(-angle);
315
painter->setBrush(m_fillColor);
316
painter->drawEllipse(QPointF(0,0), 50.0, 50.0);
318
painter->setBrush(m_penColor);
319
painter->setPen(QPen(m_penColor, 2.5, Qt::SolidLine, Qt::RoundCap));
321
<span class="comment">// eyes</span>
323
painter->drawLine(-30.0, -30.0, -20.0, -20.0);
324
painter->drawLine(-20.0, -30.0, -30.0, -20.0);
326
painter->drawLine(20.0, -30.0, 30.0, -20.0);
327
painter->drawLine(30.0, -30.0, 20.0, -20.0);
329
painter->drawChord(QRectF(-30.0, -30.0, 25.0, 70.0), 30.0*16, 120.0*16);
330
painter->drawChord(QRectF(5.0, -30.0, 25.0, 70.0), 30.0*16, 120.0*16);
333
<span class="comment">// mouth</span>
335
painter->drawLine(-28.0, 2.0, 29.0, 2.0);
337
painter->setBrush(QColor(128, 0, 64 ));
338
painter->drawChord(QRectF(-28.0, 2.0-55.0/2.0, 57.0, 55.0), 0.0, -180.0*16);
341
<span class="comment">// pupils</span>
343
painter->setPen(QPen(m_fillColor, 1.0, Qt::SolidLine, Qt::RoundCap));
344
painter->setBrush(m_fillColor);
345
painter->drawEllipse(QPointF(-12.0, -25.0), 5.0, 5.0);
346
painter->drawEllipse(QPointF(22.0, -25.0), 5.0, 5.0);
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 © 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>