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 <QtMath>
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(highest: 256),
73 QRandomGenerator::global()->bounded(highest: 256),
74 QRandomGenerator::global()->bounded(highest: 256))
75{
76 setRotation(QRandomGenerator::global()->bounded(highest: 360 * 16));
77}
78//! [0]
79
80//! [1]
81QRectF Mouse::boundingRect() const
82{
83 qreal adjust = 0.5;
84 return QRectF(-18 - adjust, -22 - adjust,
85 36 + adjust, 60 + adjust);
86}
87//! [1]
88
89//! [2]
90QPainterPath Mouse::shape() const
91{
92 QPainterPath path;
93 path.addRect(x: -10, y: -20, w: 20, h: 40);
94 return path;
95}
96//! [2]
97
98//! [3]
99void Mouse::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
100{
101 // Body
102 painter->setBrush(color);
103 painter->drawEllipse(x: -10, y: -20, w: 20, h: 40);
104
105 // Eyes
106 painter->setBrush(Qt::white);
107 painter->drawEllipse(x: -10, y: -17, w: 8, h: 8);
108 painter->drawEllipse(x: 2, y: -17, w: 8, h: 8);
109
110 // Nose
111 painter->setBrush(Qt::black);
112 painter->drawEllipse(r: QRectF(-2, -22, 4, 4));
113
114 // Pupils
115 painter->drawEllipse(r: QRectF(-8.0 + mouseEyeDirection, -17, 4, 4));
116 painter->drawEllipse(r: QRectF(4.0 + mouseEyeDirection, -17, 4, 4));
117
118 // Ears
119 painter->setBrush(scene()->collidingItems(item: this).isEmpty() ? Qt::darkYellow : Qt::red);
120 painter->drawEllipse(x: -17, y: -12, w: 16, h: 16);
121 painter->drawEllipse(x: 1, y: -12, w: 16, h: 16);
122
123 // Tail
124 QPainterPath path(QPointF(0, 20));
125 path.cubicTo(ctrlPt1x: -5, ctrlPt1y: 22, ctrlPt2x: -5, ctrlPt2y: 22, endPtx: 0, endPty: 25);
126 path.cubicTo(ctrlPt1x: 5, ctrlPt1y: 27, ctrlPt2x: 5, ctrlPt2y: 32, endPtx: 0, endPty: 30);
127 path.cubicTo(ctrlPt1x: -5, ctrlPt1y: 32, ctrlPt2x: -5, ctrlPt2y: 42, endPtx: 0, endPty: 35);
128 painter->setBrush(Qt::NoBrush);
129 painter->drawPath(path);
130}
131//! [3]
132
133//! [4]
134void Mouse::advance(int step)
135{
136 if (!step)
137 return;
138//! [4]
139 // Don't move too far away
140//! [5]
141 QLineF lineToCenter(QPointF(0, 0), mapFromScene(ax: 0, ay: 0));
142 if (lineToCenter.length() > 150) {
143 qreal angleToCenter = std::atan2(y: lineToCenter.dy(), x: lineToCenter.dx());
144 angleToCenter = normalizeAngle(angle: (Pi - angleToCenter) + Pi / 2);
145
146 if (angleToCenter < Pi && angleToCenter > Pi / 4) {
147 // Rotate left
148 angle += (angle < -Pi / 2) ? 0.25 : -0.25;
149 } else if (angleToCenter >= Pi && angleToCenter < (Pi + Pi / 2 + Pi / 4)) {
150 // Rotate right
151 angle += (angle < Pi / 2) ? 0.25 : -0.25;
152 }
153 } else if (::sin(x: angle) < 0) {
154 angle += 0.25;
155 } else if (::sin(x: angle) > 0) {
156 angle -= 0.25;
157//! [5] //! [6]
158 }
159//! [6]
160
161 // Try not to crash with any other mice
162//! [7]
163 const QList<QGraphicsItem *> dangerMice = scene()->items(polygon: QPolygonF()
164 << mapToScene(ax: 0, ay: 0)
165 << mapToScene(ax: -30, ay: -50)
166 << mapToScene(ax: 30, ay: -50));
167
168 for (const QGraphicsItem *item : dangerMice) {
169 if (item == this)
170 continue;
171
172 QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, ax: 0, ay: 0));
173 qreal angleToMouse = std::atan2(y: lineToMouse.dy(), x: lineToMouse.dx());
174 angleToMouse = normalizeAngle(angle: (Pi - angleToMouse) + Pi / 2);
175
176 if (angleToMouse >= 0 && angleToMouse < Pi / 2) {
177 // Rotate right
178 angle += 0.5;
179 } else if (angleToMouse <= TwoPi && angleToMouse > (TwoPi - Pi / 2)) {
180 // Rotate left
181 angle -= 0.5;
182//! [7] //! [8]
183 }
184//! [8] //! [9]
185 }
186//! [9]
187
188 // Add some random movement
189//! [10]
190 if (dangerMice.size() > 1 && QRandomGenerator::global()->bounded(highest: 10) == 0) {
191 if (QRandomGenerator::global()->bounded(highest: 1))
192 angle += QRandomGenerator::global()->bounded(highest: 1 / 500.0);
193 else
194 angle -= QRandomGenerator::global()->bounded(highest: 1 / 500.0);
195 }
196//! [10]
197
198//! [11]
199 speed += (-50 + QRandomGenerator::global()->bounded(highest: 100)) / 100.0;
200
201 qreal dx = ::sin(x: angle) * 10;
202 mouseEyeDirection = (qAbs(t: dx / 5) < 1) ? 0 : dx / 5;
203
204 setRotation(rotation() + dx);
205 setPos(mapToParent(ax: 0, ay: -(3 + sin(x: speed) * 3)));
206}
207//! [11]
208

source code of qtbase/examples/widgets/graphicsview/collidingmice/mouse.cpp