Car Class Reference

#include <car.h>

Inheritance diagram for Car:

Inheritance graph
[legend]
Collaboration diagram for Car:

Collaboration graph
[legend]
List of all members.

Detailed Description

Definition at line 31 of file car.h.

Public Slots

void accelerate ()
void decelerate ()
void turnLeft ()
void turnRight ()

Signals

void crashed ()

Public Member Functions

 Car ()
QRectF boundingRect () const

Protected Member Functions

void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
void timerEvent (QTimerEvent *event)

Private Attributes

QBrush color
qreal wheelsAngle
qreal speed


Constructor & Destructor Documentation

Car::Car (  ) 

Definition at line 35 of file car.cpp.

References QGraphicsItem::ItemIsFocusable, QGraphicsItem::ItemIsMovable, QGraphicsItem::setFlag(), and QObject::startTimer().

00035          : color(Qt::green), wheelsAngle(0), speed(0)
00036 {
00037     startTimer(1000 / 33);
00038     setFlag(QGraphicsItem::ItemIsMovable, true);
00039     setFlag(QGraphicsItem::ItemIsFocusable, true);
00040 }

Here is the call graph for this function:


Member Function Documentation

QRectF Car::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 30 of file car.cpp.

00031 {
00032     return QRectF(-35, -81, 70, 115);
00033 }

void Car::accelerate (  )  [slot]

Definition at line 42 of file car.cpp.

References speed.

00043 {
00044     if (speed < 10)
00045         ++speed;
00046 }

void Car::decelerate (  )  [slot]

Definition at line 48 of file car.cpp.

References speed.

00049 {
00050     if (speed > -10)
00051         --speed;
00052 }

void Car::turnLeft (  )  [slot]

Definition at line 54 of file car.cpp.

References wheelsAngle.

00055 {
00056     if (wheelsAngle > -30)
00057         wheelsAngle -= 5;
00058 }

void Car::turnRight (  )  [slot]

Definition at line 60 of file car.cpp.

References wheelsAngle.

00061 {
00062     if (wheelsAngle < 30)
00063        wheelsAngle += 5;
00064 }

void Car::crashed (  )  [signal]

void Car::paint ( QPainter painter,
const QStyleOptionGraphicsItem option,
QWidget widget = 0 
) [protected, 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 car.cpp.

References Qt::black, color, QPainter::drawEllipse(), QPainter::drawPie(), QPainter::drawRect(), Qt::gray, QPainter::restore(), QPainter::rotate(), QPainter::save(), QPainter::setBrush(), QPainter::translate(), and wheelsAngle.

00067 {
00068     Q_UNUSED(option);
00069     Q_UNUSED(widget);
00070 
00071     painter->setBrush(Qt::gray);
00072     painter->drawRect(-20, -58, 40, 2); // front axel
00073     painter->drawRect(-20, 7, 40, 2); // rear axel
00074 
00075     painter->setBrush(color);
00076     painter->drawRect(-25, -79, 50, 10); // front wing
00077 
00078     painter->drawEllipse(-25, -48, 50, 20); // side pods
00079     painter->drawRect(-25, -38, 50, 35); // side pods
00080     painter->drawRect(-5, 9, 10, 10); // back pod
00081 
00082     painter->drawEllipse(-10, -81, 20, 100); // main body
00083 
00084     painter->drawRect(-17, 19, 34, 15); // rear wing
00085 
00086     painter->setBrush(Qt::black);
00087     painter->drawPie(-5, -51, 10, 15, 0, 180 * 16);
00088     painter->drawRect(-5, -44, 10, 10); // cocpit
00089 
00090     painter->save();
00091     painter->translate(-20, -58);
00092     painter->rotate(wheelsAngle);
00093     painter->drawRect(-10, -7, 10, 15); // front left
00094     painter->restore();
00095 
00096     painter->save();
00097     painter->translate(20, -58);
00098     painter->rotate(wheelsAngle);
00099     painter->drawRect(0, -7, 10, 15); // front left
00100     painter->restore();
00101 
00102     painter->drawRect(-30, 0, 12, 17); // rear left
00103     painter->drawRect(19, 0, 12, 17);  // rear right
00104 }

Here is the call graph for this function:

void Car::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 106 of file car.cpp.

References QObject::event(), Pi, QGraphicsItem::rotate(), speed, QGraphicsItem::translate(), QGraphicsItem::update(), and wheelsAngle.

00107 {
00108     Q_UNUSED(event);
00109 
00110     const qreal axelDistance = 54;
00111     qreal wheelsAngleRads = (wheelsAngle * Pi) / 180;
00112     qreal turnDistance = ::cos(wheelsAngleRads) * axelDistance * 2;
00113     qreal turnRateRads = wheelsAngleRads / turnDistance;  // rough estimate
00114     qreal turnRate = (turnRateRads * 180) / Pi;
00115     qreal rotation = speed * turnRate;
00116     
00117     rotate(rotation);
00118     translate(0, -speed);
00119     update();
00120 }

Here is the call graph for this function:


Member Data Documentation

QBrush Car::color [private]

Definition at line 53 of file car.h.

Referenced by paint().

qreal Car::wheelsAngle [private]

Definition at line 54 of file car.h.

Referenced by paint(), timerEvent(), turnLeft(), and turnRight().

qreal Car::speed [private]

Definition at line 55 of file car.h.

Referenced by accelerate(), decelerate(), and timerEvent().


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