summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/videowidget.cpp
blob: f3974a0dba93c2393485e418ba150fa321020834 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include <qpe/resource.h>
#include <qwidget.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qslider.h>
#include <qdrawutil.h>
#include "videowidget.h"
#include "mediaplayerplugininterface.h"
#include "mediaplayerstate.h"


#ifdef Q_WS_QWS
# define USE_DIRECT_PAINTER
# include <qdirectpainter_qws.h>
# include <qgfxraster_qws.h>
#endif


extern MediaPlayerState *mediaPlayerState;


static const int xo = 2; // movable x offset
static const int yo = 0; // movable y offset


struct MediaButton {
    int  xPos, yPos;
    bool isToggle, isHeld, isDown;
    int  controlType;
};


// Layout information for the videoButtons (and if it is a toggle button or not)
MediaButton videoButtons[] = {
    { 5+0*32+xo, 200+yo, FALSE, FALSE, FALSE, 4 }, // previous
    { 5+1*32+xo, 200+yo, FALSE, FALSE, FALSE, 1 }, // stop
    { 5+2*32+xo, 200+yo,  TRUE, FALSE, FALSE, 0 }, // play
    { 5+3*32+xo, 200+yo,  TRUE, FALSE, FALSE, 2 }, // pause
    { 5+4*32+xo, 200+yo, FALSE, FALSE, FALSE, 3 }, // next
    { 5+5*32+xo, 200+yo, FALSE, FALSE, FALSE, 8 }, // playlist
    { 5+6*32+xo, 200+yo,  TRUE, FALSE, FALSE, 9 }  // fullscreen
};


static const int numButtons = (sizeof(videoButtons)/sizeof(MediaButton));


VideoWidget::VideoWidget(QWidget* parent, const char* name, WFlags f) :
    QWidget( parent, name, f ), scaledWidth( 0 ), scaledHeight( 0 ) {
    setCaption( tr("MediaPlayer") );
    setBackgroundPixmap( Resource::loadPixmap( "mpegplayer/metalFinish" ) );
    pixmaps[0] = new QPixmap( Resource::loadPixmap( "mpegplayer/mediaButton0a" ) );
    pixmaps[1] = new QPixmap( Resource::loadPixmap( "mpegplayer/mediaButton0b" ) );
    pixmaps[2] = new QPixmap( Resource::loadPixmap( "mpegplayer/mediaControls0" ) );
    currentFrame = new QImage( 220 + 2, 160, (QPixmap::defaultDepth() == 16) ? 16 : 32 );

    slider = new QSlider( Qt::Horizontal, this );
    slider->setMinValue( 0 );
    slider->setMaxValue( 1 );
    slider->setBackgroundPixmap( Resource::loadPixmap( "mpegplayer/metalFinish" ) );
    slider->setFocusPolicy( QWidget::NoFocus );
    slider->setGeometry( QRect( 7, 250, 220, 20 ) );

    connect( slider,	       SIGNAL( sliderPressed() ),      this, SLOT( sliderPressed() ) );
    connect( slider,	       SIGNAL( sliderReleased() ),     this, SLOT( sliderReleased() ) );

    connect( mediaPlayerState, SIGNAL( lengthChanged(long) ),  this, SLOT( setLength(long) ) );
    connect( mediaPlayerState, SIGNAL( positionChanged(long) ),this, SLOT( setPosition(long) ) );
    connect( mediaPlayerState, SIGNAL( positionUpdated(long) ),this, SLOT( setPosition(long) ) );
    connect( mediaPlayerState, SIGNAL( viewChanged(char) ),    this, SLOT( setView(char) ) );
    connect( mediaPlayerState, SIGNAL( pausedToggled(bool) ),  this, SLOT( setPaused(bool) ) );
    connect( mediaPlayerState, SIGNAL( playingToggled(bool) ), this, SLOT( setPlaying(bool) ) );

    // Intialise state
    setLength( mediaPlayerState->length() );
    setPosition( mediaPlayerState->position() );
    setFullscreen( mediaPlayerState->fullscreen() );
    setPaused( mediaPlayerState->paused() );
    setPlaying( mediaPlayerState->playing() );
}


VideoWidget::~VideoWidget() {
    for ( int i = 0; i < 3; i++ )
	delete pixmaps[i];
    delete currentFrame;
}


static bool videoSliderBeingMoved = FALSE;


void VideoWidget::sliderPressed() {
    videoSliderBeingMoved = TRUE;
}


void VideoWidget::sliderReleased() {
    videoSliderBeingMoved = FALSE;
    if ( slider->width() == 0 )
	return;
    long val = long((double)slider->value() * mediaPlayerState->length() / slider->width());
    mediaPlayerState->setPosition( val );
}


void VideoWidget::setPosition( long i ) {
    updateSlider( i, mediaPlayerState->length() );
}


void VideoWidget::setLength( long max ) {
    updateSlider( mediaPlayerState->position(), max );
}


