source: trunk/examples/multitouch/pinchzoom/mouse.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

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