diff options
author | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-07-10 15:24:15 -0500 |
---|---|---|
committer | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-07-10 15:24:15 -0500 |
commit | bd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch) | |
tree | 7a520322212d48ebcb9fbe1087e7fca28b76185c /tutorial | |
download | qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip |
Add Qt3 development HEAD version
Diffstat (limited to 'tutorial')
64 files changed, 3393 insertions, 0 deletions
diff --git a/tutorial/README b/tutorial/README new file mode 100644 index 0000000..156a3a8 --- /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 0000000..0e50846 --- /dev/null +++ b/tutorial/t1/main.cpp @@ -0,0 +1,21 @@ +/**************************************************************** +** +** Qt tutorial 1 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> + + +int main( int argc, char **argv ) +{ + QApplication a( argc, argv ); + + QPushButton 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 0000000..dede5b1 --- /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 0000000..67e6f9a --- /dev/null +++ b/tutorial/t10/cannon.cpp @@ -0,0 +1,80 @@ +/**************************************************************** +** +** Implementation CannonField class, Qt tutorial 10 +** +****************************************************************/ + +#include "cannon.h" +#include <qpainter.h> +#include <qpixmap.h> + + +CannonField::CannonField( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + ang = 45; + f = 0; + setPalette( QPalette( QColor( 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( QPaintEvent *e ) +{ + if ( !e->rect().intersects( cannonRect() ) ) + return; + + QRect cr = cannonRect(); + QPixmap pix( cr.size() ); + pix.fill( this, cr.topLeft() ); + + QPainter p( &pix ); + p.setBrush( blue ); + p.setPen( NoPen ); + p.translate( 0, pix.height() - 1 ); + p.drawPie( QRect( -35,-35, 70, 70 ), 0, 90*16 ); + p.rotate( -ang ); + p.drawRect( QRect(33, -4, 15, 8) ); + p.end(); + + p.begin( this ); + p.drawPixmap( cr.topLeft(), pix ); +} + + +QRect CannonField::cannonRect() const +{ + QRect r( 0, 0, 50, 50 ); + r.moveBottomLeft( rect().bottomLeft() ); + return r; +} + + +QSizePolicy CannonField::sizePolicy() const +{ + return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); +} diff --git a/tutorial/t10/cannon.h b/tutorial/t10/cannon.h new file mode 100644 index 0000000..5a317d1 --- /dev/null +++ b/tutorial/t10/cannon.h @@ -0,0 +1,43 @@ +/**************************************************************** +** +** Definition of CannonField class, Qt tutorial 10 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +#include <qwidget.h> + + +class CannonField : public QWidget +{ + Q_OBJECT +public: + CannonField( QWidget *parent=0, const char *name=0 ); + + QSizePolicy 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( QPaintEvent * ); + +private: + QRect 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 0000000..b62cfbb --- /dev/null +++ b/tutorial/t10/lcdrange.cpp @@ -0,0 +1,47 @@ +/**************************************************************** +** +** Implementation of LCDRange class, Qt tutorial 8 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + slider = new QSlider( 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 0000000..5fa2d21 --- /dev/null +++ b/tutorial/t10/lcdrange.h @@ -0,0 +1,35 @@ +/**************************************************************** +** +** Definition of LCDRange class, Qt tutorial 8 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qvbox.h> + +class QSlider; + + +class LCDRange : public QVBox +{ + Q_OBJECT +public: + LCDRange( QWidget *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: + QSlider *slider; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t10/main.cpp b/tutorial/t10/main.cpp new file mode 100644 index 0000000..6dd2bf5 --- /dev/null +++ b/tutorial/t10/main.cpp @@ -0,0 +1,75 @@ +/**************************************************************** +** +** Qt 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 QWidget +{ +public: + MyWidget( QWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + QPushButton *quit = new QPushButton( "&Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + 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)) ); + + QGridLayout *grid = new QGridLayout( this, 2, 2, 10 ); + grid->addWidget( quit, 0, 0 ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + QVBoxLayout *leftBox = new QVBoxLayout; + 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 ) +{ + QApplication::setColorSpec( QApplication::CustomColor ); + QApplication 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 0000000..202b939 --- /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 0000000..bf44a6d --- /dev/null +++ b/tutorial/t11/cannon.cpp @@ -0,0 +1,156 @@ +/**************************************************************** +** +** Implementation CannonField class, Qt tutorial 11 +** +****************************************************************/ + +#include "cannon.h" +#include <qtimer.h> +#include <qpainter.h> +#include <qpixmap.h> + +#include <math.h> + + +CannonField::CannonField( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + ang = 45; + f = 0; + timerCount = 0; + autoShootTimer = new QTimer( this, "movement handler" ); + connect( autoShootTimer, SIGNAL(timeout()), + this, SLOT(moveShot()) ); + shoot_ang = 0; + shoot_f = 0; + setPalette( QPalette( QColor( 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() +{ + QRegion r( shotRect() ); + timerCount++; + + QRect shotR = shotRect(); + + if ( shotR.x() > width() || shotR.y() > height() ) + autoShootTimer->stop(); + else + r = r.unite( QRegion( shotR ) ); + repaint( r ); +} + + +void CannonField::paintEvent( QPaintEvent *e ) +{ + QRect updateR = e->rect(); + QPainter p( this ); + + if ( updateR.intersects( cannonRect() ) ) + paintCannon( &p ); + if ( autoShootTimer->isActive() && + updateR.intersects( shotRect() ) ) + paintShot( &p ); +} + + +void CannonField::paintShot( QPainter *p ) +{ + p->setBrush( black ); + p->setPen( NoPen ); + p->drawRect( shotRect() ); +} + + +const QRect barrelRect(33, -4, 15, 8); + +void CannonField::paintCannon( QPainter *p ) +{ + QRect cr = cannonRect(); + QPixmap pix( cr.size() ); + pix.fill( this, cr.topLeft() ); + + QPainter tmp( &pix ); + tmp.setBrush( blue ); + tmp.setPen( NoPen ); + + tmp.translate( 0, pix.height() - 1 ); + tmp.drawPie( QRect( -35,-35, 70, 70 ), 0, 90*16 ); + tmp.rotate( -ang ); + tmp.drawRect( barrelRect ); + tmp.end(); + + p->drawPixmap( cr.topLeft(), pix ); +} + + +QRect CannonField::cannonRect() const +{ + QRect r( 0, 0, 50, 50 ); + r.moveBottomLeft( rect().bottomLeft() ); + return r; +} + + +QRect 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; + + QRect r = QRect( 0, 0, 6, 6 ); + r.moveCenter( QPoint( qRound(x), height() - 1 - qRound(y) ) ); + return r; +} + + +QSizePolicy CannonField::sizePolicy() const +{ + return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); +} diff --git a/tutorial/t11/cannon.h b/tutorial/t11/cannon.h new file mode 100644 index 0000000..76f895d --- /dev/null +++ b/tutorial/t11/cannon.h @@ -0,0 +1,57 @@ +/**************************************************************** +** +** Definition of CannonField class, Qt tutorial 11 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +class QTimer; + + +#include <qwidget.h> + + +class CannonField : public QWidget +{ + Q_OBJECT +public: + CannonField( QWidget *parent=0, const char *name=0 ); + + int angle() const { return ang; } + int force() const { return f; } + QSizePolicy 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( QPaintEvent * ); + +private: + void paintShot( QPainter * ); + void paintCannon( QPainter * ); + QRect cannonRect() const; + QRect shotRect() const; + + int ang; + int f; + + int timerCount; + QTimer * 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 0000000..b62cfbb --- /dev/null +++ b/tutorial/t11/lcdrange.cpp @@ -0,0 +1,47 @@ +/**************************************************************** +** +** Implementation of LCDRange class, Qt tutorial 8 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + slider = new QSlider( 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 0000000..5fa2d21 --- /dev/null +++ b/tutorial/t11/lcdrange.h @@ -0,0 +1,35 @@ +/**************************************************************** +** +** Definition of LCDRange class, Qt tutorial 8 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qvbox.h> + +class QSlider; + + +class LCDRange : public QVBox +{ + Q_OBJECT +public: + LCDRange( QWidget *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: + QSlider *slider; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t11/main.cpp b/tutorial/t11/main.cpp new file mode 100644 index 0000000..899cdf6 --- /dev/null +++ b/tutorial/t11/main.cpp @@ -0,0 +1,85 @@ +/**************************************************************** +** +** Qt 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 QWidget +{ +public: + MyWidget( QWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + QPushButton *quit = new QPushButton( "&Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + 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)) ); + + QPushButton *shoot = new QPushButton( "&Shoot", this, "shoot" ); + shoot->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( shoot, SIGNAL(clicked()), cannonField, SLOT(shoot()) ); + + QGridLayout *grid = new QGridLayout( this, 2, 2, 10 ); + grid->addWidget( quit, 0, 0 ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + QVBoxLayout *leftBox = new QVBoxLayout; + grid->addLayout( leftBox, 1, 0 ); + leftBox->addWidget( angle ); + leftBox->addWidget( force ); + + QHBoxLayout *topBox = new QHBoxLayout; + 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 ) +{ + QApplication::setColorSpec( QApplication::CustomColor ); + QApplication 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 0000000..63fc17e --- /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 0000000..5303571 --- /dev/null +++ b/tutorial/t12/cannon.cpp @@ -0,0 +1,199 @@ +/**************************************************************** +** +** Implementation CannonField class, Qt 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( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + ang = 45; + f = 0; + timerCount = 0; + autoShootTimer = new QTimer( this, "movement handler" ); + connect( autoShootTimer, SIGNAL(timeout()), + this, SLOT(moveShot()) ); + shoot_ang = 0; + shoot_f = 0; + target = QPoint( 0, 0 ); + setPalette( QPalette( QColor( 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; + QTime midnight( 0, 0, 0 ); + srand( midnight.secsTo(QTime::currentTime()) ); + } + QRegion r( targetRect() ); + target = QPoint( 200 + rand() % 190, + 10 + rand() % 255 ); + repaint( r.unite( targetRect() ) ); +} + + +void CannonField::moveShot() +{ + QRegion r( shotRect() ); + timerCount++; + + QRect 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( QRegion( shotR ) ); + } + + repaint( r ); +} + + +void CannonField::paintEvent( QPaintEvent *e ) +{ + QRect updateR = e->rect(); + QPainter 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( QPainter *p ) +{ + p->setBrush( black ); + p->setPen( NoPen ); + p->drawRect( shotRect() ); +} + + +void CannonField::paintTarget( QPainter *p ) +{ + p->setBrush( red ); + p->setPen( black ); + p->drawRect( targetRect() ); +} + + +const QRect barrelRect(33, -4, 15, 8); + +void CannonField::paintCannon( QPainter *p ) +{ + QRect cr = cannonRect(); + QPixmap pix( cr.size() ); + pix.fill( this, cr.topLeft() ); + + QPainter tmp( &pix ); + tmp.setBrush( blue ); + tmp.setPen( NoPen ); + + tmp.translate( 0, pix.height() - 1 ); + tmp.drawPie( QRect( -35,-35, 70, 70 ), 0, 90*16 ); + tmp.rotate( -ang ); + tmp.drawRect( barrelRect ); + tmp.end(); + + p->drawPixmap( cr.topLeft(), pix ); +} + + +QRect CannonField::cannonRect() const +{ + QRect r( 0, 0, 50, 50 ); + r.moveBottomLeft( rect().bottomLeft() ); + return r; +} + + +QRect 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; + + QRect r = QRect( 0, 0, 6, 6 ); + r.moveCenter( QPoint( qRound(x), height() - 1 - qRound(y) ) ); + return r; +} + + +QRect CannonField::targetRect() const +{ + QRect r( 0, 0, 20, 10 ); + r.moveCenter( QPoint(target.x(),height() - 1 - target.y()) ); + return r; +} + + +QSizePolicy CannonField::sizePolicy() const +{ + return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); +} diff --git a/tutorial/t12/cannon.h b/tutorial/t12/cannon.h new file mode 100644 index 0000000..b8824b5 --- /dev/null +++ b/tutorial/t12/cannon.h @@ -0,0 +1,64 @@ +/**************************************************************** +** +** Definition of CannonField class, Qt tutorial 12 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +class QTimer; + + +#include <qwidget.h> + + +class CannonField : public QWidget +{ + Q_OBJECT +public: + CannonField( QWidget *parent=0, const char *name=0 ); + + int angle() const { return ang; } + int force() const { return f; } + QSizePolicy 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( QPaintEvent * ); + +private: + void paintShot( QPainter * ); + void paintTarget( QPainter * ); + void paintCannon( QPainter * ); + QRect cannonRect() const; + QRect shotRect() const; + QRect targetRect() const; + + int ang; + int f; + + int timerCount; + QTimer * autoShootTimer; + float shoot_ang; + float shoot_f; + + QPoint target; +}; + + +#endif // CANNON_H diff --git a/tutorial/t12/lcdrange.cpp b/tutorial/t12/lcdrange.cpp new file mode 100644 index 0000000..bdd722d --- /dev/null +++ b/tutorial/t12/lcdrange.cpp @@ -0,0 +1,83 @@ +/**************************************************************** +** +** Implementation of LCDRange class, Qt tutorial 12 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> +#include <qlabel.h> + + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + init(); +} + + +LCDRange::LCDRange( const char *s, QWidget *parent, + const char *name ) + : QVBox( parent, name ) +{ + init(); + setText( s ); +} + + +void LCDRange::init() +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + slider = new QSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + + label = new QLabel( " ", 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 0000000..9f93334 --- /dev/null +++ b/tutorial/t12/lcdrange.h @@ -0,0 +1,43 @@ +/**************************************************************** +** +** Definition of LCDRange class, Qt tutorial 12 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qvbox.h> + +class QSlider; +class QLabel; + + +class LCDRange : public QVBox +{ + Q_OBJECT +public: + LCDRange( QWidget *parent=0, const char *name=0 ); + LCDRange( const char *s, QWidget *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(); + + QSlider *slider; + QLabel *label; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t12/main.cpp b/tutorial/t12/main.cpp new file mode 100644 index 0000000..550eb72 --- /dev/null +++ b/tutorial/t12/main.cpp @@ -0,0 +1,85 @@ +/**************************************************************** +** +** Qt 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 QWidget +{ +public: + MyWidget( QWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + QPushButton *quit = new QPushButton( "&Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + 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)) ); + + QPushButton *shoot = new QPushButton( "&Shoot", this, "shoot" ); + shoot->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( shoot, SIGNAL(clicked()), cannonField, SLOT(shoot()) ); + + QGridLayout *grid = new QGridLayout( this, 2, 2, 10 ); + grid->addWidget( quit, 0, 0 ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + QVBoxLayout *leftBox = new QVBoxLayout; + grid->addLayout( leftBox, 1, 0 ); + leftBox->addWidget( angle ); + leftBox->addWidget( force ); + + QHBoxLayout *topBox = new QHBoxLayout; + 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 ) +{ + QApplication::setColorSpec( QApplication::CustomColor ); + QApplication 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 0000000..679890f --- /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 0000000..ac8f3d1 --- /dev/null +++ b/tutorial/t13/cannon.cpp @@ -0,0 +1,231 @@ +/**************************************************************** +** +** Implementation CannonField class, Qt 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( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + ang = 45; + f = 0; + timerCount = 0; + autoShootTimer = new QTimer( this, "movement handler" ); + connect( autoShootTimer, SIGNAL(timeout()), + this, SLOT(moveShot()) ); + shoot_ang = 0; + shoot_f = 0; + target = QPoint( 0, 0 ); + gameEnded = FALSE; + setPalette( QPalette( QColor( 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; + QTime midnight( 0, 0, 0 ); + srand( midnight.secsTo(QTime::currentTime()) ); + } + QRegion r( targetRect() ); + target = QPoint( 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() +{ + QRegion r( shotRect() ); + timerCount++; + + QRect 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( QRegion( shotR ) ); + } + + repaint( r ); +} + + +void CannonField::paintEvent( QPaintEvent *e ) +{ + QRect updateR = e->rect(); + QPainter p( this ); + + if ( gameEnded ) { + p.setPen( black ); + p.setFont( QFont( "Courier", 48, QFont::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( QPainter *p ) +{ + p->setBrush( black ); + p->setPen( NoPen ); + p->drawRect( shotRect() ); +} + + +void CannonField::paintTarget( QPainter *p ) +{ + p->setBrush( red ); + p->setPen( black ); + p->drawRect( targetRect() ); +} + + +const QRect barrelRect(33, -4, 15, 8); + +void CannonField::paintCannon( QPainter *p ) +{ + QRect cr = cannonRect(); + QPixmap pix( cr.size() ); + pix.fill( this, cr.topLeft() ); + + QPainter tmp( &pix ); + tmp.setBrush( blue ); + tmp.setPen( NoPen ); + + tmp.translate( 0, pix.height() - 1 ); + tmp.drawPie( QRect( -35,-35, 70, 70 ), 0, 90*16 ); + tmp.rotate( -ang ); + tmp.drawRect( barrelRect ); + tmp.end(); + + p->drawPixmap( cr.topLeft(), pix ); +} + + +QRect CannonField::cannonRect() const +{ + QRect r( 0, 0, 50, 50 ); + r.moveBottomLeft( rect().bottomLeft() ); + return r; +} + + +QRect 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; + + QRect r = QRect( 0, 0, 6, 6 ); + r.moveCenter( QPoint( qRound(x), height() - 1 - qRound(y) ) ); + return r; +} + + +QRect CannonField::targetRect() const +{ + QRect r( 0, 0, 20, 10 ); + r.moveCenter( QPoint(target.x(),height() - 1 - target.y()) ); + return r; +} + + +bool CannonField::isShooting() const +{ + return autoShootTimer->isActive(); +} + + +QSizePolicy CannonField::sizePolicy() const +{ + return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); +} diff --git a/tutorial/t13/cannon.h b/tutorial/t13/cannon.h new file mode 100644 index 0000000..ff1ff82 --- /dev/null +++ b/tutorial/t13/cannon.h @@ -0,0 +1,71 @@ +/**************************************************************** +** +** Definition of CannonField class, Qt tutorial 13 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +class QTimer; + + +#include <qwidget.h> + + +class CannonField : public QWidget +{ + Q_OBJECT +public: + CannonField( QWidget *parent=0, const char *name=0 ); + + int angle() const { return ang; } + int force() const { return f; } + bool gameOver() const { return gameEnded; } + bool isShooting() const; + QSizePolicy 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( QPaintEvent * ); + +private: + void paintShot( QPainter * ); + void paintTarget( QPainter * ); + void paintCannon( QPainter * ); + QRect cannonRect() const; + QRect shotRect() const; + QRect targetRect() const; + + int ang; + int f; + + int timerCount; + QTimer * autoShootTimer; + float shoot_ang; + float shoot_f; + + QPoint target; + + bool gameEnded; +}; + + +#endif // CANNON_H diff --git a/tutorial/t13/gamebrd.cpp b/tutorial/t13/gamebrd.cpp new file mode 100644 index 0000000..ffb3228 --- /dev/null +++ b/tutorial/t13/gamebrd.cpp @@ -0,0 +1,129 @@ +/**************************************************************** +** +** Implementation of GameBoard class, Qt 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( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + QPushButton *quit = new QPushButton( "&Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + 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()) ); + + QPushButton *shoot = new QPushButton( "&Shoot", this, "shoot" ); + shoot->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( shoot, SIGNAL(clicked()), SLOT(fire()) ); + connect( cannonField, SIGNAL(canShoot(bool)), + shoot, SLOT(setEnabled(bool)) ); + + QPushButton *restart + = new QPushButton( "&New Game", this, "newgame" ); + restart->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( restart, SIGNAL(clicked()), this, SLOT(newGame()) ); + + hits = new QLCDNumber( 2, this, "hits" ); + shotsLeft = new QLCDNumber( 2, this, "shotsleft" ); + QLabel *hitsL = new QLabel( "HITS", this, "hitsLabel" ); + QLabel *shotsLeftL + = new QLabel( "SHOTS LEFT", this, "shotsleftLabel" ); + + QGridLayout *grid = new QGridLayout( this, 2, 2, 10 ); + grid->addWidget( quit, 0, 0 ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + QVBoxLayout *leftBox = new QVBoxLayout; + grid->addLayout( leftBox, 1, 0 ); + leftBox->addWidget( angle ); + leftBox->addWidget( force ); + + QHBoxLayout *topBox = new QHBoxLayout; + 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 0000000..1c629a4 --- /dev/null +++ b/tutorial/t13/gamebrd.h @@ -0,0 +1,40 @@ +/**************************************************************** +** +** Definition of GameBoard class, Qt tutorial 13 +** +****************************************************************/ + +#ifndef GAMEBRD_H +#define GAMEBRD_H + +#include <qwidget.h> + +class QPushButton; +class LCDRange; +class QLCDNumber; +class CannonField; + +#include "lcdrange.h" +#include "cannon.h" + + +class GameBoard : public QWidget +{ + Q_OBJECT +public: + GameBoard( QWidget *parent=0, const char *name=0 ); + +protected slots: + void fire(); + void hit(); + void missed(); + void newGame(); + +private: + QLCDNumber *hits; + QLCDNumber *shotsLeft; + CannonField *cannonField; +}; + + +#endif // GAMEBRD_H diff --git a/tutorial/t13/lcdrange.cpp b/tutorial/t13/lcdrange.cpp new file mode 100644 index 0000000..3c91b27 --- /dev/null +++ b/tutorial/t13/lcdrange.cpp @@ -0,0 +1,88 @@ +/**************************************************************** +** +** Implementation of LCDRange class, Qt tutorial 12 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> +#include <qlabel.h> +#include <qlayout.h> + + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + init(); +} + + +LCDRange::LCDRange( const char *s, QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + init(); + setText( s ); +} + + +void LCDRange::init() +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + slider = new QSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + + label = new QLabel( " ", this, "label" ); + label->setAlignment( AlignCenter ); + + connect( slider, SIGNAL(valueChanged(int)), + lcd, SLOT(display(int)) ); + connect( slider, SIGNAL(valueChanged(int)), + SIGNAL(valueChanged(int)) ); + + setFocusProxy( slider ); + + QVBoxLayout * l = new QVBoxLayout( 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 0000000..2df2686 --- /dev/null +++ b/tutorial/t13/lcdrange.h @@ -0,0 +1,42 @@ +/**************************************************************** +** +** Definition of LCDRange class, Qt tutorial 12 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qwidget.h> + +class QSlider; +class QLabel; + + +class LCDRange : public QWidget +{ + Q_OBJECT +public: + LCDRange( QWidget *parent=0, const char *name=0 ); + LCDRange( const char *s, QWidget *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(); + + QSlider *slider; + QLabel *label; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t13/main.cpp b/tutorial/t13/main.cpp new file mode 100644 index 0000000..63f4ea7 --- /dev/null +++ b/tutorial/t13/main.cpp @@ -0,0 +1,22 @@ +/**************************************************************** +** +** Qt tutorial 13 +** +****************************************************************/ + +#include <qapplication.h> + +#include "gamebrd.h" + + +int main( int argc, char **argv ) +{ + QApplication::setColorSpec( QApplication::CustomColor ); + QApplication 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 0000000..a670da6 --- /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 0000000..6757fdc --- /dev/null +++ b/tutorial/t14/cannon.cpp @@ -0,0 +1,292 @@ +/**************************************************************** +** +** Implementation CannonField class, Qt 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( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + ang = 45; + f = 0; + timerCount = 0; + autoShootTimer = new QTimer( this, "movement handler" ); + connect( autoShootTimer, SIGNAL(timeout()), + this, SLOT(moveShot()) ); + shoot_ang = 0; + shoot_f = 0; + target = QPoint( 0, 0 ); + gameEnded = FALSE; + barrelPressed = FALSE; + setPalette( QPalette( QColor( 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; + QTime midnight( 0, 0, 0 ); + srand( midnight.secsTo(QTime::currentTime()) ); + } + QRegion r( targetRect() ); + target = QPoint( 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() +{ + QRegion r( shotRect() ); + timerCount++; + + QRect 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( QRegion( shotR ) ); + } + + repaint( r ); +} + + +void CannonField::mousePressEvent( QMouseEvent *e ) +{ + if ( e->button() != LeftButton ) + return; + if ( barrelHit( e->pos() ) ) + barrelPressed = TRUE; +} + + +void CannonField::mouseMoveEvent( QMouseEvent *e ) +{ + if ( !barrelPressed ) + return; + QPoint 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( QMouseEvent *e ) +{ + if ( e->button() == LeftButton ) + barrelPressed = FALSE; +} + + +void CannonField::paintEvent( QPaintEvent *e ) +{ + QRect updateR = e->rect(); + QPainter p( this ); + + if ( gameEnded ) { + p.setPen( black ); + p.setFont( QFont( "Courier", 48, QFont::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( QPainter *p ) +{ + p->setBrush( black ); + p->setPen( NoPen ); + p->drawRect( shotRect() ); +} + + +void CannonField::paintTarget( QPainter *p ) +{ + p->setBrush( red ); + p->setPen( black ); + p->drawRect( targetRect() ); +} + +void CannonField::paintBarrier( QPainter *p ) +{ + p->setBrush( yellow ); + p->setPen( black ); + p->drawRect( barrierRect() ); +} + +const QRect barrelRect(33, -4, 15, 8); + +void CannonField::paintCannon( QPainter *p ) +{ + QRect cr = cannonRect(); + QPixmap pix( cr.size() ); + pix.fill( this, cr.topLeft() ); + + QPainter tmp( &pix ); + tmp.setBrush( blue ); + tmp.setPen( NoPen ); + + tmp.translate( 0, pix.height() - 1 ); + tmp.drawPie( QRect( -35,-35, 70, 70 ), 0, 90*16 ); + tmp.rotate( -ang ); + tmp.drawRect( barrelRect ); + tmp.end(); + + p->drawPixmap( cr.topLeft(), pix ); +} + + +QRect CannonField::cannonRect() const +{ + QRect r( 0, 0, 50, 50 ); + r.moveBottomLeft( rect().bottomLeft() ); + return r; +} + + +QRect 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; + + QRect r = QRect( 0, 0, 6, 6 ); + r.moveCenter( QPoint( qRound(x), height() - 1 - qRound(y) ) ); + return r; +} + + +QRect CannonField::targetRect() const +{ + QRect r( 0, 0, 20, 10 ); + r.moveCenter( QPoint(target.x(),height() - 1 - target.y()) ); + return r; +} + + +QRect CannonField::barrierRect() const +{ + return QRect( 145, height() - 100, 15, 100 ); +} + + +bool CannonField::barrelHit( const QPoint &p ) const +{ + QWMatrix 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(); +} + + +QSize CannonField::sizeHint() const +{ + return QSize( 400, 300 ); +} + + +QSizePolicy CannonField::sizePolicy() const +{ + return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); +} diff --git a/tutorial/t14/cannon.h b/tutorial/t14/cannon.h new file mode 100644 index 0000000..1f46c71 --- /dev/null +++ b/tutorial/t14/cannon.h @@ -0,0 +1,79 @@ +/**************************************************************** +** +** Definition of CannonField class, Qt tutorial 14 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +class QTimer; + + +#include <qwidget.h> + + +class CannonField : public QWidget +{ + Q_OBJECT +public: + CannonField( QWidget *parent=0, const char *name=0 ); + + int angle() const { return ang; } + int force() const { return f; } + bool gameOver() const { return gameEnded; } + bool isShooting() const; + QSize sizeHint() const; + QSizePolicy 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( QPaintEvent * ); + void mousePressEvent( QMouseEvent * ); + void mouseMoveEvent( QMouseEvent * ); + void mouseReleaseEvent( QMouseEvent * ); + +private: + void paintShot( QPainter * ); + void paintTarget( QPainter * ); + void paintBarrier( QPainter * ); + void paintCannon( QPainter * ); + QRect cannonRect() const; + QRect shotRect() const; + QRect targetRect() const; + QRect barrierRect() const; + bool barrelHit( const QPoint & ) const; + + int ang; + int f; + + int timerCount; + QTimer * autoShootTimer; + float shoot_ang; + float shoot_f; + + QPoint 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 0000000..6f613b2 --- /dev/null +++ b/tutorial/t14/gamebrd.cpp @@ -0,0 +1,143 @@ +/**************************************************************** +** +** Implementation of GameBoard class, Qt 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( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + QPushButton *quit = new QPushButton( "&Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + LCDRange *angle = new LCDRange( "ANGLE", this, "angle" ); + angle->setRange( 5, 70 ); + + LCDRange *force = new LCDRange( "FORCE", this, "force" ); + force->setRange( 10, 50 ); + + QVBox *box = new QVBox( this, "cannonFrame" ); + box->setFrameStyle( QFrame::WinPanel | QFrame::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()) ); + + QPushButton *shoot = new QPushButton( "&Shoot", this, "shoot" ); + shoot->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( shoot, SIGNAL(clicked()), SLOT(fire()) ); + + connect( cannonField, SIGNAL(canShoot(bool)), + shoot, SLOT(setEnabled(bool)) ); + + QPushButton *restart + = new QPushButton( "&New Game", this, "newgame" ); + restart->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( restart, SIGNAL(clicked()), this, SLOT(newGame()) ); + + hits = new QLCDNumber( 2, this, "hits" ); + shotsLeft = new QLCDNumber( 2, this, "shotsleft" ); + QLabel *hitsL = new QLabel( "HITS", this, "hitsLabel" ); + QLabel *shotsLeftL + = new QLabel( "SHOTS LEFT", this, "shotsleftLabel" ); + + QAccel *accel = new QAccel( 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(quit()) ); + + QGridLayout *grid = new QGridLayout( this, 2, 2, 10 ); + grid->addWidget( quit, 0, 0 ); + grid->addWidget( box, 1, 1 ); + grid->setColStretch( 1, 10 ); + + QVBoxLayout *leftBox = new QVBoxLayout; + grid->addLayout( leftBox, 1, 0 ); + leftBox->addWidget( angle ); + leftBox->addWidget( force ); + + QHBoxLayout *topBox = new QHBoxLayout; + 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 0000000..9902639 --- /dev/null +++ b/tutorial/t14/gamebrd.h @@ -0,0 +1,40 @@ +/**************************************************************** +** +** Definition of GameBoard class, Qt tutorial 14 +** +****************************************************************/ + +#ifndef GAMEBRD_H +#define GAMEBRD_H + +#include <qwidget.h> + +class QPushButton; +class LCDRange; +class QLCDNumber; +class CannonField; + +#include "lcdrange.h" +#include "cannon.h" + + +class GameBoard : public QWidget +{ + Q_OBJECT +public: + GameBoard( QWidget *parent=0, const char *name=0 ); + +protected slots: + void fire(); + void hit(); + void missed(); + void newGame(); + +private: + QLCDNumber *hits; + QLCDNumber *shotsLeft; + CannonField *cannonField; +}; + + +#endif // GAMEBRD_H diff --git a/tutorial/t14/lcdrange.cpp b/tutorial/t14/lcdrange.cpp new file mode 100644 index 0000000..3c91b27 --- /dev/null +++ b/tutorial/t14/lcdrange.cpp @@ -0,0 +1,88 @@ +/**************************************************************** +** +** Implementation of LCDRange class, Qt tutorial 12 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> +#include <qlabel.h> +#include <qlayout.h> + + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + init(); +} + + +LCDRange::LCDRange( const char *s, QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + init(); + setText( s ); +} + + +void LCDRange::init() +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + slider = new QSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + + label = new QLabel( " ", this, "label" ); + label->setAlignment( AlignCenter ); + + connect( slider, SIGNAL(valueChanged(int)), + lcd, SLOT(display(int)) ); + connect( slider, SIGNAL(valueChanged(int)), + SIGNAL(valueChanged(int)) ); + + setFocusProxy( slider ); + + QVBoxLayout * l = new QVBoxLayout( 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 0000000..2df2686 --- /dev/null +++ b/tutorial/t14/lcdrange.h @@ -0,0 +1,42 @@ +/**************************************************************** +** +** Definition of LCDRange class, Qt tutorial 12 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qwidget.h> + +class QSlider; +class QLabel; + + +class LCDRange : public QWidget +{ + Q_OBJECT +public: + LCDRange( QWidget *parent=0, const char *name=0 ); + LCDRange( const char *s, QWidget *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(); + + QSlider *slider; + QLabel *label; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t14/main.cpp b/tutorial/t14/main.cpp new file mode 100644 index 0000000..8434413 --- /dev/null +++ b/tutorial/t14/main.cpp @@ -0,0 +1,22 @@ +/**************************************************************** +** +** Qt tutorial 14 +** +****************************************************************/ + +#include <qapplication.h> + +#include "gamebrd.h" + + +int main( int argc, char **argv ) +{ + QApplication::setColorSpec( QApplication::CustomColor ); + QApplication 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 0000000..665b784 --- /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 0000000..b1a2bb1 --- /dev/null +++ b/tutorial/t2/main.cpp @@ -0,0 +1,25 @@ +/**************************************************************** +** +** Qt tutorial 2 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qfont.h> + + +int main( int argc, char **argv ) +{ + QApplication a( argc, argv ); + + QPushButton quit( "Quit", 0 ); + quit.resize( 75, 30 ); + quit.setFont( QFont( "Times", 18, QFont::Bold ) ); + + QObject::connect( &quit, SIGNAL(clicked()), &a, SLOT(quit()) ); + + a.setMainWidget( &quit ); + quit.show(); + return a.exec(); +} diff --git a/tutorial/t2/t2.pro b/tutorial/t2/t2.pro new file mode 100644 index 0000000..bf4781b --- /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 0000000..5a02240 --- /dev/null +++ b/tutorial/t3/main.cpp @@ -0,0 +1,28 @@ +/**************************************************************** +** +** Qt tutorial 3 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qfont.h> +#include <qvbox.h> + +int main( int argc, char **argv ) +{ + QApplication a( argc, argv ); + + QVBox box; + box.resize( 200, 120 ); + + QPushButton quit( "Quit", &box ); + quit.setFont( QFont( "Times", 18, QFont::Bold ) ); + + QObject::connect( &quit, SIGNAL(clicked()), &a, SLOT(quit()) ); + + 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 0000000..8d1f694 --- /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 0000000..3a39c00 --- /dev/null +++ b/tutorial/t4/main.cpp @@ -0,0 +1,42 @@ +/**************************************************************** +** +** Qt tutorial 4 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qfont.h> + + +class MyWidget : public QWidget +{ +public: + MyWidget( QWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + setMinimumSize( 200, 120 ); + setMaximumSize( 200, 120 ); + + QPushButton *quit = new QPushButton( "Quit", this, "quit" ); + quit->setGeometry( 62, 40, 75, 30 ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); +} + + +int main( int argc, char **argv ) +{ + QApplication 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 0000000..4688fe4 --- /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 0000000..a538a82 --- /dev/null +++ b/tutorial/t5/main.cpp @@ -0,0 +1,47 @@ +/**************************************************************** +** +** Qt tutorial 5 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qslider.h> +#include <qlcdnumber.h> +#include <qfont.h> + +#include <qvbox.h> + +class MyWidget : public QVBox +{ +public: + MyWidget( QWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + QPushButton *quit = new QPushButton( "Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + + QSlider * slider = new QSlider( 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 ) +{ + QApplication 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 0000000..3ddf02c --- /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 0000000..a649893 --- /dev/null +++ b/tutorial/t6/main.cpp @@ -0,0 +1,61 @@ +/**************************************************************** +** +** Qt 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 QVBox +{ +public: + LCDRange( QWidget *parent=0, const char *name=0 ); +}; + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + QSlider * slider = new QSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + connect( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) ); +} + +class MyWidget : public QVBox +{ +public: + MyWidget( QWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + QPushButton *quit = new QPushButton( "Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + QGrid *grid = new QGrid( 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 ) +{ + QApplication 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 0000000..19dba8e --- /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 0000000..defa133 --- /dev/null +++ b/tutorial/t7/lcdrange.cpp @@ -0,0 +1,33 @@ +/**************************************************************** +** +** Implementation of LCDRange class, Qt tutorial 7 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + slider = new QSlider( 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 0000000..0c0219b --- /dev/null +++ b/tutorial/t7/lcdrange.h @@ -0,0 +1,34 @@ +/**************************************************************** +** +** Definition of LCDRange class, Qt tutorial 7 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qvbox.h> + +class QSlider; + + +class LCDRange : public QVBox +{ + Q_OBJECT +public: + LCDRange( QWidget *parent=0, const char *name=0 ); + + int value() const; + +public slots: + void setValue( int ); + +signals: + void valueChanged( int ); + +private: + QSlider *slider; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t7/main.cpp b/tutorial/t7/main.cpp new file mode 100644 index 0000000..385fd3b --- /dev/null +++ b/tutorial/t7/main.cpp @@ -0,0 +1,55 @@ +/**************************************************************** +** +** Qt 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 QVBox +{ +public: + MyWidget( QWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + QPushButton *quit = new QPushButton( "Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + QGrid *grid = new QGrid( 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 ) +{ + QApplication 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 0000000..f83e7cd --- /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 0000000..fd9d203 --- /dev/null +++ b/tutorial/t8/cannon.cpp @@ -0,0 +1,45 @@ +/**************************************************************** +** +** Implementation CannonField class, Qt tutorial 8 +** +****************************************************************/ + +#include "cannon.h" +#include <qpainter.h> + + +CannonField::CannonField( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + ang = 45; + setPalette( QPalette( QColor( 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( QPaintEvent * ) +{ + QString s = "Angle = " + QString::number( ang ); + QPainter p( this ); + p.drawText( 200, 200, s ); +} + + +QSizePolicy CannonField::sizePolicy() const +{ + return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); +} + diff --git a/tutorial/t8/cannon.h b/tutorial/t8/cannon.h new file mode 100644 index 0000000..a0928cd --- /dev/null +++ b/tutorial/t8/cannon.h @@ -0,0 +1,36 @@ +/**************************************************************** +** +** Definition of CannonField class, Qt tutorial 8 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +#include <qwidget.h> + + +class CannonField : public QWidget +{ + Q_OBJECT +public: + CannonField( QWidget *parent=0, const char *name=0 ); + + int angle() const { return ang; } + QSizePolicy sizePolicy() const; + +public slots: + void setAngle( int degrees ); + +signals: + void angleChanged( int ); + +protected: + void paintEvent( QPaintEvent * ); + +private: + int ang; +}; + + +#endif // CANNON_H diff --git a/tutorial/t8/lcdrange.cpp b/tutorial/t8/lcdrange.cpp new file mode 100644 index 0000000..b62cfbb --- /dev/null +++ b/tutorial/t8/lcdrange.cpp @@ -0,0 +1,47 @@ +/**************************************************************** +** +** Implementation of LCDRange class, Qt tutorial 8 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + slider = new QSlider( 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 0000000..5fa2d21 --- /dev/null +++ b/tutorial/t8/lcdrange.h @@ -0,0 +1,35 @@ +/**************************************************************** +** +** Definition of LCDRange class, Qt tutorial 8 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qvbox.h> + +class QSlider; + + +class LCDRange : public QVBox +{ + Q_OBJECT +public: + LCDRange( QWidget *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: + QSlider *slider; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t8/main.cpp b/tutorial/t8/main.cpp new file mode 100644 index 0000000..5983735 --- /dev/null +++ b/tutorial/t8/main.cpp @@ -0,0 +1,65 @@ +/**************************************************************** +** +** Qt 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 QWidget +{ +public: + MyWidget( QWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + QPushButton *quit = new QPushButton( "Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + 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)) ); + + QGridLayout *grid = new QGridLayout( this, 2, 2, 10 ); + //2x2, 10 pixel border + + grid->addWidget( quit, 0, 0 ); + grid->addWidget( angle, 1, 0, Qt::AlignTop ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + angle->setValue( 60 ); + angle->setFocus(); +} + + +int main( int argc, char **argv ) +{ + QApplication 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 0000000..8eca4fa --- /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 0000000..2badb39 --- /dev/null +++ b/tutorial/t9/cannon.cpp @@ -0,0 +1,50 @@ +/**************************************************************** +** +** Implementation CannonField class, Qt tutorial 9 +** +****************************************************************/ + +#include "cannon.h" +#include <qpainter.h> + + +CannonField::CannonField( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + ang = 45; + setPalette( QPalette( QColor( 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( QPaintEvent * ) +{ + QPainter p( this ); + + p.setBrush( blue ); + p.setPen( NoPen ); + + p.translate( 0, rect().bottom() ); + p.drawPie( QRect(-35, -35, 70, 70), 0, 90*16 ); + p.rotate( -ang ); + p.drawRect( QRect(33, -4, 15, 8) ); +} + + +QSizePolicy CannonField::sizePolicy() const +{ + return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); +} diff --git a/tutorial/t9/cannon.h b/tutorial/t9/cannon.h new file mode 100644 index 0000000..a0928cd --- /dev/null +++ b/tutorial/t9/cannon.h @@ -0,0 +1,36 @@ +/**************************************************************** +** +** Definition of CannonField class, Qt tutorial 8 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +#include <qwidget.h> + + +class CannonField : public QWidget +{ + Q_OBJECT +public: + CannonField( QWidget *parent=0, const char *name=0 ); + + int angle() const { return ang; } + QSizePolicy sizePolicy() const; + +public slots: + void setAngle( int degrees ); + +signals: + void angleChanged( int ); + +protected: + void paintEvent( QPaintEvent * ); + +private: + int ang; +}; + + +#endif // CANNON_H diff --git a/tutorial/t9/lcdrange.cpp b/tutorial/t9/lcdrange.cpp new file mode 100644 index 0000000..b62cfbb --- /dev/null +++ b/tutorial/t9/lcdrange.cpp @@ -0,0 +1,47 @@ +/**************************************************************** +** +** Implementation of LCDRange class, Qt tutorial 8 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + slider = new QSlider( 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 0000000..5fa2d21 --- /dev/null +++ b/tutorial/t9/lcdrange.h @@ -0,0 +1,35 @@ +/**************************************************************** +** +** Definition of LCDRange class, Qt tutorial 8 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qvbox.h> + +class QSlider; + + +class LCDRange : public QVBox +{ + Q_OBJECT +public: + LCDRange( QWidget *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: + QSlider *slider; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t9/main.cpp b/tutorial/t9/main.cpp new file mode 100644 index 0000000..ef1712e --- /dev/null +++ b/tutorial/t9/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************** +** +** Qt 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 QWidget +{ +public: + MyWidget( QWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + QPushButton *quit = new QPushButton( "&Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + 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)) ); + + QGridLayout *grid = new QGridLayout( this, 2, 2, 10 ); + grid->addWidget( quit, 0, 0 ); + grid->addWidget( angle, 1, 0, Qt::AlignTop ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + angle->setValue( 60 ); + angle->setFocus(); +} + +int main( int argc, char **argv ) +{ + QApplication::setColorSpec( QApplication::CustomColor ); + QApplication 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 0000000..b9d995a --- /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 0000000..32b31d7 --- /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 |