summaryrefslogtreecommitdiffstats
path: root/examples/scribble/scribble.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/scribble/scribble.cpp')
-rw-r--r--examples/scribble/scribble.cpp187
1 files changed, 187 insertions, 0 deletions
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();
+}