diff options
Diffstat (limited to 'examples/scribble')
-rw-r--r-- | examples/scribble/main.cpp | 29 | ||||
-rw-r--r-- | examples/scribble/scribble.cpp | 187 | ||||
-rw-r--r-- | examples/scribble/scribble.doc | 30 | ||||
-rw-r--r-- | examples/scribble/scribble.h | 87 | ||||
-rw-r--r-- | examples/scribble/scribble.pro | 11 |
5 files changed, 344 insertions, 0 deletions
diff --git a/examples/scribble/main.cpp b/examples/scribble/main.cpp new file mode 100644 index 000000000..8e5034c5c --- /dev/null +++ b/examples/scribble/main.cpp @@ -0,0 +1,29 @@ +/**************************************************************************** +** +** 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 "scribble.h" +#include <qapplication.h> + + +int main( int argc, char **argv ) +{ + TQApplication a( argc, argv ); + + Scribble scribble; + + scribble.resize( 500, 350 ); + scribble.setCaption("TQt Example - Scribble"); + a.setMainWidget( &scribble ); + if ( TQApplication::desktop()->width() > 550 + && TQApplication::desktop()->height() > 366 ) + scribble.show(); + else + scribble.showMaximized(); + return a.exec(); +} diff --git a/examples/scribble/scribble.cpp b/examples/scribble/scribble.cpp new file mode 100644 index 000000000..438f73d5f --- /dev/null +++ b/examples/scribble/scribble.cpp @@ -0,0 +1,187 @@ +/**************************************************************************** +** +** 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 "scribble.h" + +#include <qapplication.h> +#include <qevent.h> +#include <qpainter.h> +#include <qtoolbar.h> +#include <qtoolbutton.h> +#include <qspinbox.h> +#include <qtooltip.h> +#include <qrect.h> +#include <qpoint.h> +#include <qcolordialog.h> +#include <qfiledialog.h> +#include <qcursor.h> +#include <qimage.h> +#include <qstrlist.h> +#include <qpopupmenu.h> +#include <qintdict.h> + +const bool no_writing = FALSE; + +Canvas::Canvas( TQWidget *parent, const char *name ) + : TQWidget( parent, name, WStaticContents ), pen( TQt::red, 3 ), polyline(3), + mousePressed( FALSE ), buffer( width(), height() ) +{ + + if ((qApp->argc() > 0) && !buffer.load(qApp->argv()[1])) + buffer.fill( colorGroup().base() ); + setBackgroundMode( TQWidget::PaletteBase ); +#ifndef QT_NO_CURSOR + setCursor( TQt::crossCursor ); +#endif +} + +void Canvas::save( const TQString &filename, const TQString &format ) +{ + if ( !no_writing ) + buffer.save( filename, format.upper() ); +} + +void Canvas::clearScreen() +{ + buffer.fill( colorGroup().base() ); + repaint( FALSE ); +} + +void Canvas::mousePressEvent( TQMouseEvent *e ) +{ + mousePressed = TRUE; + polyline[2] = polyline[1] = polyline[0] = e->pos(); +} + +void Canvas::mouseReleaseEvent( TQMouseEvent * ) +{ + mousePressed = FALSE; +} + +void Canvas::mouseMoveEvent( TQMouseEvent *e ) +{ + if ( mousePressed ) { + TQPainter painter; + painter.begin( &buffer ); + painter.setPen( pen ); + polyline[2] = polyline[1]; + polyline[1] = polyline[0]; + polyline[0] = e->pos(); + painter.drawPolyline( polyline ); + painter.end(); + + TQRect r = polyline.boundingRect(); + r = r.normalize(); + r.setLeft( r.left() - penWidth() ); + r.setTop( r.top() - penWidth() ); + r.setRight( r.right() + penWidth() ); + r.setBottom( r.bottom() + penWidth() ); + + bitBlt( this, r.x(), r.y(), &buffer, r.x(), r.y(), r.width(), r.height() ); + } +} + +void Canvas::resizeEvent( TQResizeEvent *e ) +{ + TQWidget::resizeEvent( e ); + + int w = width() > buffer.width() ? + width() : buffer.width(); + int h = height() > buffer.height() ? + height() : buffer.height(); + + TQPixmap tmp( buffer ); + buffer.resize( w, h ); + buffer.fill( colorGroup().base() ); + bitBlt( &buffer, 0, 0, &tmp, 0, 0, tmp.width(), tmp.height() ); +} + +void Canvas::paintEvent( TQPaintEvent *e ) +{ + TQWidget::paintEvent( e ); + + TQMemArray<TQRect> rects = e->region().rects(); + for ( uint i = 0; i < rects.count(); i++ ) { + TQRect r = rects[(int)i]; + bitBlt( this, r.x(), r.y(), &buffer, r.x(), r.y(), r.width(), r.height() ); + } +} + +//------------------------------------------------------ + +Scribble::Scribble( TQWidget *parent, const char *name ) + : TQMainWindow( parent, name ) +{ + canvas = new Canvas( this ); + setCentralWidget( canvas ); + + TQToolBar *tools = new TQToolBar( this ); + + bSave = new TQToolButton( TQPixmap(), "Save", "Save as PNG image", this, SLOT( slotSave() ), tools ); + bSave->setText( "Save as..." ); + + tools->addSeparator(); + + bPColor = new TQToolButton( TQPixmap(), "Choose Pen Color", "Choose Pen Color", this, SLOT( slotColor() ), tools ); + bPColor->setText( "Choose Pen Color..." ); + + tools->addSeparator(); + + bPWidth = new TQSpinBox( 1, 20, 1, tools ); + TQToolTip::add( bPWidth, "Choose Pen Width" ); + connect( bPWidth, SIGNAL( valueChanged( int ) ), this, SLOT( slotWidth( int ) ) ); + bPWidth->setValue( 3 ); + + tools->addSeparator(); + + bClear = new TQToolButton( TQPixmap(), "Clear Screen", "Clear Screen", this, SLOT( slotClear() ), tools ); + bClear->setText( "Clear Screen" ); +} + +void Scribble::slotSave() +{ + TQPopupMenu *menu = new TQPopupMenu( 0 ); + TQIntDict<TQString> formats; + formats.setAutoDelete( TRUE ); + + for ( unsigned int i = 0; i < TQImageIO::outputFormats().count(); i++ ) { + TQString str = TQString( TQImageIO::outputFormats().at( i ) ); + formats.insert( menu->insertItem( TQString( "%1..." ).arg( str ) ), new TQString( str ) ); + } + + menu->setMouseTracking( TRUE ); + int id = menu->exec( bSave->mapToGlobal( TQPoint( 0, bSave->height() + 1 ) ) ); + + if ( id != -1 ) { + TQString format = *formats[ id ]; + + TQString filename = TQFileDialog::getSaveFileName( TQString::null, TQString( "*.%1" ).arg( format.lower() ), this ); + if ( !filename.isEmpty() ) + canvas->save( filename, format ); + } + + delete menu; +} + +void Scribble::slotColor() +{ + TQColor c = TQColorDialog::getColor( canvas->penColor(), this ); + if ( c.isValid() ) + canvas->setPenColor( c ); +} + +void Scribble::slotWidth( int w ) +{ + canvas->setPenWidth( w ); +} + +void Scribble::slotClear() +{ + canvas->clearScreen(); +} diff --git a/examples/scribble/scribble.doc b/examples/scribble/scribble.doc new file mode 100644 index 000000000..6763df267 --- /dev/null +++ b/examples/scribble/scribble.doc @@ -0,0 +1,30 @@ + +/* +*/ +/*! \page scribble-example.html + + \ingroup examples + \title Simple Painting Application + + This example implements the famous scribble example. You can draw around + in the canvas with different pens and save the result as picture. + + <hr> + + Header file: + + \include scribble/scribble.h + + <hr> + + Implementation: + + \include scribble/scribble.cpp + + <hr> + + Main: + + \include scribble/main.cpp +*/ + diff --git a/examples/scribble/scribble.h b/examples/scribble/scribble.h new file mode 100644 index 000000000..6e23cbf82 --- /dev/null +++ b/examples/scribble/scribble.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** 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. +** +*****************************************************************************/ + +#ifndef SCRIBBLE_H +#define SCRIBBLE_H + +#include <qmainwindow.h> +#include <qpen.h> +#include <qpoint.h> +#include <qpixmap.h> +#include <qwidget.h> +#include <qstring.h> +#include <qpointarray.h> + +class TQMouseEvent; +class TQResizeEvent; +class TQPaintEvent; +class TQToolButton; +class TQSpinBox; + +class Canvas : public TQWidget +{ + Q_OBJECT + +public: + Canvas( TQWidget *parent = 0, const char *name = 0 ); + + void setPenColor( const TQColor &c ) + { pen.setColor( c ); } + + void setPenWidth( int w ) + { pen.setWidth( w ); } + + TQColor penColor() + { return pen.color(); } + + int penWidth() + { return pen.width(); } + + void save( const TQString &filename, const TQString &format ); + + void clearScreen(); + +protected: + void mousePressEvent( TQMouseEvent *e ); + void mouseReleaseEvent( TQMouseEvent *e ); + void mouseMoveEvent( TQMouseEvent *e ); + void resizeEvent( TQResizeEvent *e ); + void paintEvent( TQPaintEvent *e ); + + TQPen pen; + TQPointArray polyline; + + bool mousePressed; + + TQPixmap buffer; + +}; + +class Scribble : public TQMainWindow +{ + Q_OBJECT + +public: + Scribble( TQWidget *parent = 0, const char *name = 0 ); + +protected: + Canvas* canvas; + + TQSpinBox *bPWidth; + TQToolButton *bPColor, *bSave, *bClear; + +protected slots: + void slotSave(); + void slotColor(); + void slotWidth( int ); + void slotClear(); + +}; + +#endif diff --git a/examples/scribble/scribble.pro b/examples/scribble/scribble.pro new file mode 100644 index 000000000..35304c697 --- /dev/null +++ b/examples/scribble/scribble.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = scribble + +CONFIG += qt warn_on release +DEPENDPATH = ../../include + +REQUIRES = full-config + +HEADERS = scribble.h +SOURCES = main.cpp \ + scribble.cpp |