File: movies-main-cpp.html

package info (click to toggle)
qt-embedded 2.3.2-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 68,608 kB
  • ctags: 45,998
  • sloc: cpp: 276,654; ansic: 71,987; makefile: 29,074; sh: 12,305; yacc: 2,465; python: 1,863; perl: 481; lex: 480; xml: 68; lisp: 15
file content (278 lines) | stat: -rw-r--r-- 12,338 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!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>
Qt Toolkit - movies/main.cpp example 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: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">

<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>

<h1 align=center>Movies<br>Or the Story of the Animated GIF file</h1><br clear="all">

  The Movies example displays animated GIF files using the QMovie and
  QLabel classes.

  <hr>

  Main:
<pre>/****************************************************************************
** &#36;Id&#58; qt/examples/movies/main.cpp   2.3.2   edited 2001-06-12 $
**
** 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 name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a name="qfiledialog.h"></a><a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
#include &lt;<a name="qlabel.h"></a><a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a name="qpainter.h"></a><a href="qpainter-h.html">qpainter.h</a>&gt;
#include &lt;<a name="qmessagebox.h"></a><a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
#include &lt;<a name="qmovie.h"></a><a href="qmovie-h.html">qmovie.h</a>&gt;

class MovieScreen : public QFrame {
    Q_OBJECT
    <a name="QMovie"></a><a href="qmovie.html">QMovie</a> movie;
    <a name="QString"></a><a href="qstring.html">QString</a> filename;

public:
    MovieScreen(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
        <a name="QFrame"></a><a href="qframe.html">QFrame</a>(p, name, f)
    {
        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.
        movie.setBackgroundColor(backgroundColor());
        setBackgroundMode(NoBackground);

        // Get the movie to tell use when interesting things happen.
        movie.connectUpdate(this, SLOT(movieUpdated(const QRect&amp;)));
        movie.connectResize(this, SLOT(movieResized(const QSize&amp;)));
        movie.connectStatus(this, SLOT(movieStatus(int)));
    }

protected:

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

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

        // Only rescale is we need to - it can take CPU!
        if ( r.<a name="size"></a><a href="qrect.html#507cd9">size</a>() != pm.<a name="size"></a><a href="qpixmap.html#60b65f">size</a>() ) {
            <a name="QWMatrix"></a><a href="qwmatrix.html">QWMatrix</a> m;
            m.<a name="scale"></a><a href="qwmatrix.html#41a356">scale</a>((double)r.<a name="width"></a><a href="qrect.html#45fe95">width</a>()/pm.<a name="width"></a><a href="qpixmap.html#86dbc5">width</a>(),
                    (double)r.<a name="height"></a><a href="qrect.html#581ab8">height</a>()/pm.<a name="height"></a><a href="qpixmap.html#d5bb33">height</a>());
            pm = pm.<a name="xForm"></a><a href="qpixmap.html#ff5fcf">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.
        //
        p-&gt;<a name="drawPixmap"></a><a href="qpainter.html#c64b89">drawPixmap</a>(r.<a name="x"></a><a href="qrect.html#fccae7">x</a>(), r.<a name="y"></a><a href="qrect.html#f448f7">y</a>(), pm);

        // The on-screen display

        const char* message = 0;

        if (movie.paused()) {
            message = "PAUSED";
        } else if (movie.finished()) {
            message = "THE END";
        } else if (movie.steps() &gt; 0) {
            message = "FF &gt;&gt;";
        }

        if (message) {
            // Find a good font size...
            p-&gt;<a name="setFont"></a><a href="qpainter.html#998df2">setFont</a>(<a name="QFont"></a><a href="qfont.html">QFont</a>("Helvetica", 24));

            <a name="QFontMetrics"></a><a href="qfontmetrics.html">QFontMetrics</a> fm = p-&gt;<a name="fontMetrics"></a><a href="qpainter.html#73b2e5">fontMetrics</a>();
            if ( fm.<a name="width"></a><a href="qfontmetrics.html#3b6f39">width</a>(message) &gt; r.<a href="qrect.html#45fe95">width</a>()-10 )
                p-&gt;<a href="qpainter.html#998df2">setFont</a>(<a href="qfont.html">QFont</a>("Helvetica", 18));

            fm = p-&gt;<a href="qpainter.html#73b2e5">fontMetrics</a>();
            if ( fm.<a href="qfontmetrics.html#3b6f39">width</a>(message) &gt; r.<a href="qrect.html#45fe95">width</a>()-10 )
                p-&gt;<a href="qpainter.html#998df2">setFont</a>(<a href="qfont.html">QFont</a>("Helvetica", 14));

            fm = p-&gt;<a href="qpainter.html#73b2e5">fontMetrics</a>();
            if ( fm.<a href="qfontmetrics.html#3b6f39">width</a>(message) &gt; r.<a href="qrect.html#45fe95">width</a>()-10 )
                p-&gt;<a href="qpainter.html#998df2">setFont</a>(<a href="qfont.html">QFont</a>("Helvetica", 12));

            fm = p-&gt;<a href="qpainter.html#73b2e5">fontMetrics</a>();
            if ( fm.<a href="qfontmetrics.html#3b6f39">width</a>(message) &gt; r.<a href="qrect.html#45fe95">width</a>()-10 )
                p-&gt;<a href="qpainter.html#998df2">setFont</a>(<a href="qfont.html">QFont</a>("Helvetica", 10));

            // "Shadow" effect.
            p-&gt;setPen(black);
            p-&gt;<a name="drawText"></a><a href="qpainter.html#0f088f">drawText</a>(1, 1, width()-1, height()-1, AlignCenter, message);
            p-&gt;<a name="setPen"></a><a href="qpainter.html#0183e4">setPen</a>(white);
            p-&gt;<a href="qpainter.html#0f088f">drawText</a>(0, 0, width()-1, height()-1, AlignCenter, message);
        }
    }

    void mouseReleaseEvent(<a name="QMouseEvent"></a><a href="qmouseevent.html">QMouseEvent</a>* event)
    {
        // Do what the Help says...

        if (event-&gt;<a name="state"></a><a href="qmouseevent.html#db99dc">state</a>() &amp; ShiftButton) {
            movie.restart();
        } else if (!movie.paused()) {
            movie.pause();
        } else {
            if (event-&gt;<a name="button"></a><a href="qmouseevent.html#78482f">button</a>() &amp; LeftButton)
                movie.step((event-&gt;<a href="qmouseevent.html#db99dc">state</a>() &amp; ControlButton) ? 10 : 1);
            else if (event-&gt;<a href="qmouseevent.html#78482f">button</a>() &amp; (MidButton|RightButton))
                movie.unpause();
        }

        repaint(); // To hide/show "PAUSED".
    }

private slots:
    void movieUpdated(const QRect&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#507cd9">size</a>() != movie.framePixmap().size() ) {
            // Need to scale - redraw whole frame.
            repaint( r );
        } else {
            // Only redraw the changed area of the frame
            repaint( area.x()+r.<a href="qrect.html#fccae7">x</a>(), area.y()+r.<a href="qrect.html#fccae7">x</a>(),
                     area.width(), area.height() );
        }
    }

    void movieResized(const QSize&amp; size)
    {
        // The movie changed size, probably from its initial zero size.

        int fw = frameWidth();
        resize( size.width() + fw*2, size.height() + fw*2 );
        qApp-&gt;setMainWidget(this); // Just geometry, etc.
        qApp-&gt;setMainWidget(0); // Not Close==Quit
    }

    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 name="sprintf"></a><a href="qstring.html#926f67">sprintf</a>("Could not play movie \"%s\"", (const char*)filename);
            <a name="QMessageBox::message"></a><a href="qmessagebox.html#6f1d2d">QMessageBox::message</a>("Error", msg);
        } else if (status == QMovie::Paused || status == QMovie::EndOfMovie) {
            repaint(); // Ensure status text is displayed
        }
    }
};

// A QFileDialog that chooses movies.
//
class MovieStarter : public QFileDialog {
    Q_OBJECT
public:
    MovieStarter(const char *dir) :
        <a name="QFileDialog"></a><a href="qfiledialog.html">QFileDialog</a>(dir, "*.gif")
    {
        //behave as in getOpenFilename
        setMode( ExistingFile );
        // When a file is selected, show it as a movie.
        connect(this, SIGNAL(fileSelected(const QString&amp;)),
                this, SLOT(startMovie(const QString&amp;)));
    }

public slots:
    void startMovie(const QString&amp; filename)
    {
        if ( filename ) // Start a new movie - have it delete when closed.
            (void)new MovieScreen(filename, QMovie(filename), 0, 0, WDestructiveClose);
    }

    // QDialog's method - normally closes the file dialog.
    // We want it left open, and we want Cancel to quit everything.
    void done( int r )
    {
        if (r != Accepted) qApp-&gt;quit(); // end on Cancel
        setResult( r );

        // And don't hide.
    }
};

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

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

        // Some help text to explain the `hidden' features.
        <a name="QLabel"></a><a href="qlabel.html">QLabel</a>* help = new <a href="qlabel.html">QLabel</a>(
            "Choose some movies.\n\n"
            "Shift-click to Restart.\n"
            "Click to Pause,\n"
            "then left-click to Step,\n"
            "control-left-click to Step 10,\n"
            "right-click to Unpause\n\n"
            "Windows may be resized to enlarge movie.", 0 );
        help-&gt;<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>( "Qt Examples - Movies" );
        help-&gt;<a name="setIndent"></a><a href="qlabel.html#9077ed">setIndent</a>( 10 );
        help-&gt;<a name="show"></a><a href="qwidget.html#200ee5">show</a>();
    }

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

#include "main.moc"
</pre>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright  2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>