1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "mouse.h"
52
53#include <QGraphicsScene>
54#include <QPainter>
55#include <QRandomGenerator>
56#include <QStyleOption>
57#include <qmath.h>
58
59constexpr qreal Pi = M_PI;
60constexpr qreal TwoPi = 2 * M_PI;
61
62static qreal normalizeAngle(qreal angle)
63{
64 while (angle < 0)
65 angle += TwoPi;
66 while (angle > TwoPi)
67 angle -= TwoPi;
68 return angle;
69}
70
71//! [0]
72Mouse::Mouse() : color(QRandomGenerator::global()->bounded(256),
73 QRandomGenerator::global()->bounded(256),
74 QRandomGenerator::global()->bounded(256))
75{
76 setTransform(QTransform().rotate(QRandomGenerator::global()->bounded(360 * 16)), true);
77 startTimer(1000 / 33);
78}
79//! [0]
80
81//! [1]
82QRectF Mouse::boundingRect() const
83{
84 qreal adjust = 0.5;
85 return QRectF(-18 - adjust, -22 - adjust,
86 36 + adjust, 60 + adjust);
87}
88//! [1]
89
90//! [2]
91QPainterPath Mouse::shape() const
92{
93 QPainterPath path;
94 path.addRect(-10, -20, 20, 40);
95 return path;
96}
97//! [2]
98
99//! [3]
100void Mouse::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
101{
102 // Body
103 painter->setBrush(color);
104 painter->drawEllipse(-10, -20, 20, 40);
105
106 // Eyes
107 painter->setBrush(Qt::white);
108 painter->drawEllipse(-10, -17, 8, 8);
109 painter->drawEllipse(2, -17, 8, 8);
110
111 // Nose
112 painter->setBrush(Qt::black);
113 painter->drawEllipse(QRectF(-2, -22, 4, 4));
114
115 // Pupils
116 painter->drawEllipse(QRectF(-8.0 + mouseEyeDirection, -17, 4, 4));
117 painter->drawEllipse(QRectF(4.0 + mouseEyeDirection, -17, 4, 4));
118
119 // Ears
120 painter->setBrush(scene()->collidingItems(this).isEmpty() ? Qt::darkYellow : Qt::red);
121 painter->drawEllipse(-17, -12, 16, 16);
122 painter->drawEllipse(1, -12, 16, 16);
123
124 // Tail
125 QPainterPath path(QPointF(0, 20));
126 path.cubicTo(-5, 22, -5, 22, 0, 25);
127 path.cubicTo(5, 27, 5, 32, 0, 30);
128 path.cubicTo(-5, 32, -5, 42, 0, 35);
129 painter->setBrush(Qt::NoBrush);
130 painter->drawPath(path);
131}
132//! [3]
133
134//! [4]
135void Mouse::timerEvent(QTimerEvent *)
136{
137//! [4]
138 // Don't move too far away
139//! [5]
140 QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0));
141 if (lineToCenter.length() > 150) {
142 qreal angleToCenter = std::atan2(lineToCenter.dy(), lineToCenter.dx());
143 angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2);
144
145 if (angleToCenter < Pi && angleToCenter > Pi / 4) {
146 // Rotate left
147 angle += (angle < -Pi / 2) ? 0.25 : -0.25;
148 } else if (angleToCenter >= Pi && angleToCenter < (Pi + Pi / 2 + Pi / 4)) {
149 // Rotate right
150 angle += (angle < Pi / 2) ? 0.25 : -0.25;
151 }
152 } else if (::sin(angle) < 0) {
153 angle += 0.25;
154 } else if (::sin(angle) > 0) {
155 angle -= 0.25;
156//! [5] //! [6]
157 }
158//! [6]
159
160 // Try not to crash with any other mice
161//! [7]
162 QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF()
163 << mapToScene(0, 0)
164 << mapToScene(-30, -50)
165 << mapToScene(30, -50));
166 for (QGraphicsItem *item : dangerMice) {
167 if (item == this)
168 continue;
169
170 QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0));
171 qreal angleToMouse = std::atan2(lineToMouse.dy(), lineToMouse.dx());
172 angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2);
173
174 if (angleToMouse >= 0 && angleToMouse < Pi / 2) {
175 // Rotate right
176 angle += 0.5;
177 } else if (angleToMouse <= TwoPi && angleToMouse > (TwoPi - Pi / 2)) {
178 // Rotate left
179 angle -= 0.5;
180//! [7] //! [8]
181 }
182//! [8] //! [9]
183 }
184//! [9]
185
186 // Add some random movement
187//! [10]
188 if (dangerMice.size() > 1 && QRandomGenerator::global()->bounded(10) == 0) {
189 if (QRandomGenerator::global()->bounded(1))
190 angle += QRandomGenerator::global()->bounded(1 / 500.0);
191 else
192 angle -= QRandomGenerator::global()->bounded(1 / 500.0);
193 }
194//! [10]
195
196//! [11]
197 speed += (-50 + QRandomGenerator::global()->bounded(100)) / 100.0;
198
199 qreal dx = ::sin(angle) * 10;
200 mouseEyeDirection = (qAbs(dx / 5) < 1) ? 0 : dx / 5;
201
202 setTransform(QTransform().rotate(dx), true);
203 setPos(mapToParent(0, -(3 + sin(speed) * 3)));
204}
205//! [11]
206