source: trunk/doc/html/desktop-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.7 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/desktop/desktop.doc:5 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Painting on the Desktop</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>Painting on the Desktop</h1>
33
34
35<p>
36The desktop demo contains three routines, each of which draws
37something on the desktop. It does some nice stuff with <a href="qpainter.html">QPainter</a>,
38and also demonstrates how one can treat the desktop as a widget like
39any other.
40<p> <hr>
41<p> Implementation:
42<p> <pre>/****************************************************************************
43** $Id: desktop-example.html 2051 2007-02-21 10:04:20Z chehrlic $
44**
45** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
46**
47** This file is part of an example program for Qt. This example
48** program may be used, distributed and modified without limitation.
49**
50*****************************************************************************/
51
52#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
53#include &lt;<a href="qbitmap-h.html">qbitmap.h</a>&gt;
54#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
55#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
56#include &lt;<a href="qdropsite-h.html">qdropsite.h</a>&gt;
57#include &lt;<a href="qdragobject-h.html">qdragobject.h</a>&gt;
58#include &lt;stdio.h&gt;
59
60
61static double seed = 0.353535353535;
62static const int KINDA_RAND_MAX = 32767;
63
64static int kindaRand()
65{
66 seed = seed*147;
67 seed = seed - (double) ((int) seed);
68 return (int) ( seed*(KINDA_RAND_MAX + 1) );
69}
70
71static int velocity( int i ) // change velocity
72{
73 const int velmax = 15;
74 const int velmin = 4;
75 if ( i == 1 || i == 2 )
76 i = (kindaRand()&amp;0x7fff % velmax)/3 + velmin;
77 else
78 i = (kindaRand()&amp;0x7fff % velmax) + velmin;
79 return i;
80}
81
82//
83// Draw polygon on desktop.
84//
85
86void poly()
87{
88<a name="x1721"></a> <a href="qwidget.html">QWidget</a> *d = QApplication::<a href="qapplication.html#desktop">desktop</a>();
89<a name="x1761"></a> d-&gt;<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>( Qt::white ); // white desktop
90
91 const int maxpoints = 5;
92 const int maxcurves = 8;
93 static int xvel[maxpoints];
94 static int yvel[maxpoints];
95 int head = 0;
96 int tail = -maxcurves + 2;
97 <a href="qpointarray.html">QPointArray</a> *a = new <a href="qpointarray.html">QPointArray</a>[ maxcurves ];
98 register QPointArray *p;
99<a name="x1760"></a> <a href="qrect.html">QRect</a> r = d-&gt;<a href="qwidget.html#rect">rect</a>(); // desktop rectangle
100
101 int i;
102 for ( i=0; i&lt;maxcurves; i++ )
103 a[i].resize( maxpoints );
104 p = &amp;a[0];
105 for ( i=0; i&lt;maxpoints; i++ ) { // setup first polygon points
106<a name="x1756"></a> p-&gt;setPoint( i, (kindaRand()&amp;0x7fff) % r.<a href="qrect.html#width">width</a>(),
107<a name="x1749"></a> (kindaRand()&amp;0x7fff) % r.<a href="qrect.html#height">height</a>() );
108 xvel[i] = velocity(i);
109 yvel[i] = velocity(i);
110 }
111
112 <a href="qpainter.html">QPainter</a> paint;
113<a name="x1733"></a> paint.<a href="qpainter.html#begin">begin</a>( d ); // start painting desktop
114
115 for ( int ntimes=0; ntimes&lt;2000; ntimes++ ) {
116 paint.<a href="qpainter.html#setBrush">setBrush</a>( QColor(kindaRand()%360, 180, 255, QColor::Hsv) );
117<a name="x1735"></a> paint.<a href="qpainter.html#drawPolygon">drawPolygon</a>( a[head] );
118 if ( ++tail &gt;= maxcurves )
119 tail = 0;
120
121<a name="x1751"></a><a name="x1750"></a> int minx=r.<a href="qrect.html#left">left</a>(), maxx=r.<a href="qrect.html#right">right</a>();
122<a name="x1755"></a><a name="x1748"></a> int miny=r.<a href="qrect.html#top">top</a>(), maxy=r.<a href="qrect.html#bottom">bottom</a>();
123 int x, y;
124 p = &amp;a[head];
125 if ( ++head &gt;= maxcurves )
126 head = 0;
127 for ( i=0; i&lt;maxpoints; i++ ) { // calc new curve
128 p-&gt;point( i, &amp;x, &amp;y );
129 x += xvel[i];
130 y += yvel[i];
131 if ( x &gt;= maxx ) {
132 x = maxx - (x - maxx + 1);
133 xvel[i] = -velocity(i);
134 }
135 if ( x &lt;= minx ) {
136 x = minx + (minx - x + 1);
137 xvel[i] = velocity(i);
138 }
139 if ( y &gt;= maxy ) {
140 y = maxy - (y - maxy + 1);
141 yvel[i] = -velocity(i);
142 }
143 if ( y &lt;= miny ) {
144 y = miny + (miny - y + 1);
145 yvel[i] = velocity(i);
146 }
147 a[head].setPoint( i, x, y );
148 }
149 }
150<a name="x1737"></a> paint.<a href="qpainter.html#end">end</a>(); // painting done
151 delete[] a;
152}
153
154
155//
156// Rotate pattern on desktop.
157//
158
159void rotate()
160{
161 int i;
162 const int w = 64;
163 const int h = 64;
164 <a href="qimage.html">QImage</a> image( w, h, 8, 128 ); // create image
165 for ( i=0; i&lt;128; i++ ) // build color table
166<a name="x1730"></a> image.<a href="qimage.html#setColor">setColor</a>( i, qRgb(i,0,0) );
167 for ( int y=0; y&lt;h; y++ ) { // set image pixels
168<a name="x1729"></a> uchar *p = image.<a href="qimage.html#scanLine">scanLine</a>(y);
169 for ( int x=0; x&lt;w; x++ )
170 *p++ = (x+y)%128;
171 }
172
173 <a href="qpixmap.html">QPixmap</a> pm;
174 pm = image; // convert image to pixmap
175<a name="x1745"></a> pm.<a href="qpixmap.html#setOptimization">setOptimization</a>( QPixmap::BestOptim ); // rotation will be faster
176
177 <a href="qwidget.html">QWidget</a> *d = QApplication::<a href="qapplication.html#desktop">desktop</a>(); // w = desktop widget
178
179 for ( i=0; i&lt;=360; i += 2 ) {
180 <a href="qwmatrix.html">QWMatrix</a> m;
181<a name="x1764"></a> m.<a href="qwmatrix.html#rotate">rotate</a>( i ); // rotate coordinate system
182<a name="x1747"></a> <a href="qpixmap.html">QPixmap</a> rpm = pm.<a href="qpixmap.html#xForm">xForm</a>( m ); // rpm = rotated pixmap
183<a name="x1762"></a> d-&gt;<a href="qwidget.html#setBackgroundPixmap">setBackgroundPixmap</a>( rpm ); // set desktop pixmap
184<a name="x1763"></a> d-&gt;<a href="qwidget.html#update">update</a>(); // repaint desktop
185 }
186}
187
188//
189// Generates a marble-like pattern in pm.
190//
191
192void generateStone( <a href="qpixmap.html">QPixmap</a> *pm,
193 const <a href="qcolor.html">QColor</a> &amp;c1, const <a href="qcolor.html">QColor</a> &amp;c2, const <a href="qcolor.html">QColor</a> &amp;c3 )
194{
195 <a href="qpainter.html">QPainter</a> p;
196 <a href="qpen.html">QPen</a> p1 ( c1, 0 );
197 <a href="qpen.html">QPen</a> p2 ( c2, 0 );
198 <a href="qpen.html">QPen</a> p3 ( c3, 0 );
199
200 p.<a href="qpainter.html#begin">begin</a>( pm );
201<a name="x1746"></a> for( int i = 0 ; i &lt; pm-&gt;<a href="qpixmap.html#width">width</a>() ; i++ )
202<a name="x1743"></a> for( int j = 0 ; j &lt; pm-&gt;<a href="qpixmap.html#height">height</a>() ; j++ ) {
203 int r = kindaRand();
204 if ( r &lt; KINDA_RAND_MAX / 3 )
205<a name="x1741"></a> p.<a href="qpainter.html#setPen">setPen</a>( p1 );
206 else if ( r &lt; KINDA_RAND_MAX / 3 * 2 )
207 p.<a href="qpainter.html#setPen">setPen</a>( p2 );
208 else
209 p.<a href="qpainter.html#setPen">setPen</a>( p3 );
210<a name="x1734"></a> p.<a href="qpainter.html#drawPoint">drawPoint</a>( i,j );
211 }
212 p.<a href="qpainter.html#end">end</a>();
213}
214
215void drawShadeText( <a href="qpainter.html">QPainter</a> *p, int x, int y, const char *text,
216 const <a href="qcolor.html">QColor</a> &amp;topColor, const <a href="qcolor.html">QColor</a> &amp;bottomColor,
217 int sw = 2 )
218{
219<a name="x1739"></a> if ( !p-&gt;<a href="qpainter.html#isActive">isActive</a>() )
220 return;
221
222 p-&gt;<a href="qpainter.html#setPen">setPen</a>( bottomColor );
223<a name="x1736"></a> p-&gt;<a href="qpainter.html#drawText">drawText</a>( x+sw, y+sw, text );
224 p-&gt;<a href="qpainter.html#setPen">setPen</a>( topColor );
225 p-&gt;<a href="qpainter.html#drawText">drawText</a>( x, y, text );
226}
227
228// NOTE: desktop drag/drop is experimental
229
230class DesktopWidget : public <a href="qwidget.html">QWidget</a>, private QDropSite
231{
232public:
233 DesktopWidget( const char *s, QWidget *parent=0, const char *name=0 );
234 ~DesktopWidget();
235 void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
236
237 void dragEnterEvent( <a href="qdragenterevent.html">QDragEnterEvent</a> *e )
238 {
239<a name="x1731"></a> if ( QImageDrag::<a href="qimagedrag.html#canDecode">canDecode</a>(e) )
240<a name="x1727"></a> e-&gt;<a href="qdragmoveevent.html#accept">accept</a>();
241 }
242
243 void dragLeaveEvent( <a href="qdragleaveevent.html">QDragLeaveEvent</a> * )
244 {
245 }
246
247 void dragMoveEvent( <a href="qdragmoveevent.html">QDragMoveEvent</a> *e )
248 {
249 e-&gt;<a href="qdragmoveevent.html#accept">accept</a>();
250 }
251
252 void dropEvent( <a href="qdropevent.html">QDropEvent</a> * e )
253 {
254 <a href="qpixmap.html">QPixmap</a> pmp;
255<a name="x1732"></a> if ( QImageDrag::<a href="qimagedrag.html#decode">decode</a>( e, pmp ) ) {
256 <a href="qwidget.html#setBackgroundPixmap">setBackgroundPixmap</a>( pmp );
257 <a href="qwidget.html#update">update</a>();
258 }
259 }
260
261private:
262 <a href="qpixmap.html">QPixmap</a> *pm;
263 <a href="qstring.html">QString</a> text;
264};
265
266<a name="f483"></a>DesktopWidget::DesktopWidget( const char *s, QWidget *parent, const char *name )
267 : <a href="qwidget.html">QWidget</a>( parent, name, WType_Desktop | WPaintDesktop),
268 QDropSite(this)
269{
270 text = s;
271 pm = 0;
272}
273
274DesktopWidget::~DesktopWidget()
275{
276 delete pm;
277}
278
279void DesktopWidget::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * )
280{
281 <a href="qcolor.html">QColor</a> c1 = <a href="qwidget.html#backgroundColor">backgroundColor</a>();
282<a name="x1726"></a> <a href="qcolor.html">QColor</a> c2 = c1.<a href="qcolor.html#light">light</a>(104);
283<a name="x1725"></a> <a href="qcolor.html">QColor</a> c3 = c1.<a href="qcolor.html#dark">dark</a>(106);
284 if ( !pm ) {
285 pm = new <a href="qpixmap.html">QPixmap</a>( 64, 64 );
286 generateStone( pm, c1, c2, c3 );
287 <a href="qwidget.html#setBackgroundPixmap">setBackgroundPixmap</a>( *pm );
288 <a href="qwidget.html#update">update</a>();
289 }
290 <a href="qrect.html">QRect</a> br = <a href="qwidget.html#fontMetrics">fontMetrics</a>().boundingRect( text );
291 <a href="qpixmap.html">QPixmap</a> offscreen( br.<a href="qrect.html#width">width</a>(), br.<a href="qrect.html#height">height</a>() );
292 int x = <a href="qwidget.html#width">width</a>()/2 - br.<a href="qrect.html#width">width</a>()/2;
293 int y = <a href="qwidget.html#height">height</a>()/2 - br.<a href="qrect.html#height">height</a>()/2;
294<a name="x1742"></a> offscreen.<a href="qpixmap.html#fill">fill</a>( this, x, y );
295 <a href="qpainter.html">QPainter</a> p;
296 p.<a href="qpainter.html#begin">begin</a>( &amp;offscreen );
297<a name="x1758"></a><a name="x1757"></a> drawShadeText( &amp;p, -br.<a href="qrect.html#x">x</a>(), -br.<a href="qrect.html#y">y</a>(), text, c2, c3, 3 );
298 p.<a href="qpainter.html#end">end</a>();
299 <a href="qimage.html#bitBlt">bitBlt</a>( this, x, y, &amp;offscreen );
300}
301
302void desktopWidget( const char *s = "Trolltech" )
303{
304 DesktopWidget *t = new DesktopWidget(s);
305 t-&gt;<a href="qwidget.html#update">update</a>();
306 qApp-&gt;<a href="qapplication.html#exec">exec</a>();
307 delete t;
308}
309
310void desktopText( const char *s = "Trolltech" )
311{
312 const int border = 20;
313
314<a name="x1723"></a> <a href="qcolor.html">QColor</a> c1 = qApp-&gt;<a href="qapplication.html#palette">palette</a>().inactive().background();
315 <a href="qcolor.html">QColor</a> c2 = c1.<a href="qcolor.html#light">light</a>(104);
316 <a href="qcolor.html">QColor</a> c3 = c1.<a href="qcolor.html#dark">dark</a>(106);
317
318 <a href="qpixmap.html">QPixmap</a> pm(10,10);
319
320 <a href="qpainter.html">QPainter</a> p;
321 p.<a href="qpainter.html#begin">begin</a>( &amp;pm );
322<a name="x1738"></a> <a href="qrect.html">QRect</a> r = p.<a href="qpainter.html#fontMetrics">fontMetrics</a>().boundingRect( s );
323 p.<a href="qpainter.html#end">end</a>();
324
325 int appWidth = qApp-&gt;<a href="qapplication.html#desktop">desktop</a>()-&gt;width();
326 int appHeight = qApp-&gt;<a href="qapplication.html#desktop">desktop</a>()-&gt;height();
327 if ( r.<a href="qrect.html#width">width</a>() &gt; appWidth - border*2 )
328<a name="x1753"></a> r.<a href="qrect.html#setWidth">setWidth</a>( appWidth - border*2 );
329 if ( r.<a href="qrect.html#height">height</a>() &gt; appHeight - border*2 )
330<a name="x1752"></a> r.<a href="qrect.html#setHeight">setHeight</a>( appHeight - border*2 );
331
332<a name="x1754"></a><a name="x1744"></a> pm.<a href="qpixmap.html#resize">resize</a>( r.<a href="qrect.html#size">size</a>() + QSize( border*2, border*2 ) );
333 generateStone( &amp;pm, c1, c2, c3 );
334 p.<a href="qpainter.html#begin">begin</a>( &amp;pm );
335 drawShadeText( &amp;p, -r.<a href="qrect.html#x">x</a>() + border, -r.<a href="qrect.html#y">y</a>() + border, s, c2, c3 );
336 p.<a href="qpainter.html#end">end</a>();
337
338 qApp-&gt;<a href="qapplication.html#desktop">desktop</a>()-&gt;setBackgroundPixmap( pm );
339}
340
341//
342// The program starts here.
343//
344
345int main( int argc, char **argv )
346{
347 <a href="qapplication.html">QApplication</a> app( argc, argv );
348
349 if ( argc &gt; 1 ) {
350 <a href="qfont.html">QFont</a> f( "charter", 96, QFont::Black );
351<a name="x1728"></a> f.<a href="qfont.html#setStyleHint">setStyleHint</a>( QFont::Times );
352<a name="x1724"></a> app.<a href="qapplication.html#setFont">setFont</a>( f );
353 }
354
355 bool validOptions = FALSE;
356
357 if ( argc == 2 ) {
358 validOptions = TRUE;
359 if ( strcmp(argv[1],"-poly") == 0 )
360 poly();
361 else if ( strcmp(argv[1],"-rotate") == 0 )
362 rotate();
363 else if ( strcmp(argv[1],"-troll") == 0 )
364 desktopText();
365 else if ( strcmp(argv[1],"-trollwidget") == 0 )
366 desktopWidget();
367 else
368 validOptions = FALSE;
369 }
370 if ( argc == 3 ) {
371 validOptions = TRUE;
372 if ( strcmp(argv[1],"-shadetext") == 0 )
373 desktopText( argv[2] );
374 else if ( strcmp(argv[1],"-shadewidget") == 0 )
375 desktopWidget( argv[2] );
376 else
377 validOptions = FALSE;
378 }
379 if ( !validOptions ) {
380 fprintf( stderr, "Usage:\n\tdesktop -poly"
381 "\n\tdesktop -rotate"
382 "\n\tdesktop -troll"
383 "\n\tdesktop -trollwidget"
384 "\n\tdesktop -shadetext &lt;text&gt;"
385 "\n\tdesktop -shadewidget &lt;text&gt;\n" );
386 rotate();
387 }
388 return 0;
389}
390</pre>
391
392<p>See also <a href="examples.html">Examples</a>.
393
394<!-- eof -->
395<p><address><hr><div align=center>
396<table width=100% cellspacing=0 border=0><tr>
397<td>Copyright &copy; 2007
398<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
399<td align=right><div align=right>Qt 3.3.8</div>
400</table></div></address></body>
401</html>
Note: See TracBrowser for help on using the repository browser.