diff options
Diffstat (limited to 'tutorial/t11')
-rw-r--r-- | tutorial/t11/cannon.cpp | 156 | ||||
-rw-r--r-- | tutorial/t11/cannon.h | 57 | ||||
-rw-r--r-- | tutorial/t11/lcdrange.cpp | 47 | ||||
-rw-r--r-- | tutorial/t11/lcdrange.h | 35 | ||||
-rw-r--r-- | tutorial/t11/main.cpp | 85 | ||||
-rw-r--r-- | tutorial/t11/t11.pro | 11 |
6 files changed, 391 insertions, 0 deletions
diff --git a/tutorial/t11/cannon.cpp b/tutorial/t11/cannon.cpp new file mode 100644 index 000000000..8ffd1771b --- /dev/null +++ b/tutorial/t11/cannon.cpp @@ -0,0 +1,156 @@ +/**************************************************************** +** +** Implementation CannonField class, TQt tutorial 11 +** +****************************************************************/ + +#include "cannon.h" +#include <qtimer.h> +#include <qpainter.h> +#include <qpixmap.h> + +#include <math.h> + + +CannonField::CannonField( TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + ang = 45; + f = 0; + timerCount = 0; + autoShootTimer = new TQTimer( this, "movement handler" ); + connect( autoShootTimer, SIGNAL(timeout()), + this, SLOT(moveShot()) ); + shoot_ang = 0; + shoot_f = 0; + setPalette( TQPalette( TQColor( 250, 250, 200) ) ); +} + + +void CannonField::setAngle( int degrees ) +{ + if ( degrees < 5 ) + degrees = 5; + if ( degrees > 70 ) + degrees = 70; + if ( ang == degrees ) + return; + ang = degrees; + repaint( cannonRect(), FALSE ); + emit angleChanged( ang ); +} + + +void CannonField::setForce( int newton ) +{ + if ( newton < 0 ) + newton = 0; + if ( f == newton ) + return; + f = newton; + emit forceChanged( f ); +} + + +void CannonField::shoot() +{ + if ( autoShootTimer->isActive() ) + return; + timerCount = 0; + shoot_ang = ang; + shoot_f = f; + autoShootTimer->start( 50 ); +} + + +void CannonField::moveShot() +{ + TQRegion r( shotRect() ); + timerCount++; + + TQRect shotR = shotRect(); + + if ( shotR.x() > width() || shotR.y() > height() ) + autoShootTimer->stop(); + else + r = r.unite( TQRegion( shotR ) ); + repaint( r ); +} + + +void CannonField::paintEvent( TQPaintEvent *e ) +{ + TQRect updateR = e->rect(); + TQPainter p( this ); + + if ( updateR.intersects( cannonRect() ) ) + paintCannon( &p ); + if ( autoShootTimer->isActive() && + updateR.intersects( shotRect() ) ) + paintShot( &p ); +} + + +void CannonField::paintShot( TQPainter *p ) +{ + p->setBrush( black ); + p->setPen( NoPen ); + p->drawRect( shotRect() ); +} + + +const TQRect barrelRect(33, -4, 15, 8); + +void CannonField::paintCannon( TQPainter *p ) +{ + TQRect cr = cannonRect(); + TQPixmap pix( cr.size() ); + pix.fill( this, cr.topLeft() ); + + TQPainter tmp( &pix ); + tmp.setBrush( blue ); + tmp.setPen( NoPen ); + + tmp.translate( 0, pix.height() - 1 ); + tmp.drawPie( TQRect( -35,-35, 70, 70 ), 0, 90*16 ); + tmp.rotate( -ang ); + tmp.drawRect( barrelRect ); + tmp.end(); + + p->drawPixmap( cr.topLeft(), pix ); +} + + +TQRect CannonField::cannonRect() const +{ + TQRect r( 0, 0, 50, 50 ); + r.moveBottomLeft( rect().bottomLeft() ); + return r; +} + + +TQRect CannonField::shotRect() const +{ + const double gravity = 4; + + double time = timerCount / 4.0; + double velocity = shoot_f; + double radians = shoot_ang*3.14159265/180; + + double velx = velocity*cos( radians ); + double vely = velocity*sin( radians ); + double x0 = ( barrelRect.right() + 5 )*cos(radians); + double y0 = ( barrelRect.right() + 5 )*sin(radians); + double x = x0 + velx*time; + double y = y0 + vely*time - 0.5*gravity*time*time; + + TQRect r = TQRect( 0, 0, 6, 6 ); + r.moveCenter( TQPoint( qRound(x), height() - 1 - qRound(y) ) ); + return r; +} + + +TQSizePolicy CannonField::sizePolicy() const +{ + return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ); +} diff --git a/tutorial/t11/cannon.h b/tutorial/t11/cannon.h new file mode 100644 index 000000000..8fd448f61 --- /dev/null +++ b/tutorial/t11/cannon.h @@ -0,0 +1,57 @@ +/**************************************************************** +** +** Definition of CannonField class, TQt tutorial 11 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +class TQTimer; + + +#include <qwidget.h> + + +class CannonField : public TQWidget +{ + Q_OBJECT +public: + CannonField( TQWidget *parent=0, const char *name=0 ); + + int angle() const { return ang; } + int force() const { return f; } + TQSizePolicy sizePolicy() const; + +public slots: + void setAngle( int degrees ); + void setForce( int newton ); + void shoot(); + +private slots: + void moveShot(); + +signals: + void angleChanged( int ); + void forceChanged( int ); + +protected: + void paintEvent( TQPaintEvent * ); + +private: + void paintShot( TQPainter * ); + void paintCannon( TQPainter * ); + TQRect cannonRect() const; + TQRect shotRect() const; + + int ang; + int f; + + int timerCount; + TQTimer * autoShootTimer; + float shoot_ang; + float shoot_f; +}; + + +#endif // CANNON_H diff --git a/tutorial/t11/lcdrange.cpp b/tutorial/t11/lcdrange.cpp new file mode 100644 index 000000000..ca0aba8a8 --- /dev/null +++ b/tutorial/t11/lcdrange.cpp @@ -0,0 +1,47 @@ +/**************************************************************** +** +** Implementation of LCDRange class, TQt tutorial 8 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> + +LCDRange::LCDRange( TQWidget *parent, const char *name ) + : TQVBox( parent, name ) +{ + TQLCDNumber *lcd = new TQLCDNumber( 2, this, "lcd" ); + slider = new TQSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + connect( slider, SIGNAL(valueChanged(int)), + lcd, SLOT(display(int)) ); + connect( slider, SIGNAL(valueChanged(int)), + SIGNAL(valueChanged(int)) ); + + setFocusProxy( slider ); +} + +int LCDRange::value() const +{ + return slider->value(); +} + +void LCDRange::setValue( int value ) +{ + slider->setValue( value ); +} + +void LCDRange::setRange( int minVal, int maxVal ) +{ + if ( minVal < 0 || maxVal > 99 || minVal > maxVal ) { + qWarning( "LCDRange::setRange(%d,%d)\n" + "\tRange must be 0..99\n" + "\tand minVal must not be greater than maxVal", + minVal, maxVal ); + return; + } + slider->setRange( minVal, maxVal ); +} diff --git a/tutorial/t11/lcdrange.h b/tutorial/t11/lcdrange.h new file mode 100644 index 000000000..4a79a6ca9 --- /dev/null +++ b/tutorial/t11/lcdrange.h @@ -0,0 +1,35 @@ +/**************************************************************** +** +** Definition of LCDRange class, TQt tutorial 8 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qvbox.h> + +class TQSlider; + + +class LCDRange : public TQVBox +{ + Q_OBJECT +public: + LCDRange( TQWidget *parent=0, const char *name=0 ); + + int value() const; + +public slots: + void setValue( int ); + void setRange( int minVal, int maxVal ); + +signals: + void valueChanged( int ); + +private: + TQSlider *slider; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t11/main.cpp b/tutorial/t11/main.cpp new file mode 100644 index 000000000..cdfb36ebf --- /dev/null +++ b/tutorial/t11/main.cpp @@ -0,0 +1,85 @@ +/**************************************************************** +** +** TQt tutorial 11 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qlcdnumber.h> +#include <qfont.h> +#include <qlayout.h> + +#include "lcdrange.h" +#include "cannon.h" + + +class MyWidget: public TQWidget +{ +public: + MyWidget( TQWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + TQPushButton *tquit = new TQPushButton( "&Quit", this, "tquit" ); + tquit->setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + connect( tquit, SIGNAL(clicked()), qApp, SLOT(tquit()) ); + + LCDRange *angle = new LCDRange( this, "angle" ); + angle->setRange( 5, 70 ); + + LCDRange *force = new LCDRange( this, "force" ); + force->setRange( 10, 50 ); + + CannonField *cannonField = new CannonField( this, "cannonField" ); + + connect( angle, SIGNAL(valueChanged(int)), + cannonField, SLOT(setAngle(int)) ); + connect( cannonField, SIGNAL(angleChanged(int)), + angle, SLOT(setValue(int)) ); + + connect( force, SIGNAL(valueChanged(int)), + cannonField, SLOT(setForce(int)) ); + connect( cannonField, SIGNAL(forceChanged(int)), + force, SLOT(setValue(int)) ); + + TQPushButton *shoot = new TQPushButton( "&Shoot", this, "shoot" ); + shoot->setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + connect( shoot, SIGNAL(clicked()), cannonField, SLOT(shoot()) ); + + TQGridLayout *grid = new TQGridLayout( this, 2, 2, 10 ); + grid->addWidget( tquit, 0, 0 ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + TQVBoxLayout *leftBox = new TQVBoxLayout; + grid->addLayout( leftBox, 1, 0 ); + leftBox->addWidget( angle ); + leftBox->addWidget( force ); + + TQHBoxLayout *topBox = new TQHBoxLayout; + grid->addLayout( topBox, 0, 1 ); + topBox->addWidget( shoot ); + topBox->addStretch( 1 ); + + angle->setValue( 60 ); + force->setValue( 25 ); + angle->setFocus(); +} + +int main( int argc, char **argv ) +{ + TQApplication::setColorSpec( TQApplication::CustomColor ); + TQApplication a( argc, argv ); + + MyWidget w; + w.setGeometry( 100, 100, 500, 355 ); + a.setMainWidget( &w ); + w.show(); + return a.exec(); +} diff --git a/tutorial/t11/t11.pro b/tutorial/t11/t11.pro new file mode 100644 index 000000000..63fc17e88 --- /dev/null +++ b/tutorial/t11/t11.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = cannon.h \ + lcdrange.h +SOURCES = cannon.cpp \ + lcdrange.cpp \ + main.cpp +TARGET = t11 +REQUIRES=full-config +unix:LIBS += -lm + |