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

  1. /****************************************************************************
  2. ** $Id:  qt/main.cpp   3.0.0   edited Jul 19 11:29 $
  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 <qfiledialog.h>
  13. #include <qlabel.h>
  14. #include <qpainter.h>
  15. #include <qmessagebox.h>
  16. #include <qmovie.h>
  17.  
  18.  
  19. class MovieScreen : public QFrame {
  20.     Q_OBJECT
  21.     QMovie movie;
  22.     QString filename;
  23.  
  24. public:
  25.     MovieScreen(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
  26.         QFrame(p, name, f)
  27.     {
  28.         setCaption(fname);
  29.         filename = fname;
  30.         movie = m;
  31.  
  32.         // Set a frame around the movie.
  33.         setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
  34.  
  35.         // No background needed, since we draw on the whole widget.
  36.         movie.setBackgroundColor(backgroundColor());
  37.         setBackgroundMode(NoBackground);
  38.  
  39.         // Get the movie to tell use when interesting things happen.
  40.         movie.connectUpdate(this, SLOT(movieUpdated(const QRect&)));
  41.         movie.connectResize(this, SLOT(movieResized(const QSize&)));
  42.         movie.connectStatus(this, SLOT(movieStatus(int)));
  43.     }
  44.  
  45.  
  46. protected:
  47.  
  48.     // Draw the contents of the QFrame - the movie and on-screen-display
  49.     void drawContents(QPainter* p)
  50.     {
  51.         // Get the current movie frame.
  52.         QPixmap pm = movie.framePixmap();
  53.  
  54.         // Get the area we have to draw in.
  55.         QRect r = contentsRect();
  56.  
  57.         // Only rescale is we need to - it can take CPU!
  58.         if ( r.size() != pm.size() ) {
  59.             QWMatrix m;
  60.             m.scale((double)r.width()/pm.width(),
  61.                     (double)r.height()/pm.height());
  62.             pm = pm.xForm(m);
  63.         }
  64.  
  65.         // Draw the [possibly scaled] frame.  movieUpdated() below calls
  66.         // repaint with only the changed area, so clipping will ensure we
  67.         // only do the minimum amount of rendering.
  68.         //
  69.         p->drawPixmap(r.x(), r.y(), pm);
  70.  
  71.  
  72.         // The on-screen display
  73.  
  74.         const char* message = 0;
  75.  
  76.         if (movie.paused()) {
  77.             message = "PAUSED";
  78.         } else if (movie.finished()) {
  79.             message = "THE END";
  80.         } else if (movie.steps() > 0) {
  81.             message = "FF >>";
  82.         }
  83.  
  84.         if (message) {
  85.             // Find a good font size...
  86.             p->setFont(QFont("Helvetica", 24));
  87.  
  88.             QFontMetrics fm = p->fontMetrics();
  89.             if ( fm.width(message) > r.width()-10 )
  90.                 p->setFont(QFont("Helvetica", 18));
  91.  
  92.             fm = p->fontMetrics();
  93.             if ( fm.width(message) > r.width()-10 )
  94.                 p->setFont(QFont("Helvetica", 14));
  95.  
  96.             fm = p->fontMetrics();
  97.             if ( fm.width(message) > r.width()-10 )
  98.                 p->setFont(QFont("Helvetica", 12));
  99.  
  100.             fm = p->fontMetrics();
  101.             if ( fm.width(message) > r.width()-10 )
  102.                 p->setFont(QFont("Helvetica", 10));
  103.  
  104.             // "Shadow" effect.
  105.             p->setPen(black);
  106.             p->drawText(1, 1, width()-1, height()-1, AlignCenter, message);
  107.             p->setPen(white);
  108.             p->drawText(0, 0, width()-1, height()-1, AlignCenter, message);
  109.         }
  110.     }
  111.  
  112.     void mouseReleaseEvent(QMouseEvent* event)
  113.     {
  114.         // Do what the Help says...
  115.  
  116.         if (event->state() & ShiftButton) {
  117.             movie.restart();
  118.         } else if (!movie.paused()) {
  119.             movie.pause();
  120.         } else {
  121.             if (event->button() & LeftButton)
  122.                 movie.step((event->state() & ControlButton) ? 10 : 1);
  123.             else if (event->button() & (MidButton|RightButton))
  124.                 movie.unpause();
  125.         }
  126.  
  127.         repaint(); // To hide/show "PAUSED".
  128.     }
  129.  
  130. private slots:
  131.     void movieUpdated(const QRect& area)
  132.     {
  133.         if (!isVisible())
  134.             show();
  135.  
  136.         // The given area of the movie has changed.
  137.  
  138.         QRect r = contentsRect();
  139.  
  140.         if ( r.size() != movie.framePixmap().size() ) {
  141.             // Need to scale - redraw whole frame.
  142.             repaint( r );
  143.         } else {
  144.             // Only redraw the changed area of the frame
  145.             repaint( area.x()+r.x(), area.y()+r.x(),
  146.                      area.width(), area.height() );
  147.         }
  148.     }
  149.  
  150.     void movieResized(const QSize& size)
  151.     {
  152.         // The movie changed size, probably from its initial zero size.
  153.  
  154.         int fw = frameWidth();
  155.         resize( size.width() + fw*2, size.height() + fw*2 );
  156.         qApp->setMainWidget(this); // Just geometry, etc.
  157.         qApp->setMainWidget(0); // Not Close==Quit
  158.     }
  159.  
  160.     void movieStatus(int status)
  161.     {
  162.         // The movie has sent us a status message.
  163.  
  164.         if (status < 0) {
  165.             QString msg;
  166.             msg.sprintf("Could not play movie \"%s\"", (const char*)filename);
  167.             QMessageBox::warning(this, "movies", msg);
  168.         } else if (status == QMovie::Paused || status == QMovie::EndOfMovie) {
  169.             repaint(); // Ensure status text is displayed
  170.         }
  171.     }
  172. };
  173.  
  174.  
  175. // A QFileDialog that chooses movies.
  176. //
  177. class MovieStarter: public QFileDialog {
  178.     Q_OBJECT
  179. public:
  180.     MovieStarter(const char *dir);
  181.  
  182. public slots:
  183.     void startMovie(const QString& filename);
  184.     // QDialog's method - normally closes the file dialog.
  185.     // We want it left open, and we want Cancel to quit everything.
  186.     void done( int r );
  187. };
  188.  
  189.  
  190. MovieStarter::MovieStarter(const char *dir)
  191.     : QFileDialog(dir, "*.gif *.mng")
  192. {
  193.     //behave as in getOpenFilename
  194.     setMode( ExistingFile );
  195.     // When a file is selected, show it as a movie.
  196.     connect(this, SIGNAL(fileSelected(const QString&)),
  197.         this, SLOT(startMovie(const QString&)));
  198. }
  199.  
  200.  
  201. void MovieStarter::startMovie(const QString& filename)
  202. {
  203.     if ( filename ) // Start a new movie - have it delete when closed.
  204.     (void)new MovieScreen( filename, QMovie(filename), 0, 0,
  205.                    WDestructiveClose);
  206. }
  207.  
  208. void MovieStarter::done( int r )
  209. {
  210.     if (r != Accepted)
  211.     qApp->quit(); // end on Cancel
  212.     setResult( r );
  213.  
  214.     // And don't hide.
  215. }
  216.  
  217.  
  218. int main(int argc, char **argv)
  219. {
  220.     QApplication a(argc, argv);
  221.  
  222.     if (argc > 1) {
  223.         // Commandline mode - show movies given on the command line
  224.         //
  225.         for (int arg=1; arg<argc; arg++)
  226.             (void)new MovieScreen(argv[arg], QMovie(argv[arg]), 0, 0,
  227.                                   Qt::WDestructiveClose);
  228.         QObject::connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));
  229.     } else {
  230.         // "GUI" mode - open a chooser for movies
  231.         //
  232.         MovieStarter* fd = new MovieStarter(".");
  233.         fd->show();
  234.  
  235.         // Some help text to explain the `hidden' features.
  236.         QLabel* help 
  237.         = new QLabel( "Choose some movies.\n\n"
  238.               "Shift-click to Restart.\n"
  239.               "Click to Pause,\n"
  240.               "then left-click to Step,\n"
  241.               "control-left-click to Step 10,\n"
  242.               "right-click to Unpause\n\n"
  243.               "Windows may be resized to enlarge movie.", 0, 0, Qt::WDestructiveClose );
  244.         help->setCaption( "Qt Examples - Movies" );
  245.         help->setIndent( 10 );
  246.         help->show();
  247.     }
  248.  
  249.     // Go!
  250.     return a.exec();
  251. }
  252.  
  253. #include "main.moc"
  254.