|
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 "car.h" |
|
43 #include <QtGui/QtGui> |
|
44 #include <math.h> |
|
45 |
|
46 static const double Pi = 3.14159265358979323846264338327950288419717; |
|
47 |
|
48 QRectF Car::boundingRect() const |
|
49 { |
|
50 return QRectF(-35, -81, 70, 115); |
|
51 } |
|
52 |
|
53 Car::Car() : color(Qt::green), wheelsAngle(0), speed(0) |
|
54 { |
|
55 startTimer(1000 / 33); |
|
56 setFlag(QGraphicsItem::ItemIsMovable, true); |
|
57 setFlag(QGraphicsItem::ItemIsFocusable, true); |
|
58 } |
|
59 |
|
60 void Car::accelerate() |
|
61 { |
|
62 if (speed < 10) |
|
63 ++speed; |
|
64 } |
|
65 |
|
66 void Car::decelerate() |
|
67 { |
|
68 if (speed > -10) |
|
69 --speed; |
|
70 } |
|
71 |
|
72 void Car::turnLeft() |
|
73 { |
|
74 if (wheelsAngle > -30) |
|
75 wheelsAngle -= 5; |
|
76 } |
|
77 |
|
78 void Car::turnRight() |
|
79 { |
|
80 if (wheelsAngle < 30) |
|
81 wheelsAngle += 5; |
|
82 } |
|
83 |
|
84 void Car::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
85 { |
|
86 Q_UNUSED(option); |
|
87 Q_UNUSED(widget); |
|
88 |
|
89 painter->setBrush(Qt::gray); |
|
90 painter->drawRect(-20, -58, 40, 2); // front axel |
|
91 painter->drawRect(-20, 7, 40, 2); // rear axel |
|
92 |
|
93 painter->setBrush(color); |
|
94 painter->drawRect(-25, -79, 50, 10); // front wing |
|
95 |
|
96 painter->drawEllipse(-25, -48, 50, 20); // side pods |
|
97 painter->drawRect(-25, -38, 50, 35); // side pods |
|
98 painter->drawRect(-5, 9, 10, 10); // back pod |
|
99 |
|
100 painter->drawEllipse(-10, -81, 20, 100); // main body |
|
101 |
|
102 painter->drawRect(-17, 19, 34, 15); // rear wing |
|
103 |
|
104 painter->setBrush(Qt::black); |
|
105 painter->drawPie(-5, -51, 10, 15, 0, 180 * 16); |
|
106 painter->drawRect(-5, -44, 10, 10); // cocpit |
|
107 |
|
108 painter->save(); |
|
109 painter->translate(-20, -58); |
|
110 painter->rotate(wheelsAngle); |
|
111 painter->drawRect(-10, -7, 10, 15); // front left |
|
112 painter->restore(); |
|
113 |
|
114 painter->save(); |
|
115 painter->translate(20, -58); |
|
116 painter->rotate(wheelsAngle); |
|
117 painter->drawRect(0, -7, 10, 15); // front left |
|
118 painter->restore(); |
|
119 |
|
120 painter->drawRect(-30, 0, 12, 17); // rear left |
|
121 painter->drawRect(19, 0, 12, 17); // rear right |
|
122 } |
|
123 |
|
124 void Car::timerEvent(QTimerEvent *event) |
|
125 { |
|
126 Q_UNUSED(event); |
|
127 |
|
128 const qreal axelDistance = 54; |
|
129 qreal wheelsAngleRads = (wheelsAngle * Pi) / 180; |
|
130 qreal turnDistance = ::cos(wheelsAngleRads) * axelDistance * 2; |
|
131 qreal turnRateRads = wheelsAngleRads / turnDistance; // rough estimate |
|
132 qreal turnRate = (turnRateRads * 180) / Pi; |
|
133 qreal rotation = speed * turnRate; |
|
134 |
|
135 rotate(rotation); |
|
136 translate(0, -speed); |
|
137 update(); |
|
138 } |