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
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA. 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 <qapplication.h>
#include <qfiledialog.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qpainter.h>
#include <qmessagebox.h>
#include <qmovie.h>
#include <qvbox.h>
class MovieScreen : public QFrame {
Q_OBJECT
QMovie movie;
QString filename;
QSize sh;
public:
MovieScreen(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
QFrame(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.
movie.setBackgroundColor(backgroundColor());
setBackgroundMode(NoBackground);
// Get the movie to tell use when interesting things happen.
movie.connectUpdate(this, SLOT(movieUpdated(const QRect&)));
movie.connectResize(this, SLOT(movieResized(const QSize&)));
movie.connectStatus(this, SLOT(movieStatus(int)));
setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
}
QSize 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.
QPixmap pm = movie.framePixmap();
// Get the area we have to draw in.
QRect r = contentsRect();
if ( !pm.isNull() ) {
// Only rescale is we need to - it can take CPU!
if ( r.size() != pm.size() ) {
QWMatrix m;
m.scale((double)r.width()/pm.width(),
(double)r.height()/pm.height());
pm = pm.xForm(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->drawPixmap(r.x(), r.y(), 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() > 0) {
message = "FF >>";
}
if (message) {
// Find a good font size...
p->setFont(QFont("Helvetica", 24));
QFontMetrics fm = p->fontMetrics();
if ( fm.width(message) > r.width()-10 )
p->setFont(QFont("Helvetica", 18));
fm = p->fontMetrics();
if ( fm.width(message) > r.width()-10 )
p->setFont(QFont("Helvetica", 14));
fm = p->fontMetrics();
if ( fm.width(message) > r.width()-10 )
p->setFont(QFont("Helvetica", 12));
fm = p->fontMetrics();
if ( fm.width(message) > r.width()-10 )
p->setFont(QFont("Helvetica", 10));
// "Shadow" effect.
p->setPen(black);
p->drawText(1, 1, width()-1, height()-1, AlignCenter, message);
p->setPen(white);
p->drawText(0, 0, width()-1, height()-1, AlignCenter, message);
}
}
public slots:
void restart()
{
movie.restart();
repaint();
}
void togglePause()
{
if ( movie.paused() )
movie.unpause();
else
movie.pause();
repaint();
}
void step()
{
movie.step();
repaint();
}
void step10()
{
movie.step(10);
repaint();
}
private slots:
void movieUpdated(const QRect& area)
{
if (!isVisible())
show();
// The given area of the movie has changed.
QRect r = contentsRect();
if ( r.size() != movie.framePixmap().size() ) {
// Need to scale - redraw whole frame.
repaint( r );
} else {
// Only redraw the changed area of the frame
repaint( area.x()+r.x(), area.y()+r.x(),
area.width(), area.height() );
}
}
void movieResized(const QSize& size)
{
// The movie changed size, probably from its initial zero size.
int fw = frameWidth();
sh = QSize( size.width() + fw*2, size.height() + fw*2 );
updateGeometry();
if ( parentWidget() && parentWidget()->isHidden() )
parentWidget()->show();
}
void movieStatus(int status)
{
// The movie has sent us a status message.
if (status < 0) {
QString msg;
msg.sprintf("Could not play movie \"%s\"", (const char*)filename);
QMessageBox::warning(this, "movies", msg);
parentWidget()->close();
} else if (status == QMovie::Paused || status == QMovie::EndOfMovie) {
repaint(); // Ensure status text is displayed
}
}
};
class MoviePlayer : public QVBox {
MovieScreen* movie;
public:
MoviePlayer(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
QVBox(p,name,f)
{
movie = new MovieScreen(fname, m, this);
QHBox* hb = new QHBox(this);
QPushButton* btn;
btn = new QPushButton("<<", hb);
connect(btn, SIGNAL(clicked()), movie, SLOT(restart()));
btn = new QPushButton("||", hb);
connect(btn, SIGNAL(clicked()), movie, SLOT(togglePause()));
btn = new QPushButton(">|", hb);
connect(btn, SIGNAL(clicked()), movie, SLOT(step()));
btn = new QPushButton(">>|", hb);
connect(btn, SIGNAL(clicked()), movie, SLOT(step10()));
}
};
// A QFileDialog that chooses movies.
//
class MovieStarter: public QFileDialog {
Q_OBJECT
public:
MovieStarter(const char *dir);
public slots:
void startMovie(const QString& 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 );
};
MovieStarter::MovieStarter(const char *dir)
: QFileDialog(dir, "*.gif *.mng")
{
//behave as in getOpenFilename
setMode( ExistingFile );
// When a file is selected, show it as a movie.
connect(this, SIGNAL(fileSelected(const QString&)),
this, SLOT(startMovie(const QString&)));
}
void MovieStarter::startMovie(const QString& filename)
{
if ( filename ) // Start a new movie - have it delete when closed.
(new MoviePlayer( filename, QMovie(filename), 0, 0,
WDestructiveClose))->show();
}
void MovieStarter::done( int r )
{
if (r != Accepted)
qApp->quit(); // end on Cancel
setResult( r );
// And don't hide.
}
int main(int argc, char **argv)
{
QApplication a(argc, argv);
if (argc > 1) {
// Commandline mode - show movies given on the command line
//
bool gui=TRUE;
for (int arg=1; arg<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::connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));
} else {
// "GUI" mode - open a chooser for movies
//
MovieStarter* fd = new MovieStarter(".");
fd->show();
}
// Go!
return a.exec();
}
#include "main.moc"
|