| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$ | 
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows: | 
|---|
| 11 | ** | 
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without | 
|---|
| 13 | ** modification, are permitted provided that the following conditions are | 
|---|
| 14 | ** met: | 
|---|
| 15 | **   * Redistributions of source code must retain the above copyright | 
|---|
| 16 | **     notice, this list of conditions and the following disclaimer. | 
|---|
| 17 | **   * Redistributions in binary form must reproduce the above copyright | 
|---|
| 18 | **     notice, this list of conditions and the following disclaimer in | 
|---|
| 19 | **     the documentation and/or other materials provided with the | 
|---|
| 20 | **     distribution. | 
|---|
| 21 | **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor | 
|---|
| 22 | **     the names of its contributors may be used to endorse or promote | 
|---|
| 23 | **     products derived from this software without specific prior written | 
|---|
| 24 | **     permission. | 
|---|
| 25 | ** | 
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
|---|
| 37 | ** $QT_END_LICENSE$ | 
|---|
| 38 | ** | 
|---|
| 39 | ****************************************************************************/ | 
|---|
| 40 |  | 
|---|
| 41 | #include "mouse.h" | 
|---|
| 42 |  | 
|---|
| 43 | #include <QGraphicsScene> | 
|---|
| 44 | #include <QPainter> | 
|---|
| 45 | #include <QStyleOption> | 
|---|
| 46 |  | 
|---|
| 47 | #include <math.h> | 
|---|
| 48 |  | 
|---|
| 49 | static const double Pi = 3.14159265358979323846264338327950288419717; | 
|---|
| 50 | static double TwoPi = 2.0 * Pi; | 
|---|
| 51 |  | 
|---|
| 52 | static qreal normalizeAngle(qreal angle) | 
|---|
| 53 | { | 
|---|
| 54 | while (angle < 0) | 
|---|
| 55 | angle += TwoPi; | 
|---|
| 56 | while (angle > TwoPi) | 
|---|
| 57 | angle -= TwoPi; | 
|---|
| 58 | return angle; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | //! [0] | 
|---|
| 62 | Mouse::Mouse() | 
|---|
| 63 | : angle(0), speed(0), mouseEyeDirection(0), | 
|---|
| 64 | color(qrand() % 256, qrand() % 256, qrand() % 256) | 
|---|
| 65 | { | 
|---|
| 66 | rotate(qrand() % (360 * 16)); | 
|---|
| 67 | startTimer(1000 / 33); | 
|---|
| 68 | } | 
|---|
| 69 | //! [0] | 
|---|
| 70 |  | 
|---|
| 71 | //! [1] | 
|---|
| 72 | QRectF 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] | 
|---|
| 81 | QPainterPath Mouse::shape() const | 
|---|
| 82 | { | 
|---|
| 83 | QPainterPath path; | 
|---|
| 84 | path.addRect(-10, -20, 20, 40); | 
|---|
| 85 | return path; | 
|---|
| 86 | } | 
|---|
| 87 | //! [2] | 
|---|
| 88 |  | 
|---|
| 89 | //! [3] | 
|---|
| 90 | void 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] | 
|---|
| 125 | void Mouse::timerEvent(QTimerEvent *) | 
|---|
| 126 | { | 
|---|
| 127 | //! [4] | 
|---|
| 128 | // Don't move too far away | 
|---|
| 129 | //! [5] | 
|---|
| 130 | QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0)); | 
|---|
| 131 | if (lineToCenter.length() > 150) { | 
|---|
| 132 | qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length()); | 
|---|
| 133 | if (lineToCenter.dy() < 0) | 
|---|
| 134 | angleToCenter = TwoPi - angleToCenter; | 
|---|
| 135 | angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2); | 
|---|
| 136 |  | 
|---|
| 137 | if (angleToCenter < Pi && angleToCenter > Pi / 4) { | 
|---|
| 138 | // Rotate left | 
|---|
| 139 | angle += (angle < -Pi / 2) ? 0.25 : -0.25; | 
|---|
| 140 | } else if (angleToCenter >= Pi && angleToCenter < (Pi + Pi / 2 + Pi / 4)) { | 
|---|
| 141 | // Rotate right | 
|---|
| 142 | angle += (angle < Pi / 2) ? 0.25 : -0.25; | 
|---|
| 143 | } | 
|---|
| 144 | } else if (::sin(angle) < 0) { | 
|---|
| 145 | angle += 0.25; | 
|---|
| 146 | } else if (::sin(angle) > 0) { | 
|---|
| 147 | angle -= 0.25; | 
|---|
| 148 | //! [5] //! [6] | 
|---|
| 149 | } | 
|---|
| 150 | //! [6] | 
|---|
| 151 |  | 
|---|
| 152 | // Try not to crash with any other mice | 
|---|
| 153 | //! [7] | 
|---|
| 154 | QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF() | 
|---|
| 155 | << mapToScene(0, 0) | 
|---|
| 156 | << mapToScene(-30, -50) | 
|---|
| 157 | << mapToScene(30, -50)); | 
|---|
| 158 | foreach (QGraphicsItem *item, dangerMice) { | 
|---|
| 159 | if (item == this) | 
|---|
| 160 | continue; | 
|---|
| 161 |  | 
|---|
| 162 | QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0)); | 
|---|
| 163 | qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length()); | 
|---|
| 164 | if (lineToMouse.dy() < 0) | 
|---|
| 165 | angleToMouse = TwoPi - angleToMouse; | 
|---|
| 166 | angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2); | 
|---|
| 167 |  | 
|---|
| 168 | if (angleToMouse >= 0 && angleToMouse < Pi / 2) { | 
|---|
| 169 | // Rotate right | 
|---|
| 170 | angle += 0.5; | 
|---|
| 171 | } else if (angleToMouse <= TwoPi && angleToMouse > (TwoPi - Pi / 2)) { | 
|---|
| 172 | // Rotate left | 
|---|
| 173 | angle -= 0.5; | 
|---|
| 174 | //! [7] //! [8] | 
|---|
| 175 | } | 
|---|
| 176 | //! [8] //! [9] | 
|---|
| 177 | } | 
|---|
| 178 | //! [9] | 
|---|
| 179 |  | 
|---|
| 180 | // Add some random movement | 
|---|
| 181 | //! [10] | 
|---|
| 182 | if (dangerMice.size() > 1 && (qrand() % 10) == 0) { | 
|---|
| 183 | if (qrand() % 1) | 
|---|
| 184 | angle += (qrand() % 100) / 500.0; | 
|---|
| 185 | else | 
|---|
| 186 | angle -= (qrand() % 100) / 500.0; | 
|---|
| 187 | } | 
|---|
| 188 | //! [10] | 
|---|
| 189 |  | 
|---|
| 190 | //! [11] | 
|---|
| 191 | speed += (-50 + qrand() % 100) / 100.0; | 
|---|
| 192 |  | 
|---|
| 193 | qreal dx = ::sin(angle) * 10; | 
|---|
| 194 | mouseEyeDirection = (qAbs(dx / 5) < 1) ? 0 : dx / 5; | 
|---|
| 195 |  | 
|---|
| 196 | rotate(dx); | 
|---|
| 197 | setPos(mapToParent(0, -(3 + sin(speed) * 3))); | 
|---|
| 198 | } | 
|---|
| 199 | //! [11] | 
|---|