void VideoWidget::setView( char view ) {
    if ( view == 'v' ) {
	makeVisible();
    } else {
	// Effectively blank the view next time we show it so it looks nicer
	scaledWidth = 0;
	scaledHeight = 0;
	hide();
    }
}


void VideoWidget::updateSlider( long i, long max ) {
    // Will flicker too much if we don't do this
    if ( max == 0 )
	return;
    int width = slider->width();
    int val = int((double)i * width / max);
    if ( !mediaPlayerState->fullscreen() && !videoSliderBeingMoved ) {
	if ( slider->value() != val )
	    slider->setValue( val );
	if ( slider->maxValue() != width )
	    slider->setMaxValue( width );
    }
}


void VideoWidget::setToggleButton( int i, bool down ) {
    if ( down != videoButtons[i].isDown )
	toggleButton( i );
}


void VideoWidget::toggleButton( int i ) {
    videoButtons[i].isDown = !videoButtons[i].isDown;
    QPainter p(this);
    paintButton ( &p, i );
}


void VideoWidget::paintButton( QPainter *p, int i ) {
    int x = videoButtons[i].xPos;
    int y = videoButtons[i].yPos;
    int offset = 10 + videoButtons[i].isDown;
    p->drawPixmap( x, y, *pixmaps[videoButtons[i].isDown] );
    p->drawPixmap( x + 1 + offset, y + offset, *pixmaps[2], 9 * videoButtons[i].controlType, 0, 9, 9 );
}


void VideoWidget::mouseMoveEvent( QMouseEvent *event ) {
    for ( int i = 0; i < numButtons; i++ ) {
	int x = videoButtons[i].xPos;
	int y = videoButtons[i].yPos;
	if ( event->state() == QMouseEvent::LeftButton ) {
	    // The test to see if the mouse click is inside the circular button or not
	    // (compared with the radius squared to avoid a square-root of our distance)
	    int radius = 16;
	    QPoint center = QPoint( x + radius, y + radius );
	    QPoint dXY = center - event->pos();
	    int dist = dXY.x() * dXY.x() + dXY.y() * dXY.y();
	    bool isOnButton = dist <= (radius * radius);
	    if ( isOnButton != videoButtons[i].isHeld ) {
		videoButtons[i].isHeld = isOnButton;
		toggleButton(i);
	    }
	} else {
	    if ( videoButtons[i].isHeld ) {
		videoButtons[i].isHeld = FALSE;
		if ( !videoButtons[i].isToggle ) 
		    setToggleButton( i, FALSE );
		switch (i) {
		    case VideoPlay:       mediaPlayerState->setPlaying(videoButtons[i].isDown); return;
		    case VideoStop:       mediaPlayerState->setPlaying(FALSE); return;
		    case VideoPause:      mediaPlayerState->setPaused(videoButtons[i].isDown); return;
		    case VideoNext:       mediaPlayerState->setNext(); return;
		    case VideoPrevious:   mediaPlayerState->setPrev(); return;
		    case VideoPlayList:   mediaPlayerState->setList(); return;
		    case VideoFullscreen: mediaPlayerState->setFullscreen( TRUE ); makeVisible(); return;
		}
	    }
	}
    }
}


void VideoWidget::mousePressEvent( QMouseEvent *event ) {
    mouseMoveEvent( event );
}


void VideoWidget::mouseReleaseEvent( QMouseEvent *event ) {
    if ( mediaPlayerState->fullscreen() ) {
	mediaPlayerState->setFullscreen( FALSE );
	makeVisible();
    } else { 
	mouseMoveEvent( event );
    }
}


void VideoWidget::makeVisible() {
    if ( mediaPlayerState->fullscreen() ) {
	setBackgroundMode( QWidget::NoBackground );
	showFullScreen();
	resize( qApp->desktop()->size() );
	slider->hide();
    } else {
	setBackgroundPixmap( Resource::loadPixmap( "mpegplayer/metalFinish" ) );
	showNormal();
	showMaximized();
	slider->show();
    }
}


void VideoWidget::paintEvent( QPaintEvent * ) {
    QPainter p( this );

    if ( mediaPlayerState->fullscreen() ) {
	// Clear the background 
	p.setBrush( QBrush( Qt::black ) );
	p.drawRect( rect() );

	// Draw the current frame
	//p.drawImage( ); // If using directpainter we won't have a copy except whats on the screen
    } else {
	// draw border
	qDrawShadePanel( &p, 4, 15, 230, 170, colorGroup(), TRUE, 5, NULL );

	// Clear the movie screen first
	p.setBrush( QBrush( Qt::black ) );
	p.drawRect( 9, 20, 220, 160 );

	// draw current frame (centrally positioned from scaling to maintain aspect ratio)
	p.drawImage( 9 + (220 - scaledWidth) / 2, 20 + (160 - scaledHeight) / 2, *currentFrame, 0, 0, scaledWidth, scaledHeight );

	// draw the buttons
	for ( int i = 0; i < numButtons; i++ )
	    paintButton( &p, i );

	// draw the slider
	slider->repaint( TRUE );
    }
}


