Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > 397cf13019f8c526877f271962f26f4c > files > 2185

libqt3-devel-3.1.1-13mdk.ppc.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/reggie/tmp/qt-3.1-reggie-23625/qt-x11-free-3.1.1/examples/scribble/scribble.doc:5 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple Painting Application</title>
<style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Simple Painting Application</h1>

   
<p> 
This example implements the famous scribble example. You can draw around
in the canvas with different pens and save the result as picture.
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id: qt/scribble.h   3.1.1   edited Nov 8 10:35 $
**
** Copyright ( C ) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef SCRIBBLE_H
#define SCRIBBLE_H

#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;
#include &lt;<a href="qpen-h.html">qpen.h</a>&gt;
#include &lt;<a href="qpoint-h.html">qpoint.h</a>&gt;
#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
#include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;
#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
#include &lt;<a href="qpointarray-h.html">qpointarray.h</a>&gt;

class QMouseEvent;
class QResizeEvent;
class QPaintEvent;
class QToolButton;
class QSpinBox;

class Canvas : public <a href="qwidget.html">QWidget</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>

public:
    Canvas( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );

    void setPenColor( const <a href="qcolor.html">QColor</a> &amp;c )
<a name="x919"></a>    { pen.<a href="qpen.html#setColor">setColor</a>( c ); }

    void setPenWidth( int w )
<a name="x920"></a>    { pen.<a href="qpen.html#setWidth">setWidth</a>( w ); }

    <a href="qcolor.html">QColor</a> penColor()
<a name="x918"></a>    { return pen.<a href="qpen.html#color">color</a>(); }

    int penWidth()
<a name="x921"></a>    { return pen.<a href="qpen.html#width">width</a>(); }

    void save( const <a href="qstring.html">QString</a> &amp;filename, const <a href="qstring.html">QString</a> &amp;format );

    void clearScreen();

protected:
    void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *e );
    void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> *e );
    void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *e );
    void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> *e );
    void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> *e );

    <a href="qpen.html">QPen</a> pen;
    <a href="qpointarray.html">QPointArray</a> polyline;

    bool mousePressed;

    <a href="qpixmap.html">QPixmap</a> buffer;

};

class Scribble : public <a href="qmainwindow.html">QMainWindow</a>
{
    Q_OBJECT

public:
    Scribble( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );

protected:
    Canvas* canvas;

    <a href="qspinbox.html">QSpinBox</a> *bPWidth;
    <a href="qtoolbutton.html">QToolButton</a> *bPColor, *bSave, *bClear;

protected slots:
    void slotSave();
    void slotColor();
    void slotWidth( int );
    void slotClear();

};

#endif
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/scribble.cpp   3.1.1   edited Nov 8 10:35 $
**
** Copyright ( C ) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "scribble.h"

#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qevent-h.html">qevent.h</a>&gt;
#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
#include &lt;<a href="qtoolbar-h.html">qtoolbar.h</a>&gt;
#include &lt;<a href="qtoolbutton-h.html">qtoolbutton.h</a>&gt;
#include &lt;<a href="qspinbox-h.html">qspinbox.h</a>&gt;
#include &lt;<a href="qtooltip-h.html">qtooltip.h</a>&gt;
#include &lt;<a href="qrect-h.html">qrect.h</a>&gt;
#include &lt;<a href="qpoint-h.html">qpoint.h</a>&gt;
#include &lt;<a href="qcolordialog-h.html">qcolordialog.h</a>&gt;
#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
#include &lt;<a href="qcursor-h.html">qcursor.h</a>&gt;
#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
#include &lt;<a href="qstrlist-h.html">qstrlist.h</a>&gt;
#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
#include &lt;<a href="qintdict-h.html">qintdict.h</a>&gt;

const bool no_writing = FALSE;

<a name="f343"></a>Canvas::Canvas( <a href="qwidget.html">QWidget</a> *parent, const char *name )
    : <a href="qwidget.html">QWidget</a>( parent, name, WStaticContents ), pen( Qt::red, 3 ), polyline(3),
      mousePressed( FALSE ), buffer( <a href="qwidget.html#width">width</a>(), height() )
{

<a name="x923"></a><a name="x922"></a>    if ((qApp-&gt;<a href="qapplication.html#argc">argc</a>() &gt; 0) &amp;&amp; !buffer.load(qApp-&gt;<a href="qapplication.html#argv">argv</a>()[1]))
        buffer.fill( <a href="qwidget.html#colorGroup">colorGroup</a>().base() );
    <a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>( QWidget::PaletteBase );
#ifndef QT_NO_CURSOR
    <a href="qwidget.html#setCursor">setCursor</a>( Qt::crossCursor );
#endif
}

void <a name="f344"></a>Canvas::save( const <a href="qstring.html">QString</a> &amp;filename, const <a href="qstring.html">QString</a> &amp;format )
{
    if ( !no_writing )
<a name="x959"></a>        buffer.save( filename, format.<a href="qstring.html#upper">upper</a>() );
}

