summaryrefslogtreecommitdiffstats
path: root/doc/html/scribble-example.html
blob: 6d0ee2343296755e76d64db4598eb6cf565933e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/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"><!--
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.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 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 &lt;<a href="tqmainwindow-h.html">tqmainwindow.h</a>&gt;
#include &lt;<a href="qpen-h.html">ntqpen.h</a>&gt;
#include &lt;<a href="qpoint-h.html">ntqpoint.h</a>&gt;
#include &lt;<a href="qpixmap-h.html">ntqpixmap.h</a>&gt;
#include &lt;<a href="tqwidget-h.html">tqwidget.h</a>&gt;
#include &lt;<a href="tqstring-h.html">tqstring.h</a>&gt;
#include &lt;<a href="qpointarray-h.html">ntqpointarray.h</a>&gt;

class TQMouseEvent;
class TQResizeEvent;
class TQPaintEvent;
class TQToolButton;
class TQSpinBox;

class Canvas : public <a href="tqwidget.html">TQWidget</a>
{
    <a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>

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

    void setPenColor( const <a href="tqcolor.html">TQColor</a> &amp;c )
<a name="x903"></a>    { pen.<a href="ntqpen.html#setColor">setColor</a>( c ); }

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

    <a href="tqcolor.html">TQColor</a> penColor()
<a name="x902"></a>    { return pen.<a href="ntqpen.html#color">color</a>(); }

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

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

    void clearScreen();

protected:
    void mousePressEvent( <a href="qmouseevent.html">TQMouseEvent</a> *e );
    void mouseReleaseEvent( <a href="qmouseevent.html">TQMouseEvent</a> *e );
    void mouseMoveEvent( <a href="qmouseevent.html">TQMouseEvent</a> *e );
    void resizeEvent( <a href="qresizeevent.html">TQResizeEvent</a> *e );
    void paintEvent( <a href="tqpaintevent.html">TQPaintEvent</a> *e );

    <a href="ntqpen.html">TQPen</a> pen;
    <a href="ntqpointarray.html">TQPointArray</a> polyline;

    bool mousePressed;

    <a href="ntqpixmap.html">TQPixmap</a> buffer;

};

class Scribble : public <a href="tqmainwindow.html">TQMainWindow</a>
{
    TQ_OBJECT

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

protected:
    Canvas* canvas;

