summaryrefslogtreecommitdiffstats
path: root/doc/html/desktop-example.html
blob: 1d778ace448c25fa18d364bb09cc94ca0262058f (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!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/desktop/desktop.doc:5 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Painting on the Desktop</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>Painting on the Desktop</h1>

 
<p> 
The desktop demo contains three routines, each of which draws
something on the desktop.  It does some nice stuff with <a href="ntqpainter.html">TQPainter</a>,
and also demonstrates how one can treat the desktop as a widget like
any other.
<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id: qt/desktop.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="qimage-h.html">ntqimage.h</a>&gt;
#include &lt;<a href="qbitmap-h.html">ntqbitmap.h</a>&gt;
#include &lt;<a href="qpainter-h.html">ntqpainter.h</a>&gt;
#include &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;
#include &lt;<a href="qdropsite-h.html">ntqdropsite.h</a>&gt;
#include &lt;<a href="qdragobject-h.html">ntqdragobject.h</a>&gt;
#include &lt;stdio.h&gt;


static double seed = 0.353535353535;
static const int KINDA_RAND_MAX = 32767;

static int kindaRand()
{
    seed = seed*147;
    seed = seed - (double) ((int) seed);
    return (int) ( seed*(KINDA_RAND_MAX + 1) );
}

static int velocity( int i )                    // change velocity
{
    const int velmax = 15;
    const int velmin = 4;
    if ( i == 1 || i == 2 )
        i = (kindaRand()&amp;0x7fff % velmax)/3 + velmin;
    else
        i = (kindaRand()&amp;0x7fff % velmax) + velmin;
    return i;
}

//
// Draw polygon on desktop.
//

void poly()
{
<a name="x1721"></a>    <a href="ntqwidget.html">TQWidget</a> *d = TQApplication::<a href="ntqapplication.html#desktop">desktop</a>();
<a name="x1761"></a>    d-&gt;<a href="ntqwidget.html#setBackgroundColor">setBackgroundColor</a>( TQt::white );         // white desktop

    const int maxpoints = 5;
    const int maxcurves = 8;
    static int xvel[maxpoints];
    static int yvel[maxpoints];
    int head = 0;
    int tail = -maxcurves + 2;
    <a href="ntqpointarray.html">TQPointArray</a> *a = new <a href="ntqpointarray.html">TQPointArray</a>[ maxcurves ];
    register TQPointArray *p;
<a name="x1760"></a>    <a href="ntqrect.html">TQRect</a> r = d-&gt;<a href="ntqwidget.html#rect">rect</a>();                        // desktop rectangle

    int i;
    for ( i=0; i&lt;maxcurves; i++ )
        a[i].resize( maxpoints );
    p = &amp;a[0];
    for ( i=0; i&lt;maxpoints; i++ ) {             // setup first polygon points
<a name="x1756"></a>        p-&gt;setPoint( i, (kindaRand()&amp;0x7fff) % r.<a href="ntqrect.html#width">width</a>(),
<a name="x1749"></a>                        (kindaRand()&amp;0x7fff) % r.<a href="ntqrect.html#height">height</a>() );
        xvel[i] = velocity(i);
        yvel[i] = velocity(i);
    }

    <a href="ntqpainter.html">TQPainter</a> paint;
<a name="x1733"></a>    paint.<a href="ntqpainter.html#begin">begin</a>( d );                           // start painting desktop

    for ( int ntimes=0; ntimes&lt;2000; ntimes++ ) {
        paint.<a href="ntqpainter.html#setBrush">setBrush</a>( TQColor(kindaRand()%360, 180, 255, TQColor::Hsv) );
<a name="x1735"></a>        paint.<a href="ntqpainter.html#drawPolygon">drawPolygon</a>( a[head] );
        if ( ++tail &gt;= maxcurves )
            tail = 0;

<a name="x1751"></a><a name="x1750"></a>        int minx=r.<a href="ntqrect.html#left">left</a>(), maxx=r.<a href="ntqrect.html#right">right</a>();
<a name="x1755"></a><a name="x1748"></a>        int miny=r.<a href="ntqrect.html#top">top</a>(),  maxy=r.<a href="ntqrect.html#bottom">bottom</a>();
        int x, y;
        p = &amp;a[head];
        if ( ++head &gt;= maxcurves )
            head = 0;
        for ( i=0; i&lt;maxpoints; i++ ) {         // calc new curve
            p-&gt;point( i, &amp;x, &amp;y );
            x += xvel[i];
            y += yvel[i];
            if ( x &gt;= maxx ) {
                x = maxx - (x - maxx + 1);
                xvel[i] = -velocity(i);
            }
            if ( x &lt;= minx ) {
                x = minx + (minx - x + 1);
                xvel[i] = velocity(i);
            }
            if ( y &gt;= maxy ) {
                y = maxy - (y - maxy + 1);
                yvel[i] = -velocity(i);
            }
            if ( y &lt;= miny ) {
                y = miny + (miny - y + 1);
                yvel[i] = velocity(i);
            }
            a[head].setPoint( i, x, y );
        }
    }
<a name="x1737"></a>    paint.<a href="ntqpainter.html#end">end</a>();                                // painting done
    delete[] a;
}


//
// Rotate pattern on desktop.
//

void rotate()
{
    int i;
    const int w = 64;
    const int h = 64;
    <a href="ntqimage.html">TQImage</a> image( w, h, 8, 128 );               // create image
    for ( i=0; i&lt;128; i++ )                     // build color table
<a name="x1730"></a>        image.<a href="ntqimage.html#setColor">setColor</a>( i, qRgb(i,0,0) );
    for ( int y=0; y&lt;h; y++ ) {                 // set image pixels
<a name="x1729"></a>        uchar *p = image.<a href="ntqimage.html#scanLine">scanLine</a>(y);
        for ( int x=0; x&lt;w; x++ )
            *p++ = (x+y)%128;
    }

    <a href="ntqpixmap.html">TQPixmap</a> pm;
    pm = image;                                 // convert image to pixmap
<a name="x1745"></a>    pm.<a href="ntqpixmap.html#setOptimization">setOptimization</a>( TQPixmap::BestOptim );   // rotation will be faster

    <a href="ntqwidget.html">TQWidget</a> *d = TQApplication::<a href="ntqapplication.html#desktop">desktop</a>();       // w = desktop widget

    for ( i=0; i&lt;=360; i += 2 ) {
        <a href="ntqwmatrix.html">TQWMatrix</a> m;
<a name="x1764"></a>        m.<a href="ntqwmatrix.html#rotate">rotate</a>( i );                          // rotate coordinate system
<a name="x1747"></a>        <a href="ntqpixmap.html">TQPixmap</a> rpm = pm.<a href="ntqpixmap.html#xForm">xForm</a>( m );            // rpm = rotated pixmap
<a name="x1762"></a>        d-&gt;<a href="ntqwidget.html#setBackgroundPixmap">setBackgroundPixmap</a>( rpm );          // set desktop pixmap
<a name="x1763"></a>        d-&gt;<a href="ntqwidget.html#update">update</a>();                            // repaint desktop
    }
}

//
// Generates a marble-like pattern in pm.
//

void generateStone( <a href="ntqpixmap.html">TQPixmap</a> *pm,
                    const <a href="ntqcolor.html">TQColor</a> &amp;c1, const <a href="ntqcolor.html">TQColor</a> &amp;c2, const <a href="ntqcolor.html">TQColor</a> &amp;c3 )
{
    <a href="ntqpainter.html">TQPainter</a> p;
    <a href="ntqpen.html">TQPen</a> p1 ( c1, 0 );
    <a href="ntqpen.html">TQPen</a> p2 ( c2, 0 );
    <a href="ntqpen.html">TQPen</a> p3 ( c3, 0 );

    p.<a href="ntqpainter.html#begin">begin</a>( pm );
<a name="x1746"></a>    for( int i = 0 ; i &lt; pm-&gt;<a href="ntqpixmap.html#width">width</a>() ; i++ )
<a name="x1743"></a>        for( int j = 0 ; j &lt; pm-&gt;<a href="ntqpixmap.html#height">height</a>() ; j++ ) {
            int r = kindaRand();
            if ( r &lt; KINDA_RAND_MAX / 3 )
<a name="x1741"></a>                p.<a href="ntqpainter.html#setPen">setPen</a>( p1 );
            else if ( r &lt; KINDA_RAND_MAX / 3 * 2 )
                p.<a href="ntqpainter.html#setPen">setPen</a>( p2 );
            else
                p.<a href="ntqpainter.html#setPen">setPen</a>( p3 );
<a name="x1734"></a>            p.<a href="ntqpainter.html#drawPoint">drawPoint</a>( i,j );
        }
    p.<a href="ntqpainter.html#end">end</a>();
}

void drawShadeText( <a href="ntqpainter.html">TQPainter</a> *p, int x, int y, const char *text,
                    const <a href="ntqcolor.html">TQColor</a> &amp;topColor, const <a href="ntqcolor.html">TQColor</a> &amp;bottomColor,
                    int sw = 2 )
{
<a name="x1739"></a>    if ( !p-&gt;<a href="ntqpainter.html#isActive">isActive</a>() )
        return;

    p-&gt;<a href="ntqpainter.html#setPen">setPen</a>( bottomColor );
<a name="x1736"></a>    p-&gt;<a href="ntqpainter.html#drawText">drawText</a>( x+sw, y+sw, text );
    p-&gt;<a href="ntqpainter.html#setPen">setPen</a>( topColor );
    p-&gt;<a href="ntqpainter.html#drawText">drawText</a>( x, y, text );
}

// NOTE: desktop drag/drop is experimental

class DesktopWidget : public <a href="ntqwidget.html">TQWidget</a>, private TQDropSite
{
public:
    DesktopWidget( const char *s, TQWidget *parent=0, const char *name=0 );
   ~DesktopWidget();
    void paintEvent( <a href="qpaintevent.html">TQPaintEvent</a> * );

    void dragEnterEvent( <a href="qdragenterevent.html">TQDragEnterEvent</a> *e )
    {
<a name="x1731"></a>        if ( TQImageDrag::<a href="qimagedrag.html#canDecode">canDecode</a>(e) )
<a name="x1727"></a>            e-&gt;<a href="qdragmoveevent.html#accept">accept</a>();
    }

    void dragLeaveEvent( <a href="qdragleaveevent.html">TQDragLeaveEvent</a> * )
    {
    }

    void dragMoveEvent( <a href="qdragmoveevent.html">TQDragMoveEvent</a> *e )
    {
        e-&gt;<a href="qdragmoveevent.html#accept">accept</a>();
    }

    void dropEvent( <a href="qdropevent.html">TQDropEvent</a> * e )
    {
        <a href="ntqpixmap.html">TQPixmap</a> pmp;
<a name="x1732"></a>        if ( TQImageDrag::<a href="qimagedrag.html#decode">decode</a>( e, pmp ) ) {
            <a href="ntqwidget.html#setBackgroundPixmap">setBackgroundPixmap</a>( pmp );
            <a href="ntqwidget.html#update">update</a>();
        }
    }

private:
    <a href="ntqpixmap.html">TQPixmap</a> *pm;
    <a href="ntqstring.html">TQString</a> text;
};

<a name="f483"></a>DesktopWidget::DesktopWidget( const char *s, TQWidget *parent, const char *name )
    : <a href="ntqwidget.html">TQWidget</a>( parent, name, WType_Desktop | WPaintDesktop),
        TQDropSite(this)
{
    text = s;
    pm   = 0;
}

DesktopWidget::~DesktopWidget()
{
    delete pm;
}

void DesktopWidget::<a href="ntqwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">TQPaintEvent</a> * )
{
    <a href="ntqcolor.html">TQColor</a> c1 = <a href="ntqwidget.html#backgroundColor">backgroundColor</a>();
<a name="x1726"></a>    <a href="ntqcolor.html">TQColor</a> c2 = c1.<a href="ntqcolor.html#light">light</a>(104);
<a name="x1725"></a>    <a href="ntqcolor.html">TQColor</a> c3 = c1.<a href="ntqcolor.html#dark">dark</a>(106);
    if ( !pm ) {
        pm = new <a href="ntqpixmap.html">TQPixmap</a>( 64, 64 );
        generateStone( pm, c1, c2, c3 );
        <a href="ntqwidget.html#setBackgroundPixmap">setBackgroundPixmap</a>( *pm );
        <a href="ntqwidget.html#update">update</a>();
    }
    <a href="ntqrect.html">TQRect</a> br = <a href="ntqwidget.html#fontMetrics">fontMetrics</a>().boundingRect( text );
    <a href="ntqpixmap.html">TQPixmap</a> offscreen( br.<a href="ntqrect.html#width">width</a>(), br.<a href="ntqrect.html#height">height</a>() );
    int x = <a href="ntqwidget.html#width">width</a>()/2  - br.<a href="ntqrect.html#width">width</a>()/2;
    int y = <a href="ntqwidget.html#height">height</a>()/2 - br.<a href="ntqrect.html#height">height</a>()/2;
<a name="x1742"></a>    offscreen.<a href="ntqpixmap.html#fill">fill</a>( this, x, y );
    <a href="ntqpainter.html">TQPainter</a> p;
    p.<a href="ntqpainter.html#begin">begin</a>( &amp;offscreen );
<a name="x1758"></a><a name="x1757"></a>    drawShadeText( &amp;p, -br.<a href="ntqrect.html#x">x</a>(), -br.<a href="ntqrect.html#y">y</a>(), text, c2, c3, 3 );
    p.<a href="ntqpainter.html#end">end</a>();
    <a href="ntqimage.html#bitBlt">bitBlt</a>( this, x, y, &amp;offscreen );
}

