summaryrefslogtreecommitdiffstats
path: root/examples/movies/main.cpp
blob: 50249b93f0041c9cb2bf60cb7e700a145fa76b1a (plain)
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 TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <ntqapplication.h>
#include <tqfiledialog.h>
#include <tqpushbutton.h>
#include <tqlabel.h>
#include <tqpainter.h>
#include <ntqmessagebox.h>
#include <tqmovie.h>
#include <ntqvbox.h>


class MovieScreen : public TQFrame {
    TQ_OBJECT
    TQMovie movie;
    TQString filename;
    TQSize sh;

public:
    MovieScreen(const char* fname, TQMovie m, TQWidget* p=0, const char* name=0, WFlags f=0) :
        TQFrame(p, name, f),
	sh(100,100)
    {
        setCaption(fname);
        filename = fname;
        movie = m;

        // Set a frame around the movie.
        setFrameStyle(TQFrame::WinPanel|TQFrame::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, TQ_SLOT(movieUpdated(const TQRect&)));
        movie.connectResize(this, TQ_SLOT(movieResized(const TQSize&)));
        movie.connectStatus(this, TQ_SLOT(movieStatus(int)));

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

    TQSize sizeHint() const
    {
	return sh;
    }

protected:

    // Draw the contents of the TQFrame - the movie and on-screen-display
    void drawContents(TQPainter* p)
    {
        // Get the current movie frame.
        TQPixmap pm = movie.framePixmap();

        // Get the area we have to draw in.
        TQRect r = contentsRect();

	if ( !pm.isNull() ) {
	    // Only rescale is we need to - it can take CPU!
	    if ( r.size() != pm.size() ) {
		TQWMatrix 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(TQFont("Helvetica", 24));

            TQFontMetrics fm = p->fontMetrics();
            if ( fm.width(message) > r.width()-10 )
                p->setFont(TQFont("Helvetica", 18));

            fm = p->fontMetrics();
            if ( fm.width(message) > r.width()-10 )
                p->setFont(TQFont("Helvetica", 14));

            fm = p->fontMetrics();
            if ( fm.width(message) > r.width()-10 )
                p->setFont(TQFont("Helvetica", 12));

            fm = p->fontMetrics();
            if ( fm.width(message) > r.width()-10 )
                p->setFont(TQFont("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 TQRect& area)
    {
        if (!isVisible())
            show();

        // The given area of the movie has changed.

        TQRect 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 TQSize& size)
    {
        // The movie changed size, probably from its initial zero size.

        int fw = frameWidth();
        sh = TQSize( 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) {
	    TQString msg;
	    msg.sprintf("Could not play movie \"%s\"", (const char*)filename);
	    TQMessageBox::warning(this, "movies", msg);
	    parentWidget()->close();
        } else if (status == TQMovie::Paused || status == TQMovie::EndOfMovie) {
            repaint(); // Ensure status text is displayed
        }
    }
};

class MoviePlayer : public TQVBox {
    MovieScreen* movie;
public:
    MoviePlayer(const char* fname, TQMovie m, TQWidget* p=0, const char* name=0, WFlags f=0) :
	TQVBox(p,name,f)
    {
	movie = new MovieScreen(fname, m, this);
	TQHBox* hb = new TQHBox(this);
	TQPushButton* btn;
	btn = new TQPushButton("<<", hb);
	connect(btn, TQ_SIGNAL(clicked()), movie, TQ_SLOT(restart()));
	btn = new TQPushButton("||", hb);
	connect(btn, TQ_SIGNAL(clicked()), movie, TQ_SLOT(togglePause()));
	btn = new TQPushButton(">|", hb);
	connect(btn, TQ_SIGNAL(clicked()), movie, TQ_SLOT(step()));
	btn = new TQPushButton(">>|", hb);
	connect(btn, TQ_SIGNAL(clicked()), movie, TQ_SLOT(step10()));
    }
};


// A TQFileDialog that chooses movies.
//
class MovieStarter: public TQFileDialog {
    TQ_OBJECT
public:
    MovieStarter(const char *dir);

public slots:
    void startMovie(const TQString& filename);
    // TQDialog'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)
    : TQFileDialog(dir, "*.gif *.mng")
{
    //behave as in getOpenFilename
    setMode( ExistingFile );
    // When a file is selected, show it as a movie.
    connect(this, TQ_SIGNAL(fileSelected(const TQString&)),
	    this, TQ_SLOT(startMovie(const TQString&)));
}


void MovieStarter::startMovie(const TQString& filename)
{
    if ( filename ) // Start a new movie - have it delete when closed.
	(new MoviePlayer( filename, TQMovie(filename), 0, 0,
			       WDestructiveClose))->show();
}

void MovieStarter::done( int r )
{
    if (r != Accepted)
	tqApp->quit(); // end on Cancel
    setResult( r );

    // And don't hide.
}


int main(int argc, char **argv)
{
    TQApplication 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 ( TQString(argv[arg]) == "-i" )
		gui = !gui;
	    else if ( gui )
		(void)new MoviePlayer(argv[arg], TQMovie(argv[arg]), 0, 0,
				      TQt::WDestructiveClose);
	    else
		(void)new MovieScreen(argv[arg], TQMovie(argv[arg]), 0, 0,
				      TQt::WDestructiveClose);
	}
        TQObject::connect(tqApp, TQ_SIGNAL(lastWindowClosed()), tqApp, TQ_SLOT(quit()));
    } else {
        // "GUI" mode - open a chooser for movies
        //
        MovieStarter* fd = new MovieStarter(".");
        fd->show();
    }

    // Go!
    return a.exec();
}

#include "main.moc"