Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 792

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/examples/movies/movies.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Movies or the Story of the Animated GIF file</title>
<style type="text/css"><!--
h3.fn,span.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&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;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>Movies or the Story of the Animated GIF file</h1>

   
<p> 
The Movies example displays animated GIF files using the <a href="qmovie.html">QMovie</a> and
<a href="qlabel.html">QLabel</a> classes.
<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id:  qt/main.cpp   3.0.2   edited Dec 18 19:56 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
#include &lt;<a href="qmovie-h.html">qmovie.h</a>&gt;
#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;


class MovieScreen : public <a href="qframe.html">QFrame</a> {
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
    <a href="qmovie.html">QMovie</a> movie;
    <a href="qstring.html">QString</a> filename;
    <a href="qsize.html">QSize</a> sh;

public:
    MovieScreen(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
        <a href="qframe.html">QFrame</a>(p, name, f),
        sh(100,100)
    {
        setCaption(fname);
        filename = fname;
        movie = m;

        // Set a frame around the movie.
        setFrameStyle(QFrame::WinPanel|QFrame::Sunken);

        // No background needed, since we draw on the whole widget.
<a name="x1110"></a>        movie.<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>(backgroundColor());
        setBackgroundMode(NoBackground);

        // Get the movie to tell use when interesting things happen.
<a name="x1087"></a>        movie.<a href="qmovie.html#connectUpdate">connectUpdate</a>(this, SLOT(movieUpdated(const <a href="qrect.html">QRect</a>&amp;)));
<a name="x1085"></a>        movie.<a href="qmovie.html#connectResize">connectResize</a>(this, SLOT(movieResized(const <a href="qsize.html">QSize</a>&amp;)));
<a name="x1086"></a>        movie.<a href="qmovie.html#connectStatus">connectStatus</a>(this, SLOT(movieStatus(int)));

        setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
    }

    <a href="qsize.html">QSize</a> sizeHint() const
    {
        return sh;
    }

protected:

    // Draw the contents of the QFrame - the movie and on-screen-display
    void drawContents(QPainter* p)
    {
        // Get the current movie frame.
<a name="x1089"></a>        <a href="qpixmap.html">QPixmap</a> pm = movie.<a href="qmovie.html#framePixmap">framePixmap</a>();

        // Get the area we have to draw in.
        <a href="qrect.html">QRect</a> r = contentsRect();

<a name="x1098"></a>        if ( !pm.<a href="qpixmap.html#isNull">isNull</a>() ) {
            // Only rescale is we need to - it can take CPU!
<a name="x1103"></a><a name="x1099"></a>            if ( r.<a href="qrect.html#size">size</a>() != pm.<a href="qpixmap.html#size">size</a>() ) {
                <a href="qwmatrix.html">QWMatrix</a> m;
<a name="x1111"></a><a name="x1104"></a><a name="x1100"></a>                m.<a href="qwmatrix.html#scale">scale</a>((double)r.<a href="qrect.html#width">width</a>()/pm.<a href="qpixmap.html#width">width</a>(),
<a name="x1102"></a><a name="x1097"></a>                        (double)r.<a href="qrect.html#height">height</a>()/pm.<a href="qpixmap.html#height">height</a>());
<a name="x1101"></a>                pm = pm.<a href="qpixmap.html#xForm">xForm</a>(m);
            }

            // Draw the [possibly scaled] frame.  movieUpdated() below calls
            // repaint with only the changed area, so clipping will ensure we
            // only do the minimum amount of rendering.
            //
<a name="x1106"></a><a name="x1105"></a>            p-&gt;drawPixmap(r.<a href="qrect.html#x">x</a>(), r.<a href="qrect.html#y">y</a>(), pm);
        }


        // The on-screen display

        const char* message = 0;

<a name="x1091"></a>        if (movie.<a href="qmovie.html#paused">paused</a>()) {
            message = "PAUSED";
<a name="x1088"></a>        } else if (movie.<a href="qmovie.html#finished">finished</a>()) {
            message = "THE END";
<a name="x1094"></a>        } else if (movie.<a href="qmovie.html#steps">steps</a>() &gt; 0) {
            message = "FF &gt;&gt;";
        }

        if (message) {
            // Find a good font size...
            p-&gt;setFont(QFont("Helvetica", 24));

            <a href="qfontmetrics.html">QFontMetrics</a> fm = p-&gt;fontMetrics();
<a name="x1083"></a>            if ( fm.<a href="qfontmetrics.html#width">width</a>(message) &gt; r.<a href="qrect.html#width">width</a>()-10 )
                p-&gt;setFont(QFont("Helvetica", 18));

            fm = p-&gt;fontMetrics();
            if ( fm.<a href="qfontmetrics.html#width">width</a>(message) &gt; r.<a href="qrect.html#width">width</a>()-10 )
                p-&gt;setFont(QFont("Helvetica", 14));

            fm = p-&gt;fontMetrics();
            if ( fm.<a href="qfontmetrics.html#width">width</a>(message) &gt; r.<a href="qrect.html#width">width</a>()-10 )
                p-&gt;setFont(QFont("Helvetica", 12));

            fm = p-&gt;fontMetrics();
            if ( fm.<a href="qfontmetrics.html#width">width</a>(message) &gt; r.<a href="qrect.html#width">width</a>()-10 )
                p-&gt;setFont(QFont("Helvetica", 10));

            // "Shadow" effect.
            p-&gt;setPen(black);
            p-&gt;drawText(1, 1, width()-1, height()-1, AlignCenter, message);
            p-&gt;setPen(white);
            p-&gt;drawText(0, 0, width()-1, height()-1, AlignCenter, message);
        }
    }

public slots:
    void restart()
    {
<a name="x1092"></a>        movie.<a href="qmovie.html#restart">restart</a>();
        repaint();
    }

    void togglePause()
    {
        if ( movie.<a href="qmovie.html#paused">paused</a>() )
<a name="x1095"></a>            movie.<a href="qmovie.html#unpause">unpause</a>();
        else
<a name="x1090"></a>            movie.<a href="qmovie.html#pause">pause</a>();
        repaint();
    }

    void step()
    {
<a name="x1093"></a>        movie.<a href="qmovie.html#step">step</a>();
        repaint();
    }

    void step10()
    {
        movie.<a href="qmovie.html#step">step</a>(10);
        repaint();
    }

private slots:
    void movieUpdated(const <a href="qrect.html">QRect</a>&amp; area)
    {
        if (!isVisible())
            show();

        // The given area of the movie has changed.

        <a href="qrect.html">QRect</a> r = contentsRect();

        if ( r.<a href="qrect.html#size">size</a>() != movie.<a href="qmovie.html#framePixmap">framePixmap</a>().size() ) {
            // Need to scale - redraw whole frame.
            repaint( r );
        } else {
            // Only redraw the changed area of the frame
            repaint( area.<a href="qrect.html#x">x</a>()+r.<a href="qrect.html#x">x</a>(), area.<a href="qrect.html#y">y</a>()+r.<a href="qrect.html#x">x</a>(),
                     area.<a href="qrect.html#width">width</a>(), area.<a href="qrect.html#height">height</a>() );
        }
    }

    void movieResized(const <a href="qsize.html">QSize</a>&amp; size)
    {
        // The movie changed size, probably from its initial zero size.

        int fw = frameWidth();
<a name="x1108"></a><a name="x1107"></a>        sh = QSize( size.<a href="qsize.html#width">width</a>() + fw*2, size.<a href="qsize.html#height">height</a>() + fw*2 );
        <a href="qwidget.html#updateGeometry">updateGeometry</a>();
        if ( <a href="qwidget.html#parentWidget">parentWidget</a>() &amp;&amp; parentWidget()-&gt;isHidden() )
            <a href="qwidget.html#parentWidget">parentWidget</a>()-&gt;show();
    }

    void movieStatus(int status)
    {
        // The movie has sent us a status message.

        if (status &lt; 0) {
            <a href="qstring.html">QString</a> msg;
            msg.<a href="qstring.html#sprintf">sprintf</a>("Could not play movie \"%s\"", (const char*)filename);
<a name="x1084"></a>            QMessageBox::<a href="qmessagebox.html#warning">warning</a>(this, "movies", msg);
            <a href="qwidget.html#parentWidget">parentWidget</a>()-&gt;close();
        } else if (status == QMovie::Paused || status == QMovie::EndOfMovie) {
            <a href="qwidget.html#repaint">repaint</a>(); // Ensure status text is displayed
        }
    }
};

class MoviePlayer : public <a href="qvbox.html">QVBox</a> {
    MovieScreen* movie;
public:
    MoviePlayer(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
        <a href="qvbox.html">QVBox</a>(p,name,f)
    {
        movie = new MovieScreen(fname, m, this);
        <a href="qhbox.html">QHBox</a>* hb = new <a href="qhbox.html">QHBox</a>(this);
        <a href="qpushbutton.html">QPushButton</a>* btn;
        btn = new <a href="qpushbutton.html">QPushButton</a>("&lt;&lt;", hb);
        <a href="qobject.html#connect">connect</a>(btn, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), movie, SLOT(<a href="qmovie.html#restart">restart</a>()));
        btn = new <a href="qpushbutton.html">QPushButton</a>("||", hb);
        <a href="qobject.html#connect">connect</a>(btn, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), movie, SLOT(togglePause()));
        btn = new <a href="qpushbutton.html">QPushButton</a>("&gt;|", hb);
        <a href="qobject.html#connect">connect</a>(btn, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), movie, SLOT(<a href="qmovie.html#step">step</a>()));
        btn = new <a href="qpushbutton.html">QPushButton</a>("&gt;&gt;|", hb);
        <a href="qobject.html#connect">connect</a>(btn, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), movie, SLOT(step10()));
    }
};