void <a name="f345"></a>Canvas::clearScreen()
{
    buffer.fill( <a href="qwidget.html#colorGroup">colorGroup</a>().base() );
    <a href="qwidget.html#repaint">repaint</a>( FALSE );
}

<a name="x965"></a>void Canvas::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
{
    mousePressed = TRUE;
    polyline[2] = polyline[1] = polyline[0] = e-&gt;<a href="qmouseevent.html#pos">pos</a>();
}

<a name="x966"></a>void Canvas::<a href="qwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * )
{
    mousePressed = FALSE;
}

<a name="x964"></a>void Canvas::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
{
    if ( mousePressed ) {
        <a href="qpainter.html">QPainter</a> painter;
        painter.<a href="qpainter.html#begin">begin</a>( &amp;buffer );
        painter.<a href="qpainter.html#setPen">setPen</a>( pen );
        polyline[2] = polyline[1];
        polyline[1] = polyline[0];
        polyline[0] = e-&gt;<a href="qmouseevent.html#pos">pos</a>();
<a name="x934"></a>        painter.<a href="qpainter.html#drawPolyline">drawPolyline</a>( polyline );
        painter.<a href="qpainter.html#end">end</a>();

        <a href="qrect.html">QRect</a> r = polyline.boundingRect();
<a name="x945"></a>        r = r.<a href="qrect.html#normalize">normalize</a>();
<a name="x948"></a><a name="x944"></a>        r.<a href="qrect.html#setLeft">setLeft</a>( r.<a href="qrect.html#left">left</a>() - penWidth() );
<a name="x951"></a><a name="x950"></a>        r.<a href="qrect.html#setTop">setTop</a>( r.<a href="qrect.html#top">top</a>() - penWidth() );
<a name="x949"></a><a name="x946"></a>        r.<a href="qrect.html#setRight">setRight</a>( r.<a href="qrect.html#right">right</a>() + penWidth() );
<a name="x947"></a><a name="x942"></a>        r.<a href="qrect.html#setBottom">setBottom</a>( r.<a href="qrect.html#bottom">bottom</a>() + penWidth() );

<a name="x954"></a><a name="x953"></a><a name="x943"></a>        <a href="qpaintdevice.html#bitBlt-2">bitBlt</a>( this, r.<a href="qrect.html#x">x</a>(), r.<a href="qrect.html#y">y</a>(), &amp;buffer, r.<a href="qrect.html#x">x</a>(), r.<a href="qrect.html#y">y</a>(), r.<a href="qrect.html#width">width</a>(), r.<a href="qrect.html#height">height</a>() );
    }
}

<a name="x968"></a>void Canvas::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> *e )
{
    QWidget::<a href="qwidget.html#resizeEvent">resizeEvent</a>( e );

    int w = <a href="qwidget.html#width">width</a>() &gt; buffer.width() ?
            <a href="qwidget.html#width">width</a>() : buffer.width();
    int h = <a href="qwidget.html#height">height</a>() &gt; buffer.height() ?
            <a href="qwidget.html#height">height</a>() : buffer.height();

    <a href="qpixmap.html">QPixmap</a> tmp( buffer );
    buffer.resize( w, h );
    buffer.fill( <a href="qwidget.html#colorGroup">colorGroup</a>().base() );
<a name="x939"></a><a name="x938"></a>    <a href="qpaintdevice.html#bitBlt-2">bitBlt</a>( &amp;buffer, 0, 0, &amp;tmp, 0, 0, tmp.<a href="qpixmap.html#width">width</a>(), tmp.<a href="qpixmap.html#height">height</a>() );
}

<a name="x967"></a>void Canvas::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e )
{
    QWidget::<a href="qwidget.html#paintEvent">paintEvent</a>( e );

<a name="x937"></a>    <a href="qmemarray.html">QMemArray</a>&lt;QRect&gt; rects = e-&gt;<a href="qpaintevent.html#region">region</a>().rects();
<a name="x930"></a>    for ( uint i = 0; i &lt; rects.<a href="qmemarray.html#count">count</a>(); i++ ) {
        <a href="qrect.html">QRect</a> r = rects[(int)i];
        <a href="qpaintdevice.html#bitBlt-2">bitBlt</a>( this, r.<a href="qrect.html#x">x</a>(), r.<a href="qrect.html#y">y</a>(), &amp;buffer, r.<a href="qrect.html#x">x</a>(), r.<a href="qrect.html#y">y</a>(), r.<a href="qrect.html#width">width</a>(), r.<a href="qrect.html#height">height</a>() );
    }
}

//------------------------------------------------------

