summaryrefslogtreecommitdiffstats
path: root/doc/html/drawdemo-example.html
blob: 4cf02e215bab13d14194b0f2dfd7630cf56da3d2 (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
<!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/drawdemo/drawdemo.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Draw Demo</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>Draw Demo</h1>

   
<p> 
This example demonstrates several drawing functions and printer output.
You can easily add you own drawing functions.
<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/drawdemo.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 &lt;<a href="qwidget-h.html">ntqwidget.h</a>&gt;
#include &lt;<a href="qpainter-h.html">ntqpainter.h</a>&gt;
#include &lt;<a href="qprinter-h.html">ntqprinter.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">ntqpushbutton.h</a>&gt;
#include &lt;<a href="qradiobutton-h.html">ntqradiobutton.h</a>&gt;
#include &lt;<a href="qbuttongroup-h.html">ntqbuttongroup.h</a>&gt;
#include &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;
#include &lt;math.h&gt;

//
// First we define the functionality our demo should present
// to the user. You might add different demo-modes if you wish so.
//

//
// This function draws a color wheel.
// The coordinate system x=(0..500), y=(0..500) spans the paint device.
//

void drawColorWheel( <a href="ntqpainter.html">TQPainter</a> *p )
{
    <a href="ntqfont.html">TQFont</a> f( "times", 18, TQFont::Bold );
<a name="x1075"></a>    p-&gt;<a href="ntqpainter.html#setFont">setFont</a>( f );
<a name="x1076"></a>    p-&gt;<a href="ntqpainter.html#setPen">setPen</a>( TQt::black );
<a name="x1077"></a>    p-&gt;<a href="ntqpainter.html#setWindow">setWindow</a>( 0, 0, 500, 500 );             // defines coordinate system

    for ( int i=0; i&lt;36; i++ ) {                // draws 36 rotated rectangles

        <a href="ntqwmatrix.html">TQWMatrix</a> matrix;
<a name="x1097"></a>        matrix.<a href="ntqwmatrix.html#translate">translate</a>( 250.0F, 250.0F );     // move to center
<a name="x1096"></a>        matrix.<a href="ntqwmatrix.html#shear">shear</a>( 0.0F, 0.3F );             // twist it
<a name="x1095"></a>        matrix.<a href="ntqwmatrix.html#rotate">rotate</a>( (float)i*10 );           // rotate 0,10,20,.. degrees
<a name="x1078"></a>        p-&gt;<a href="ntqpainter.html#setWorldMatrix">setWorldMatrix</a>( matrix );            // use this world matrix

        <a href="ntqcolor.html">TQColor</a> c;
<a name="x1063"></a>        c.<a href="ntqcolor.html#setHsv">setHsv</a>( i*10, 255, 255 );             // rainbow effect
<a name="x1074"></a>        p-&gt;<a href="ntqpainter.html#setBrush">setBrush</a>( c );                       // solid fill with color c
<a name="x1070"></a>        p-&gt;<a href="ntqpainter.html#drawRect">drawRect</a>( 70, -10, 80, 10 );         // draw the rectangle

        <a href="ntqstring.html">TQString</a> n;
        n.<a href="ntqstring.html#sprintf">sprintf</a>( "H=%d", i*10 );
<a name="x1072"></a>        p-&gt;<a href="ntqpainter.html#drawText">drawText</a>( 80+70+5, 0, n );           // draw the hue number
    }
}


//
// This function draws a few lines of text using different fonts.
//

void drawFonts( <a href="ntqpainter.html">TQPainter</a> *p )
{
    static const char *fonts[] = { "Helvetica", "Courier", "Times", 0 };
    static int   sizes[] = { 10, 12, 18, 24, 36, 0 };
    int f = 0;
    int y = 0;
    while ( fonts[f] ) {
        int s = 0;
        while ( sizes[s] ) {
            <a href="ntqfont.html">TQFont</a> font( fonts[f], sizes[s] );
            p-&gt;<a href="ntqpainter.html#setFont">setFont</a>( font );
<a name="x1073"></a>            <a href="ntqfontmetrics.html">TQFontMetrics</a> fm = p-&gt;<a href="ntqpainter.html#fontMetrics">fontMetrics</a>();
<a name="x1064"></a>            y += fm.<a href="ntqfontmetrics.html#ascent">ascent</a>();
            p-&gt;<a href="ntqpainter.html#drawText">drawText</a>( 10, y, "Quartz Glyph Job Vex'd Cwm Finks" );
<a name="x1065"></a>            y += fm.<a href="ntqfontmetrics.html#descent">descent</a>();
            s++;
        }
        f++;
    }
}


//
// This function draws some shapes
//

void drawShapes( <a href="ntqpainter.html">TQPainter</a> *p )
{
    <a href="ntqbrush.html">TQBrush</a> b1( TQt::blue );
    <a href="ntqbrush.html">TQBrush</a> b2( TQt::green, TQt::Dense6Pattern );          // green 12% fill
    <a href="ntqbrush.html">TQBrush</a> b3( TQt::NoBrush );                           // void brush
    <a href="ntqbrush.html">TQBrush</a> b4( TQt::CrossPattern );                      // black cross pattern

    p-&gt;<a href="ntqpainter.html#setPen">setPen</a>( TQt::red );
    p-&gt;<a href="ntqpainter.html#setBrush">setBrush</a>( b1 );
    p-&gt;<a href="ntqpainter.html#drawRect">drawRect</a>( 10, 10, 200, 100 );
    p-&gt;<a href="ntqpainter.html#setBrush">setBrush</a>( b2 );
<a name="x1071"></a>    p-&gt;<a href="ntqpainter.html#drawRoundRect">drawRoundRect</a>( 10, 150, 200, 100, 20, 20 );
    p-&gt;<a href="ntqpainter.html#setBrush">setBrush</a>( b3 );
<a name="x1068"></a>    p-&gt;<a href="ntqpainter.html#drawEllipse">drawEllipse</a>( 250, 10, 200, 100 );
    p-&gt;<a href="ntqpainter.html#setBrush">setBrush</a>( b4 );
<a name="x1069"></a>    p-&gt;<a href="ntqpainter.html#drawPie">drawPie</a>( 250, 150, 200, 100, 45*16, 90*16 );
}


typedef void (*draw_func)(TQPainter*);

struct DrawThing {
    draw_func    f;
    const char  *name;
};

//
// All previously implemented functions are collected
// in the following "table".
// If you implement different functionality, your new draw
// function must be assigned here with a function pointer and
// description.
// Leave the zeros at the end, they will be used
// as markers referring to the end of the array.
//

DrawThing ourDrawFunctions[] = {
// name of the function, title presented to the user
    { drawColorWheel,   "Draw color wheel" },
    { drawFonts,        "Draw fonts" },
    { drawShapes,       "Draw shapes" },
    { 0,                0 } };



class DrawView : public <a href="ntqwidget.html">TQWidget</a>
{
    <a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>
public:
    DrawView();
    ~DrawView();
public slots:
    void   updateIt( int );
    void   printIt();
protected:
    void   drawIt( <a href="ntqpainter.html">TQPainter</a> * );
    void   paintEvent( <a href="qpaintevent.html">TQPaintEvent</a> * );
    void   resizeEvent( <a href="qresizeevent.html">TQResizeEvent</a> * );
private:
    <a href="ntqprinter.html">TQPrinter</a>     *printer;
    <a href="ntqbuttongroup.html">TQButtonGroup</a> *bgroup;
    <a href="ntqpushbutton.html">TQPushButton</a>  *print;
    int           drawindex;
    int           maxindex;
};


//
// Construct the DrawView with buttons.
//

<a name="f367"></a>DrawView::DrawView()
{
    <a href="ntqwidget.html#setCaption">setCaption</a>( "TQt Draw Demo Application" );
    <a href="ntqwidget.html#setBackgroundMode">setBackgroundMode</a>(PaletteBase);

    // Create a button group to contain all buttons
    bgroup = new <a href="ntqbuttongroup.html">TQButtonGroup</a>( this );
<a name="x1088"></a>    bgroup-&gt;<a href="ntqwidget.html#resize">resize</a>( 200, 200 );
<a name="x1062"></a>    <a href="ntqobject.html#connect">connect</a>( bgroup, TQ_SIGNAL(<a href="ntqbuttongroup.html#clicked">clicked</a>(int)), TQ_SLOT(updateIt(int)) );

    // Calculate the size for the radio buttons
    int maxwidth = 80;
    int maxheight = 10;
    int i;
    const char *n;
<a name="x1084"></a>    <a href="ntqfontmetrics.html">TQFontMetrics</a> fm = bgroup-&gt;<a href="ntqwidget.html#fontMetrics">fontMetrics</a>();

    // Find out the longest function description.
    // Here we make use of the last "0,0"-entry in the
    // ourDrawFunctions-array.
    for ( i=0; (n=ourDrawFunctions[i].name) != 0; i++ ) {
<a name="x1066"></a>        int w = fm.<a href="ntqfontmetrics.html#width">width</a>( n );
        maxwidth = TQMAX(w,maxwidth); // TQMAX is a macro defined in ntqglobal.h
                                     // and returns the biggest of to values.
        // Due to its macro nature one should use it with care and with
        // constant parameters only.
    }

    maxwidth = maxwidth + 30;                   // allow 30 pixels for radiobuttons

    for ( i=0; (n=ourDrawFunctions[i].name) != 0; i++ ) {
        <a href="ntqradiobutton.html">TQRadioButton</a> *rb = new <a href="ntqradiobutton.html">TQRadioButton</a>( n, bgroup );
<a name="x1091"></a>        rb-&gt;<a href="ntqwidget.html#setGeometry">setGeometry</a>( 10, i*30+10, maxwidth, 30 );

        maxheight += 30;

        if ( i == 0 )
<a name="x1082"></a>            rb-&gt;<a href="ntqradiobutton.html#setChecked">setChecked</a>( TRUE );
    }

    maxheight += 10;                            // maxheight is now 10 pixels upper margin
                                                // plus number_of_drawfunctions * 30
                                                // plus 10 pixels lower margin

    drawindex = 0;                              // draw first thing
    maxindex  = i;

    maxwidth += 20;                             // add some margin, this results in the
                                                // final width of bgroup

    bgroup-&gt;<a href="ntqwidget.html#resize">resize</a>( maxwidth, maxheight );      // resize bgroup to its final size
                                                // when no printersupport is provided


// If -- at compile time -- printer support will be disabled,
// we won't set up printing functionality.

#ifndef TQT_NO_PRINTER

    printer = new <a href="ntqprinter.html">TQPrinter</a>;

    // Create and setup the print button
    print = new <a href="ntqpushbutton.html">TQPushButton</a>( "Print...", bgroup );
<a name="x1081"></a>    print-&gt;<a href="ntqwidget.html#resize">resize</a>( 80, 30 );
<a name="x1093"></a><a name="x1080"></a>    print-&gt;<a href="ntqwidget.html#move">move</a>( maxwidth/2 - print-&gt;<a href="ntqwidget.html#width">width</a>()/2, maxindex*30+20 );
    <a href="ntqobject.html#connect">connect</a>( print, TQ_SIGNAL(<a href="ntqbutton.html#clicked">clicked</a>()), TQ_SLOT(printIt()) );

    // Resize bgroup to its final size when printersupport is given.
<a name="x1094"></a><a name="x1085"></a>    bgroup-&gt;<a href="ntqwidget.html#resize">resize</a>( maxwidth, print-&gt;<a href="ntqwidget.html#y">y</a>()+print-&gt;<a href="ntqwidget.html#height">height</a>()+10 );

#endif

    <a href="ntqwidget.html#resize">resize</a>( 640,300 );
}

//
// Clean up.
//
DrawView::~DrawView()
{
#ifndef TQT_NO_PRINTER
    delete printer;
#endif
}

//
// Called when a radio button is clicked.
//

void <a name="f368"></a>DrawView::updateIt( int index )
{
    if ( index &lt; maxindex ) {
        drawindex = index;
        <a href="ntqwidget.html#update">update</a>();
    }
}

//
// Calls the drawing function as specified by the radio buttons.
//

void <a name="f369"></a>DrawView::drawIt( <a href="ntqpainter.html">TQPainter</a> *p )
{
    (*ourDrawFunctions[drawindex].f)(p);
}

//
// Called when the print button is clicked.
//

void <a name="f370"></a>DrawView::printIt()
{
<a name="x1079"></a>    if ( printer-&gt;<a href="ntqprinter.html#setup">setup</a>( this ) ) {
        <a href="ntqpainter.html">TQPainter</a> paint;
<a name="x1067"></a>        if( !paint.<a href="ntqpainter.html#begin">begin</a>( printer ) )
            return;
        drawIt( &amp;paint );
    }
}

//
// Called when the widget needs to be updated.
//

void DrawView::<a href="ntqwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">TQPaintEvent</a> * )
{
    <a href="ntqpainter.html">TQPainter</a> paint( this );
    drawIt( &amp;paint );
}

//
// Called when the widget has been resized.
// Moves the button group to the upper right corner
// of the widget.

<a name="x1089"></a>void DrawView::<a href="ntqwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">TQResizeEvent</a> * )
{
<a name="x1086"></a>    bgroup-&gt;<a href="ntqwidget.html#move">move</a>( <a href="ntqwidget.html#width">width</a>()-bgroup-&gt;<a href="ntqwidget.html#width">width</a>(), 0 );
}


//
// Create and display our widget.
//

#include "drawdemo.moc"

int main( int argc, char **argv )
{
    <a href="ntqapplication.html">TQApplication</a> app( argc, argv );
    DrawView   draw;
    app.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>( &amp;draw );
    draw.<a href="ntqwidget.html#setCaption">setCaption</a>("TQt Example - Drawdemo");
    draw.<a href="ntqwidget.html#show">show</a>();
    return app.<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>