mouse.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2006-2007 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of the example classes of the Qt Toolkit.
6 **
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://trolltech.com/products/qt/licenses/licensing/opensource/
13 **
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
17 ** or contact the sales department at sales@trolltech.com.
18 **
19 ** In addition, as a special exception, Trolltech gives you certain
20 ** additional rights. These rights are described in the Trolltech GPL
21 ** Exception version 1.0, which can be found at
22 ** http://www.trolltech.com/products/qt/gplexception/ and in the file
23 ** GPL_EXCEPTION.txt in this package.
24 **
25 ** In addition, as a special exception, Trolltech, as the sole copyright
26 ** holder for Qt Designer, grants users of the Qt/Eclipse Integration
27 ** plug-in the right for the Qt/Eclipse Integration to link to
28 ** functionality provided by Qt Designer and its related libraries.
29 **
30 ** Trolltech reserves all rights not expressly granted herein.
31 **
32 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
33 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
34 **
35 ****************************************************************************/
36 
37 #include "mouse.h"
38 
39 #include "mouse.moc"
40 
41 #include <QGraphicsScene>
42 #include <QPainter>
43 #include <QStyleOption>
44 
45 #include <math.h>
46 
47 static const double Pi = 3.14159265358979323846264338327950288419717;
48 static double TwoPi = 2.0 * Pi;
49 
50 static qreal normalizeAngle(qreal angle)
51 {
52  while (angle < 0)
53  angle += TwoPi;
54  while (angle > TwoPi)
55  angle -= TwoPi;
56  return angle;
57 }
58 
60  : angle(0), speed(0), mouseEyeDirection(0),
61  color(qrand() % 256, qrand() % 256, qrand() % 256)
62 {
63  rotate(qrand() % (360 * 16));
64  startTimer(1000 / 33);
65 }
66 
67 QRectF Mouse::boundingRect() const
68 {
69  qreal adjust = 0.5;
70  return QRectF(-18 - adjust, -22 - adjust,
71  36 + adjust, 60 + adjust);
72 }
73 
74 QPainterPath Mouse::shape() const
75 {
76  QPainterPath path;
77  path.addRect(-10, -20, 20, 40);
78  return path;
79 }
80 
81 void Mouse::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
82 {
83  // Body
84  painter->setBrush(color);
85  painter->drawEllipse(-10, -20, 20, 40);
86 
87  // Eyes
88  painter->setBrush(Qt::white);
89  painter->drawEllipse(-10, -17, 8, 8);
90  painter->drawEllipse(2, -17, 8, 8);
91 
92  // Nose
93  painter->setBrush(Qt::black);
94  painter->drawEllipse(QRectF(-2, -22, 4, 4));
95 
96  // Pupils
97  painter->drawEllipse(QRectF(-8.0 + mouseEyeDirection, -17, 4, 4));
98  painter->drawEllipse(QRectF(4.0 + mouseEyeDirection, -17, 4, 4));
99 
100  // Ears
101  painter->setBrush(scene()->collidingItems(this).isEmpty() ? Qt::darkYellow : Qt::red);
102  painter->drawEllipse(-17, -12, 16, 16);
103  painter->drawEllipse(1, -12, 16, 16);
104 
105  // Tail
106  QPainterPath path(QPointF(0, 20));
107  path.cubicTo(-5, 22, -5, 22, 0, 25);
108  path.cubicTo(5, 27, 5, 32, 0, 30);
109  path.cubicTo(-5, 32, -5, 42, 0, 35);
110  painter->setBrush(Qt::NoBrush);
111  painter->drawPath(path);
112 }
113 
114 void Mouse::timerEvent(QTimerEvent *)
115 {
116  // Don't move too far away
117  QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0));
118  if (lineToCenter.length() > 150) {
119  qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length());
120  if (lineToCenter.dy() < 0)
121  angleToCenter = TwoPi - angleToCenter;
122  angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2);
123 
124  if (angleToCenter < Pi && angleToCenter > Pi / 4) {
125  // Rotate left
126  angle += (angle < -Pi / 2) ? 0.25 : -0.25;
127  } else if (angleToCenter >= Pi && angleToCenter < (Pi + Pi / 2 + Pi / 4)) {
128  // Rotate right
129  angle += (angle < Pi / 2) ? 0.25 : -0.25;
130  }
131  } else if (::sin(angle) < 0) {
132  angle += 0.25;
133  } else if (::sin(angle) > 0) {
134  angle -= 0.25;
135  }
136 
137  // Try not to crash with any other mice
138  QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF()
139  << mapToScene(0, 0)
140  << mapToScene(-30, -50)
141  << mapToScene(30, -50));
142  foreach (QGraphicsItem *item, dangerMice) {
143  if (item == this)
144  continue;
145 
146  QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0));
147  qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length());
148  if (lineToMouse.dy() < 0)
149  angleToMouse = TwoPi - angleToMouse;
150  angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2);
151 
152  if (angleToMouse >= 0 && angleToMouse < Pi / 2) {
153  // Rotate right
154  angle += 0.5;
155  } else if (angleToMouse <= TwoPi && angleToMouse > (TwoPi - Pi / 2)) {
156  // Rotate left
157  angle -= 0.5;
158  }
159  }
160 
161  // Add some random movement
162  if (dangerMice.size() > 1 && (qrand() % 10) == 0) {
163  if (qrand() % 1)
164  angle += (qrand() % 100) / 500.0;
165  else
166  angle -= (qrand() % 100) / 500.0;
167  }
168 
169  speed += (-50 + qrand() % 100) / 100.0;
170 
171  qreal dx = ::sin(angle) * 10;
172  mouseEyeDirection = (qAbs(dx / 5) < 1) ? 0 : dx / 5;
173 
174  rotate(dx);
175  setPos(mapToParent(0, -(3 + sin(speed) * 3)));
176 }
static const double Pi
Definition: mouse.cpp:47
Mouse()
Definition: mouse.cpp:59
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: mouse.cpp:81
qreal angle
Definition: mouse.h:59
qreal speed
Definition: mouse.h:60
QColor color
Definition: mouse.h:62
QRectF boundingRect() const
Definition: mouse.cpp:67
static double TwoPi
Definition: mouse.cpp:48
static qreal normalizeAngle(qreal angle)
Definition: mouse.cpp:50
qreal mouseEyeDirection
Definition: mouse.h:61
void timerEvent(QTimerEvent *event)
Definition: mouse.cpp:114
QPainterPath shape() const
Definition: mouse.cpp:74


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23