<a name="f346"></a>Scribble::Scribble( <a href="qwidget.html">QWidget</a> *parent, const char *name )
    : <a href="qmainwindow.html">QMainWindow</a>( parent, name )
{
    canvas = new Canvas( this );
    <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( canvas );

    <a href="qtoolbar.html">QToolBar</a> *tools = new <a href="qtoolbar.html">QToolBar</a>( this );

    bSave = new <a href="qtoolbutton.html">QToolButton</a>( QPixmap(), "Save", "Save as PNG image", this, SLOT( slotSave() ), tools );
<a name="x924"></a>    bSave-&gt;<a href="qbutton.html#setText">setText</a>( "Save as..." );

<a name="x960"></a>    tools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();

    bPColor = new <a href="qtoolbutton.html">QToolButton</a>( QPixmap(), "Choose Pen Color", "Choose Pen Color", this, SLOT( slotColor() ), tools );
    bPColor-&gt;<a href="qbutton.html#setText">setText</a>( "Choose Pen Color..." );

    tools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();

    bPWidth = new <a href="qspinbox.html">QSpinBox</a>( 1, 20, 1, tools );
<a name="x961"></a>    QToolTip::<a href="qtooltip.html#add">add</a>( bPWidth, "Choose Pen Width" );
<a name="x956"></a>    <a href="qobject.html#connect">connect</a>( bPWidth, SIGNAL( <a href="qspinbox.html#valueChanged">valueChanged</a>( int ) ), this, SLOT( slotWidth( int ) ) );
<a name="x955"></a>    bPWidth-&gt;<a href="qspinbox.html#setValue">setValue</a>( 3 );

    tools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();

    bClear = new <a href="qtoolbutton.html">QToolButton</a>( QPixmap(), "Clear Screen", "Clear Screen", this, SLOT( slotClear() ), tools );
    bClear-&gt;<a href="qbutton.html#setText">setText</a>( "Clear Screen" );
}

void <a name="f347"></a>Scribble::slotSave()
{
    <a href="qpopupmenu.html">QPopupMenu</a> *menu = new <a href="qpopupmenu.html">QPopupMenu</a>( 0 );
    <a href="qintdict.html">QIntDict</a>&lt;QString&gt; formats;
<a name="x941"></a>    formats.<a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>( TRUE );

    for ( unsigned int i = 0; i &lt; QImageIO::<a href="qimageio.html#outputFormats">outputFormats</a>().count(); i++ ) {
<a name="x928"></a>        <a href="qstring.html">QString</a> str = QString( QImageIO::<a href="qimageio.html#outputFormats">outputFormats</a>().at( i ) );
<a name="x929"></a>        formats.<a href="qintdict.html#insert">insert</a>( menu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( QString( "%1..." ).arg( str ) ), new <a href="qstring.html">QString</a>( str ) );
    }

<a name="x969"></a>    menu-&gt;<a href="qwidget.html#setMouseTracking">setMouseTracking</a>( TRUE );
<a name="x963"></a><a name="x962"></a><a name="x940"></a>    int id = menu-&gt;<a href="qpopupmenu.html#exec">exec</a>( bSave-&gt;<a href="qwidget.html#mapToGlobal">mapToGlobal</a>( QPoint( 0, bSave-&gt;<a href="qwidget.html#height">height</a>() + 1 ) ) );

    if ( id != -1 ) {
        <a href="qstring.html">QString</a> format = *formats[ id ];

<a name="x958"></a>        <a href="qstring.html">QString</a> filename = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>( <a href="qstring.html#QString-null">QString::null</a>, QString( "*.%1" ).arg( format.<a href="qstring.html#lower">lower</a>() ), this );
        if ( !filename.<a href="qstring.html#isEmpty">isEmpty</a>() )
            canvas-&gt;save( filename, format );
    }

    delete menu;
}

void <a name="f348"></a>Scribble::slotColor()
{
<a name="x926"></a>    <a href="qcolor.html">QColor</a> c = QColorDialog::<a href="qcolordialog.html#getColor">getColor</a>( canvas-&gt;penColor(), this );
<a name="x925"></a>    if ( c.<a href="qcolor.html#isValid">isValid</a>() )
        canvas-&gt;setPenColor( c );
}

void <a name="f349"></a>Scribble::slotWidth( int w )
{
    canvas-&gt;setPenWidth( w );
}

void <a name="f350"></a>Scribble::slotClear()
{
    canvas-&gt;clearScreen();
}
</pre>

<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp   3.1.1   edited Nov 8 10:35 $
**
** Copyright ( C ) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "scribble.h"
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;


int main( int argc, char **argv )
{
    <a href="qapplication.html">QApplication</a> a( argc, argv );

    Scribble scribble;

    scribble.<a href="qwidget.html#resize">resize</a>( 500, 350 );
    scribble.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Scribble");
    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;scribble );
<a name="x970"></a>    if ( QApplication::<a href="qapplication.html#desktop">desktop</a>()-&gt;width() &gt; 550
         &amp;&amp; QApplication::<a href="qapplication.html#desktop">desktop</a>()-&gt;height() &gt; 366 )
        scribble.<a href="qwidget.html#show">show</a>();
    else
<a name="x976"></a>        scribble.<a href="qwidget.html#showMaximized">showMaximized</a>();
    return a.<a href="qapplication.html#exec">exec</a>();
}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2002 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.1.1</div>
</table></div></address></body>
</html>