| 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 |
|
|---|
| 12 | CannonField::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 |
|
|---|
| 21 | void 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 |
|
|---|
| 35 | void 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 |
|
|---|
| 46 | void 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 |
|
|---|
| 69 | QRect CannonField::cannonRect() const
|
|---|
| 70 | {
|
|---|
| 71 | QRect r( 0, 0, 50, 50 );
|
|---|
| 72 | r.moveBottomLeft( rect().bottomLeft() );
|
|---|
| 73 | return r;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | QSizePolicy CannonField::sizePolicy() const
|
|---|
| 78 | {
|
|---|
| 79 | return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
|
|---|
| 80 | }
|
|---|