examples/multitouch/pinchzoom/mouse.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 
       
    50 static const double Pi = 3.14159265358979323846264338327950288419717;
       
    51 static double TwoPi = 2.0 * Pi;
       
    52 
       
    53 static 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]
       
    63 Mouse::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]
       
    73 QRectF 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]
       
    82 QPainterPath Mouse::shape() const
       
    83 {
       
    84     QPainterPath path;
       
    85     path.addRect(-10, -20, 20, 40);
       
    86     return path;
       
    87 }
       
    88 //! [2]
       
    89 
       
    90 //! [3]
       
    91 void 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]
       
   126 void 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]