source: trunk/examples/dbus/remotecontrolledcar/car/car.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 4.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "car.h"
43#include <QtGui/QtGui>
44#include <math.h>
45
46static const double Pi = 3.14159265358979323846264338327950288419717;
47
48QRectF Car::boundingRect() const
49{
50 return QRectF(-35, -81, 70, 115);
51}
52
53Car::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
60void Car::accelerate()
61{
62 if (speed < 10)
63 ++speed;
64}
65
66void Car::decelerate()
67{
68 if (speed > -10)
69 --speed;
70}
71
72void Car::turnLeft()
73{
74 if (wheelsAngle > -30)
75 wheelsAngle -= 5;
76}
77
78void Car::turnRight()
79{
80 if (wheelsAngle < 30)
81 wheelsAngle += 5;
82}
83
84void 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
124void 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}
Note: See TracBrowser for help on using the repository browser.