source: trunk/doc/html/scribble-example.html@ 190

Last change on this file since 190 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 16.6 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/scribble/scribble.doc:5 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Simple Painting Application</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<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>
33
34
35<p>
36This example implements the famous scribble example. You can draw around
37in the canvas with different pens and save the result as picture.
38<p> <hr>
39<p> Header file:
40<p> <pre>/****************************************************************************
41** $Id: scribble-example.html 2051 2007-02-21 10:04:20Z chehrlic $
42**
43** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
44**
45** This file is part of an example program for Qt. This example
46** program may be used, distributed and modified without limitation.
47**
48*****************************************************************************/
49
50#ifndef SCRIBBLE_H
51#define SCRIBBLE_H
52
53#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;
54#include &lt;<a href="qpen-h.html">qpen.h</a>&gt;
55#include &lt;<a href="qpoint-h.html">qpoint.h</a>&gt;
56#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
57#include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;
58#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
59#include &lt;<a href="qpointarray-h.html">qpointarray.h</a>&gt;
60
61class QMouseEvent;
62class QResizeEvent;
63class QPaintEvent;
64class QToolButton;
65class QSpinBox;
66
67class Canvas : public <a href="qwidget.html">QWidget</a>
68{
69 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
70
71public:
72 Canvas( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );
73
74 void setPenColor( const <a href="qcolor.html">QColor</a> &amp;c )
75<a name="x903"></a> { pen.<a href="qpen.html#setColor">setColor</a>( c ); }
76
77 void setPenWidth( int w )
78<a name="x904"></a> { pen.<a href="qpen.html#setWidth">setWidth</a>( w ); }
79
80 <a href="qcolor.html">QColor</a> penColor()
81<a name="x902"></a> { return pen.<a href="qpen.html#color">color</a>(); }
82
83 int penWidth()
84<a name="x905"></a> { return pen.<a href="qpen.html#width">width</a>(); }
85
86 void save( const <a href="qstring.html">QString</a> &amp;filename, const <a href="qstring.html">QString</a> &amp;format );
87
88 void clearScreen();
89
90protected:
91 void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> *e );
92 void mouseReleaseEvent( <a href="qmouseevent.html">QMouseEvent</a> *e );
93 void mouseMoveEvent( <a href="qmouseevent.html">QMouseEvent</a> *e );
94 void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> *e );
95 void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> *e );
96
97 <a href="qpen.html">QPen</a> pen;
98 <a href="qpointarray.html">QPointArray</a> polyline;
99
100 bool mousePressed;
101
102 <a href="qpixmap.html">QPixmap</a> buffer;
103
104};
105
106class Scribble : public <a href="qmainwindow.html">QMainWindow</a>
107{
108 Q_OBJECT
109
110public:
111 Scribble( <a href="qwidget.html">QWidget</a> *parent = 0, const char *name = 0 );
112
113protected:
114 Canvas* canvas;
115
116 <a href="qspinbox.html">QSpinBox</a> *bPWidth;
117 <a href="qtoolbutton.html">QToolButton</a> *bPColor, *bSave, *bClear;
118
119protected slots:
120 void slotSave();
121 void slotColor();
122 void slotWidth( int );
123 void slotClear();
124
125};
126
127#endif
128</pre>
129
130<p> <hr>
131<p> Implementation:
132<p> <pre>/****************************************************************************
133** $Id: scribble-example.html 2051 2007-02-21 10:04:20Z chehrlic $
134**
135** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
136**
137** This file is part of an example program for Qt. This example
138** program may be used, distributed and modified without limitation.
139**
140*****************************************************************************/
141
142#include "scribble.h"
143
144#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
145#include &lt;<a href="qevent-h.html">qevent.h</a>&gt;
146#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
147#include &lt;<a href="qtoolbar-h.html">qtoolbar.h</a>&gt;
148#include &lt;<a href="qtoolbutton-h.html">qtoolbutton.h</a>&gt;
149#include &lt;<a href="qspinbox-h.html">qspinbox.h</a>&gt;
150#include &lt;<a href="qtooltip-h.html">qtooltip.h</a>&gt;
151#include &lt;<a href="qrect-h.html">qrect.h</a>&gt;
152#include &lt;<a href="qpoint-h.html">qpoint.h</a>&gt;
153#include &lt;<a href="qcolordialog-h.html">qcolordialog.h</a>&gt;
154#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
155#include &lt;<a href="qcursor-h.html">qcursor.h</a>&gt;
156#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
157#include &lt;<a href="qstrlist-h.html">qstrlist.h</a>&gt;
158#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
159#include &lt;<a href="qintdict-h.html">qintdict.h</a>&gt;
160
161const bool no_writing = FALSE;
162
163<a name="f338"></a>Canvas::Canvas( <a href="qwidget.html">QWidget</a> *parent, const char *name )
164 : <a href="qwidget.html">QWidget</a>( parent, name, WStaticContents ), pen( Qt::red, 3 ), polyline(3),
165 mousePressed( FALSE ), buffer( <a href="qwidget.html#width">width</a>(), height() )
166{
167
168<a name="x907"></a><a name="x906"></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]))
169 buffer.fill( <a href="qwidget.html#colorGroup">colorGroup</a>().base() );
170 <a href="qwidget.html#setBackgroundMode">setBackgroundMode</a>( QWidget::PaletteBase );
171#ifndef QT_NO_CURSOR
172 <a href="qwidget.html#setCursor">setCursor</a>( Qt::crossCursor );
173#endif
174}
175
176void <a name="f339"></a>Canvas::save( const <a href="qstring.html">QString</a> &amp;filename, const <a href="qstring.html">QString</a> &amp;format )
177{
178 if ( !no_writing )
179<a name="x942"></a> buffer.save( filename, format.<a href="qstring.html#upper">upper</a>() );
180}
181
182void <a name="f340"></a>Canvas::clearScreen()
183{
184 buffer.fill( <a href="qwidget.html#colorGroup">colorGroup</a>().base() );
185 <a href="qwidget.html#repaint">repaint</a>( FALSE );
186}
187
188<a name="x949"></a>void Canvas::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
189{
190 mousePressed = TRUE;
191 polyline[2] = polyline[1] = polyline[0] = e-&gt;<a href="qmouseevent.html#pos">pos</a>();
192}
193
194<a name="x950"></a>void Canvas::<a href="qwidget.html#mouseReleaseEvent">mouseReleaseEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * )
195{
196 mousePressed = FALSE;
197}
198
199<a name="x948"></a>void Canvas::<a href="qwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> *e )
200{
201 if ( mousePressed ) {
202 <a href="qpainter.html">QPainter</a> painter;
203 painter.<a href="qpainter.html#begin">begin</a>( &amp;buffer );
204 painter.<a href="qpainter.html#setPen">setPen</a>( pen );
205 polyline[2] = polyline[1];
206 polyline[1] = polyline[0];
207 polyline[0] = e-&gt;<a href="qmouseevent.html#pos">pos</a>();
208<a name="x917"></a> painter.<a href="qpainter.html#drawPolyline">drawPolyline</a>( polyline );
209<a name="x918"></a> painter.<a href="qpainter.html#end">end</a>();
210
211 <a href="qrect.html">QRect</a> r = polyline.boundingRect();
212<a name="x928"></a> r = r.<a href="qrect.html#normalize">normalize</a>();
213<a name="x931"></a><a name="x927"></a> r.<a href="qrect.html#setLeft">setLeft</a>( r.<a href="qrect.html#left">left</a>() - penWidth() );
214<a name="x934"></a><a name="x933"></a> r.<a href="qrect.html#setTop">setTop</a>( r.<a href="qrect.html#top">top</a>() - penWidth() );
215<a name="x932"></a><a name="x929"></a> r.<a href="qrect.html#setRight">setRight</a>( r.<a href="qrect.html#right">right</a>() + penWidth() );
216<a name="x930"></a><a name="x925"></a> r.<a href="qrect.html#setBottom">setBottom</a>( r.<a href="qrect.html#bottom">bottom</a>() + penWidth() );
217
218<a name="x937"></a><a name="x936"></a><a name="x926"></a> <a href="qimage.html#bitBlt">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>() );
219 }
220}
221
222<a name="x952"></a>void Canvas::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> *e )
223{
224 QWidget::<a href="qwidget.html#resizeEvent">resizeEvent</a>( e );
225
226 int w = <a href="qwidget.html#width">width</a>() &gt; buffer.width() ?
227 <a href="qwidget.html#width">width</a>() : buffer.width();
228 int h = <a href="qwidget.html#height">height</a>() &gt; buffer.height() ?
229 <a href="qwidget.html#height">height</a>() : buffer.height();
230
231 <a href="qpixmap.html">QPixmap</a> tmp( buffer );
232 buffer.resize( w, h );
233 buffer.fill( <a href="qwidget.html#colorGroup">colorGroup</a>().base() );
234<a name="x922"></a><a name="x921"></a> <a href="qimage.html#bitBlt">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>() );
235}
236
237<a name="x951"></a>void Canvas::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> *e )
238{
239 QWidget::<a href="qwidget.html#paintEvent">paintEvent</a>( e );
240
241<a name="x920"></a> <a href="qmemarray.html">QMemArray</a>&lt;QRect&gt; rects = e-&gt;<a href="qpaintevent.html#region">region</a>().rects();
242<a name="x913"></a> for ( uint i = 0; i &lt; rects.<a href="qmemarray.html#count">count</a>(); i++ ) {
243 <a href="qrect.html">QRect</a> r = rects[(int)i];
244 <a href="qimage.html#bitBlt">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>() );
245 }
246}
247
248//------------------------------------------------------
249
250<a name="f341"></a>Scribble::Scribble( <a href="qwidget.html">QWidget</a> *parent, const char *name )
251 : <a href="qmainwindow.html">QMainWindow</a>( parent, name )
252{
253 canvas = new Canvas( this );
254 <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( canvas );
255
256 <a href="qtoolbar.html">QToolBar</a> *tools = new <a href="qtoolbar.html">QToolBar</a>( this );
257
258 bSave = new <a href="qtoolbutton.html">QToolButton</a>( QPixmap(), "Save", "Save as PNG image", this, SLOT( slotSave() ), tools );
259<a name="x944"></a> bSave-&gt;<a href="qbutton.html#setText">setText</a>( "Save as..." );
260
261<a name="x943"></a> tools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
262
263 bPColor = new <a href="qtoolbutton.html">QToolButton</a>( QPixmap(), "Choose Pen Color", "Choose Pen Color", this, SLOT( slotColor() ), tools );
264 bPColor-&gt;<a href="qbutton.html#setText">setText</a>( "Choose Pen Color..." );
265
266 tools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
267
268 bPWidth = new <a href="qspinbox.html">QSpinBox</a>( 1, 20, 1, tools );
269<a name="x945"></a> QToolTip::<a href="qtooltip.html#add">add</a>( bPWidth, "Choose Pen Width" );
270<a name="x939"></a> <a href="qobject.html#connect">connect</a>( bPWidth, SIGNAL( <a href="qspinbox.html#valueChanged">valueChanged</a>( int ) ), this, SLOT( slotWidth( int ) ) );
271<a name="x938"></a> bPWidth-&gt;<a href="qspinbox.html#setValue">setValue</a>( 3 );
272
273 tools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
274
275 bClear = new <a href="qtoolbutton.html">QToolButton</a>( QPixmap(), "Clear Screen", "Clear Screen", this, SLOT( slotClear() ), tools );
276 bClear-&gt;<a href="qbutton.html#setText">setText</a>( "Clear Screen" );
277}
278
279void <a name="f342"></a>Scribble::slotSave()
280{
281 <a href="qpopupmenu.html">QPopupMenu</a> *menu = new <a href="qpopupmenu.html">QPopupMenu</a>( 0 );
282 <a href="qintdict.html">QIntDict</a>&lt;QString&gt; formats;
283<a name="x924"></a> formats.<a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>( TRUE );
284
285 for ( unsigned int i = 0; i &lt; QImageIO::<a href="qimageio.html#outputFormats">outputFormats</a>().count(); i++ ) {
286<a name="x911"></a> <a href="qstring.html">QString</a> str = QString( QImageIO::<a href="qimageio.html#outputFormats">outputFormats</a>().at( i ) );
287<a name="x912"></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 ) );
288 }
289
290<a name="x953"></a> menu-&gt;<a href="qwidget.html#setMouseTracking">setMouseTracking</a>( TRUE );
291<a name="x947"></a><a name="x946"></a><a name="x923"></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 ) ) );
292
293 if ( id != -1 ) {
294 <a href="qstring.html">QString</a> format = *formats[ id ];
295
296<a name="x941"></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 );
297 if ( !filename.<a href="qstring.html#isEmpty">isEmpty</a>() )
298 canvas-&gt;save( filename, format );
299 }
300
301 delete menu;
302}
303
304void <a name="f343"></a>Scribble::slotColor()
305{
306<a name="x909"></a> <a href="qcolor.html">QColor</a> c = QColorDialog::<a href="qcolordialog.html#getColor">getColor</a>( canvas-&gt;penColor(), this );
307<a name="x908"></a> if ( c.<a href="qcolor.html#isValid">isValid</a>() )
308 canvas-&gt;setPenColor( c );
309}
310
311void <a name="f344"></a>Scribble::slotWidth( int w )
312{
313 canvas-&gt;setPenWidth( w );
314}
315
316void <a name="f345"></a>Scribble::slotClear()
317{
318 canvas-&gt;clearScreen();
319}
320</pre>
321
322<p> <hr>
323<p> Main:
324<p> <pre>/****************************************************************************
325** $Id: scribble-example.html 2051 2007-02-21 10:04:20Z chehrlic $
326**
327** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
328**
329** This file is part of an example program for Qt. This example
330** program may be used, distributed and modified without limitation.
331**
332*****************************************************************************/
333
334#include "scribble.h"
335#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
336
337
338int main( int argc, char **argv )
339{
340 <a href="qapplication.html">QApplication</a> a( argc, argv );
341
342 Scribble scribble;
343
344 scribble.<a href="qwidget.html#resize">resize</a>( 500, 350 );
345 scribble.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Scribble");
346 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;scribble );
347<a name="x954"></a> if ( QApplication::<a href="qapplication.html#desktop">desktop</a>()-&gt;width() &gt; 550
348 &amp;&amp; QApplication::<a href="qapplication.html#desktop">desktop</a>()-&gt;height() &gt; 366 )
349 scribble.<a href="qwidget.html#show">show</a>();
350 else
351<a name="x960"></a> scribble.<a href="qwidget.html#showMaximized">showMaximized</a>();
352 return a.<a href="qapplication.html#exec">exec</a>();
353}
354</pre>
355
356<p>See also <a href="examples.html">Examples</a>.
357
358<!-- eof -->
359<p><address><hr><div align=center>
360<table width=100% cellspacing=0 border=0><tr>
361<td>Copyright &copy; 2007
362<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
363<td align=right><div align=right>Qt 3.3.8</div>
364</table></div></address></body>
365</html>
Note: See TracBrowser for help on using the repository browser.