void desktopWidget( const char *s = "Trolltech" )
{
    DesktopWidget *t = new DesktopWidget(s);
    t-&gt;<a href="ntqwidget.html#update">update</a>();
    qApp-&gt;<a href="ntqapplication.html#exec">exec</a>();
    delete t;
}

void desktopText( const char *s = "Trolltech" )
{
    const int border = 20;

<a name="x1723"></a>    <a href="ntqcolor.html">TQColor</a> c1 =  qApp-&gt;<a href="ntqapplication.html#palette">palette</a>().inactive().background();
    <a href="ntqcolor.html">TQColor</a> c2 = c1.<a href="ntqcolor.html#light">light</a>(104);
    <a href="ntqcolor.html">TQColor</a> c3 = c1.<a href="ntqcolor.html#dark">dark</a>(106);

    <a href="ntqpixmap.html">TQPixmap</a> pm(10,10);

    <a href="ntqpainter.html">TQPainter</a> p;
    p.<a href="ntqpainter.html#begin">begin</a>( &amp;pm );
<a name="x1738"></a>    <a href="ntqrect.html">TQRect</a> r = p.<a href="ntqpainter.html#fontMetrics">fontMetrics</a>().boundingRect( s );
    p.<a href="ntqpainter.html#end">end</a>();

    int appWidth  =  qApp-&gt;<a href="ntqapplication.html#desktop">desktop</a>()-&gt;width();
    int appHeight =  qApp-&gt;<a href="ntqapplication.html#desktop">desktop</a>()-&gt;height();
    if ( r.<a href="ntqrect.html#width">width</a>() &gt; appWidth - border*2 )
<a name="x1753"></a>        r.<a href="ntqrect.html#setWidth">setWidth</a>( appWidth - border*2 );
    if ( r.<a href="ntqrect.html#height">height</a>() &gt; appHeight - border*2 )
<a name="x1752"></a>        r.<a href="ntqrect.html#setHeight">setHeight</a>( appHeight - border*2 );

<a name="x1754"></a><a name="x1744"></a>    pm.<a href="ntqpixmap.html#resize">resize</a>( r.<a href="ntqrect.html#size">size</a>() + TQSize( border*2, border*2 ) );
    generateStone( &amp;pm, c1, c2, c3 );
    p.<a href="ntqpainter.html#begin">begin</a>( &amp;pm );
    drawShadeText( &amp;p, -r.<a href="ntqrect.html#x">x</a>() + border, -r.<a href="ntqrect.html#y">y</a>() + border, s, c2, c3 );
    p.<a href="ntqpainter.html#end">end</a>();

    qApp-&gt;<a href="ntqapplication.html#desktop">desktop</a>()-&gt;setBackgroundPixmap( pm );
}

