Mouse Class Reference

#include <mouse.h>

Inheritance diagram for Mouse:

Inheritance graph
[legend]
Collaboration diagram for Mouse:

Collaboration graph
[legend]
List of all members.

Detailed Description

Definition at line 30 of file mouse.h.

Public Member Functions

 Mouse ()
QRectF boundingRect () const
QPainterPath shape () const
void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)

Protected Member Functions

void timerEvent (QTimerEvent *event)

Private Attributes

qreal angle
qreal speed
qreal mouseEyeDirection
QColor color


Constructor & Destructor Documentation

Mouse::Mouse (  ) 

Definition at line 44 of file mouse.cpp.

References qrand(), QGraphicsItem::rotate(), and QObject::startTimer().

00045     : angle(0), speed(0), mouseEyeDirection(0),
00046       color(qrand() % 256, qrand() % 256, qrand() % 256)
00047 {
00048     rotate(qrand() % (360 * 16));
00049     startTimer(1000 / 33);
00050 }

Here is the call graph for this function:


Member Function Documentation

QRectF Mouse::boundingRect (  )  const [virtual]

This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be restricted to inside an item's bounding rect. QGraphicsView uses this to determine whether the item requires redrawing.

Although the item's shape can be arbitrary, the bounding rect is always rectangular, and it is unaffected by the items' transformation (scale(), rotate(), etc.).

Reimplement this function to let QGraphicsView determine what parts of the widget, if any, need to be redrawn.

Note: For shapes that paint an outline / stroke, it is important to include half the pen width in the bounding rect. It is not necessary to compensate for antialiasing, though.

Example:

    QRectF CircleItem::boundingRect() const
    {
        qreal penWidth = 1;
        return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
                      diameter + penWidth, diameter + penWidth);
    }

See also:
shape(), contains(), {The Graphics View Coordinate System}

Implements QGraphicsItem.

Definition at line 52 of file mouse.cpp.

00053 {
00054     qreal adjust = 0.5;
00055     return QRectF(-20 - adjust, -22 - adjust,
00056                   40 + adjust, 83 + adjust);
00057 }

QPainterPath Mouse::shape (  )  const [virtual]

Returns the shape of this item as a QPainterPath in local coordinates. The shape is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions.

The default implementation calls boundingRect() to return a simple rectangular shape, but subclasses can reimplement this function to return a more accurate shape for non-rectangular items. For example, a round item may choose to return an elliptic shape for better collision detection. For example:

        QPainterPath RoundItem::shape() const
        {
            QPainterPath path;
            path.addEllipse(boundingRect());
            return path;
        }

This function is called by the default implementations of contains() and collidesWithPath().

See also:
boundingRect(), contains()

Reimplemented from QGraphicsItem.

Definition at line 59 of file mouse.cpp.

References path.

00060 {
00061     QPainterPath path;
00062     path.addRect(-10, -20, 20, 40);
00063     return path;
00064 }

void Mouse::paint ( QPainter painter,
const QStyleOptionGraphicsItem option,
QWidget widget 
) [virtual]

This function, which is usually called by QGraphicsView, paints the contents of an item in local coordinates.

Reimplement this function in a QGraphicsItem subclass to provide the item's painting implementation, using painter. The option parameter provides style options for the item, such as its state, exposed area and its level-of-detail hints. The widget argument is optional. If provided, it points to the widget that is being painted on; otherwise, it is 0.

        void RoundRectItem::paint(QPainter *painter,
                                  const QStyleOptionGraphicsItem *option,
                                  QWidget *widget)
        {
            painter->drawRoundRect(-10, -10, 20, 20);
        }

The painter's pen is 0-width by default, and its pen is initialized to the QPalette::Text brush from the paint device's palette. The brush is initialized to QPalette::Window.

All painting is done in local coordinates.

Implements QGraphicsItem.

Definition at line 66 of file mouse.cpp.

References Qt::black, QGraphicsItem::collidingItems(), color, Qt::darkYellow, QPainter::drawEllipse(), QPainter::drawPath(), isEmpty(), mouseEyeDirection, Qt::NoBrush, path, Qt::red, QGraphicsItem::scene(), QPainter::setBrush(), and Qt::white.

