home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / desktop / desktop.cpp next >
C/C++ Source or Header  |  2001-10-11  |  8KB  |  349 lines

  1. /****************************************************************************
  2. ** $Id:  qt/desktop.cpp   3.0.0   edited Jul 30 22:54 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include <qimage.h>
  12. #include <qbitmap.h>
  13. #include <qpainter.h>
  14. #include <qapplication.h>
  15. #include <qdropsite.h>
  16. #include <qdragobject.h>
  17. #include <stdio.h>
  18.  
  19.  
  20. static double seed = 0.353535353535;
  21. static const int KINDA_RAND_MAX = 32767;
  22.  
  23. static int kindaRand()
  24. {
  25.     seed = seed*147;
  26.     seed = seed - (double) ((int) seed);
  27.     return (int) ( seed*(KINDA_RAND_MAX + 1) );
  28. }
  29.  
  30. static int velocity( int i )            // change velocity
  31. {
  32.     const int velmax = 15;
  33.     const int velmin = 4;
  34.     if ( i == 1 || i == 2 )
  35.     i = (kindaRand()&0x7fff % velmax)/3 + velmin;
  36.     else
  37.     i = (kindaRand()&0x7fff % velmax) + velmin;
  38.     return i;
  39. }
  40.  
  41. //
  42. // Draw polygon on desktop.
  43. //
  44.  
  45. void poly()
  46. {
  47.     QWidget *d = QApplication::desktop();
  48.     d->setBackgroundColor( Qt::white );        // white desktop
  49.  
  50.     const int maxpoints = 5;
  51.     const int maxcurves = 8;
  52.     static int xvel[maxpoints];
  53.     static int yvel[maxpoints];
  54.     int head = 0;
  55.     int tail = -maxcurves + 2;
  56.     QPointArray *a = new QPointArray[ maxcurves ];
  57.     register QPointArray *p;
  58.     QRect r = d->rect();            // desktop rectangle
  59.  
  60.     int i;
  61.     for ( i=0; i<maxcurves; i++ )
  62.     a[i].resize( maxpoints );
  63.     p = &a[0];
  64.     for ( i=0; i<maxpoints; i++ ) {        // setup first polygon points
  65.     p->setPoint( i, (kindaRand()&0x7fff) % r.width(),
  66.             (kindaRand()&0x7fff) % r.height() );
  67.     xvel[i] = velocity(i);
  68.     yvel[i] = velocity(i);
  69.     }
  70.  
  71.     QPainter paint;
  72.     paint.begin( d );                // start painting desktop
  73.  
  74.     for ( int ntimes=0; ntimes<2000; ntimes++ ) {
  75.     paint.setBrush( QColor(kindaRand()%360, 180, 255, QColor::Hsv) );
  76.     paint.drawPolygon( a[head] );
  77.     if ( ++tail >= maxcurves )
  78.         tail = 0;
  79.  
  80.     int minx=r.left(), maxx=r.right();
  81.     int miny=r.top(),  maxy=r.bottom();
  82.     int x, y;
  83.     p = &a[head];
  84.     if ( ++head >= maxcurves )
  85.         head = 0;
  86.     for ( i=0; i<maxpoints; i++ ) {        // calc new curve
  87.         p->point( i, &x, &y );
  88.         x += xvel[i];
  89.         y += yvel[i];
  90.         if ( x >= maxx ) {
  91.         x = maxx - (x - maxx + 1);
  92.         xvel[i] = -velocity(i);
  93.         }
  94.         if ( x <= minx ) {
  95.         x = minx + (minx - x + 1);
  96.         xvel[i] = velocity(i);
  97.         }
  98.         if ( y >= maxy ) {
  99.         y = maxy - (y - maxy + 1);
  100.         yvel[i] = -velocity(i);
  101.         }
  102.         if ( y <= miny ) {
  103.         y = miny + (miny - y + 1);
  104.         yvel[i] = velocity(i);
  105.         }
  106.         a[head].setPoint( i, x, y );
  107.     }
  108.     }
  109.     paint.end();                // painting done
  110.     delete[] a;
  111. }
  112.  
  113.  
  114. //
  115. // Rotate pattern on desktop.
  116. //
  117.  
  118. void rotate()
  119. {
  120.     int i;
  121.     const int w = 64;
  122.     const int h = 64;
  123.     QImage image( w, h, 8, 128 );        // create image
  124.     for ( i=0; i<128; i++ )            // build color table
  125.     image.setColor( i, qRgb(i,0,0) );
  126.     for ( int y=0; y<h; y++ ) {            // set image pixels
  127.     uchar *p = image.scanLine(y);
  128.     for ( int x=0; x<w; x++ )
  129.         *p++ = (x+y)%128;
  130.     }
  131.  
  132.     QPixmap pm;
  133.     pm = image;                    // convert image to pixmap
  134.     pm.setOptimization( QPixmap::BestOptim );    // rotation will be faster
  135.  
  136.     QWidget *d = QApplication::desktop();    // w = desktop widget
  137.  
  138.     for ( i=0; i<=360; i += 2 ) {
  139.     QWMatrix m;
  140.     m.rotate( i );                // rotate coordinate system
  141.     QPixmap rpm = pm.xForm( m );        // rpm = rotated pixmap
  142.     d->setBackgroundPixmap( rpm );        // set desktop pixmap
  143.     d->update();                // repaint desktop
  144.     }
  145. }
  146.  
  147. //
  148. // Generates a marble-like pattern in pm.
  149. //
  150.  
  151. void generateStone( QPixmap *pm,
  152.             const QColor &c1, const QColor &c2, const QColor &c3 )
  153. {
  154.     QPainter p;
  155.     QPen p1 ( c1, 0 );
  156.     QPen p2 ( c2, 0 );
  157.     QPen p3 ( c3, 0 );
  158.  
  159.     p.begin( pm );
  160.     for( int i = 0 ; i < pm->width() ; i++ )
  161.     for( int j = 0 ; j < pm->height() ; j++ ) {
  162.         int r = kindaRand();
  163.         if ( r < KINDA_RAND_MAX / 3 )
  164.         p.setPen( p1 );
  165.         else if ( r < KINDA_RAND_MAX / 3 * 2 )
  166.         p.setPen( p2 );
  167.         else
  168.         p.setPen( p3 );
  169.         p.drawPoint( i,j );
  170.     }
  171.     p.end();
  172. }
  173.  
  174. void drawShadeText( QPainter *p, int x, int y, const char *text,
  175.             const QColor &topColor, const QColor &bottomColor,
  176.             int sw = 2 )
  177. {
  178.     if ( !p->isActive() )
  179.     return;
  180.  
  181.     p->setPen( bottomColor );
  182.     p->drawText( x+sw, y+sw, text );
  183.     p->setPen( topColor );
  184.     p->drawText( x, y, text );
  185. }
  186.  
  187. // NOTE: desktop drag/drop is experimental
  188.  
  189. class DesktopWidget : public QWidget, private QDropSite
  190. {
  191. public:
  192.     DesktopWidget( const char *s, QWidget *parent=0, const char *name=0 );
  193.    ~DesktopWidget();
  194.     void paintEvent( QPaintEvent * );
  195.  
  196.     void dragEnterEvent( QDragEnterEvent *e )
  197.     {
  198.     if ( QImageDrag::canDecode(e) )
  199.         e->accept();
  200.     }
  201.  
  202.     void dragLeaveEvent( QDragLeaveEvent * )
  203.     {
  204.     }
  205.  
  206.     void dragMoveEvent( QDragMoveEvent *e )
  207.     {
  208.     e->accept();
  209.     }
  210.  
  211.     void dropEvent( QDropEvent * e )
  212.     {
  213.     QPixmap pmp;
  214.     if ( QImageDrag::decode( e, pmp ) ) {
  215.         setBackgroundPixmap( pmp );
  216.         update();
  217.     }
  218.     }
  219.  
  220. private:
  221.     QPixmap *pm;
  222.     QString text;
  223. };
  224.  
  225. DesktopWidget::DesktopWidget( const char *s, QWidget *parent, const char *name )
  226.     : QWidget( parent, name, WType_Desktop | WPaintDesktop),
  227.     QDropSite(this)
  228. {
  229.     text = s;
  230.     pm     = 0;
  231. }
  232.  
  233. DesktopWidget::~DesktopWidget()
  234. {
  235.     delete pm;
  236. }
  237.  
  238. void DesktopWidget::paintEvent( QPaintEvent * )
  239. {
  240.     QColor c1 = backgroundColor();
  241.     QColor c2 = c1.light(104);
  242.     QColor c3 = c1.dark(106);
  243.     if ( !pm ) {
  244.     pm = new QPixmap( 64, 64 );
  245.     generateStone( pm, c1, c2, c3 );
  246.     setBackgroundPixmap( *pm );
  247.     update();
  248.     }
  249.     QRect br = fontMetrics().boundingRect( text );
  250.     QPixmap offscreen( br.width(), br.height() );
  251.     int x = width()/2  - br.width()/2;
  252.     int y = height()/2 - br.height()/2;
  253.     offscreen.fill( this, x, y );
  254.     QPainter p;
  255.     p.begin( &offscreen );
  256.     drawShadeText( &p, -br.x(), -br.y(), text, c2, c3, 3 );
  257.     p.end();
  258.     bitBlt( this, x, y, &offscreen );
  259. }
  260.  
  261. void desktopWidget( const char *s = "Trolltech" )
  262. {
  263.     DesktopWidget *t = new DesktopWidget(s);
  264.     t->update();
  265.     qApp->exec();
  266.     delete t;
  267. }
  268.  
  269. void desktopText( const char *s = "Trolltech" )
  270. {
  271.     const int border = 20;
  272.  
  273.     QColor c1 =     qApp->palette().inactive().background();
  274.     QColor c2 = c1.light(104);
  275.     QColor c3 = c1.dark(106);
  276.  
  277.     QPixmap pm(10,10);
  278.  
  279.     QPainter p;
  280.     p.begin( &pm );
  281.     QRect r = p.fontMetrics().boundingRect( s );
  282.     p.end();
  283.  
  284.     int appWidth  =  qApp->desktop()->width();
  285.     int appHeight =  qApp->desktop()->height();
  286.     if ( r.width() > appWidth - border*2 )
  287.     r.setWidth( appWidth - border*2 );
  288.     if ( r.height() > appHeight - border*2 )
  289.     r.setHeight( appHeight - border*2 );
  290.  
  291.     pm.resize( r.size() + QSize( border*2, border*2 ) );
  292.     generateStone( &pm, c1, c2, c3 );
  293.     p.begin( &pm );
  294.     drawShadeText( &p, -r.x() + border, -r.y() + border, s, c2, c3 );
  295.     p.end();
  296.  
  297.     qApp->desktop()->setBackgroundPixmap( pm );
  298. }
  299.  
  300. //
  301. // The program starts here.
  302. //
  303.  
  304. int main( int argc, char **argv )
  305. {
  306.     QApplication app( argc, argv );
  307.  
  308.     if ( argc > 1 ) {
  309.     QFont f( "charter", 96, QFont::Black );
  310.     f.setStyleHint( QFont::Times );
  311.     app.setFont( f );
  312.     }
  313.  
  314.     bool validOptions = FALSE;
  315.  
  316.     if ( argc == 2 ) {
  317.     validOptions = TRUE;
  318.     if ( strcmp(argv[1],"-poly") == 0 )
  319.         poly();
  320.     else if ( strcmp(argv[1],"-rotate") == 0 )
  321.         rotate();
  322.     else if ( strcmp(argv[1],"-troll") == 0 )
  323.         desktopText();
  324.     else if ( strcmp(argv[1],"-trollwidget") == 0 )
  325.         desktopWidget();
  326.     else
  327.         validOptions = FALSE;
  328.     }
  329.     if ( argc == 3 ) {
  330.     validOptions = TRUE;
  331.     if ( strcmp(argv[1],"-shadetext") == 0 )
  332.         desktopText( argv[2] );
  333.     else if ( strcmp(argv[1],"-shadewidget") == 0 )
  334.         desktopWidget( argv[2] );
  335.     else
  336.         validOptions = FALSE;
  337.     }
  338.     if ( !validOptions ) {
  339.     fprintf( stderr, "Usage:\n\tdesktop -poly"
  340.                    "\n\tdesktop -rotate"
  341.                    "\n\tdesktop -troll"
  342.                    "\n\tdesktop -trollwidget"
  343.                    "\n\tdesktop -shadetext <text>"
  344.                    "\n\tdesktop -shadewidget <text>\n" );
  345.     rotate();
  346.     }
  347.     return 0;
  348. }
  349.