source: trunk/tutorial/t10/cannon.cpp@ 68

Last change on this file since 68 was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 1.6 KB
Line 
1/****************************************************************
2**
3** Implementation CannonField class, Qt tutorial 10
4**
5****************************************************************/
6
7#include "cannon.h"
8#include <qpainter.h>
9#include <qpixmap.h>
10
11
12CannonField::CannonField( QWidget *parent, const char *name )
13 : QWidget( parent, name )
14{
15 ang = 45;
16 f = 0;
17 setPalette( QPalette( QColor( 250, 250, 200) ) );
18}
19
20
21void CannonField::setAngle( int degrees )
22{
23 if ( degrees < 5 )
24 degrees = 5;
25 if ( degrees > 70 )
26 degrees = 70;
27 if ( ang == degrees )
28 return;
29 ang = degrees;
30 repaint( cannonRect(), FALSE );
31 emit angleChanged( ang );
32}
33
34
35void CannonField::setForce( int newton )
36{
37 if ( newton < 0 )
38 newton = 0;
39 if ( f == newton )
40 return;
41 f = newton;
42 emit forceChanged( f );
43}
44
45
46void CannonField::paintEvent( QPaintEvent *e )
47{
48 if ( !e->rect().intersects( cannonRect() ) )
49 return;
50
51 QRect cr = cannonRect();
52 QPixmap pix( cr.size() );
53 pix.fill( this, cr.topLeft() );
54
55 QPainter p( &pix );
56 p.setBrush( blue );
57 p.setPen( NoPen );
58 p.translate( 0, pix.height() - 1 );
59 p.drawPie( QRect( -35,-35, 70, 70 ), 0, 90*16 );
60 p.rotate( -ang );
61 p.drawRect( QRect(33, -4, 15, 8) );
62 p.end();
63
64 p.begin( this );
65 p.drawPixmap( cr.topLeft(), pix );
66}
67
68
69QRect CannonField::cannonRect() const
70{
71 QRect r( 0, 0, 50, 50 );
72 r.moveBottomLeft( rect().bottomLeft() );
73 return r;
74}
75
76
77QSizePolicy CannonField::sizePolicy() const
78{
79 return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
80}
Note: See TracBrowser for help on using the repository browser.