source: trunk/examples/graphicsview/collidingmice/mouse.cpp@ 385

Last change on this file since 385 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "mouse.h"
43
44#include <QGraphicsScene>
45#include <QPainter>
46#include <QStyleOption>
47
48#include <math.h>
49
50static const double Pi = 3.14159265358979323846264338327950288419717;
51static double TwoPi = 2.0 * Pi;
52
53static qreal normalizeAngle(qreal angle)
54{
55 while (angle < 0)
56 angle += TwoPi;
57 while (angle > TwoPi)
58 angle -= TwoPi;
59 return angle;
60}
61
62//! [0]
63Mouse::Mouse()
64 : angle(0), speed(0), mouseEyeDirection(0),
65 color(qrand() % 256, qrand() % 256, qrand() % 256)
66{
67 rotate(qrand() % (360 * 16));
68}
69//! [0]
70
71//! [1]
72QRectF Mouse::boundingRect() const
73{
74 qreal adjust = 0.5;
75 return QRectF(-18 - adjust, -22 - adjust,
76 36 + adjust, 60 + adjust);
77}
78//! [1]
79
80//! [2]
81QPainterPath Mouse::shape() const
82{
83 QPainterPath path;
84 path.addRect(-10, -20, 20, 40);
85 return path;
86}
87//! [2]
88
89//! [3]
90void Mouse::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
91{
92 // Body
93 painter->setBrush(color);
94 painter->drawEllipse(-10, -20, 20, 40);
95
96 // Eyes
97 painter->setBrush(Qt::white);
98 painter->drawEllipse(-10, -17, 8, 8);
99 painter->drawEllipse(2, -17, 8, 8);
100
101 // Nose
102 painter->setBrush(Qt::black);
103 painter->drawEllipse(QRectF(-2, -22, 4, 4));
104
105 // Pupils
106 painter->drawEllipse(QRectF(-8.0 + mouseEyeDirection, -17, 4, 4));
107 painter->drawEllipse(QRectF(4.0 + mouseEyeDirection, -17, 4, 4));
108
109 // Ears
110 painter->setBrush(scene()->collidingItems(this).isEmpty() ? Qt::darkYellow : Qt::red);
111 painter->drawEllipse(-17, -12, 16, 16);
112 painter->drawEllipse(1, -12, 16, 16);
113
114 // Tail
115 QPainterPath path(QPointF(0, 20));
116 path.cubicTo(-5, 22, -5, 22, 0, 25);
117 path.cubicTo(5, 27, 5, 32, 0, 30);
118 path.cubicTo(-5, 32, -5, 42, 0, 35);
119 painter->setBrush(Qt::NoBrush);
120 painter->drawPath(path);
121}
122//! [3]
123
124//! [4]
125void Mouse::advance(int step)
126{
127 if (!step)
128 return;
129//! [4]
130 // Don't move too far away
131//! [5]
132 QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0));
133 if (lineToCenter.length() > 150) {
134 qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length());
135 if (lineToCenter.dy() < 0)
136 angleToCenter = TwoPi - angleToCenter;
137 angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2);
138
139 if (angleToCenter < Pi && angleToCenter > Pi / 4) {
140 // Rotate left
141 angle += (angle < -Pi / 2) ? 0.25 : -0.25;
142 } else if (angleToCenter >= Pi && angleToCenter < (Pi + Pi / 2 + Pi / 4)) {
143 // Rotate right
144 angle += (angle < Pi / 2) ? 0.25 : -0.25;
145 }
146 } else if (::sin(angle) < 0) {
147 angle += 0.25;
148 } else if (::sin(angle) > 0) {
149 angle -= 0.25;
150//! [5] //! [6]
151 }
152//! [6]
153
154 // Try not to crash with any other mice
155//! [7]
156 QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF()
157 << mapToScene(0, 0)
158 << mapToScene(-30, -50)
159 << mapToScene(30, -50));
160 foreach (QGraphicsItem *item, dangerMice) {
161 if (item == this)
162 continue;
163
164 QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0));
165 qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length());
166 if (lineToMouse.dy() < 0)
167 angleToMouse = TwoPi - angleToMouse;
168 angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2);
169
170 if (angleToMouse >= 0 && angleToMouse < Pi / 2) {
171 // Rotate right
172 angle += 0.5;
173 } else if (angleToMouse <= TwoPi && angleToMouse > (TwoPi - Pi / 2)) {
174 // Rotate left
175 angle -= 0.5;
176//! [7] //! [8]
177 }
178//! [8] //! [9]
179 }
180//! [9]
181
182 // Add some random movement
183//! [10]
184 if (dangerMice.size() > 1 && (qrand() % 10) == 0) {
185 if (qrand() % 1)
186 angle += (qrand() % 100) / 500.0;
187 else
188 angle -= (qrand() % 100) / 500.0;
189 }
190//! [10]
191
192//! [11]
193 speed += (-50 + qrand() % 100) / 100.0;
194
195 qreal dx = ::sin(angle) * 10;
196 mouseEyeDirection = (qAbs(dx / 5) < 1) ? 0 : dx / 5;
197
198 rotate(dx);
199 setPos(mapToParent(0, -(3 + sin(speed) * 3)));
200}
201//! [11]
Note: See TracBrowser for help on using the repository browser.