00067 {
00068     // Body
00069     painter->setBrush(color);
00070     painter->drawEllipse(-10, -20, 20, 40);
00071 
00072     // Eyes
00073     painter->setBrush(Qt::white);
00074     painter->drawEllipse(-10, -17, 8, 8);
00075     painter->drawEllipse(2, -17, 8, 8);
00076 
00077     // Nose
00078     painter->setBrush(Qt::black);
00079     painter->drawEllipse(QRectF(-2, -22, 4, 4));
00080 
00081     // Pupils
00082     painter->drawEllipse(QRectF(-8.0 + mouseEyeDirection, -17, 4, 4));
00083     painter->drawEllipse(QRectF(4.0 + mouseEyeDirection, -17, 4, 4));
00084 
00085     // Ears
00086     painter->setBrush(scene()->collidingItems(this).isEmpty() ? Qt::darkYellow : Qt::red);
00087     painter->drawEllipse(-17, -12, 16, 16);
00088     painter->drawEllipse(1, -12, 16, 16);
00089 
00090     // Tail
00091     QPainterPath path(QPointF(0, 20));
00092     path.cubicTo(-5, 22, -5, 22, 0, 25);
00093     path.cubicTo(5, 27, 5, 32, 0, 30);
00094     path.cubicTo(-5, 32, -5, 42, 0, 35);
00095     painter->setBrush(Qt::NoBrush);
00096     painter->drawPath(path);
00097 }

Here is the call graph for this function:

void Mouse::timerEvent ( QTimerEvent event  )  [protected, virtual]

This event handler can be reimplemented in a subclass to receive timer events for the object.

QTimer provides a higher-level interface to the timer functionality, and also more general information about timers. The timer event is passed in the event parameter.

See also:
startTimer(), killTimer(), event()

Reimplemented from QObject.

Definition at line 99 of file mouse.cpp.

References angle, QLineF::dx(), QLineF::dy(), QGraphicsScene::items(), QLineF::length(), QGraphicsItem::mapFromItem(), QGraphicsItem::mapFromScene(), QGraphicsItem::mapToParent(), QGraphicsItem::mapToScene(), mouseEyeDirection, normalizeAngle(), Pi, qrand(), QGraphicsItem::rotate(), QGraphicsItem::scene(), QGraphicsItem::setPos(), QList< T >::size(), speed, and TwoPi.

00100 {
00101     // Don't move too far away
00102     QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0));
00103     if (lineToCenter.length() > 150) {
00104         qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length());
00105         if (lineToCenter.dy() < 0)
00106             angleToCenter = TwoPi - angleToCenter;
00107         angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2);
00108 
00109         if (angleToCenter < Pi && angleToCenter > Pi / 4) {
00110             // Rotate left
00111             angle += (angle < -Pi / 2) ? 0.25 : -0.25;
00112         } else if (angleToCenter >= Pi && angleToCenter < (Pi + Pi / 2 + Pi / 4)) {
00113             // Rotate right
00114             angle += (angle < Pi / 2) ? 0.25 : -0.25;
00115         }
00116     } else if (::sin(angle) < 0) {
00117         angle += 0.25;
00118     } else if (::sin(angle) > 0) {
00119         angle -= 0.25;
00120     }
00121 
00122     // Try not to crash with any other mice
00123     QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF()
00124                                                        << mapToScene(0, 0)
00125                                                        << mapToScene(-30, -50)
00126                                                        << mapToScene(30, -50));
00127     foreach (QGraphicsItem *item, dangerMice) {
00128         if (item == this)
00129             continue;
00130         
00131         QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0));
00132         qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length());
00133         if (lineToMouse.dy() < 0)
00134             angleToMouse = TwoPi - angleToMouse;
00135         angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2);
00136 
00137         if (angleToMouse >= 0 && angleToMouse < Pi / 2) {
00138             // Rotate right
00139             angle += 0.5;
00140         } else if (angleToMouse <= TwoPi && angleToMouse > (TwoPi - Pi / 2)) {
00141             // Rotate left
00142             angle -= 0.5;
00143         }
00144     }
00145 
00146     // Add some random movement
00147     if (dangerMice.size() > 1 && (qrand() % 10) == 0) {
00148         if (qrand() % 1)
00149             angle += (qrand() % 100) / 500.0;
00150         else
00151             angle -= (qrand() % 100) / 500.0;
00152     }
00153 
00154     speed += (-50 + qrand() % 100) / 100.0;
00155 
00156     qreal dx = ::sin(angle) * 10;
00157     mouseEyeDirection = (qAbs(dx / 5) < 1) ? 0 : dx / 5;
00158 
00159     rotate(dx);
00160     setPos(mapToParent(0, -(3 + sin(speed) * 3)));
00161 }

Here is the call graph for this function:


Member Data Documentation

qreal Mouse::angle [private]

Definition at line 46 of file mouse.h.

Referenced by timerEvent().

qreal Mouse::speed [private]

Definition at line 47 of file mouse.h.

Referenced by timerEvent().

qreal Mouse::mouseEyeDirection [private]

Definition at line 48 of file mouse.h.

Referenced by paint(), and timerEvent().

QColor Mouse::color [private]

Definition at line 49 of file mouse.h.

Referenced by paint().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 15:14:07 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1