summaryrefslogtreecommitdiffstats
path: root/tutorial/t12
diff options
context:
space:
mode:
authorTimothy Pearson <kb9vqf@pearsoncomputing.net>2011-11-08 12:31:36 -0600
committerTimothy Pearson <kb9vqf@pearsoncomputing.net>2011-11-08 12:31:36 -0600
commitd796c9dd933ab96ec83b9a634feedd5d32e1ba3f (patch)
tree6e3dcca4f77e20ec8966c666aac7c35bd4704053 /tutorial/t12
downloadtqt3-d796c9dd933ab96ec83b9a634feedd5d32e1ba3f.tar.gz
tqt3-d796c9dd933ab96ec83b9a634feedd5d32e1ba3f.zip
Test conversion to TQt3 from Qt3 8c6fc1f8e35fd264dd01c582ca5e7549b32ab731
Diffstat (limited to 'tutorial/t12')
-rw-r--r--tutorial/t12/cannon.cpp199
-rw-r--r--tutorial/t12/cannon.h64
-rw-r--r--tutorial/t12/lcdrange.cpp83
-rw-r--r--tutorial/t12/lcdrange.h43
-rw-r--r--tutorial/t12/main.cpp85
-rw-r--r--tutorial/t12/t12.pro10
6 files changed, 484 insertions, 0 deletions
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