| 1 | /**************************************************************************** | 
|---|
| 2 | ** $Id: scribble.cpp 2 2005-11-16 15:49:26Z dmik $ | 
|---|
| 3 | ** | 
|---|
| 4 | ** Copyright ( C ) 1992-2000 Trolltech AS.  All rights reserved. | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of an example program for Qt.  This example | 
|---|
| 7 | ** program may be used, distributed and modified without limitation. | 
|---|
| 8 | ** | 
|---|
| 9 | *****************************************************************************/ | 
|---|
| 10 |  | 
|---|
| 11 | #include "scribble.h" | 
|---|
| 12 |  | 
|---|
| 13 | #include <qapplication.h> | 
|---|
| 14 | #include <qevent.h> | 
|---|
| 15 | #include <qpainter.h> | 
|---|
| 16 | #include <qtoolbar.h> | 
|---|
| 17 | #include <qtoolbutton.h> | 
|---|
| 18 | #include <qspinbox.h> | 
|---|
| 19 | #include <qtooltip.h> | 
|---|
| 20 | #include <qrect.h> | 
|---|
| 21 | #include <qpoint.h> | 
|---|
| 22 | #include <qcolordialog.h> | 
|---|
| 23 | #include <qfiledialog.h> | 
|---|
| 24 | #include <qcursor.h> | 
|---|
| 25 | #include <qimage.h> | 
|---|
| 26 | #include <qstrlist.h> | 
|---|
| 27 | #include <qpopupmenu.h> | 
|---|
| 28 | #include <qintdict.h> | 
|---|
| 29 |  | 
|---|
| 30 | const bool no_writing = FALSE; | 
|---|
| 31 |  | 
|---|
| 32 | Canvas::Canvas( QWidget *parent, const char *name ) | 
|---|
| 33 | : QWidget( parent, name, WStaticContents ), pen( Qt::red, 3 ), polyline(3), | 
|---|
| 34 | mousePressed( FALSE ), buffer( width(), height() ) | 
|---|
| 35 | { | 
|---|
| 36 |  | 
|---|
| 37 | if ((qApp->argc() > 0) && !buffer.load(qApp->argv()[1])) | 
|---|
| 38 | buffer.fill( colorGroup().base() ); | 
|---|
| 39 | setBackgroundMode( QWidget::PaletteBase ); | 
|---|
| 40 | #ifndef QT_NO_CURSOR | 
|---|
| 41 | setCursor( Qt::crossCursor ); | 
|---|
| 42 | #endif | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | void Canvas::save( const QString &filename, const QString &format ) | 
|---|
| 46 | { | 
|---|
| 47 | if ( !no_writing ) | 
|---|
| 48 | buffer.save( filename, format.upper() ); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | void Canvas::clearScreen() | 
|---|
| 52 | { | 
|---|
| 53 | buffer.fill( colorGroup().base() ); | 
|---|
| 54 | repaint( FALSE ); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | void Canvas::mousePressEvent( QMouseEvent *e ) | 
|---|
| 58 | { | 
|---|
| 59 | mousePressed = TRUE; | 
|---|
| 60 | polyline[2] = polyline[1] = polyline[0] = e->pos(); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | void Canvas::mouseReleaseEvent( QMouseEvent * ) | 
|---|
| 64 | { | 
|---|
| 65 | mousePressed = FALSE; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | void Canvas::mouseMoveEvent( QMouseEvent *e ) | 
|---|
| 69 | { | 
|---|
| 70 | if ( mousePressed ) { | 
|---|
| 71 | QPainter painter; | 
|---|
| 72 | painter.begin( &buffer ); | 
|---|
| 73 | painter.setPen( pen ); | 
|---|
| 74 | polyline[2] = polyline[1]; | 
|---|
| 75 | polyline[1] = polyline[0]; | 
|---|
| 76 | polyline[0] = e->pos(); | 
|---|
| 77 | painter.drawPolyline( polyline ); | 
|---|
| 78 | painter.end(); | 
|---|
| 79 |  | 
|---|
| 80 | QRect r = polyline.boundingRect(); | 
|---|
| 81 | r = r.normalize(); | 
|---|
| 82 | r.setLeft( r.left() - penWidth() ); | 
|---|
| 83 | r.setTop( r.top() - penWidth() ); | 
|---|
| 84 | r.setRight( r.right() + penWidth() ); | 
|---|
| 85 | r.setBottom( r.bottom() + penWidth() ); | 
|---|
| 86 |  | 
|---|
| 87 | bitBlt( this, r.x(), r.y(), &buffer, r.x(), r.y(), r.width(), r.height() ); | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | void Canvas::resizeEvent( QResizeEvent *e ) | 
|---|
| 92 | { | 
|---|
| 93 | QWidget::resizeEvent( e ); | 
|---|
| 94 |  | 
|---|
| 95 | int w = width() > buffer.width() ? | 
|---|
| 96 | width() : buffer.width(); | 
|---|
| 97 | int h = height() > buffer.height() ? | 
|---|
| 98 | height() : buffer.height(); | 
|---|
| 99 |  | 
|---|
| 100 | QPixmap tmp( buffer ); | 
|---|
| 101 | buffer.resize( w, h ); | 
|---|
| 102 | buffer.fill( colorGroup().base() ); | 
|---|
| 103 | bitBlt( &buffer, 0, 0, &tmp, 0, 0, tmp.width(), tmp.height() ); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void Canvas::paintEvent( QPaintEvent *e ) | 
|---|
| 107 | { | 
|---|
| 108 | QWidget::paintEvent( e ); | 
|---|
| 109 |  | 
|---|
| 110 | QMemArray<QRect> rects = e->region().rects(); | 
|---|
| 111 | for ( uint i = 0; i < rects.count(); i++ ) { | 
|---|
| 112 | QRect r = rects[(int)i]; | 
|---|
| 113 | bitBlt( this, r.x(), r.y(), &buffer, r.x(), r.y(), r.width(), r.height() ); | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | //------------------------------------------------------ | 
|---|
| 118 |  | 
|---|
| 119 | Scribble::Scribble( QWidget *parent, const char *name ) | 
|---|
| 120 | : QMainWindow( parent, name ) | 
|---|
| 121 | { | 
|---|
| 122 | canvas = new Canvas( this ); | 
|---|
| 123 | setCentralWidget( canvas ); | 
|---|
| 124 |  | 
|---|
| 125 | QToolBar *tools = new QToolBar( this ); | 
|---|
| 126 |  | 
|---|
| 127 | bSave = new QToolButton( QPixmap(), "Save", "Save as PNG image", this, SLOT( slotSave() ), tools ); | 
|---|
| 128 | bSave->setText( "Save as..." ); | 
|---|
| 129 |  | 
|---|
| 130 | tools->addSeparator(); | 
|---|
| 131 |  | 
|---|
| 132 | bPColor = new QToolButton( QPixmap(), "Choose Pen Color", "Choose Pen Color", this, SLOT( slotColor() ), tools ); | 
|---|
| 133 | bPColor->setText( "Choose Pen Color..." ); | 
|---|
| 134 |  | 
|---|
| 135 | tools->addSeparator(); | 
|---|
| 136 |  | 
|---|
| 137 | bPWidth = new QSpinBox( 1, 20, 1, tools ); | 
|---|
| 138 | QToolTip::add( bPWidth, "Choose Pen Width" ); | 
|---|
| 139 | connect( bPWidth, SIGNAL( valueChanged( int ) ), this, SLOT( slotWidth( int ) ) ); | 
|---|
| 140 | bPWidth->setValue( 3 ); | 
|---|
| 141 |  | 
|---|
| 142 | tools->addSeparator(); | 
|---|
| 143 |  | 
|---|
| 144 | bClear = new QToolButton( QPixmap(), "Clear Screen", "Clear Screen", this, SLOT( slotClear() ), tools ); | 
|---|
| 145 | bClear->setText( "Clear Screen" ); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | void Scribble::slotSave() | 
|---|
| 149 | { | 
|---|
| 150 | QPopupMenu *menu = new QPopupMenu( 0 ); | 
|---|
| 151 | QIntDict<QString> formats; | 
|---|
| 152 | formats.setAutoDelete( TRUE ); | 
|---|
| 153 |  | 
|---|
| 154 | for ( unsigned int i = 0; i < QImageIO::outputFormats().count(); i++ ) { | 
|---|
| 155 | QString str = QString( QImageIO::outputFormats().at( i ) ); | 
|---|
| 156 | formats.insert( menu->insertItem( QString( "%1..." ).arg( str ) ), new QString( str ) ); | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | menu->setMouseTracking( TRUE ); | 
|---|
| 160 | int id = menu->exec( bSave->mapToGlobal( QPoint( 0, bSave->height() + 1 ) ) ); | 
|---|
| 161 |  | 
|---|
| 162 | if ( id != -1 ) { | 
|---|
| 163 | QString format = *formats[ id ]; | 
|---|
| 164 |  | 
|---|
| 165 | QString filename = QFileDialog::getSaveFileName( QString::null, QString( "*.%1" ).arg( format.lower() ), this ); | 
|---|
| 166 | if ( !filename.isEmpty() ) | 
|---|
| 167 | canvas->save( filename, format ); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | delete menu; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | void Scribble::slotColor() | 
|---|
| 174 | { | 
|---|
| 175 | QColor c = QColorDialog::getColor( canvas->penColor(), this ); | 
|---|
| 176 | if ( c.isValid() ) | 
|---|
| 177 | canvas->setPenColor( c ); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | void Scribble::slotWidth( int w ) | 
|---|
| 181 | { | 
|---|
| 182 | canvas->setPenWidth( w ); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | void Scribble::slotClear() | 
|---|
| 186 | { | 
|---|
| 187 | canvas->clearScreen(); | 
|---|
| 188 | } | 
|---|