    <a href="ntqspinbox.html">TQSpinBox</a> *bPWidth;
    <a href="tqtoolbutton.html">TQToolButton</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.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 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 &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;
#include &lt;<a href="qevent-h.html">ntqevent.h</a>&gt;
#include &lt;<a href="tqpainter-h.html">tqpainter.h</a>&gt;
#include &lt;<a href="tqtoolbar-h.html">tqtoolbar.h</a>&gt;
#include &lt;<a href="tqtoolbutton-h.html">tqtoolbutton.h</a>&gt;
#include &lt;<a href="qspinbox-h.html">ntqspinbox.h</a>&gt;
#include &lt;<a href="tqtooltip-h.html">tqtooltip.h</a>&gt;
#include &lt;<a href="qrect-h.html">ntqrect.h</a>&gt;
#include &lt;<a href="qpoint-h.html">ntqpoint.h</a>&gt;
#include &lt;<a href="tqcolordialog-h.html">tqcolordialog.h</a>&gt;
#include &lt;<a href="tqfiledialog-h.html">tqfiledialog.h</a>&gt;
#include &lt;<a href="qcursor-h.html">ntqcursor.h</a>&gt;
#include &lt;<a href="tqimage-h.html">tqimage.h</a>&gt;
#include &lt;<a href="tqstrlist-h.html">tqstrlist.h</a>&gt;
#include &lt;<a href="tqpopupmenu-h.html">tqpopupmenu.h</a>&gt;
#include &lt;<a href="tqintdict-h.html">tqintdict.h</a>&gt;

const bool no_writing = FALSE;

<a name="f338"></a>Canvas::Canvas( <a href="tqwidget.html">TQWidget</a> *parent, const char *name )
    : <a href="tqwidget.html">TQWidget</a>( parent, name, WStaticContents ), pen( TQt::red, 3 ), polyline(3),
      mousePressed( FALSE ), buffer( <a href="tqwidget.html#width">width</a>(), height() )
{

<a name="x907"></a><a name="x906"></a>    if ((tqApp-&gt;<a href="ntqapplication.html#argc">argc</a>() &gt; 0) &amp;&amp; !buffer.load(tqApp-&gt;<a href="ntqapplication.html#argv">argv</a>()[1]))
        buffer.fill( <a href="tqwidget.html#colorGroup">colorGroup</a>().base() );
    <a href="tqwidget.html#setBackgroundMode">setBackgroundMode</a>( TQWidget::PaletteBase );
#ifndef TQT_NO_CURSOR
    <a href="tqwidget.html#setCursor">setCursor</a>( TQt::crossCursor );
#endif
}

void <a name="f339"></a>Canvas::save( const <a href="tqstring.html">TQString</a> &amp;filename, const <a href="tqstring.html">TQString</a> &amp;format )
{
    if ( !no_writing )
<a name="x942"></a>        buffer.save( filename, format.<a href="tqstring.html#upper">upper</a>() );
}

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

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

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

<a name="x948"></a>void Canvas::<a href="tqwidget.html#mouseMoveEvent">mouseMoveEvent</a>( <a href="qmouseevent.html">TQMouseEvent</a> *e )
{
    if ( mousePressed ) {
        <a href="tqpainter.html">TQPainter</a> painter;
        painter.<a href="tqpainter.html#begin">begin</a>( &amp;buffer );
        painter.<a href="tqpainter.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="x917"></a>        painter.<a href="tqpainter.html#drawPolyline">drawPolyline</a>( polyline );
<a name="x918"></a>        painter.<a href="tqpainter.html#end">end</a>();

        <a href="ntqrect.html">TQRect</a> r = polyline.boundingRect();
<a name="x928"></a>        r = r.<a href="ntqrect.html#normalize">normalize</a>();
<a name="x931"></a><a name="x927"></a>        r.<a href="ntqrect.html#setLeft">setLeft</a>( r.<a href="ntqrect.html#left">left</a>() - penWidth() );
<a name="x934"></a><a name="x933"></a>        r.<a href="ntqrect.html#setTop">setTop</a>( r.<a href="ntqrect.html#top">top</a>() - penWidth() );
<a name="x932"></a><a name="x929"></a>        r.<a href="ntqrect.html#setRight">setRight</a>( r.<a href="ntqrect.html#right">right</a>() + penWidth() );
<a name="x930"></a><a name="x925"></a>        r.<a href="ntqrect.html#setBottom">setBottom</a>( r.<a href="ntqrect.html#bottom">bottom</a>() + penWidth() );

<a name="x937"></a><a name="x936"></a><a name="x926"></a>        <a href="tqimage.html#bitBlt">bitBlt</a>( this, r.<a href="ntqrect.html#x">x</a>(), r.<a href="ntqrect.html#y">y</a>(), &amp;buffer, r.<a href="ntqrect.html#x">x</a>(), r.<a href="ntqrect.html#y">y</a>(), r.<a href="ntqrect.html#width">width</a>(), r.<a href="ntqrect.html#height">height</a>() );
    }
}

<a name="x952"></a>void Canvas::<a href="tqwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">TQResizeEvent</a> *e )
{
    TQWidget::<a href="tqwidget.html#resizeEvent">resizeEvent</a>( e );

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

    <a href="ntqpixmap.html">TQPixmap</a> tmp( buffer );
    buffer.resize( w, h );
    buffer.fill( <a href="tqwidget.html#colorGroup">colorGroup</a>().base() );
<a name="x922"></a><a name="x921"></a>    <a href="tqimage.html#bitBlt">bitBlt</a>( &amp;buffer, 0, 0, &amp;tmp, 0, 0, tmp.<a href="ntqpixmap.html#width">width</a>(), tmp.<a href="ntqpixmap.html#height">height</a>() );
}

<a name="x951"></a>void Canvas::<a href="tqwidget.html#paintEvent">paintEvent</a>( <a href="tqpaintevent.html">TQPaintEvent</a> *e )
{
    TQWidget::<a href="tqwidget.html#paintEvent">paintEvent</a>( e );

<a name="x920"></a>    <a href="tqmemarray.html">TQMemArray</a>&lt;TQRect&gt; rects = e-&gt;<a href="tqpaintevent.html#region">region</a>().rects();
<a name="x913"></a>    for ( uint i = 0; i &lt; rects.<a href="tqmemarray.html#count">count</a>(); i++ ) {
        <a href="ntqrect.html">TQRect</a> r = rects[(int)i];
        <a href="tqimage.html#bitBlt">bitBlt</a>( this, r.<a href="ntqrect.html#x">x</a>(), r.<a href="ntqrect.html#y">y</a>(), &amp;buffer, r.<a href="ntqrect.html#x">x</a>(), r.<a href="ntqrect.html#y">y</a>(), r.<a href="ntqrect.html#width">width</a>(), r.<a href="ntqrect.html#height">height</a>() );
    }
}

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

