<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
< 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 Classes< / font > < / a >
| < a href = "mainclasses.html" >
< font color = "#004faf" > Main Classes< / font > < / a >
| < a href = "annotated.html" >
< font color = "#004faf" > Annotated< / font > < / a >
| < a href = "groups.html" >
< font color = "#004faf" > Grouped 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 = "tqpainter.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 < < a href = "tqimage-h.html" > tqimage.h< / a > >
#include < < a href = "tqbitmap-h.html" > tqbitmap.h< / a > >
#include < < a href = "tqpainter-h.html" > tqpainter.h< / a > >
#include < < a href = "tqapplication-h.html" > tqapplication.h< / a > >
#include < < a href = "tqdropsite-h.html" > tqdropsite.h< / a > >
#include < < a href = "tqdragobject-h.html" > tqdragobject.h< / a > >
#include < stdio.h>
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()& 0x7fff % velmax)/3 + velmin;
else
i = (kindaRand()& 0x7fff % velmax) + velmin;
return i;
}
//
// Draw polygon on desktop.
//
void poly()
{
< a name = "x1721" > < / a > < a href = "tqwidget.html" > TQWidget< / a > *d = TQApplication::< a href = "tqapplication.html#desktop" > desktop< / a > ();
< a name = "x1761" > < / a > d-> < a href = "tqwidget.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 = "tqpointarray.html" > TQPointArray< / a > *a = new < a href = "tqpointarray.html" > TQPointArray< / a > [ maxcurves ];
TQPointArray *p;
< a name = "x1760" > < / a > < a href = "tqrect.html" > TQRect< / a > r = d-> < a href = "tqwidget.html#rect" > rect< / a > (); // desktop rectangle
int i;
for ( i=0; i< maxcurves; i++ )
a[i].resize( maxpoints );
p = & a[0];
for ( i=0; i< maxpoints; i++ ) { // setup first polygon points
< a name = "x1756" > < / a > p-> setPoint( i, (kindaRand()& 0x7fff) % r.< a href = "tqrect.html#width" > width< / a > (),
< a name = "x1749" > < / a > (kindaRand()& 0x7fff) % r.< a href = "tqrect.html#height" > height< / a > () );
xvel[i] = velocity(i);
yvel[i] = velocity(i);
}
< a href = "tqpainter.html" > TQPainter< / a > paint;
< a name = "x1733" > < / a > paint.< a href = "tqpainter.html#begin" > begin< / a > ( d ); // start painting desktop
for ( int ntimes=0; ntimes< 2000; ntimes++ ) {
paint.< a href = "tqpainter.html#setBrush" > setBrush< / a > ( TQColor(kindaRand()%360, 180, 255, TQColor::Hsv) );
< a name = "x1735" > < / a > paint.< a href = "tqpainter.html#drawPolygon" > drawPolygon< / a > ( a[head] );
if ( ++tail > = maxcurves )
tail = 0;
< a name = "x1751" > < / a > < a name = "x1750" > < / a > int minx=r.< a href = "tqrect.html#left" > left< / a > (), maxx=r.< a href = "tqrect.html#right" > right< / a > ();
< a name = "x1755" > < / a > < a name = "x1748" > < / a > int miny=r.< a href = "tqrect.html#top" > top< / a > (), maxy=r.< a href = "tqrect.html#bottom" > bottom< / a > ();
int x, y;
p = & a[head];
if ( ++head > = maxcurves )
head = 0;
for ( i=0; i< maxpoints; i++ ) { // calc new curve
p-> point( i, & x, & y );
x += xvel[i];
y += yvel[i];
if ( x > = maxx ) {
x = maxx - (x - maxx + 1);
xvel[i] = -velocity(i);
}
if ( x < = minx ) {
x = minx + (minx - x + 1);
xvel[i] = velocity(i);
}
if ( y > = maxy ) {
y = maxy - (y - maxy + 1);
yvel[i] = -velocity(i);
}
if ( y < = miny ) {
y = miny + (miny - y + 1);
yvel[i] = velocity(i);
}
a[head].setPoint( i, x, y );
}
}
< a name = "x1737" > < / a > paint.< a href = "tqpainter.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 = "tqimage.html" > TQImage< / a > image( w, h, 8, 128 ); // create image
for ( i=0; i< 128; i++ ) // build color table
< a name = "x1730" > < / a > image.< a href = "tqimage.html#setColor" > setColor< / a > ( i, tqRgb(i,0,0) );
for ( int y=0; y< h; y++ ) { // set image pixels
< a name = "x1729" > < / a > uchar *p = image.< a href = "tqimage.html#scanLine" > scanLine< / a > (y);
for ( int x=0; x< w; x++ )
*p++ = (x+y)%128;
}
< a href = "tqpixmap.html" > TQPixmap< / a > pm;
pm = image; // convert image to pixmap
< a name = "x1745" > < / a > pm.< a href = "tqpixmap.html#setOptimization" > setOptimization< / a > ( TQPixmap::BestOptim ); // rotation will be faster
< a href = "tqwidget.html" > TQWidget< / a > *d = TQApplication::< a href = "tqapplication.html#desktop" > desktop< / a > (); // w = desktop widget
for ( i=0; i< =360; i += 2 ) {
< a href = "tqwmatrix.html" > TQWMatrix< / a > m;
< a name = "x1764" > < / a > m.< a href = "tqwmatrix.html#rotate" > rotate< / a > ( i ); // rotate coordinate system
< a name = "x1747" > < / a > < a href = "tqpixmap.html" > TQPixmap< / a > rpm = pm.< a href = "tqpixmap.html#xForm" > xForm< / a > ( m ); // rpm = rotated pixmap
< a name = "x1762" > < / a > d-> < a href = "tqwidget.html#setBackgroundPixmap" > setBackgroundPixmap< / a > ( rpm ); // set desktop pixmap
< a name = "x1763" > < / a > d-> < a href = "tqwidget.html#update" > update< / a > (); // repaint desktop
}
}
//
// Generates a marble-like pattern in pm.
//
void generateStone( < a href = "tqpixmap.html" > TQPixmap< / a > *pm,
const < a href = "tqcolor.html" > TQColor< / a > & c1, const < a href = "tqcolor.html" > TQColor< / a > & c2, const < a href = "tqcolor.html" > TQColor< / a > & c3 )
{
< a href = "tqpainter.html" > TQPainter< / a > p;
< a href = "tqpen.html" > TQPen< / a > p1 ( c1, 0 );
< a href = "tqpen.html" > TQPen< / a > p2 ( c2, 0 );
< a href = "tqpen.html" > TQPen< / a > p3 ( c3, 0 );
p.< a href = "tqpainter.html#begin" > begin< / a > ( pm );
< a name = "x1746" > < / a > for( int i = 0 ; i < pm-> < a href = "tqpixmap.html#width" > width< / a > () ; i++ )
< a name = "x1743" > < / a > for( int j = 0 ; j < pm-> < a href = "tqpixmap.html#height" > height< / a > () ; j++ ) {
int r = kindaRand();
if ( r < KINDA_RAND_MAX / 3 )
< a name = "x1741" > < / a > p.< a href = "tqpainter.html#setPen" > setPen< / a > ( p1 );
else if ( r < KINDA_RAND_MAX / 3 * 2 )
p.< a href = "tqpainter.html#setPen" > setPen< / a > ( p2 );
else
p.< a href = "tqpainter.html#setPen" > setPen< / a > ( p3 );
< a name = "x1734" > < / a > p.< a href = "tqpainter.html#drawPoint" > drawPoint< / a > ( i,j );
}
p.< a href = "tqpainter.html#end" > end< / a > ();
}
void drawShadeText( < a href = "tqpainter.html" > TQPainter< / a > *p, int x, int y, const char *text,
const < a href = "tqcolor.html" > TQColor< / a > & topColor, const < a href = "tqcolor.html" > TQColor< / a > & bottomColor,
int sw = 2 )
{
< a name = "x1739" > < / a > if ( !p-> < a href = "tqpainter.html#isActive" > isActive< / a > () )
return;
p-> < a href = "tqpainter.html#setPen" > setPen< / a > ( bottomColor );
< a name = "x1736" > < / a > p-> < a href = "tqpainter.html#drawText" > drawText< / a > ( x+sw, y+sw, text );
p-> < a href = "tqpainter.html#setPen" > setPen< / a > ( topColor );
p-> < a href = "tqpainter.html#drawText" > drawText< / a > ( x, y, text );
}
// NOTE: desktop drag/drop is experimental
class DesktopWidget : public < a href = "tqwidget.html" > TQWidget< / a > , private TQDropSite
{
public:
DesktopWidget( const char *s, TQWidget *parent=0, const char *name=0 );
~DesktopWidget();
void paintEvent( < a href = "tqpaintevent.html" > TQPaintEvent< / a > * );
void dragEnterEvent( < a href = "tqdragenterevent.html" > TQDragEnterEvent< / a > *e )
{
< a name = "x1731" > < / a > if ( TQImageDrag::< a href = "tqimagedrag.html#canDecode" > canDecode< / a > (e) )
< a name = "x1727" > < / a > e-> < a href = "tqdragmoveevent.html#accept" > accept< / a > ();
}
void dragLeaveEvent( < a href = "tqdragleaveevent.html" > TQDragLeaveEvent< / a > * )
{
}
void dragMoveEvent( < a href = "tqdragmoveevent.html" > TQDragMoveEvent< / a > *e )
{
e-> < a href = "tqdragmoveevent.html#accept" > accept< / a > ();
}
void dropEvent( < a href = "tqdropevent.html" > TQDropEvent< / a > * e )
{
< a href = "tqpixmap.html" > TQPixmap< / a > pmp;
< a name = "x1732" > < / a > if ( TQImageDrag::< a href = "tqimagedrag.html#decode" > decode< / a > ( e, pmp ) ) {
< a href = "tqwidget.html#setBackgroundPixmap" > setBackgroundPixmap< / a > ( pmp );
< a href = "tqwidget.html#update" > update< / a > ();
}
}
private:
< a href = "tqpixmap.html" > TQPixmap< / a > *pm;
< a href = "tqstring.html" > TQString< / a > text;
};
< a name = "f483" > < / a > DesktopWidget::DesktopWidget( const char *s, TQWidget *parent, const char *name )
: < a href = "tqwidget.html" > TQWidget< / a > ( parent, name, WType_Desktop | WPaintDesktop),
TQDropSite(this)
{
text = s;
pm = 0;
}
DesktopWidget::~DesktopWidget()
{
delete pm;
}
void DesktopWidget::< a href = "tqwidget.html#paintEvent" > paintEvent< / a > ( < a href = "tqpaintevent.html" > TQPaintEvent< / a > * )
{
< a href = "tqcolor.html" > TQColor< / a > c1 = < a href = "tqwidget.html#backgroundColor" > backgroundColor< / a > ();
< a name = "x1726" > < / a > < a href = "tqcolor.html" > TQColor< / a > c2 = c1.< a href = "tqcolor.html#light" > light< / a > (104);
< a name = "x1725" > < / a > < a href = "tqcolor.html" > TQColor< / a > c3 = c1.< a href = "tqcolor.html#dark" > dark< / a > (106);
if ( !pm ) {
pm = new < a href = "tqpixmap.html" > TQPixmap< / a > ( 64, 64 );
generateStone( pm, c1, c2, c3 );
< a href = "tqwidget.html#setBackgroundPixmap" > setBackgroundPixmap< / a > ( *pm );
< a href = "tqwidget.html#update" > update< / a > ();
}
< a href = "tqrect.html" > TQRect< / a > br = < a href = "tqwidget.html#fontMetrics" > fontMetrics< / a > ().boundingRect( text );
< a href = "tqpixmap.html" > TQPixmap< / a > offscreen( br.< a href = "tqrect.html#width" > width< / a > (), br.< a href = "tqrect.html#height" > height< / a > () );
int x = < a href = "tqwidget.html#width" > width< / a > ()/2 - br.< a href = "tqrect.html#width" > width< / a > ()/2;
int y = < a href = "tqwidget.html#height" > height< / a > ()/2 - br.< a href = "tqrect.html#height" > height< / a > ()/2;
< a name = "x1742" > < / a > offscreen.< a href = "tqpixmap.html#fill" > fill< / a > ( this, x, y );
< a href = "tqpainter.html" > TQPainter< / a > p;
p.< a href = "tqpainter.html#begin" > begin< / a > ( & offscreen );
< a name = "x1758" > < / a > < a name = "x1757" > < / a > drawShadeText( & p, -br.< a href = "tqrect.html#x" > x< / a > (), -br.< a href = "tqrect.html#y" > y< / a > (), text, c2, c3, 3 );
p.< a href = "tqpainter.html#end" > end< / a > ();
< a href = "tqimage.html#bitBlt" > bitBlt< / a > ( this, x, y, & offscreen );
}
void desktopWidget( const char *s = "Trolltech" )
{
DesktopWidget *t = new DesktopWidget(s);
t-> < a href = "tqwidget.html#update" > update< / a > ();
tqApp-> < a href = "tqapplication.html#exec" > exec< / a > ();
delete t;
}
void desktopText( const char *s = "Trolltech" )
{
const int border = 20;
< a name = "x1723" > < / a > < a href = "tqcolor.html" > TQColor< / a > c1 = tqApp-> < a href = "tqapplication.html#palette" > palette< / a > ().inactive().background();
< a href = "tqcolor.html" > TQColor< / a > c2 = c1.< a href = "tqcolor.html#light" > light< / a > (104);
< a href = "tqcolor.html" > TQColor< / a > c3 = c1.< a href = "tqcolor.html#dark" > dark< / a > (106);
< a href = "tqpixmap.html" > TQPixmap< / a > pm(10,10);
< a href = "tqpainter.html" > TQPainter< / a > p;
p.< a href = "tqpainter.html#begin" > begin< / a > ( & pm );
< a name = "x1738" > < / a > < a href = "tqrect.html" > TQRect< / a > r = p.< a href = "tqpainter.html#fontMetrics" > fontMetrics< / a > ().boundingRect( s );
p.< a href = "tqpainter.html#end" > end< / a > ();
int appWidth = tqApp-> < a href = "tqapplication.html#desktop" > desktop< / a > ()-> width();
int appHeight = tqApp-> < a href = "tqapplication.html#desktop" > desktop< / a > ()-> height();
if ( r.< a href = "tqrect.html#width" > width< / a > () > appWidth - border*2 )
< a name = "x1753" > < / a > r.< a href = "tqrect.html#setWidth" > setWidth< / a > ( appWidth - border*2 );
if ( r.< a href = "tqrect.html#height" > height< / a > () > appHeight - border*2 )
< a name = "x1752" > < / a > r.< a href = "tqrect.html#setHeight" > setHeight< / a > ( appHeight - border*2 );
< a name = "x1754" > < / a > < a name = "x1744" > < / a > pm.< a href = "tqpixmap.html#resize" > resize< / a > ( r.< a href = "tqrect.html#size" > size< / a > () + TQSize( border*2, border*2 ) );
generateStone( & pm, c1, c2, c3 );
p.< a href = "tqpainter.html#begin" > begin< / a > ( & pm );
drawShadeText( & p, -r.< a href = "tqrect.html#x" > x< / a > () + border, -r.< a href = "tqrect.html#y" > y< / a > () + border, s, c2, c3 );
p.< a href = "tqpainter.html#end" > end< / a > ();
tqApp-> < a href = "tqapplication.html#desktop" > desktop< / a > ()-> setBackgroundPixmap( pm );
}
//
// The program starts here.
//
int main( int argc, char **argv )
{
< a href = "tqapplication.html" > TQApplication< / a > app( argc, argv );
if ( argc > 1 ) {
< a href = "tqfont.html" > TQFont< / a > f( "charter", 96, TQFont::Black );
< a name = "x1728" > < / a > f.< a href = "tqfont.html#setStyleHint" > setStyleHint< / a > ( TQFont::Times );
< a name = "x1724" > < / a > app.< a href = "tqapplication.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 < text> "
"\n\tdesktop -shadewidget < text> \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 © 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 >