diff options
Diffstat (limited to 'examples/drawlines')
-rw-r--r-- | examples/drawlines/README | 3 | ||||
-rw-r--r-- | examples/drawlines/connect.cpp | 132 | ||||
-rw-r--r-- | examples/drawlines/drawlines.doc | 20 | ||||
-rw-r--r-- | examples/drawlines/drawlines.pro | 9 |
4 files changed, 164 insertions, 0 deletions
diff --git a/examples/drawlines/README b/examples/drawlines/README new file mode 100644 index 000000000..2a7c577c6 --- /dev/null +++ b/examples/drawlines/README @@ -0,0 +1,3 @@ +The connect program connects points with lines. Drag the mouse +inside the widget and release it. It was inspired by an example in a +Windows book. diff --git a/examples/drawlines/connect.cpp b/examples/drawlines/connect.cpp new file mode 100644 index 000000000..cfd785ba7 --- /dev/null +++ b/examples/drawlines/connect.cpp @@ -0,0 +1,132 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qwidget.h> +#include <qpainter.h> +#include <qapplication.h> +#include <stdlib.h> + + +const int MAXPOINTS = 2000; // maximum number of points +const int MAXCOLORS = 40; + + +// +// ConnectWidget - draws connected lines +// + +class ConnectWidget : public TQWidget +{ +public: + ConnectWidget( TQWidget *parent=0, const char *name=0 ); + ~ConnectWidget(); +protected: + void paintEvent( TQPaintEvent * ); + void mousePressEvent( TQMouseEvent *); + void mouseReleaseEvent( TQMouseEvent *); + void mouseMoveEvent( TQMouseEvent *); +private: + TQPoint *points; // point array + TQColor *colors; // color array + int count; // count = number of points + bool down; // TRUE if mouse down +}; + + +// +// Constructs a ConnectWidget. +// + +ConnectWidget::ConnectWidget( TQWidget *parent, const char *name ) + : TQWidget( parent, name, WStaticContents ) +{ + setBackgroundColor( white ); // white background + count = 0; + down = FALSE; + points = new TQPoint[MAXPOINTS]; + colors = new TQColor[MAXCOLORS]; + for ( int i=0; i<MAXCOLORS; i++ ) // init color array + colors[i] = TQColor( rand()&255, rand()&255, rand()&255 ); +} + +ConnectWidget::~ConnectWidget() +{ + delete[] points; // cleanup + delete[] colors; +} + + +// +// Handles paint events for the connect widget. +// + +void ConnectWidget::paintEvent( TQPaintEvent * ) +{ + TQPainter paint( this ); + for ( int i=0; i<count-1; i++ ) { // connect all points + for ( int j=i+1; j<count; j++ ) { + paint.setPen( colors[rand()%MAXCOLORS] ); // set random pen color + paint.drawLine( points[i], points[j] ); // draw line + } + } +} + + +// +// Handles mouse press events for the connect widget. +// + +void ConnectWidget::mousePressEvent( TQMouseEvent * ) +{ + down = TRUE; + count = 0; // start recording points + erase(); // erase widget contents +} + + +// +// Handles mouse release events for the connect widget. +// + +void ConnectWidget::mouseReleaseEvent( TQMouseEvent * ) +{ + down = FALSE; // done recording points + update(); // draw the lines +} + + +// +// Handles mouse move events for the connect widget. +// + +void ConnectWidget::mouseMoveEvent( TQMouseEvent *e ) +{ + if ( down && count < MAXPOINTS ) { + TQPainter paint( this ); + points[count++] = e->pos(); // add point + paint.drawPoint( e->pos() ); // plot point + } +} + + +// +// Create and display a ConnectWidget. +// + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + ConnectWidget connect; +#ifndef QT_NO_WIDGET_TOPEXTRA // for TQt/Embedded minimal build + connect.setCaption( "TQt Example - Draw lines"); +#endif + a.setMainWidget( &connect ); + connect.show(); + return a.exec(); +} diff --git a/examples/drawlines/drawlines.doc b/examples/drawlines/drawlines.doc new file mode 100644 index 000000000..f0abeec3b --- /dev/null +++ b/examples/drawlines/drawlines.doc @@ -0,0 +1,20 @@ + +/* +*/ +/*! \page drawlines-example.html + + \ingroup examples + \title Connect the Points + + This example shows very simple mouse-based user interaction and + painting without any world transform matrix or other advanced + features. Run the program, click the button, move the mouse, + release the button, and watch the lines get drawn. + + <hr> + + Implementation: + + \include drawlines/connect.cpp +*/ + diff --git a/examples/drawlines/drawlines.pro b/examples/drawlines/drawlines.pro new file mode 100644 index 000000000..7c4eeb8c1 --- /dev/null +++ b/examples/drawlines/drawlines.pro @@ -0,0 +1,9 @@ +TEMPLATE = app +TARGET = drawlines + +CONFIG += qt warn_on release +DEPENDPATH = ../../include + + +HEADERS = +SOURCES = connect.cpp |