// A QFileDialog that chooses movies.
//
class MovieStarter: public <a href="qfiledialog.html">QFileDialog</a> {
    Q_OBJECT
public:
    MovieStarter(const char *dir);

public slots:
    void startMovie(const <a href="qstring.html">QString</a>&amp; filename);
    // QDialog's method - normally closes the file dialog.
    // We want it left open, and we want Cancel to quit everything.
    void done( int r );
};


<a name="f336"></a>MovieStarter::MovieStarter(const char *dir)
    : <a href="qfiledialog.html">QFileDialog</a>(dir, "*.gif *.mng")
{
    //behave as in getOpenFilename
    <a href="qfiledialog.html#setMode">setMode</a>( ExistingFile );
    // When a file is selected, show it as a movie.
    <a href="qobject.html#connect">connect</a>(this, SIGNAL(<a href="qfiledialog.html#fileSelected">fileSelected</a>(const <a href="qstring.html">QString</a>&amp;)),
            this, SLOT(startMovie(const <a href="qstring.html">QString</a>&amp;)));
}


void <a name="f337"></a>MovieStarter::startMovie(const <a href="qstring.html">QString</a>&amp; filename)
{
    if ( filename ) // Start a new movie - have it delete when closed.
        (new MoviePlayer( filename, QMovie(filename), 0, 0,
                               WDestructiveClose))-&gt;show();
}

