diff options
Diffstat (limited to 'tutorial')
64 files changed, 3393 insertions, 0 deletions
diff --git a/tutorial/README b/tutorial/README new file mode 100644 index 000000000..156a3a8f4 --- /dev/null +++ b/tutorial/README @@ -0,0 +1,8 @@ +Here are the programs in the Qt tutorial. The tutorial itself is in +../doc/html/tutorial.html (or http://doc.trolltech.com/3.0/tutorial.html). + +Typing 'make' in this directory builds all the programs (t1/t1, +t2/t2, t3/t3 and so on). Typing 'make' in each subdirectory builds +just that tutorial program. + +Feel free to send comments about the tutorial to qt-bugs@trolltech.com. diff --git a/tutorial/t1/main.cpp b/tutorial/t1/main.cpp new file mode 100644 index 000000000..23f72032d --- /dev/null +++ b/tutorial/t1/main.cpp @@ -0,0 +1,21 @@ +/**************************************************************** +** +** TQt tutorial 1 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> + + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + + TQPushButton hello( "Hello world!", 0 ); + hello.resize( 100, 30 ); + + a.setMainWidget( &hello ); + hello.show(); + return a.exec(); +} diff --git a/tutorial/t1/t1.pro b/tutorial/t1/t1.pro new file mode 100644 index 000000000..dede5b191 --- /dev/null +++ b/tutorial/t1/t1.pro @@ -0,0 +1,6 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = +SOURCES = main.cpp +TARGET = t1 +REQUIRES=small-config diff --git a/tutorial/t10/cannon.cpp b/tutorial/t10/cannon.cpp new file mode 100644 index 000000000..fa2b2c103 --- /dev/null +++ b/tutorial/t10/cannon.cpp @@ -0,0 +1,80 @@ +/**************************************************************** +** +** Implementation CannonField class, TQt tutorial 10 +** +****************************************************************/ + +#include "cannon.h" +#include <qpainter.h> +#include <qpixmap.h> + + +CannonField::CannonField( TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + ang = 45; + 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::paintEvent( TQPaintEvent *e ) +{ + if ( !e->rect().intersects( cannonRect() ) ) + return; + + TQRect cr = cannonRect(); + TQPixmap pix( cr.size() ); + pix.fill( this, cr.topLeft() ); + + TQPainter p( &pix ); + p.setBrush( blue ); + p.setPen( NoPen ); + p.translate( 0, pix.height() - 1 ); + p.drawPie( TQRect( -35,-35, 70, 70 ), 0, 90*16 ); + p.rotate( -ang ); + p.drawRect( TQRect(33, -4, 15, 8) ); + p.end(); + + p.begin( this ); + p.drawPixmap( cr.topLeft(), pix ); +} + + +TQRect CannonField::cannonRect() const +{ + TQRect r( 0, 0, 50, 50 ); + r.moveBottomLeft( rect().bottomLeft() ); + return r; +} + + +TQSizePolicy CannonField::sizePolicy() const +{ + return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ); +} diff --git a/tutorial/t10/cannon.h b/tutorial/t10/cannon.h new file mode 100644 index 000000000..1b1c16828 --- /dev/null +++ b/tutorial/t10/cannon.h @@ -0,0 +1,43 @@ +/**************************************************************** +** +** Definition of CannonField class, TQt tutorial 10 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +#include <qwidget.h> + + +class CannonField : public TQWidget +{ + Q_OBJECT +public: + CannonField( TQWidget *parent=0, const char *name=0 ); + + TQSizePolicy sizePolicy() const; + + int angle() const { return ang; } + int force() const { return f; } + +public slots: + void setAngle( int degrees ); + void setForce( int newton ); + +signals: + void angleChanged( int ); + void forceChanged( int ); + +protected: + void paintEvent( TQPaintEvent * ); + +private: + TQRect cannonRect() const; + + int ang; + int f; +}; + + +#endif // CANNON_H diff --git a/tutorial/t10/lcdrange.cpp b/tutorial/t10/lcdrange.cpp new file mode 100644 index 000000000..ca0aba8a8 --- /dev/null +++ b/tutorial/t10/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/t10/lcdrange.h b/tutorial/t10/lcdrange.h new file mode 100644 index 000000000..4a79a6ca9 --- /dev/null +++ b/tutorial/t10/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/t10/main.cpp b/tutorial/t10/main.cpp new file mode 100644 index 000000000..a0c659d72 --- /dev/null +++ b/tutorial/t10/main.cpp @@ -0,0 +1,75 @@ +/**************************************************************** +** +** TQt tutorial 10 +** +****************************************************************/ + +#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)) ); + + 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 ); + + 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/t10/t10.pro b/tutorial/t10/t10.pro new file mode 100644 index 000000000..202b939a6 --- /dev/null +++ b/tutorial/t10/t10.pro @@ -0,0 +1,9 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = cannon.h \ + lcdrange.h +SOURCES = cannon.cpp \ + lcdrange.cpp \ + main.cpp +TARGET = t10 +REQUIRES=full-config 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 + diff --git a/tutorial/t12/cannon.cpp b/tutorial/t12/cannon.cpp new file mode 100644 index 000000000..0be33e345 --- /dev/null +++ b/tutorial/t12/cannon.cpp @@ -0,0 +1,199 @@ +/**************************************************************** +** +** Implementation CannonField class, TQt tutorial 12 +** +****************************************************************/ + +#include "cannon.h" +#include <qtimer.h> +#include <qpainter.h> +#include <qpixmap.h> +#include <qdatetime.h> + +#include <math.h> +#include <stdlib.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; + target = TQPoint( 0, 0 ); + setPalette( TQPalette( TQColor( 250, 250, 200) ) ); + newTarget(); +} + + +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::newTarget() +{ + static bool first_time = TRUE; + if ( first_time ) { + first_time = FALSE; + TQTime midnight( 0, 0, 0 ); + srand( midnight.secsTo(TQTime::currentTime()) ); + } + TQRegion r( targetRect() ); + target = TQPoint( 200 + rand() % 190, + 10 + rand() % 255 ); + repaint( r.unite( targetRect() ) ); +} + + +void CannonField::moveShot() +{ + TQRegion r( shotRect() ); + timerCount++; + + TQRect shotR = shotRect(); + + if ( shotR.intersects( targetRect() ) ) { + autoShootTimer->stop(); + emit hit(); + } else if ( shotR.x() > width() || shotR.y() > height() ) { + autoShootTimer->stop(); + emit missed(); + } 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 ); + if ( updateR.intersects( targetRect() ) ) + paintTarget( &p ); +} + + +void CannonField::paintShot( TQPainter *p ) +{ + p->setBrush( black ); + p->setPen( NoPen ); + p->drawRect( shotRect() ); +} + + +void CannonField::paintTarget( TQPainter *p ) +{ + p->setBrush( red ); + p->setPen( black ); + p->drawRect( targetRect() ); +} + + +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; +} + + +TQRect CannonField::targetRect() const +{ + TQRect r( 0, 0, 20, 10 ); + r.moveCenter( TQPoint(target.x(),height() - 1 - target.y()) ); + return r; +} + + +TQSizePolicy CannonField::sizePolicy() const +{ + return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ); +} diff --git a/tutorial/t12/cannon.h b/tutorial/t12/cannon.h new file mode 100644 index 000000000..1f242e085 --- /dev/null +++ b/tutorial/t12/cannon.h @@ -0,0 +1,64 @@ +/**************************************************************** +** +** Definition of CannonField class, TQt tutorial 12 +** +****************************************************************/ + +#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(); + void newTarget(); + +private slots: + void moveShot(); + +signals: + void hit(); + void missed(); + void angleChanged( int ); + void forceChanged( int ); + +protected: + void paintEvent( TQPaintEvent * ); + +private: + void paintShot( TQPainter * ); + void paintTarget( TQPainter * ); + void paintCannon( TQPainter * ); + TQRect cannonRect() const; + TQRect shotRect() const; + TQRect targetRect() const; + + int ang; + int f; + + int timerCount; + TQTimer * autoShootTimer; + float shoot_ang; + float shoot_f; + + TQPoint target; +}; + + +#endif // CANNON_H diff --git a/tutorial/t12/lcdrange.cpp b/tutorial/t12/lcdrange.cpp new file mode 100644 index 000000000..30da9fff8 --- /dev/null +++ b/tutorial/t12/lcdrange.cpp @@ -0,0 +1,83 @@ +/**************************************************************** +** +** Implementation of LCDRange class, TQt tutorial 12 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> +#include <qlabel.h> + + +LCDRange::LCDRange( TQWidget *parent, const char *name ) + : TQVBox( parent, name ) +{ + init(); +} + + +LCDRange::LCDRange( const char *s, TQWidget *parent, + const char *name ) + : TQVBox( parent, name ) +{ + init(); + setText( s ); +} + + +void LCDRange::init() +{ + TQLCDNumber *lcd = new TQLCDNumber( 2, this, "lcd" ); + slider = new TQSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + + label = new TQLabel( " ", this, "label" ); + label->setAlignment( AlignCenter ); + + 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(); +} + + +const char *LCDRange::text() const +{ + return label->text(); +} + + +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 ); +} + + +void LCDRange::setText( const char *s ) +{ + label->setText( s ); +} diff --git a/tutorial/t12/lcdrange.h b/tutorial/t12/lcdrange.h new file mode 100644 index 000000000..732e75834 --- /dev/null +++ b/tutorial/t12/lcdrange.h @@ -0,0 +1,43 @@ +/**************************************************************** +** +** Definition of LCDRange class, TQt tutorial 12 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qvbox.h> + +class TQSlider; +class TQLabel; + + +class LCDRange : public TQVBox +{ + Q_OBJECT +public: + LCDRange( TQWidget *parent=0, const char *name=0 ); + LCDRange( const char *s, TQWidget *parent=0, + const char *name=0 ); + + int value() const; + const char *text() const; + +public slots: + void setValue( int ); + void setRange( int minVal, int maxVal ); + void setText( const char * ); + +signals: + void valueChanged( int ); + +private: + void init(); + + TQSlider *slider; + TQLabel *label; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t12/main.cpp b/tutorial/t12/main.cpp new file mode 100644 index 000000000..20c291b39 --- /dev/null +++ b/tutorial/t12/main.cpp @@ -0,0 +1,85 @@ +/**************************************************************** +** +** TQt tutorial 12 +** +****************************************************************/ + +#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( "ANGLE", this, "angle" ); + angle->setRange( 5, 70 ); + + LCDRange *force = new LCDRange( "FORCE", 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/t12/t12.pro b/tutorial/t12/t12.pro new file mode 100644 index 000000000..679890f78 --- /dev/null +++ b/tutorial/t12/t12.pro @@ -0,0 +1,10 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = cannon.h \ + lcdrange.h +SOURCES = cannon.cpp \ + lcdrange.cpp \ + main.cpp +TARGET = t12 +REQUIRES=full-config +unix:LIBS += -lm diff --git a/tutorial/t13/cannon.cpp b/tutorial/t13/cannon.cpp new file mode 100644 index 000000000..b04403107 --- /dev/null +++ b/tutorial/t13/cannon.cpp @@ -0,0 +1,231 @@ +/**************************************************************** +** +** Implementation CannonField class, TQt tutorial 13 +** +****************************************************************/ + +#include "cannon.h" +#include <qtimer.h> +#include <qpainter.h> +#include <qpixmap.h> +#include <qdatetime.h> + +#include <math.h> +#include <stdlib.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; + target = TQPoint( 0, 0 ); + gameEnded = FALSE; + setPalette( TQPalette( TQColor( 250, 250, 200) ) ); + newTarget(); +} + + +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 ( isShooting() ) + return; + timerCount = 0; + shoot_ang = ang; + shoot_f = f; + autoShootTimer->start( 50 ); + emit canShoot( FALSE ); +} + + +void CannonField::newTarget() +{ + static bool first_time = TRUE; + if ( first_time ) { + first_time = FALSE; + TQTime midnight( 0, 0, 0 ); + srand( midnight.secsTo(TQTime::currentTime()) ); + } + TQRegion r( targetRect() ); + target = TQPoint( 200 + rand() % 190, + 10 + rand() % 255 ); + repaint( r.unite( targetRect() ) ); +} + +void CannonField::setGameOver() +{ + if ( gameEnded ) + return; + if ( isShooting() ) + autoShootTimer->stop(); + gameEnded = TRUE; + repaint(); +} + +void CannonField::restartGame() +{ + if ( isShooting() ) + autoShootTimer->stop(); + gameEnded = FALSE; + repaint(); + emit canShoot( TRUE ); +} + +void CannonField::moveShot() +{ + TQRegion r( shotRect() ); + timerCount++; + + TQRect shotR = shotRect(); + + if ( shotR.intersects( targetRect() ) ) { + autoShootTimer->stop(); + emit hit(); + emit canShoot( TRUE ); + } else if ( shotR.x() > width() || shotR.y() > height() ) { + autoShootTimer->stop(); + emit missed(); + emit canShoot( TRUE ); + } else { + r = r.unite( TQRegion( shotR ) ); + } + + repaint( r ); +} + + +void CannonField::paintEvent( TQPaintEvent *e ) +{ + TQRect updateR = e->rect(); + TQPainter p( this ); + + if ( gameEnded ) { + p.setPen( black ); + p.setFont( TQFont( "Courier", 48, TQFont::Bold ) ); + p.drawText( rect(), AlignCenter, "Game Over" ); + } + if ( updateR.intersects( cannonRect() ) ) + paintCannon( &p ); + if ( isShooting() && updateR.intersects( shotRect() ) ) + paintShot( &p ); + if ( !gameEnded && updateR.intersects( targetRect() ) ) + paintTarget( &p ); +} + + +void CannonField::paintShot( TQPainter *p ) +{ + p->setBrush( black ); + p->setPen( NoPen ); + p->drawRect( shotRect() ); +} + + +void CannonField::paintTarget( TQPainter *p ) +{ + p->setBrush( red ); + p->setPen( black ); + p->drawRect( targetRect() ); +} + + +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; +} + + +TQRect CannonField::targetRect() const +{ + TQRect r( 0, 0, 20, 10 ); + r.moveCenter( TQPoint(target.x(),height() - 1 - target.y()) ); + return r; +} + + +bool CannonField::isShooting() const +{ + return autoShootTimer->isActive(); +} + + +TQSizePolicy CannonField::sizePolicy() const +{ + return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ); +} diff --git a/tutorial/t13/cannon.h b/tutorial/t13/cannon.h new file mode 100644 index 000000000..9d3910ea2 --- /dev/null +++ b/tutorial/t13/cannon.h @@ -0,0 +1,71 @@ +/**************************************************************** +** +** Definition of CannonField class, TQt tutorial 13 +** +****************************************************************/ + +#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; } + bool gameOver() const { return gameEnded; } + bool isShooting() const; + TQSizePolicy sizePolicy() const; + +public slots: + void setAngle( int degrees ); + void setForce( int newton ); + void shoot(); + void newTarget(); + void setGameOver(); + void restartGame(); + +private slots: + void moveShot(); + +signals: + void hit(); + void missed(); + void angleChanged( int ); + void forceChanged( int ); + void canShoot( bool ); + +protected: + void paintEvent( TQPaintEvent * ); + +private: + void paintShot( TQPainter * ); + void paintTarget( TQPainter * ); + void paintCannon( TQPainter * ); + TQRect cannonRect() const; + TQRect shotRect() const; + TQRect targetRect() const; + + int ang; + int f; + + int timerCount; + TQTimer * autoShootTimer; + float shoot_ang; + float shoot_f; + + TQPoint target; + + bool gameEnded; +}; + + +#endif // CANNON_H diff --git a/tutorial/t13/gamebrd.cpp b/tutorial/t13/gamebrd.cpp new file mode 100644 index 000000000..cf15f9ef2 --- /dev/null +++ b/tutorial/t13/gamebrd.cpp @@ -0,0 +1,129 @@ +/**************************************************************** +** +** Implementation of GameBoard class, TQt tutorial 13 +** +****************************************************************/ + +#include "gamebrd.h" + +#include <qfont.h> +#include <qapplication.h> +#include <qlabel.h> +#include <qpushbutton.h> +#include <qlcdnumber.h> +#include <qlayout.h> + +#include "lcdrange.h" +#include "cannon.h" + +GameBoard::GameBoard( 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( "ANGLE", this, "angle" ); + angle->setRange( 5, 70 ); + + LCDRange *force = new LCDRange( "FORCE", this, "force" ); + force->setRange( 10, 50 ); + + 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)) ); + + connect( cannonField, SIGNAL(hit()), + this, SLOT(hit()) ); + connect( cannonField, SIGNAL(missed()), + this, SLOT(missed()) ); + + TQPushButton *shoot = new TQPushButton( "&Shoot", this, "shoot" ); + shoot->setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + connect( shoot, SIGNAL(clicked()), SLOT(fire()) ); + connect( cannonField, SIGNAL(canShoot(bool)), + shoot, SLOT(setEnabled(bool)) ); + + TQPushButton *restart + = new TQPushButton( "&New Game", this, "newgame" ); + restart->setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + connect( restart, SIGNAL(clicked()), this, SLOT(newGame()) ); + + hits = new TQLCDNumber( 2, this, "hits" ); + shotsLeft = new TQLCDNumber( 2, this, "shotsleft" ); + TQLabel *hitsL = new TQLabel( "HITS", this, "hitsLabel" ); + TQLabel *shotsLeftL + = new TQLabel( "SHOTS LEFT", this, "shotsleftLabel" ); + + 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->addWidget( hits ); + topBox->addWidget( hitsL ); + topBox->addWidget( shotsLeft ); + topBox->addWidget( shotsLeftL ); + topBox->addStretch( 1 ); + topBox->addWidget( restart ); + + angle->setValue( 60 ); + force->setValue( 25 ); + angle->setFocus(); + + newGame(); +} + + +void GameBoard::fire() +{ + if ( cannonField->gameOver() || cannonField->isShooting() ) + return; + shotsLeft->display( shotsLeft->intValue() - 1 ); + cannonField->shoot(); +} + + +void GameBoard::hit() +{ + hits->display( hits->intValue() + 1 ); + if ( shotsLeft->intValue() == 0 ) + cannonField->setGameOver(); + else + cannonField->newTarget(); +} + + +void GameBoard::missed() +{ + if ( shotsLeft->intValue() == 0 ) + cannonField->setGameOver(); +} + + +void GameBoard::newGame() +{ + shotsLeft->display( 15 ); + hits->display( 0 ); + cannonField->restartGame(); + cannonField->newTarget(); +} diff --git a/tutorial/t13/gamebrd.h b/tutorial/t13/gamebrd.h new file mode 100644 index 000000000..2d3cafee5 --- /dev/null +++ b/tutorial/t13/gamebrd.h @@ -0,0 +1,40 @@ +/**************************************************************** +** +** Definition of GameBoard class, TQt tutorial 13 +** +****************************************************************/ + +#ifndef GAMEBRD_H +#define GAMEBRD_H + +#include <qwidget.h> + +class TQPushButton; +class LCDRange; +class TQLCDNumber; +class CannonField; + +#include "lcdrange.h" +#include "cannon.h" + + +class GameBoard : public TQWidget +{ + Q_OBJECT +public: + GameBoard( TQWidget *parent=0, const char *name=0 ); + +protected slots: + void fire(); + void hit(); + void missed(); + void newGame(); + +private: + TQLCDNumber *hits; + TQLCDNumber *shotsLeft; + CannonField *cannonField; +}; + + +#endif // GAMEBRD_H diff --git a/tutorial/t13/lcdrange.cpp b/tutorial/t13/lcdrange.cpp new file mode 100644 index 000000000..aff272af6 --- /dev/null +++ b/tutorial/t13/lcdrange.cpp @@ -0,0 +1,88 @@ +/**************************************************************** +** +** Implementation of LCDRange class, TQt tutorial 12 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> +#include <qlabel.h> +#include <qlayout.h> + + +LCDRange::LCDRange( TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + init(); +} + + +LCDRange::LCDRange( const char *s, TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + init(); + setText( s ); +} + + +void LCDRange::init() +{ + TQLCDNumber *lcd = new TQLCDNumber( 2, this, "lcd" ); + slider = new TQSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + + label = new TQLabel( " ", this, "label" ); + label->setAlignment( AlignCenter ); + + connect( slider, SIGNAL(valueChanged(int)), + lcd, SLOT(display(int)) ); + connect( slider, SIGNAL(valueChanged(int)), + SIGNAL(valueChanged(int)) ); + + setFocusProxy( slider ); + + TQVBoxLayout * l = new TQVBoxLayout( this ); + l->addWidget( lcd, 1 ); + l->addWidget( slider ); + l->addWidget( label ); +} + + +int LCDRange::value() const +{ + return slider->value(); +} + + +const char *LCDRange::text() const +{ + return label->text(); +} + + +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 ); +} + + +void LCDRange::setText( const char *s ) +{ + label->setText( s ); +} diff --git a/tutorial/t13/lcdrange.h b/tutorial/t13/lcdrange.h new file mode 100644 index 000000000..874dd169a --- /dev/null +++ b/tutorial/t13/lcdrange.h @@ -0,0 +1,42 @@ +/**************************************************************** +** +** Definition of LCDRange class, TQt tutorial 12 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qwidget.h> + +class TQSlider; +class TQLabel; + + +class LCDRange : public TQWidget +{ + Q_OBJECT +public: + LCDRange( TQWidget *parent=0, const char *name=0 ); + LCDRange( const char *s, TQWidget *parent=0, const char *name=0 ); + + int value() const; + const char *text() const; + +public slots: + void setValue( int ); + void setRange( int minVal, int maxVal ); + void setText( const char * ); + +signals: + void valueChanged( int ); + +private: + void init(); + + TQSlider *slider; + TQLabel *label; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t13/main.cpp b/tutorial/t13/main.cpp new file mode 100644 index 000000000..f7922ece8 --- /dev/null +++ b/tutorial/t13/main.cpp @@ -0,0 +1,22 @@ +/**************************************************************** +** +** TQt tutorial 13 +** +****************************************************************/ + +#include <qapplication.h> + +#include "gamebrd.h" + + +int main( int argc, char **argv ) +{ + TQApplication::setColorSpec( TQApplication::CustomColor ); + TQApplication a( argc, argv ); + + GameBoard gb; + gb.setGeometry( 100, 100, 500, 355 ); + a.setMainWidget( &gb ); + gb.show(); + return a.exec(); +} diff --git a/tutorial/t13/t13.pro b/tutorial/t13/t13.pro new file mode 100644 index 000000000..a670da605 --- /dev/null +++ b/tutorial/t13/t13.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = cannon.h \ + gamebrd.h \ + lcdrange.h +SOURCES = cannon.cpp \ + gamebrd.cpp \ + lcdrange.cpp \ + main.cpp +TARGET = t13 +REQUIRES=full-config +unix:LIBS += -lm + diff --git a/tutorial/t14/cannon.cpp b/tutorial/t14/cannon.cpp new file mode 100644 index 000000000..bf9d7e49d --- /dev/null +++ b/tutorial/t14/cannon.cpp @@ -0,0 +1,292 @@ +/**************************************************************** +** +** Implementation CannonField class, TQt tutorial 14 +** +****************************************************************/ + +#include "cannon.h" +#include <qtimer.h> +#include <qpainter.h> +#include <qpixmap.h> +#include <qdatetime.h> + +#include <math.h> +#include <stdlib.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; + target = TQPoint( 0, 0 ); + gameEnded = FALSE; + barrelPressed = FALSE; + setPalette( TQPalette( TQColor( 250, 250, 200) ) ); + newTarget(); +} + + +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 ( isShooting() ) + return; + timerCount = 0; + shoot_ang = ang; + shoot_f = f; + autoShootTimer->start( 50 ); + emit canShoot( FALSE ); +} + + +void CannonField::newTarget() +{ + static bool first_time = TRUE; + if ( first_time ) { + first_time = FALSE; + TQTime midnight( 0, 0, 0 ); + srand( midnight.secsTo(TQTime::currentTime()) ); + } + TQRegion r( targetRect() ); + target = TQPoint( 200 + rand() % 190, + 10 + rand() % 255 ); + repaint( r.unite( targetRect() ) ); +} + +void CannonField::setGameOver() +{ + if ( gameEnded ) + return; + if ( isShooting() ) + autoShootTimer->stop(); + gameEnded = TRUE; + repaint(); +} + +void CannonField::restartGame() +{ + if ( isShooting() ) + autoShootTimer->stop(); + gameEnded = FALSE; + repaint(); + emit canShoot( TRUE ); +} + +void CannonField::moveShot() +{ + TQRegion r( shotRect() ); + timerCount++; + + TQRect shotR = shotRect(); + + if ( shotR.intersects( targetRect() ) ) { + autoShootTimer->stop(); + emit hit(); + emit canShoot( TRUE ); + } else if ( shotR.x() > width() || shotR.y() > height() || + shotR.intersects(barrierRect()) ) { + autoShootTimer->stop(); + emit missed(); + emit canShoot( TRUE ); + } else { + r = r.unite( TQRegion( shotR ) ); + } + + repaint( r ); +} + + +void CannonField::mousePressEvent( TQMouseEvent *e ) +{ + if ( e->button() != LeftButton ) + return; + if ( barrelHit( e->pos() ) ) + barrelPressed = TRUE; +} + + +void CannonField::mouseMoveEvent( TQMouseEvent *e ) +{ + if ( !barrelPressed ) + return; + TQPoint pnt = e->pos(); + if ( pnt.x() <= 0 ) + pnt.setX( 1 ); + if ( pnt.y() >= height() ) + pnt.setY( height() - 1 ); + double rad = atan(((double)rect().bottom()-pnt.y())/pnt.x()); + setAngle( qRound ( rad*180/3.14159265 ) ); +} + + +void CannonField::mouseReleaseEvent( TQMouseEvent *e ) +{ + if ( e->button() == LeftButton ) + barrelPressed = FALSE; +} + + +void CannonField::paintEvent( TQPaintEvent *e ) +{ + TQRect updateR = e->rect(); + TQPainter p( this ); + + if ( gameEnded ) { + p.setPen( black ); + p.setFont( TQFont( "Courier", 48, TQFont::Bold ) ); + p.drawText( rect(), AlignCenter, "Game Over" ); + } + if ( updateR.intersects( cannonRect() ) ) + paintCannon( &p ); + if ( updateR.intersects( barrierRect() ) ) + paintBarrier( &p ); + if ( isShooting() && updateR.intersects( shotRect() ) ) + paintShot( &p ); + if ( !gameEnded && updateR.intersects( targetRect() ) ) + paintTarget( &p ); +} + +void CannonField::paintShot( TQPainter *p ) +{ + p->setBrush( black ); + p->setPen( NoPen ); + p->drawRect( shotRect() ); +} + + +void CannonField::paintTarget( TQPainter *p ) +{ + p->setBrush( red ); + p->setPen( black ); + p->drawRect( targetRect() ); +} + +void CannonField::paintBarrier( TQPainter *p ) +{ + p->setBrush( yellow ); + p->setPen( black ); + p->drawRect( barrierRect() ); +} + +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; +} + + +TQRect CannonField::targetRect() const +{ + TQRect r( 0, 0, 20, 10 ); + r.moveCenter( TQPoint(target.x(),height() - 1 - target.y()) ); + return r; +} + + +TQRect CannonField::barrierRect() const +{ + return TQRect( 145, height() - 100, 15, 100 ); +} + + +bool CannonField::barrelHit( const TQPoint &p ) const +{ + TQWMatrix mtx; + mtx.translate( 0, height() - 1 ); + mtx.rotate( -ang ); + mtx = mtx.invert(); + return barrelRect.contains( mtx.map(p) ); +} + + +bool CannonField::isShooting() const +{ + return autoShootTimer->isActive(); +} + + +TQSize CannonField::sizeHint() const +{ + return TQSize( 400, 300 ); +} + + +TQSizePolicy CannonField::sizePolicy() const +{ + return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ); +} diff --git a/tutorial/t14/cannon.h b/tutorial/t14/cannon.h new file mode 100644 index 000000000..21c4aecc2 --- /dev/null +++ b/tutorial/t14/cannon.h @@ -0,0 +1,79 @@ +/**************************************************************** +** +** Definition of CannonField class, TQt tutorial 14 +** +****************************************************************/ + +#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; } + bool gameOver() const { return gameEnded; } + bool isShooting() const; + TQSize sizeHint() const; + TQSizePolicy sizePolicy() const; + +public slots: + void setAngle( int degrees ); + void setForce( int newton ); + void shoot(); + void newTarget(); + void setGameOver(); + void restartGame(); + +private slots: + void moveShot(); + +signals: + void hit(); + void missed(); + void angleChanged( int ); + void forceChanged( int ); + void canShoot( bool ); + +protected: + void paintEvent( TQPaintEvent * ); + void mousePressEvent( TQMouseEvent * ); + void mouseMoveEvent( TQMouseEvent * ); + void mouseReleaseEvent( TQMouseEvent * ); + +private: + void paintShot( TQPainter * ); + void paintTarget( TQPainter * ); + void paintBarrier( TQPainter * ); + void paintCannon( TQPainter * ); + TQRect cannonRect() const; + TQRect shotRect() const; + TQRect targetRect() const; + TQRect barrierRect() const; + bool barrelHit( const TQPoint & ) const; + + int ang; + int f; + + int timerCount; + TQTimer * autoShootTimer; + float shoot_ang; + float shoot_f; + + TQPoint target; + + bool gameEnded; + bool barrelPressed; +}; + + +#endif // CANNON_H diff --git a/tutorial/t14/gamebrd.cpp b/tutorial/t14/gamebrd.cpp new file mode 100644 index 000000000..2570eb423 --- /dev/null +++ b/tutorial/t14/gamebrd.cpp @@ -0,0 +1,143 @@ +/**************************************************************** +** +** Implementation of GameBoard class, TQt tutorial 14 +** +****************************************************************/ + +#include "gamebrd.h" + +#include <qfont.h> +#include <qapplication.h> +#include <qlabel.h> +#include <qaccel.h> +#include <qpushbutton.h> +#include <qlcdnumber.h> +#include <qlayout.h> +#include <qvbox.h> + +#include "lcdrange.h" +#include "cannon.h" + +GameBoard::GameBoard( 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( "ANGLE", this, "angle" ); + angle->setRange( 5, 70 ); + + LCDRange *force = new LCDRange( "FORCE", this, "force" ); + force->setRange( 10, 50 ); + + TQVBox *box = new TQVBox( this, "cannonFrame" ); + box->setFrameStyle( TQFrame::WinPanel | TQFrame::Sunken ); + + cannonField = new CannonField( box, "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)) ); + + connect( cannonField, SIGNAL(hit()), + this, SLOT(hit()) ); + connect( cannonField, SIGNAL(missed()), + this, SLOT(missed()) ); + + TQPushButton *shoot = new TQPushButton( "&Shoot", this, "shoot" ); + shoot->setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + connect( shoot, SIGNAL(clicked()), SLOT(fire()) ); + + connect( cannonField, SIGNAL(canShoot(bool)), + shoot, SLOT(setEnabled(bool)) ); + + TQPushButton *restart + = new TQPushButton( "&New Game", this, "newgame" ); + restart->setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + connect( restart, SIGNAL(clicked()), this, SLOT(newGame()) ); + + hits = new TQLCDNumber( 2, this, "hits" ); + shotsLeft = new TQLCDNumber( 2, this, "shotsleft" ); + TQLabel *hitsL = new TQLabel( "HITS", this, "hitsLabel" ); + TQLabel *shotsLeftL + = new TQLabel( "SHOTS LEFT", this, "shotsleftLabel" ); + + TQAccel *accel = new TQAccel( this ); + accel->connectItem( accel->insertItem( Key_Enter ), + this, SLOT(fire()) ); + accel->connectItem( accel->insertItem( Key_Return ), + this, SLOT(fire()) ); + accel->connectItem( accel->insertItem( CTRL+Key_Q ), + qApp, SLOT(tquit()) ); + + TQGridLayout *grid = new TQGridLayout( this, 2, 2, 10 ); + grid->addWidget( tquit, 0, 0 ); + grid->addWidget( box, 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->addWidget( hits ); + topBox->addWidget( hitsL ); + topBox->addWidget( shotsLeft ); + topBox->addWidget( shotsLeftL ); + topBox->addStretch( 1 ); + topBox->addWidget( restart ); + + angle->setValue( 60 ); + force->setValue( 25 ); + angle->setFocus(); + + newGame(); +} + + +void GameBoard::fire() +{ + if ( cannonField->gameOver() || cannonField->isShooting() ) + return; + shotsLeft->display( shotsLeft->intValue() - 1 ); + cannonField->shoot(); +} + + +void GameBoard::hit() +{ + hits->display( hits->intValue() + 1 ); + if ( shotsLeft->intValue() == 0 ) + cannonField->setGameOver(); + else + cannonField->newTarget(); +} + + +void GameBoard::missed() +{ + if ( shotsLeft->intValue() == 0 ) + cannonField->setGameOver(); +} + + +void GameBoard::newGame() +{ + shotsLeft->display( 15 ); + hits->display( 0 ); + cannonField->restartGame(); + cannonField->newTarget(); +} diff --git a/tutorial/t14/gamebrd.h b/tutorial/t14/gamebrd.h new file mode 100644 index 000000000..4a4306c97 --- /dev/null +++ b/tutorial/t14/gamebrd.h @@ -0,0 +1,40 @@ +/**************************************************************** +** +** Definition of GameBoard class, TQt tutorial 14 +** +****************************************************************/ + +#ifndef GAMEBRD_H +#define GAMEBRD_H + +#include <qwidget.h> + +class TQPushButton; +class LCDRange; +class TQLCDNumber; +class CannonField; + +#include "lcdrange.h" +#include "cannon.h" + + +class GameBoard : public TQWidget +{ + Q_OBJECT +public: + GameBoard( TQWidget *parent=0, const char *name=0 ); + +protected slots: + void fire(); + void hit(); + void missed(); + void newGame(); + +private: + TQLCDNumber *hits; + TQLCDNumber *shotsLeft; + CannonField *cannonField; +}; + + +#endif // GAMEBRD_H diff --git a/tutorial/t14/lcdrange.cpp b/tutorial/t14/lcdrange.cpp new file mode 100644 index 000000000..aff272af6 --- /dev/null +++ b/tutorial/t14/lcdrange.cpp @@ -0,0 +1,88 @@ +/**************************************************************** +** +** Implementation of LCDRange class, TQt tutorial 12 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> +#include <qlabel.h> +#include <qlayout.h> + + +LCDRange::LCDRange( TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + init(); +} + + +LCDRange::LCDRange( const char *s, TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + init(); + setText( s ); +} + + +void LCDRange::init() +{ + TQLCDNumber *lcd = new TQLCDNumber( 2, this, "lcd" ); + slider = new TQSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + + label = new TQLabel( " ", this, "label" ); + label->setAlignment( AlignCenter ); + + connect( slider, SIGNAL(valueChanged(int)), + lcd, SLOT(display(int)) ); + connect( slider, SIGNAL(valueChanged(int)), + SIGNAL(valueChanged(int)) ); + + setFocusProxy( slider ); + + TQVBoxLayout * l = new TQVBoxLayout( this ); + l->addWidget( lcd, 1 ); + l->addWidget( slider ); + l->addWidget( label ); +} + + +int LCDRange::value() const +{ + return slider->value(); +} + + +const char *LCDRange::text() const +{ + return label->text(); +} + + +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 ); +} + + +void LCDRange::setText( const char *s ) +{ + label->setText( s ); +} diff --git a/tutorial/t14/lcdrange.h b/tutorial/t14/lcdrange.h new file mode 100644 index 000000000..874dd169a --- /dev/null +++ b/tutorial/t14/lcdrange.h @@ -0,0 +1,42 @@ +/**************************************************************** +** +** Definition of LCDRange class, TQt tutorial 12 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qwidget.h> + +class TQSlider; +class TQLabel; + + +class LCDRange : public TQWidget +{ + Q_OBJECT +public: + LCDRange( TQWidget *parent=0, const char *name=0 ); + LCDRange( const char *s, TQWidget *parent=0, const char *name=0 ); + + int value() const; + const char *text() const; + +public slots: + void setValue( int ); + void setRange( int minVal, int maxVal ); + void setText( const char * ); + +signals: + void valueChanged( int ); + +private: + void init(); + + TQSlider *slider; + TQLabel *label; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t14/main.cpp b/tutorial/t14/main.cpp new file mode 100644 index 000000000..5f2b439ea --- /dev/null +++ b/tutorial/t14/main.cpp @@ -0,0 +1,22 @@ +/**************************************************************** +** +** TQt tutorial 14 +** +****************************************************************/ + +#include <qapplication.h> + +#include "gamebrd.h" + + +int main( int argc, char **argv ) +{ + TQApplication::setColorSpec( TQApplication::CustomColor ); + TQApplication a( argc, argv ); + + GameBoard gb; + gb.setGeometry( 100, 100, 500, 355 ); + a.setMainWidget( &gb ); + gb.show(); + return a.exec(); +} diff --git a/tutorial/t14/t14.pro b/tutorial/t14/t14.pro new file mode 100644 index 000000000..665b7844f --- /dev/null +++ b/tutorial/t14/t14.pro @@ -0,0 +1,12 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = cannon.h \ + gamebrd.h \ + lcdrange.h +SOURCES = cannon.cpp \ + gamebrd.cpp \ + lcdrange.cpp \ + main.cpp +TARGET = t14 +REQUIRES=full-config +unix:LIBS += -lm diff --git a/tutorial/t2/main.cpp b/tutorial/t2/main.cpp new file mode 100644 index 000000000..1b3da3f1d --- /dev/null +++ b/tutorial/t2/main.cpp @@ -0,0 +1,25 @@ +/**************************************************************** +** +** TQt tutorial 2 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qfont.h> + + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + + TQPushButton tquit( "Quit", 0 ); + tquit.resize( 75, 30 ); + tquit.setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + TQObject::connect( &tquit, SIGNAL(clicked()), &a, SLOT(tquit()) ); + + a.setMainWidget( &tquit ); + tquit.show(); + return a.exec(); +} diff --git a/tutorial/t2/t2.pro b/tutorial/t2/t2.pro new file mode 100644 index 000000000..bf4781b5f --- /dev/null +++ b/tutorial/t2/t2.pro @@ -0,0 +1,6 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = +SOURCES = main.cpp +TARGET = t2 +REQUIRES=small-config diff --git a/tutorial/t3/main.cpp b/tutorial/t3/main.cpp new file mode 100644 index 000000000..34f47fc95 --- /dev/null +++ b/tutorial/t3/main.cpp @@ -0,0 +1,28 @@ +/**************************************************************** +** +** TQt tutorial 3 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qfont.h> +#include <qvbox.h> + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + + TQVBox box; + box.resize( 200, 120 ); + + TQPushButton tquit( "Quit", &box ); + tquit.setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + TQObject::connect( &tquit, SIGNAL(clicked()), &a, SLOT(tquit()) ); + + a.setMainWidget( &box ); + box.show(); + + return a.exec(); +} diff --git a/tutorial/t3/t3.pro b/tutorial/t3/t3.pro new file mode 100644 index 000000000..8d1f69407 --- /dev/null +++ b/tutorial/t3/t3.pro @@ -0,0 +1,6 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = +SOURCES = main.cpp +TARGET = t3 +REQUIRES=small-config diff --git a/tutorial/t4/main.cpp b/tutorial/t4/main.cpp new file mode 100644 index 000000000..04562d076 --- /dev/null +++ b/tutorial/t4/main.cpp @@ -0,0 +1,42 @@ +/**************************************************************** +** +** TQt tutorial 4 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qfont.h> + + +class MyWidget : public TQWidget +{ +public: + MyWidget( TQWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + setMinimumSize( 200, 120 ); + setMaximumSize( 200, 120 ); + + TQPushButton *tquit = new TQPushButton( "Quit", this, "tquit" ); + tquit->setGeometry( 62, 40, 75, 30 ); + tquit->setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + connect( tquit, SIGNAL(clicked()), qApp, SLOT(tquit()) ); +} + + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + + MyWidget w; + w.setGeometry( 100, 100, 200, 120 ); + a.setMainWidget( &w ); + w.show(); + return a.exec(); +} diff --git a/tutorial/t4/t4.pro b/tutorial/t4/t4.pro new file mode 100644 index 000000000..4688fe411 --- /dev/null +++ b/tutorial/t4/t4.pro @@ -0,0 +1,6 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = +SOURCES = main.cpp +TARGET = t4 +REQUIRES=small-config diff --git a/tutorial/t5/main.cpp b/tutorial/t5/main.cpp new file mode 100644 index 000000000..64ee0436f --- /dev/null +++ b/tutorial/t5/main.cpp @@ -0,0 +1,47 @@ +/**************************************************************** +** +** TQt tutorial 5 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qslider.h> +#include <qlcdnumber.h> +#include <qfont.h> + +#include <qvbox.h> + +class MyWidget : public TQVBox +{ +public: + MyWidget( TQWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( TQWidget *parent, const char *name ) + : TQVBox( parent, name ) +{ + TQPushButton *tquit = new TQPushButton( "Quit", this, "tquit" ); + tquit->setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + connect( tquit, SIGNAL(clicked()), qApp, SLOT(tquit()) ); + + TQLCDNumber *lcd = new TQLCDNumber( 2, this, "lcd" ); + + TQSlider * slider = new TQSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + + connect( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) ); +} + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + + MyWidget w; + a.setMainWidget( &w ); + w.show(); + return a.exec(); +} diff --git a/tutorial/t5/t5.pro b/tutorial/t5/t5.pro new file mode 100644 index 000000000..3ddf02c88 --- /dev/null +++ b/tutorial/t5/t5.pro @@ -0,0 +1,6 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = +SOURCES = main.cpp +TARGET = t5 +REQUIRES=large-config diff --git a/tutorial/t6/main.cpp b/tutorial/t6/main.cpp new file mode 100644 index 000000000..16d593f2e --- /dev/null +++ b/tutorial/t6/main.cpp @@ -0,0 +1,61 @@ +/**************************************************************** +** +** TQt tutorial 6 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qslider.h> +#include <qlcdnumber.h> +#include <qfont.h> +#include <qvbox.h> +#include <qgrid.h> + +class LCDRange : public TQVBox +{ +public: + LCDRange( TQWidget *parent=0, const char *name=0 ); +}; + +LCDRange::LCDRange( TQWidget *parent, const char *name ) + : TQVBox( parent, name ) +{ + TQLCDNumber *lcd = new TQLCDNumber( 2, this, "lcd" ); + TQSlider * slider = new TQSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + connect( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) ); +} + +class MyWidget : public TQVBox +{ +public: + MyWidget( TQWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( TQWidget *parent, const char *name ) + : TQVBox( parent, name ) +{ + TQPushButton *tquit = new TQPushButton( "Quit", this, "tquit" ); + tquit->setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + connect( tquit, SIGNAL(clicked()), qApp, SLOT(tquit()) ); + + TQGrid *grid = new TQGrid( 4, this ); + + for( int r = 0 ; r < 4 ; r++ ) + for( int c = 0 ; c < 4 ; c++ ) + (void)new LCDRange( grid ); +} + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + + MyWidget w; + a.setMainWidget( &w ); + w.show(); + return a.exec(); +} diff --git a/tutorial/t6/t6.pro b/tutorial/t6/t6.pro new file mode 100644 index 000000000..19dba8e91 --- /dev/null +++ b/tutorial/t6/t6.pro @@ -0,0 +1,6 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = +SOURCES = main.cpp +TARGET = t6 +REQUIRES=large-config diff --git a/tutorial/t7/lcdrange.cpp b/tutorial/t7/lcdrange.cpp new file mode 100644 index 000000000..ab057d1ec --- /dev/null +++ b/tutorial/t7/lcdrange.cpp @@ -0,0 +1,33 @@ +/**************************************************************** +** +** Implementation of LCDRange class, TQt tutorial 7 +** +****************************************************************/ + +#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)) ); +} + +int LCDRange::value() const +{ + return slider->value(); +} + +void LCDRange::setValue( int value ) +{ + slider->setValue( value ); +} diff --git a/tutorial/t7/lcdrange.h b/tutorial/t7/lcdrange.h new file mode 100644 index 000000000..365c73289 --- /dev/null +++ b/tutorial/t7/lcdrange.h @@ -0,0 +1,34 @@ +/**************************************************************** +** +** Definition of LCDRange class, TQt tutorial 7 +** +****************************************************************/ + +#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 ); + +signals: + void valueChanged( int ); + +private: + TQSlider *slider; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t7/main.cpp b/tutorial/t7/main.cpp new file mode 100644 index 000000000..246797707 --- /dev/null +++ b/tutorial/t7/main.cpp @@ -0,0 +1,55 @@ +/**************************************************************** +** +** TQt tutorial 7 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qlcdnumber.h> +#include <qfont.h> +#include <qvbox.h> +#include <qgrid.h> + +#include "lcdrange.h" + + +class MyWidget : public TQVBox +{ +public: + MyWidget( TQWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( TQWidget *parent, const char *name ) + : TQVBox( parent, name ) +{ + TQPushButton *tquit = new TQPushButton( "Quit", this, "tquit" ); + tquit->setFont( TQFont( "Times", 18, TQFont::Bold ) ); + + connect( tquit, SIGNAL(clicked()), qApp, SLOT(tquit()) ); + + TQGrid *grid = new TQGrid( 4, this ); + + LCDRange *previous = 0; + for( int r = 0 ; r < 4 ; r++ ) { + for( int c = 0 ; c < 4 ; c++ ) { + LCDRange* lr = new LCDRange( grid ); + if ( previous ) + connect( lr, SIGNAL(valueChanged(int)), + previous, SLOT(setValue(int)) ); + previous = lr; + } + } +} + + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + + MyWidget w; + a.setMainWidget( &w ); + w.show(); + return a.exec(); +} diff --git a/tutorial/t7/t7.pro b/tutorial/t7/t7.pro new file mode 100644 index 000000000..f83e7cd9c --- /dev/null +++ b/tutorial/t7/t7.pro @@ -0,0 +1,7 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = lcdrange.h +SOURCES = lcdrange.cpp \ + main.cpp +TARGET = t7 +REQUIRES=large-config diff --git a/tutorial/t8/cannon.cpp b/tutorial/t8/cannon.cpp new file mode 100644 index 000000000..d5f3b6ba0 --- /dev/null +++ b/tutorial/t8/cannon.cpp @@ -0,0 +1,45 @@ +/**************************************************************** +** +** Implementation CannonField class, TQt tutorial 8 +** +****************************************************************/ + +#include "cannon.h" +#include <qpainter.h> + + +CannonField::CannonField( TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + ang = 45; + 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(); + emit angleChanged( ang ); +} + + +void CannonField::paintEvent( TQPaintEvent * ) +{ + TQString s = "Angle = " + TQString::number( ang ); + TQPainter p( this ); + p.drawText( 200, 200, s ); +} + + +TQSizePolicy CannonField::sizePolicy() const +{ + return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ); +} + diff --git a/tutorial/t8/cannon.h b/tutorial/t8/cannon.h new file mode 100644 index 000000000..bbd271d1a --- /dev/null +++ b/tutorial/t8/cannon.h @@ -0,0 +1,36 @@ +/**************************************************************** +** +** Definition of CannonField class, TQt tutorial 8 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +#include <qwidget.h> + + +class CannonField : public TQWidget +{ + Q_OBJECT +public: + CannonField( TQWidget *parent=0, const char *name=0 ); + + int angle() const { return ang; } + TQSizePolicy sizePolicy() const; + +public slots: + void setAngle( int degrees ); + +signals: + void angleChanged( int ); + +protected: + void paintEvent( TQPaintEvent * ); + +private: + int ang; +}; + + +#endif // CANNON_H diff --git a/tutorial/t8/lcdrange.cpp b/tutorial/t8/lcdrange.cpp new file mode 100644 index 000000000..ca0aba8a8 --- /dev/null +++ b/tutorial/t8/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/t8/lcdrange.h b/tutorial/t8/lcdrange.h new file mode 100644 index 000000000..4a79a6ca9 --- /dev/null +++ b/tutorial/t8/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/t8/main.cpp b/tutorial/t8/main.cpp new file mode 100644 index 000000000..9acd0382d --- /dev/null +++ b/tutorial/t8/main.cpp @@ -0,0 +1,65 @@ +/**************************************************************** +** +** TQt tutorial 8 +** +****************************************************************/ + +#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 ); + + CannonField *cannonField + = new CannonField( this, "cannonField" ); + + connect( angle, SIGNAL(valueChanged(int)), + cannonField, SLOT(setAngle(int)) ); + connect( cannonField, SIGNAL(angleChanged(int)), + angle, SLOT(setValue(int)) ); + + TQGridLayout *grid = new TQGridLayout( this, 2, 2, 10 ); + //2x2, 10 pixel border + + grid->addWidget( tquit, 0, 0 ); + grid->addWidget( angle, 1, 0, TQt::AlignTop ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + angle->setValue( 60 ); + angle->setFocus(); +} + + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + + MyWidget w; + w.setGeometry( 100, 100, 500, 355 ); + a.setMainWidget( &w ); + w.show(); + return a.exec(); +} diff --git a/tutorial/t8/t8.pro b/tutorial/t8/t8.pro new file mode 100644 index 000000000..8eca4fa5c --- /dev/null +++ b/tutorial/t8/t8.pro @@ -0,0 +1,9 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = cannon.h \ + lcdrange.h +SOURCES = cannon.cpp \ + lcdrange.cpp \ + main.cpp +TARGET = t8 +REQUIRES=large-config diff --git a/tutorial/t9/cannon.cpp b/tutorial/t9/cannon.cpp new file mode 100644 index 000000000..ffd47e029 --- /dev/null +++ b/tutorial/t9/cannon.cpp @@ -0,0 +1,50 @@ +/**************************************************************** +** +** Implementation CannonField class, TQt tutorial 9 +** +****************************************************************/ + +#include "cannon.h" +#include <qpainter.h> + + +CannonField::CannonField( TQWidget *parent, const char *name ) + : TQWidget( parent, name ) +{ + ang = 45; + 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(); + emit angleChanged( ang ); +} + + +void CannonField::paintEvent( TQPaintEvent * ) +{ + TQPainter p( this ); + + p.setBrush( blue ); + p.setPen( NoPen ); + + p.translate( 0, rect().bottom() ); + p.drawPie( TQRect(-35, -35, 70, 70), 0, 90*16 ); + p.rotate( -ang ); + p.drawRect( TQRect(33, -4, 15, 8) ); +} + + +TQSizePolicy CannonField::sizePolicy() const +{ + return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ); +} diff --git a/tutorial/t9/cannon.h b/tutorial/t9/cannon.h new file mode 100644 index 000000000..bbd271d1a --- /dev/null +++ b/tutorial/t9/cannon.h @@ -0,0 +1,36 @@ +/**************************************************************** +** +** Definition of CannonField class, TQt tutorial 8 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +#include <qwidget.h> + + +class CannonField : public TQWidget +{ + Q_OBJECT +public: + CannonField( TQWidget *parent=0, const char *name=0 ); + + int angle() const { return ang; } + TQSizePolicy sizePolicy() const; + +public slots: + void setAngle( int degrees ); + +signals: + void angleChanged( int ); + +protected: + void paintEvent( TQPaintEvent * ); + +private: + int ang; +}; + + +#endif // CANNON_H diff --git a/tutorial/t9/lcdrange.cpp b/tutorial/t9/lcdrange.cpp new file mode 100644 index 000000000..ca0aba8a8 --- /dev/null +++ b/tutorial/t9/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/t9/lcdrange.h b/tutorial/t9/lcdrange.h new file mode 100644 index 000000000..4a79a6ca9 --- /dev/null +++ b/tutorial/t9/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/t9/main.cpp b/tutorial/t9/main.cpp new file mode 100644 index 000000000..506b66f9b --- /dev/null +++ b/tutorial/t9/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************** +** +** TQt tutorial 9 +** +****************************************************************/ + +#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 ); + + CannonField *cannonField = new CannonField( this, "cannonField" ); + + connect( angle, SIGNAL(valueChanged(int)), + cannonField, SLOT(setAngle(int)) ); + connect( cannonField, SIGNAL(angleChanged(int)), + angle, SLOT(setValue(int)) ); + + TQGridLayout *grid = new TQGridLayout( this, 2, 2, 10 ); + grid->addWidget( tquit, 0, 0 ); + grid->addWidget( angle, 1, 0, TQt::AlignTop ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + angle->setValue( 60 ); + 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/t9/t9.pro b/tutorial/t9/t9.pro new file mode 100644 index 000000000..b9d995a33 --- /dev/null +++ b/tutorial/t9/t9.pro @@ -0,0 +1,9 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = cannon.h \ + lcdrange.h +SOURCES = cannon.cpp \ + lcdrange.cpp \ + main.cpp +TARGET = t9 +REQUIRES=full-config diff --git a/tutorial/tutorial.pro b/tutorial/tutorial.pro new file mode 100644 index 000000000..32b31d7b6 --- /dev/null +++ b/tutorial/tutorial.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS = t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 |