source: trunk/doc/html/t11-cannon-cpp.html@ 190

Last change on this file since 190 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 7.0 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial.doc:1503 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>t11/cannon.cpp Example File</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>t11/cannon.cpp Example File</h1>
33
34
35<pre>/****************************************************************
36**
37** Implementation CannonField class, Qt tutorial 11
38**
39****************************************************************/
40
41#include "cannon.h"
42#include &lt;<a href="qtimer-h.html">qtimer.h</a>&gt;
43#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
44#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
45
46#include &lt;math.h&gt;
47
48
49<a name="f57"></a>CannonField::CannonField( <a href="qwidget.html">QWidget</a> *parent, const char *name )
50 : <a href="qwidget.html">QWidget</a>( parent, name )
51{
52 ang = 45;
53 f = 0;
54 timerCount = 0;
55 autoShootTimer = new <a href="qtimer.html">QTimer</a>( this, "movement handler" );
56 <a href="qobject.html#connect">connect</a>( autoShootTimer, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()),
57 this, SLOT(moveShot()) );
58 shoot_ang = 0;
59 shoot_f = 0;
60 <a href="qwidget.html#setPalette">setPalette</a>( QPalette( QColor( 250, 250, 200) ) );
61}
62
63
64void <a name="f58"></a>CannonField::setAngle( int degrees )
65{
66 if ( degrees &lt; 5 )
67 degrees = 5;
68 if ( degrees &gt; 70 )
69 degrees = 70;
70 if ( ang == degrees )
71 return;
72 ang = degrees;
73 <a href="qwidget.html#repaint">repaint</a>( cannonRect(), FALSE );
74 emit angleChanged( ang );
75}
76
77
78void <a name="f59"></a>CannonField::setForce( int newton )
79{
80 if ( newton &lt; 0 )
81 newton = 0;
82 if ( f == newton )
83 return;
84 f = newton;
85 emit forceChanged( f );
86}
87
88
89void <a name="f60"></a>CannonField::shoot()
90{
91 if ( autoShootTimer-&gt;<a href="qtimer.html#isActive">isActive</a>() )
92 return;
93 timerCount = 0;
94 shoot_ang = ang;
95 shoot_f = f;
96 autoShootTimer-&gt;<a href="qtimer.html#start">start</a>( 50 );
97}
98
99
100void <a name="f61"></a>CannonField::moveShot()
101{
102 <a href="qregion.html">QRegion</a> r( shotRect() );
103 timerCount++;
104
105 <a href="qrect.html">QRect</a> shotR = shotRect();
106
107 if ( shotR.<a href="qrect.html#x">x</a>() &gt; width() || shotR.<a href="qrect.html#y">y</a>() &gt; height() )
108 autoShootTimer-&gt;<a href="qtimer.html#stop">stop</a>();
109 else
110 r = r.<a href="qrect.html#unite">unite</a>( QRegion( shotR ) );
111 <a href="qwidget.html#repaint">repaint</a>( r );
112}
113
114
115void CannonField::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e )
116{
117 <a href="qrect.html">QRect</a> updateR = e-&gt;<a href="qpaintevent.html#rect">rect</a>();
118 <a href="qpainter.html">QPainter</a> p( this );
119
120 if ( updateR.<a href="qrect.html#intersects">intersects</a>( cannonRect() ) )
121 paintCannon( &amp;p );
122 if ( autoShootTimer-&gt;<a href="qtimer.html#isActive">isActive</a>() &amp;&amp;
123 updateR.<a href="qrect.html#intersects">intersects</a>( shotRect() ) )
124 paintShot( &amp;p );
125}
126
127
128void <a name="f62"></a>CannonField::paintShot( <a href="qpainter.html">QPainter</a> *p )
129{
130 p-&gt;<a href="qpainter.html#setBrush">setBrush</a>( black );
131 p-&gt;<a href="qpainter.html#setPen">setPen</a>( NoPen );
132 p-&gt;<a href="qpainter.html#drawRect">drawRect</a>( shotRect() );
133}
134
135
136const <a href="qrect.html">QRect</a> barrelRect(33, -4, 15, 8);
137
138void <a name="f63"></a>CannonField::paintCannon( <a href="qpainter.html">QPainter</a> *p )
139{
140 <a href="qrect.html">QRect</a> cr = cannonRect();
141 <a href="qpixmap.html">QPixmap</a> pix( cr.<a href="qrect.html#size">size</a>() );
142 pix.<a href="qpixmap.html#fill">fill</a>( this, cr.<a href="qrect.html#topLeft">topLeft</a>() );
143
144 <a href="qpainter.html">QPainter</a> tmp( &amp;pix );
145 tmp.<a href="qpainter.html#setBrush">setBrush</a>( blue );
146 tmp.<a href="qpainter.html#setPen">setPen</a>( NoPen );
147
148 tmp.<a href="qpainter.html#translate">translate</a>( 0, pix.<a href="qpixmap.html#height">height</a>() - 1 );
149 tmp.<a href="qpainter.html#drawPie">drawPie</a>( QRect( -35,-35, 70, 70 ), 0, 90*16 );
150 tmp.<a href="qpainter.html#rotate">rotate</a>( -ang );
151 tmp.<a href="qpainter.html#drawRect">drawRect</a>( barrelRect );
152 tmp.<a href="qpainter.html#end">end</a>();
153
154 p-&gt;<a href="qpainter.html#drawPixmap">drawPixmap</a>( cr.<a href="qrect.html#topLeft">topLeft</a>(), pix );
155}
156
157
158QRect <a name="f64"></a>CannonField::cannonRect() const
159{
160 <a href="qrect.html">QRect</a> r( 0, 0, 50, 50 );
161 r.<a href="qrect.html#moveBottomLeft">moveBottomLeft</a>( <a href="qwidget.html#rect">rect</a>().bottomLeft() );
162 return r;
163}
164
165
166QRect <a name="f65"></a>CannonField::shotRect() const
167{
168 const double gravity = 4;
169
170 double time = timerCount / 4.0;
171 double velocity = shoot_f;
172 double radians = shoot_ang*3.14159265/180;
173
174 double velx = velocity*cos( radians );
175 double vely = velocity*sin( radians );
176 double x0 = ( barrelRect.<a href="qrect.html#right">right</a>() + 5 )*cos(radians);
177 double y0 = ( barrelRect.<a href="qrect.html#right">right</a>() + 5 )*sin(radians);
178 double x = x0 + velx*time;
179 double y = y0 + vely*time - 0.5*gravity*time*time;
180
181 <a href="qrect.html">QRect</a> r = QRect( 0, 0, 6, 6 );
182 r.<a href="qrect.html#moveCenter">moveCenter</a>( QPoint( qRound(x), height() - 1 - qRound(y) ) );
183 return r;
184}
185
186
187QSizePolicy CannonField::<a href="qwidget.html#sizePolicy">sizePolicy</a>() const
188{
189 return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
190}
191</pre><!-- eof -->
192<p><address><hr><div align=center>
193<table width=100% cellspacing=0 border=0><tr>
194<td>Copyright &copy; 2007
195<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
196<td align=right><div align=right>Qt 3.3.8</div>
197</table></div></address></body>
198</html>
Note: See TracBrowser for help on using the repository browser.