<a name="x1081"></a>void MovieStarter::<a href="qdialog.html#done">done</a>( int r )
{
    if (r != Accepted)
        qApp-&gt;<a href="qapplication.html#quit">quit</a>(); // end on Cancel
    <a href="qdialog.html#setResult">setResult</a>( r );

    // And don't hide.
}


int main(int argc, char **argv)
{
    <a href="qapplication.html">QApplication</a> a(argc, argv);

    if (argc &gt; 1) {
        // Commandline mode - show movies given on the command line
        //
        bool gui=TRUE;
        for (int arg=1; arg&lt;argc; arg++) {
            if ( QString(argv[arg]) == "-i" )
                gui = !gui;
            else if ( gui )
                (void)new MoviePlayer(argv[arg], QMovie(argv[arg]), 0, 0,
                                      Qt::WDestructiveClose);
            else
                (void)new MovieScreen(argv[arg], QMovie(argv[arg]), 0, 0,
                                      Qt::WDestructiveClose);
        }
        QObject::<a href="qobject.html#connect">connect</a>(qApp, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()));
    } else {
        // "GUI" mode - open a chooser for movies
        //
        MovieStarter* fd = new MovieStarter(".");
<a name="x1082"></a>        fd-&gt;<a href="qdialog.html#show">show</a>();
    }

    // Go!
    return a.<a href="qapplication.html#exec">exec</a>();
}

#include "main.moc"
</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 &copy; 2001 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.0.2</div>
</table></div></address></body>
</html>