void VideoWidget::closeEvent( QCloseEvent* ) {
    mediaPlayerState->setList();
}


bool VideoWidget::playVideo() {
    bool result = FALSE;

    int stream = 0; 

    int sw = mediaPlayerState->curDecoder()->videoWidth( stream );
    int sh = mediaPlayerState->curDecoder()->videoHeight( stream );
    int dd = QPixmap::defaultDepth();
    int w = height();
    int h = width();

    ColorFormat format = (dd == 16) ? RGB565 : BGRA8888;

    if ( mediaPlayerState->fullscreen() ) {
#ifdef USE_DIRECT_PAINTER
	QDirectPainter p(this);

	if ( ( qt_screen->transformOrientation() == 3 ) &&
	     ( ( dd == 16 ) || ( dd == 32 ) ) && ( p.numRects() == 1 ) ) {

	    w = 320;
	    h = 240;

	    if ( mediaPlayerState->scaled() ) {
		// maintain aspect ratio
		if ( w * sh > sw * h ) 
		    w = sw * h / sh;
		else
		    h = sh * w / sw;
	    } else  {
		w = sw;
		h = sh;	    
	    }

	    w--; // we can't allow libmpeg to overwrite.
	    QPoint roff = qt_screen->mapToDevice( p.offset(), QSize( qt_screen->width(), qt_screen->height() ) );
	    
	    int ox = roff.x() - height() + 2 + (height() - w) / 2;
	    int oy = roff.y() + (width() - h) / 2;
	    int sx = 0, sy = 0;

	    uchar* fp = p.frameBuffer() + p.lineStep() * oy;
	    fp += dd * ox / 8;
	    uchar **jt = new uchar*[h];
	    for ( int i = h; i; i-- ) {
		jt[h - i] = fp;
		fp += p.lineStep();
	    }

	    result = mediaPlayerState->curDecoder()->videoReadScaledFrame( jt, sx, sy, sw, sh, w, h, format, 0) == 0;

	    delete [] jt;
	} else {
#endif
	    QPainter p(this);

	    w = 320;
	    h = 240;

	    if ( mediaPlayerState->scaled() ) {
		// maintain aspect ratio
		if ( w * sh > sw * h ) 
		    w = sw * h / sh;
		else
		    h = sh * w / sw;
	    } else  {
		w = sw;
		h = sh;	    
	    }

	    int bytes = ( dd == 16 ) ? 2 : 4;
	    QImage tempFrame( w, h, bytes << 3 );
	    result = mediaPlayerState->curDecoder()->videoReadScaledFrame( tempFrame.jumpTable(),
							0, 0, sw, sh, w, h, format, 0) == 0;
	    if ( result && mediaPlayerState->fullscreen() ) {

		int rw = h, rh = w;
		QImage rotatedFrame( rw, rh, bytes << 3 );

		ushort* in  = (ushort*)tempFrame.bits();
		ushort* out = (ushort*)rotatedFrame.bits();
		int spl = rotatedFrame.bytesPerLine() / bytes;
		for (int x=0; x<h; x++) {
		    if ( bytes == 2 ) {
			ushort* lout = out++ + (w - 1)*spl;
			for (int y=0; y<w; y++) {
			    *lout=*in++;
			    lout-=spl;
			}
		    } else {
			ulong* lout = ((ulong *)out)++ + (w - 1)*spl;
			for (int y=0; y<w; y++) {
			    *lout=*((ulong*)in)++;
			    lout-=spl;
			}
		    }
		}

		p.drawImage( (240 - rw) / 2, (320 - rh) / 2, rotatedFrame, 0, 0, rw, rh );
	    }
#ifdef USE_DIRECT_PAINTER
	}
#endif
    } else {

	w = 220;
	h = 160;

	// maintain aspect ratio
	if ( w * sh > sw * h ) 
	    w = sw * h / sh;
	else
	    h = sh * w / sw;

	result = mediaPlayerState->curDecoder()->videoReadScaledFrame( currentFrame->jumpTable(), 0, 0, sw, sh, w, h, format, 0) == 0;

	QPainter p( this );

	// Image changed size, therefore need to blank the possibly unpainted regions first
	if ( scaledWidth != w || scaledHeight != h ) {
	    p.setBrush( QBrush( Qt::black ) );
	    p.drawRect( 9, 20, 220, 160 );
	}

	scaledWidth = w;
	scaledHeight = h;

	if ( result ) {
	    p.drawImage( 9 + (220 - scaledWidth) / 2, 20 + (160 - scaledHeight) / 2, *currentFrame, 0, 0, scaledWidth, scaledHeight );
	}

    }

    return result;
}