<a name="f341"></a>Scribble::Scribble( <a href="tqwidget.html">TQWidget</a> *parent, const char *name )
    : <a href="tqmainwindow.html">TQMainWindow</a>( parent, name )
{
    canvas = new Canvas( this );
    <a href="tqmainwindow.html#setCentralWidget">setCentralWidget</a>( canvas );

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

    bSave = new <a href="tqtoolbutton.html">TQToolButton</a>( TQPixmap(), "Save", "Save as PNG image", this, TQ_SLOT( slotSave() ), tools );
<a name="x944"></a>    bSave-&gt;<a href="ntqbutton.html#setText">setText</a>( "Save as..." );

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

    bPColor = new <a href="tqtoolbutton.html">TQToolButton</a>( TQPixmap(), "Choose Pen Color", "Choose Pen Color", this, TQ_SLOT( slotColor() ), tools );
    bPColor-&gt;<a href="ntqbutton.html#setText">setText</a>( "Choose Pen Color..." );

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

    bPWidth = new <a href="ntqspinbox.html">TQSpinBox</a>( 1, 20, 1, tools );
<a name="x945"></a>    TQToolTip::<a href="tqtooltip.html#add">add</a>( bPWidth, "Choose Pen Width" );
<a name="x939"></a>    <a href="tqobject.html#connect">connect</a>( bPWidth, TQ_SIGNAL( <a href="ntqspinbox.html#valueChanged">valueChanged</a>( int ) ), this, TQ_SLOT( slotWidth( int ) ) );
<a name="x938"></a>    bPWidth-&gt;<a href="ntqspinbox.html#setValue">setValue</a>( 3 );

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

    bClear = new <a href="tqtoolbutton.html">TQToolButton</a>( TQPixmap(), "Clear Screen", "Clear Screen", this, TQ_SLOT( slotClear() ), tools );
    bClear-&gt;<a href="ntqbutton.html#setText">setText</a>( "Clear Screen" );
}

void <a name="f342"></a>Scribble::slotSave()
{
    <a href="tqpopupmenu.html">TQPopupMenu</a> *menu = new <a href="tqpopupmenu.html">TQPopupMenu</a>( 0 );
    <a href="tqintdict.html">TQIntDict</a>&lt;TQString&gt; formats;
<a name="x924"></a>    formats.<a href="tqptrcollection.html#setAutoDelete">setAutoDelete</a>( TRUE );

    for ( unsigned int i = 0; i &lt; TQImageIO::<a href="tqimageio.html#outputFormats">outputFormats</a>().count(); i++ ) {
<a name="x911"></a>        <a href="tqstring.html">TQString</a> str = TQString( TQImageIO::<a href="tqimageio.html#outputFormats">outputFormats</a>().at( i ) );
<a name="x912"></a>        formats.<a href="tqintdict.html#insert">insert</a>( menu-&gt;<a href="tqmenudata.html#insertItem">insertItem</a>( TQString( "%1..." ).arg( str ) ), new <a href="tqstring.html">TQString</a>( str ) );
    }

<a name="x953"></a>    menu-&gt;<a href="tqwidget.html#setMouseTracking">setMouseTracking</a>( TRUE );
<a name="x947"></a><a name="x946"></a><a name="x923"></a>    int id = menu-&gt;<a href="tqpopupmenu.html#exec">exec</a>( bSave-&gt;<a href="tqwidget.html#mapToGlobal">mapToGlobal</a>( TQPoint( 0, bSave-&gt;<a href="tqwidget.html#height">height</a>() + 1 ) ) );

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

<a name="x941"></a>        <a href="tqstring.html">TQString</a> filename = TQFileDialog::<a href="tqfiledialog.html#getSaveFileName">getSaveFileName</a>( <a href="tqstring.html#TQString-null">TQString::null</a>, TQString( "*.%1" ).arg( format.<a href="tqstring.html#lower">lower</a>() ), this );
        if ( !filename.<a href="tqstring.html#isEmpty">isEmpty</a>() )
            canvas-&gt;save( filename, format );
    }

    delete menu;
}

void <a name="f343"></a>Scribble::slotColor()
{
<a name="x909"></a>    <a href="tqcolor.html">TQColor</a> c = TQColorDialog::<a href="tqcolordialog.html#getColor">getColor</a>( canvas-&gt;penColor(), this );
<a name="x908"></a>    if ( c.<a href="tqcolor.html#isValid">isValid</a>() )
        canvas-&gt;setPenColor( c );
}

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

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

<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 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 &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;


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

    Scribble scribble;

    scribble.<a href="tqwidget.html#resize">resize</a>( 500, 350 );
    scribble.<a href="tqwidget.html#setCaption">setCaption</a>("TQt Example - Scribble");
    a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>( &amp;scribble );
<a name="x954"></a>    if ( TQApplication::<a href="ntqapplication.html#desktop">desktop</a>()-&gt;width() &gt; 550
         &amp;&amp; TQApplication::<a href="ntqapplication.html#desktop">desktop</a>()-&gt;height() &gt; 366 )
        scribble.<a href="tqwidget.html#show">show</a>();
    else
<a name="x960"></a>        scribble.<a href="tqwidget.html#showMaximized">showMaximized</a>();
    return a.<a href="ntqapplication.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; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>