~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/tutorial/t12/cannonfield.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
/****************************************************************
 
30
**
 
31
** Implementation CannonField class, Qt tutorial 12
 
32
**
 
33
****************************************************************/
 
34
 
 
35
#include <QDateTime>
 
36
#include <QPaintEvent>
 
37
#include <QPainter>
 
38
#include <QTimer>
 
39
 
 
40
#include <math.h>
 
41
#include <stdlib.h>
 
42
 
 
43
#include "cannonfield.h"
 
44
 
 
45
CannonField::CannonField(QWidget *parent)
 
46
    : QWidget(parent)
 
47
{
 
48
    currentAngle = 45;
 
49
    currentForce = 0;
 
50
    timerCount = 0;
 
51
    autoShootTimer = new QTimer(this);
 
52
    connect(autoShootTimer, SIGNAL(timeout()), this, SLOT(moveShot()));
 
53
    shootAngle = 0;
 
54
    shootForce = 0;
 
55
    target = QPoint(0, 0);
 
56
    setPalette(QPalette(QColor(250, 250, 200)));
 
57
    newTarget();
 
58
}
 
59
 
 
60
void CannonField::setAngle(int angle)
 
61
{
 
62
    if (angle < 5)
 
63
        angle = 5;
 
64
    if (angle > 70)
 
65
        angle = 70;
 
66
    if (currentAngle == angle)
 
67
        return;
 
68
    currentAngle = angle;
 
69
    update(cannonRect());
 
70
    emit angleChanged(currentAngle);
 
71
}
 
72
 
 
73
void CannonField::setForce(int force)
 
74
{
 
75
    if (force < 0)
 
76
        force = 0;
 
77
    if (currentForce == force)
 
78
        return;
 
79
    currentForce = force;
 
80
    emit forceChanged(currentForce);
 
81
}
 
82
 
 
83
void CannonField::shoot()
 
84
{
 
85
    if (autoShootTimer->isActive())
 
86
        return;
 
87
    timerCount = 0;
 
88
    shootAngle = currentAngle;
 
89
    shootForce = currentForce;
 
90
    autoShootTimer->start(5);
 
91
}
 
92
 
 
93
void CannonField::newTarget()
 
94
{
 
95
    static bool firstTime = true;
 
96
 
 
97
    if (firstTime) {
 
98
        firstTime = false;
 
99
        QTime midnight(0, 0, 0);
 
100
        srand(midnight.secsTo(QTime::currentTime()));
 
101
    }
 
102
    target = QPoint(200 + rand() % 190, 10 + rand() % 255);
 
103
    update();
 
104
}
 
105
 
 
106
void CannonField::moveShot()
 
107
{
 
108
    QRegion region = shotRect();
 
109
    ++timerCount;
 
110
 
 
111
    QRect shotR = shotRect();
 
112
 
 
113
    if (shotR.intersects(targetRect())) {
 
114
        autoShootTimer->stop();
 
115
        emit hit();
 
116
    } else if (shotR.x() > width() || shotR.y() > height()) {
 
117
        autoShootTimer->stop();
 
118
        emit missed();
 
119
    } else {
 
120
        region = region.unite(shotR);
 
121
    }
 
122
    update(region);
 
123
}
 
124
 
 
125
void CannonField::paintEvent(QPaintEvent * /* event */)
 
126
{
 
127
    QPainter painter(this);
 
128
 
 
129
    paintCannon(painter);
 
130
    if (autoShootTimer->isActive())
 
131
        paintShot(painter);
 
132
    paintTarget(painter);
 
133
}
 
134
 
 
135
void CannonField::paintShot(QPainter &painter)
 
136
{
 
137
    painter.setPen(Qt::NoPen);
 
138
    painter.setBrush(Qt::black);
 
139
    painter.drawRect(shotRect());
 
140
}
 
141
 
 
142
void CannonField::paintTarget(QPainter &painter)
 
143
{
 
144
    painter.setPen(Qt::black);
 
145
    painter.setBrush(Qt::red);
 
146
    painter.drawRect(targetRect());
 
147
}
 
148
 
 
149
const QRect barrelRect(33, -4, 15, 8);
 
150
 
 
151
void CannonField::paintCannon(QPainter &painter)
 
152
{
 
153
    painter.setPen(Qt::NoPen);
 
154
    painter.setBrush(Qt::blue);
 
155
 
 
156
    painter.save();
 
157
    painter.translate(0, height());
 
158
    painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16);
 
159
    painter.rotate(-currentAngle);
 
160
    painter.drawRect(barrelRect);
 
161
    painter.restore();
 
162
}
 
163
 
 
164
QRect CannonField::cannonRect() const
 
165
{
 
166
    QRect result(0, 0, 50, 50);
 
167
    result.moveBottomLeft(rect().bottomLeft());
 
168
    return result;
 
169
}
 
170
 
 
171
QRect CannonField::shotRect() const
 
172
{
 
173
    const double gravity = 4;
 
174
 
 
175
    double time = timerCount / 40.0;
 
176
    double velocity = shootForce;
 
177
    double radians = shootAngle * 3.14159265 / 180;
 
178
 
 
179
    double velx = velocity * cos(radians);
 
180
    double vely = velocity * sin(radians);
 
181
    double x0 = (barrelRect.right() + 5) * cos(radians);
 
182
    double y0 = (barrelRect.right() + 5) * sin(radians);
 
183
    double x = x0 + velx * time;
 
184
    double y = y0 + vely * time - 0.5 * gravity * time * time;
 
185
 
 
186
    QRect result(0, 0, 6, 6);
 
187
    result.moveCenter(QPoint(qRound(x), height() - 1 - qRound(y)));
 
188
    return result;
 
189
}
 
190
 
 
191
QRect CannonField::targetRect() const
 
192
{
 
193
    QRect result(0, 0, 20, 10);
 
194
    result.moveCenter(QPoint(target.x(), height() - 1 - target.y()));
 
195
    return result;
 
196
}