source: trunk/examples/movies/main.cpp@ 169

Last change on this file since 169 was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

  • Property svn:keywords set to Id
File size: 7.7 KB
Line 
1/****************************************************************************
2** $Id: main.cpp 160 2006-12-11 20:15:57Z dmik $
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 <qpushbutton.h>
14#include <qlabel.h>
15#include <qpainter.h>
16#include <qmessagebox.h>
17#include <qmovie.h>
18#include <qvbox.h>
19
20
21class MovieScreen : public QFrame {
22 Q_OBJECT
23 QMovie movie;
24 QString filename;
25 QSize sh;
26
27public:
28 MovieScreen(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
29 QFrame(p, name, f),
30 sh(100,100)
31 {
32 setCaption(fname);
33 filename = fname;
34 movie = m;
35
36 // Set a frame around the movie.
37 setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
38
39 // No background needed, since we draw on the whole widget.
40 movie.setBackgroundColor(backgroundColor());
41 setBackgroundMode(NoBackground);
42
43 // Get the movie to tell use when interesting things happen.
44 movie.connectUpdate(this, SLOT(movieUpdated(const QRect&)));
45 movie.connectResize(this, SLOT(movieResized(const QSize&)));
46 movie.connectStatus(this, SLOT(movieStatus(int)));
47
48 setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
49 }
50
51 QSize sizeHint() const
52 {
53 return sh;
54 }
55
56protected:
57
58 // Draw the contents of the QFrame - the movie and on-screen-display
59 void drawContents(QPainter* p)
60 {
61 // Get the current movie frame.
62 QPixmap pm = movie.framePixmap();
63
64 // Get the area we have to draw in.
65 QRect r = contentsRect();
66
67 if ( !pm.isNull() ) {
68 // Only rescale is we need to - it can take CPU!
69 if ( r.size() != pm.size() ) {
70 QWMatrix m;
71 m.scale((double)r.width()/pm.width(),
72 (double)r.height()/pm.height());
73 pm = pm.xForm(m);
74 }
75
76 // Draw the [possibly scaled] frame. movieUpdated() below calls
77 // repaint with only the changed area, so clipping will ensure we
78 // only do the minimum amount of rendering.
79 //
80 p->drawPixmap(r.x(), r.y(), pm);
81 }
82
83
84 // The on-screen display
85
86 const char* message = 0;
87
88 if (movie.paused()) {
89 message = "PAUSED";
90 } else if (movie.finished()) {
91 message = "THE END";
92 } else if (movie.steps() > 0) {
93 message = "FF >>";
94 }
95
96 if (message) {
97 // Find a good font size...
98 p->setFont(QFont("Helvetica", 24));
99
100 QFontMetrics fm = p->fontMetrics();
101 if ( fm.width(message) > r.width()-10 )
102 p->setFont(QFont("Helvetica", 18));
103
104 fm = p->fontMetrics();
105 if ( fm.width(message) > r.width()-10 )
106 p->setFont(QFont("Helvetica", 14));
107
108 fm = p->fontMetrics();
109 if ( fm.width(message) > r.width()-10 )
110 p->setFont(QFont("Helvetica", 12));
111
112 fm = p->fontMetrics();
113 if ( fm.width(message) > r.width()-10 )
114 p->setFont(QFont("Helvetica", 10));
115
116 // "Shadow" effect.
117 p->setPen(black);
118 p->drawText(1, 1, width()-1, height()-1, AlignCenter, message);
119 p->setPen(white);
120 p->drawText(0, 0, width()-1, height()-1, AlignCenter, message);
121 }
122 }
123
124public slots:
125 void restart()
126 {
127 movie.restart();
128 repaint();
129 }
130
131 void togglePause()
132 {
133 if ( movie.paused() )
134 movie.unpause();
135 else
136 movie.pause();
137 repaint();
138 }
139
140 void step()
141 {
142 movie.step();
143 repaint();
144 }
145
146 void step10()
147 {
148 movie.step(10);
149 repaint();
150 }
151
152private slots:
153 void movieUpdated(const QRect& area)
154 {
155 if (!isVisible())
156 show();
157
158 // The given area of the movie has changed.
159
160 QRect r = contentsRect();
161
162 if ( r.size() != movie.framePixmap().size() ) {
163 // Need to scale - redraw whole frame.
164 repaint( r );
165 } else {
166 // Only redraw the changed area of the frame
167 repaint( area.x()+r.x(), area.y()+r.x(),
168 area.width(), area.height() );
169 }
170 }
171
172 void movieResized(const QSize& size)
173 {
174 // The movie changed size, probably from its initial zero size.
175
176 int fw = frameWidth();
177 sh = QSize( size.width() + fw*2, size.height() + fw*2 );
178 updateGeometry();
179 if ( parentWidget() && parentWidget()->isHidden() )
180 parentWidget()->show();
181 }
182
183 void movieStatus(int status)
184 {
185 // The movie has sent us a status message.
186
187 if (status < 0) {
188 QString msg;
189 msg.sprintf("Could not play movie \"%s\"", (const char*)filename);
190 QMessageBox::warning(this, "movies", msg);
191 parentWidget()->close();
192 } else if (status == QMovie::Paused || status == QMovie::EndOfMovie) {
193 repaint(); // Ensure status text is displayed
194 }
195 }
196};
197
198class MoviePlayer : public QVBox {
199 MovieScreen* movie;
200public:
201 MoviePlayer(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
202 QVBox(p,name,f)
203 {
204 movie = new MovieScreen(fname, m, this);
205 QHBox* hb = new QHBox(this);
206 QPushButton* btn;
207 btn = new QPushButton("<<", hb);
208 connect(btn, SIGNAL(clicked()), movie, SLOT(restart()));
209 btn = new QPushButton("||", hb);
210 connect(btn, SIGNAL(clicked()), movie, SLOT(togglePause()));
211 btn = new QPushButton(">|", hb);
212 connect(btn, SIGNAL(clicked()), movie, SLOT(step()));
213 btn = new QPushButton(">>|", hb);
214 connect(btn, SIGNAL(clicked()), movie, SLOT(step10()));
215 }
216};
217
218
219// A QFileDialog that chooses movies.
220//
221class MovieStarter: public QFileDialog {
222 Q_OBJECT
223public:
224 MovieStarter(const char *dir);
225
226public slots:
227 void startMovie(const QString& filename);
228 // QDialog's method - normally closes the file dialog.
229 // We want it left open, and we want Cancel to quit everything.
230 void done( int r );
231};
232
233
234MovieStarter::MovieStarter(const char *dir)
235 : QFileDialog(dir, "*.gif *.mng")
236{
237 //behave as in getOpenFilename
238 setMode( ExistingFile );
239 // When a file is selected, show it as a movie.
240 connect(this, SIGNAL(fileSelected(const QString&)),
241 this, SLOT(startMovie(const QString&)));
242}
243
244
245void MovieStarter::startMovie(const QString& filename)
246{
247 if ( filename ) // Start a new movie - have it delete when closed.
248 (new MoviePlayer( filename, QMovie(filename), 0, 0,
249 WDestructiveClose))->show();
250}
251
252void MovieStarter::done( int r )
253{
254 if (r != Accepted)
255 qApp->quit(); // end on Cancel
256 setResult( r );
257
258 // And don't hide.
259}
260
261
262int main(int argc, char **argv)
263{
264 QApplication a(argc, argv);
265
266 if (argc > 1) {
267 // Commandline mode - show movies given on the command line
268 //
269 bool gui=TRUE;
270 for (int arg=1; arg<argc; arg++) {
271 if ( QString(argv[arg]) == "-i" )
272 gui = !gui;
273 else if ( gui )
274 (void)new MoviePlayer(argv[arg], QMovie(argv[arg]), 0, 0,
275 Qt::WDestructiveClose);
276 else
277 (void)new MovieScreen(argv[arg], QMovie(argv[arg]), 0, 0,
278 Qt::WDestructiveClose);
279 }
280 QObject::connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));
281 } else {
282 // "GUI" mode - open a chooser for movies
283 //
284 MovieStarter* fd = new MovieStarter(".");
285 fd->show();
286 }
287
288 // Go!
289 return a.exec();
290}
291
292#include "main.moc"
Note: See TracBrowser for help on using the repository browser.