//
// The program starts here.
//

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

    if ( argc &gt; 1 ) {
        <a href="ntqfont.html">TQFont</a> f( "charter", 96, TQFont::Black );
<a name="x1728"></a>        f.<a href="ntqfont.html#setStyleHint">setStyleHint</a>( TQFont::Times );
<a name="x1724"></a>        app.<a href="ntqapplication.html#setFont">setFont</a>( f );
    }

    bool validOptions = FALSE;

    if ( argc == 2 ) {
        validOptions = TRUE;
        if ( strcmp(argv[1],"-poly") == 0 )
            poly();
        else if ( strcmp(argv[1],"-rotate") == 0 )
            rotate();
        else if ( strcmp(argv[1],"-troll") == 0 )
            desktopText();
        else if ( strcmp(argv[1],"-trollwidget") == 0 )
            desktopWidget();
        else
            validOptions = FALSE;
    }
    if ( argc == 3 ) {
        validOptions = TRUE;
        if ( strcmp(argv[1],"-shadetext") == 0 )
            desktopText( argv[2] );
        else if ( strcmp(argv[1],"-shadewidget") == 0 )
            desktopWidget( argv[2] );
        else
            validOptions = FALSE;
    }
    if ( !validOptions ) {
        fprintf( stderr, "Usage:\n\tdesktop -poly"
                               "\n\tdesktop -rotate"
                               "\n\tdesktop -troll"
                               "\n\tdesktop -trollwidget"
                               "\n\tdesktop -shadetext &lt;text&gt;"
                               "\n\tdesktop -shadewidget &lt;text&gt;\n" );
        rotate();
    }
    return 0;
}
</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>