source: trunk/doc/html/movies-example.html@ 208

Last change on this file since 208 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 14.5 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/movies/movies.doc:4 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Movies or the Story of the Animated GIF file</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<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>
33
34
35<p>
36The Movies example displays MNG and animated GIF files using the <a href="qmovie.html">QMovie</a> and
37<a href="qlabel.html">QLabel</a> classes.
38<p> The movies will only read an animated GIF if GIF reading was enabled when Qt was built.
39<p> <hr>
40<p> Main:
41<p> <pre>/****************************************************************************
42** $Id: movies-example.html 2051 2007-02-21 10:04:20Z chehrlic $
43**
44** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
45**
46** This file is part of an example program for Qt. This example
47** program may be used, distributed and modified without limitation.
48**
49*****************************************************************************/
50
51#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
52#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
53#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
54#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
55#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
56#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
57#include &lt;<a href="qmovie-h.html">qmovie.h</a>&gt;
58#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
59
60
61class MovieScreen : public <a href="qframe.html">QFrame</a> {
62 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
63 <a href="qmovie.html">QMovie</a> movie;
64 <a href="qstring.html">QString</a> filename;
65 <a href="qsize.html">QSize</a> sh;
66
67public:
68 MovieScreen(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
69 <a href="qframe.html">QFrame</a>(p, name, f),
70 sh(100,100)
71 {
72 setCaption(fname);
73 filename = fname;
74 movie = m;
75
76 // Set a frame around the movie.
77 setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
78
79 // No background needed, since we draw on the whole widget.
80<a name="x523"></a> movie.<a href="qwidget.html#setBackgroundColor">setBackgroundColor</a>(backgroundColor());
81 setBackgroundMode(NoBackground);
82
83 // Get the movie to tell use when interesting things happen.
84<a name="x500"></a> movie.<a href="qmovie.html#connectUpdate">connectUpdate</a>(this, SLOT(movieUpdated(const <a href="qrect.html">QRect</a>&amp;)));
85<a name="x498"></a> movie.<a href="qmovie.html#connectResize">connectResize</a>(this, SLOT(movieResized(const <a href="qsize.html">QSize</a>&amp;)));
86<a name="x499"></a> movie.<a href="qmovie.html#connectStatus">connectStatus</a>(this, SLOT(movieStatus(int)));
87
88 setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
89 }
90
91 <a href="qsize.html">QSize</a> sizeHint() const
92 {
93 return sh;
94 }
95
96protected:
97
98 // Draw the contents of the QFrame - the movie and on-screen-display
99 void drawContents(QPainter* p)
100 {
101 // Get the current movie frame.
102<a name="x502"></a> <a href="qpixmap.html">QPixmap</a> pm = movie.<a href="qmovie.html#framePixmap">framePixmap</a>();
103
104 // Get the area we have to draw in.
105 <a href="qrect.html">QRect</a> r = contentsRect();
106
107<a name="x511"></a> if ( !pm.<a href="qpixmap.html#isNull">isNull</a>() ) {
108 // Only rescale is we need to - it can take CPU!
109<a name="x516"></a><a name="x512"></a> if ( r.<a href="qrect.html#size">size</a>() != pm.<a href="qpixmap.html#size">size</a>() ) {
110 <a href="qwmatrix.html">QWMatrix</a> m;
111<a name="x524"></a><a name="x517"></a><a name="x513"></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>(),
112<a name="x515"></a><a name="x510"></a> (double)r.<a href="qrect.html#height">height</a>()/pm.<a href="qpixmap.html#height">height</a>());
113<a name="x514"></a> pm = pm.<a href="qpixmap.html#xForm">xForm</a>(m);
114 }
115
116 // Draw the [possibly scaled] frame. movieUpdated() below calls
117 // repaint with only the changed area, so clipping will ensure we
118 // only do the minimum amount of rendering.
119 //
120<a name="x519"></a><a name="x518"></a> p-&gt;drawPixmap(r.<a href="qrect.html#x">x</a>(), r.<a href="qrect.html#y">y</a>(), pm);
121 }
122
123
124 // The on-screen display
125
126 const char* message = 0;
127
128<a name="x504"></a> if (movie.<a href="qmovie.html#paused">paused</a>()) {
129 message = "PAUSED";
130<a name="x501"></a> } else if (movie.<a href="qmovie.html#finished">finished</a>()) {
131 message = "THE END";
132<a name="x507"></a> } else if (movie.<a href="qmovie.html#steps">steps</a>() &gt; 0) {
133 message = "FF &gt;&gt;";
134 }
135
136 if (message) {
137 // Find a good font size...
138 p-&gt;setFont(QFont("Helvetica", 24));
139
140 <a href="qfontmetrics.html">QFontMetrics</a> fm = p-&gt;fontMetrics();
141<a name="x496"></a> if ( fm.<a href="qfontmetrics.html#width">width</a>(message) &gt; r.<a href="qrect.html#width">width</a>()-10 )
142 p-&gt;setFont(QFont("Helvetica", 18));
143
144 fm = p-&gt;fontMetrics();
145 if ( fm.<a href="qfontmetrics.html#width">width</a>(message) &gt; r.<a href="qrect.html#width">width</a>()-10 )
146 p-&gt;setFont(QFont("Helvetica", 14));
147
148 fm = p-&gt;fontMetrics();
149 if ( fm.<a href="qfontmetrics.html#width">width</a>(message) &gt; r.<a href="qrect.html#width">width</a>()-10 )
150 p-&gt;setFont(QFont("Helvetica", 12));
151
152 fm = p-&gt;fontMetrics();
153 if ( fm.<a href="qfontmetrics.html#width">width</a>(message) &gt; r.<a href="qrect.html#width">width</a>()-10 )
154 p-&gt;setFont(QFont("Helvetica", 10));
155
156 // "Shadow" effect.
157 p-&gt;setPen(black);
158 p-&gt;drawText(1, 1, width()-1, height()-1, AlignCenter, message);
159 p-&gt;setPen(white);
160 p-&gt;drawText(0, 0, width()-1, height()-1, AlignCenter, message);
161 }
162 }
163
164public slots:
165 void restart()
166 {
167<a name="x505"></a> movie.<a href="qmovie.html#restart">restart</a>();
168 repaint();
169 }
170
171 void togglePause()
172 {
173 if ( movie.<a href="qmovie.html#paused">paused</a>() )
174<a name="x508"></a> movie.<a href="qmovie.html#unpause">unpause</a>();
175 else
176<a name="x503"></a> movie.<a href="qmovie.html#pause">pause</a>();
177 repaint();
178 }
179
180 void step()
181 {
182<a name="x506"></a> movie.<a href="qmovie.html#step">step</a>();
183 repaint();
184 }
185
186 void step10()
187 {
188 movie.<a href="qmovie.html#step">step</a>(10);
189 repaint();
190 }
191
192private slots:
193 void movieUpdated(const <a href="qrect.html">QRect</a>&amp; area)
194 {
195 if (!isVisible())
196 show();
197
198 // The given area of the movie has changed.
199
200 <a href="qrect.html">QRect</a> r = contentsRect();
201
202 if ( r.<a href="qrect.html#size">size</a>() != movie.<a href="qmovie.html#framePixmap">framePixmap</a>().size() ) {
203 // Need to scale - redraw whole frame.
204 repaint( r );
205 } else {
206 // Only redraw the changed area of the frame
207 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>(),
208 area.<a href="qrect.html#width">width</a>(), area.<a href="qrect.html#height">height</a>() );
209 }
210 }
211
212 void movieResized(const <a href="qsize.html">QSize</a>&amp; size)
213 {
214 // The movie changed size, probably from its initial zero size.
215
216 int fw = frameWidth();
217<a name="x521"></a><a name="x520"></a> sh = QSize( size.<a href="qsize.html#width">width</a>() + fw*2, size.<a href="qsize.html#height">height</a>() + fw*2 );
218 <a href="qwidget.html#updateGeometry">updateGeometry</a>();
219 if ( <a href="qwidget.html#parentWidget">parentWidget</a>() &amp;&amp; parentWidget()-&gt;isHidden() )
220 <a href="qwidget.html#parentWidget">parentWidget</a>()-&gt;show();
221 }
222
223 void movieStatus(int status)
224 {
225 // The movie has sent us a status message.
226
227 if (status &lt; 0) {
228 <a href="qstring.html">QString</a> msg;
229 msg.<a href="qstring.html#sprintf">sprintf</a>("Could not play movie \"%s\"", (const char*)filename);
230 QMessageBox::<a href="qmessagebox.html#warning">warning</a>(this, "movies", msg);
231 <a href="qwidget.html#parentWidget">parentWidget</a>()-&gt;close();
232 } else if (status == QMovie::Paused || status == QMovie::EndOfMovie) {
233 <a href="qwidget.html#repaint">repaint</a>(); // Ensure status text is displayed
234 }
235 }
236};
237
238class MoviePlayer : public <a href="qvbox.html">QVBox</a> {
239 MovieScreen* movie;
240public:
241 MoviePlayer(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
242 <a href="qvbox.html">QVBox</a>(p,name,f)
243 {
244 movie = new MovieScreen(fname, m, this);
245 <a href="qhbox.html">QHBox</a>* hb = new <a href="qhbox.html">QHBox</a>(this);
246 <a href="qpushbutton.html">QPushButton</a>* btn;
247 btn = new <a href="qpushbutton.html">QPushButton</a>("&lt;&lt;", hb);
248 <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>()));
249 btn = new <a href="qpushbutton.html">QPushButton</a>("||", hb);
250 <a href="qobject.html#connect">connect</a>(btn, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), movie, SLOT(togglePause()));
251 btn = new <a href="qpushbutton.html">QPushButton</a>("&gt;|", hb);
252 <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>()));
253 btn = new <a href="qpushbutton.html">QPushButton</a>("&gt;&gt;|", hb);
254 <a href="qobject.html#connect">connect</a>(btn, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), movie, SLOT(step10()));
255 }
256};
257
258
259// A QFileDialog that chooses movies.
260//
261class MovieStarter: public <a href="qfiledialog.html">QFileDialog</a> {
262 Q_OBJECT
263public:
264 MovieStarter(const char *dir);
265
266public slots:
267 void startMovie(const <a href="qstring.html">QString</a>&amp; filename);
268 // QDialog's method - normally closes the file dialog.
269 // We want it left open, and we want Cancel to quit everything.
270 void done( int r );
271};
272
273
274<a name="f258"></a>MovieStarter::MovieStarter(const char *dir)
275 : <a href="qfiledialog.html">QFileDialog</a>(dir, "*.gif *.mng")
276{
277 //behave as in getOpenFilename
278 <a href="qfiledialog.html#setMode">setMode</a>( ExistingFile );
279 // When a file is selected, show it as a movie.
280 <a href="qobject.html#connect">connect</a>(this, SIGNAL(<a href="qfiledialog.html#fileSelected">fileSelected</a>(const <a href="qstring.html">QString</a>&amp;)),
281 this, SLOT(startMovie(const <a href="qstring.html">QString</a>&amp;)));
282}
283
284
285void <a name="f259"></a>MovieStarter::startMovie(const <a href="qstring.html">QString</a>&amp; filename)
286{
287 if ( filename ) // Start a new movie - have it delete when closed.
288 (new MoviePlayer( filename, QMovie(filename), 0, 0,
289 WDestructiveClose))-&gt;show();
290}
291
292<a name="x495"></a>void MovieStarter::<a href="qdialog.html#done">done</a>( int r )
293{
294 if (r != Accepted)
295 qApp-&gt;<a href="qapplication.html#quit">quit</a>(); // end on Cancel
296 <a href="qdialog.html#setResult">setResult</a>( r );
297
298 // And don't hide.
299}
300
301
302int main(int argc, char **argv)
303{
304 <a href="qapplication.html">QApplication</a> a(argc, argv);
305
306 if (argc &gt; 1) {
307 // Commandline mode - show movies given on the command line
308 //
309 bool gui=TRUE;
310 for (int arg=1; arg&lt;argc; arg++) {
311 if ( QString(argv[arg]) == "-i" )
312 gui = !gui;
313 else if ( gui )
314 (void)new MoviePlayer(argv[arg], QMovie(argv[arg]), 0, 0,
315 Qt::WDestructiveClose);
316 else
317 (void)new MovieScreen(argv[arg], QMovie(argv[arg]), 0, 0,
318 Qt::WDestructiveClose);
319 }
320 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>()));
321 } else {
322 // "GUI" mode - open a chooser for movies
323 //
324 MovieStarter* fd = new MovieStarter(".");
325<a name="x494"></a> fd-&gt;<a href="qdialog.html#show">show</a>();
326 }
327
328 // Go!
329 return a.<a href="qapplication.html#exec">exec</a>();
330}
331
332#include "main.moc"
333</pre>
334
335<p>See also <a href="examples.html">Examples</a>.
336
337<!-- eof -->
338<p><address><hr><div align=center>
339<table width=100% cellspacing=0 border=0><tr>
340<td>Copyright &copy; 2007
341<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
342<td align=right><div align=right>Qt 3.3.8</div>
343</table></div></address></body>
344</html>
Note: See TracBrowser for help on using the repository browser.