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

  1. /****************************************************************************
  2. ** $Id:  qt/picture.cpp   3.0.0   edited Sep 25 18:45 $
  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 <qapplication.h>
  12. #include <qpainter.h>
  13. #include <qpicture.h>
  14. #include <qpixmap.h>
  15. #include <qwidget.h>
  16. #include <qmessagebox.h>
  17. #include <qfile.h>
  18. #include <ctype.h>
  19.  
  20.  
  21. void paintCar( QPainter *p )            // paint a car
  22. {
  23.     QPointArray a;
  24.     QBrush brush( Qt::yellow, Qt::SolidPattern );
  25.     p->setBrush( brush );            // use solid, yellow brush
  26.  
  27.     a.setPoints( 5, 50,50, 350,50, 450,120, 450,250, 50,250 );
  28.     p->drawPolygon( a );            // draw car body
  29.  
  30.     QFont f( "courier", 12, QFont::Bold );
  31.     p->setFont( f );
  32.  
  33.     QColor windowColor( 120, 120, 255 );    // a light blue color
  34.     brush.setColor( windowColor );        // set this brush color
  35.     p->setBrush( brush );            // set brush
  36.     p->drawRect( 80, 80, 250, 70 );        // car window
  37.     p->drawText( 180, 80, 150, 70, Qt::AlignCenter, "--  Qt  --\nTrolltech AS" );
  38.  
  39.     QPixmap pixmap;
  40.     if ( pixmap.load("flag.bmp") )        // load and draw image
  41.     p->drawPixmap( 100, 85, pixmap );
  42.  
  43.     p->setBackgroundMode( Qt::OpaqueMode );    // set opaque mode
  44.     p->setBrush( Qt::DiagCrossPattern );    // black diagonal cross pattern
  45.     p->drawEllipse( 90, 210, 80, 80 );        // back wheel
  46.     p->setBrush( Qt::CrossPattern );        // black cross fill pattern
  47.     p->drawEllipse( 310, 210, 80, 80 );        // front wheel
  48. }
  49.  
  50.  
  51. class PictureDisplay : public QWidget        // picture display widget
  52. {
  53. public:
  54.     PictureDisplay( const char *fileName );
  55.    ~PictureDisplay();
  56. protected:
  57.     void    paintEvent( QPaintEvent * );
  58.     void    keyPressEvent( QKeyEvent * );
  59. private:
  60.     QPicture   *pict;
  61.     QString    name;
  62. };
  63.  
  64. PictureDisplay::PictureDisplay( const char *fileName )
  65. {
  66.     pict = new QPicture;
  67.     name = fileName;
  68.     if ( !pict->load(fileName) ) {        // cannot load picture
  69.     delete pict;
  70.     pict = 0;
  71.     name.sprintf( "Not able to load picture: %s", fileName );
  72.     }
  73. }
  74.  
  75. PictureDisplay::~PictureDisplay()
  76. {
  77.     delete pict;
  78. }
  79.  
  80. void PictureDisplay::paintEvent( QPaintEvent * )
  81. {
  82.     QPainter paint( this );            // paint widget
  83.     if ( pict )
  84.         paint.drawPicture( *pict );        // draw picture
  85.     else
  86.     paint.drawText( rect(), AlignCenter, name );
  87. }
  88.  
  89. void PictureDisplay::keyPressEvent( QKeyEvent *k )
  90. {
  91.     switch ( tolower(k->ascii()) ) {
  92.     case 'r':                // reload
  93.         pict->load( name );
  94.         update();
  95.         break;
  96.     case 'q':                // quit
  97.         QApplication::exit();
  98.         break;
  99.     }
  100. }
  101.  
  102.  
  103. int main( int argc, char **argv )
  104. {
  105.     QApplication a( argc, argv );        // QApplication required!
  106.  
  107.     const char *fileName = "car.pic";        // default picture file name
  108.  
  109.     if ( argc == 2 )                // use argument as file name
  110.     fileName = argv[1];
  111.  
  112.     if ( !QFile::exists(fileName) ) {
  113.     QPicture pict;                // our picture
  114.     QPainter paint;                // our painter
  115.  
  116.     paint.begin( &pict );            // begin painting onto picture
  117.     paintCar( &paint );            // paint!
  118.     paint.end();                // painting done
  119.  
  120.     pict.save( fileName );            // save picture
  121.     QMessageBox::information(0, "Qt Example - Picture", "Saved.  Run me again!");
  122.     return 0;
  123.     } else {
  124.     PictureDisplay test( fileName );    // create picture display
  125.     a.setMainWidget( &test);        // set main widget
  126.     test.setCaption("Qt Example - Picture");
  127.     test.show();                // show it
  128.  
  129.     return a.exec();            // start event loop
  130.     }
  131. }
  132.