~ubuntu-branches/debian/squeeze/smplayer/squeeze

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
/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2007 Ricardo Villalba <rvm@escomposlinux.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "mplayerwindow.h"
#include "constants.h"
#include "qt3_4_compat.h"
#include "images.h"
#include "global.h"
#include "desktopinfo.h"

#include <qlabel.h>
#include <qtimer.h>
#include <qcursor.h>
#include <qevent.h>
#include <qlayout.h>
#include <qpixmap.h>
#include <qpainter.h>

Screen::Screen(QWidget* parent, const char* name, WFlags fl) 
#if USE_GL_WIDGET
	: QGLWidget(parent, name, 0, fl | Qt::WStaticContents | Qt::WNoAutoErase) 
#else
	: QWidget(parent, name, fl | Qt::WStaticContents | Qt::WNoAutoErase) 
#endif
{
	setMouseTracking(TRUE);
	setFocusPolicy( NOFOCUS );
	setMinimumSize( QSize(0,0) );

	cursor_pos = QPoint(0,0);
	last_cursor_pos = QPoint(0,0);

	QTimer *timer = new QTimer(this);
	connect( timer, SIGNAL(timeout()), this, SLOT(checkMousePos()) );
	timer->start(2000, FALSE);

	#if QT_VERSION >= 0x040000
	setAttribute(Qt::WA_NoSystemBackground);
	setAttribute(Qt::WA_StaticContents);
    //setAttribute( Qt::WA_OpaquePaintEvent );
	setAttribute(Qt::WA_PaintOnScreen);
	setAttribute(Qt::WA_PaintUnclipped);
	//setAttribute(Qt::WA_PaintOutsidePaintEvent);
	#endif
}

Screen::~Screen() {
}

void Screen::paintEvent( QPaintEvent * e ) {
	//qDebug("Screen::paintEvent");
	QPainter painter(this);
	painter.eraseRect( e->rect() );
	//painter.fillRect( e->rect(), QColor(255,0,0) );
}


void Screen::checkMousePos() {
	//qDebug("Screen::checkMousePos");
	
	if ( cursor_pos == last_cursor_pos ) {
		//qDebug(" same pos");
		if (cursor().shape() != Qt::BlankCursor) {
			//qDebug(" hiding mouse cursor");
			setCursor(QCursor(Qt::BlankCursor));
		}
	} else {
		last_cursor_pos = cursor_pos;
	}
}

void Screen::mouseMoveEvent( QMouseEvent * e ) {
	//qDebug("Screen::mouseMoveEvent");
	//qDebug(" pos: x: %d y: %d", e->pos().x(), e->pos().y() );
	cursor_pos = e->pos();

	if (cursor().shape() != Qt::ArrowCursor) {
		//qDebug(" showing mouse cursor" );
		setCursor(QCursor(Qt::ArrowCursor));
	}

	emit mouseMoved( e->pos() );
}

/* ---------------------------------------------------------------------- */

MplayerLayer::MplayerLayer(QWidget* parent, const char* name, WFlags fl) 
	: Screen(parent, name, fl) 
{
	allow_clearing = true;
}

MplayerLayer::~MplayerLayer() {
}

void MplayerLayer::allowClearingBackground(bool b) {
	qDebug("MplayerLayer::allowClearingBackground: %d", b);
	allow_clearing = b;
}

void MplayerLayer::paintEvent( QPaintEvent * e ) {
	//qDebug("MplayerLayer::paintEvent: allow_clearing: %d", allow_clearing);
	if (allow_clearing) Screen::paintEvent(e);
}


/* ---------------------------------------------------------------------- */

MplayerWindow::MplayerWindow(QWidget* parent, const char* name, WFlags fl) 
	: Screen(parent, name, fl) 
{
	offset_x = 0;
	offset_y = 0;
	zoom_factor = 1.0;

	#if QT_VERSION >= 0x040000
	setAutoFillBackground(TRUE);
	#endif
	setBackgroundColor( QColor(0,0,0) );

	mplayerlayer = new MplayerLayer( this, "mplayerlayer" );
	#if QT_VERSION >= 0x040000
	mplayerlayer->setAutoFillBackground(TRUE);
	#endif
	//mplayerlayer->setBackgroundColor( COLORKEY );
	//setColorKey( COLORKEY );

	logo = new QLabel( mplayerlayer, "logo" );
	#if QT_VERSION >= 0x040000
	logo->setAutoFillBackground(TRUE);
	#endif
	logo->setBackgroundColor( QColor(0,0,0) );

	QVBoxLayout * mplayerlayerLayout = new QVBoxLayout( mplayerlayer );
	mplayerlayerLayout->addWidget( logo, 0, Qt::AlignHCenter | Qt::AlignVCenter   );

    aspect = (double) 4 / 3;
	monitoraspect = 0;

	setSizePolicy( QSizePolicy::Expanding , QSizePolicy::Expanding );
	setFocusPolicy( STRONGFOCUS );

	connect( mplayerlayer, SIGNAL(mouseMoved(QPoint)),
             this, SLOT(translatePos(QPoint)) );

	languageChange();
}

MplayerWindow::~MplayerWindow() {
}

void MplayerWindow::setColorKey( QColor c ) {
	mplayerlayer->setBackgroundColor( c );
}

void MplayerWindow::languageChange() {
	qDebug("MplayerWindow::languageChange");

	logo->setPixmap( Images::icon("background") );
}

void MplayerWindow::allowClearingBackground(bool b) {
	mplayerlayer->allowClearingBackground(b);
}

bool MplayerWindow::isClearingBackgroundAllowed() {
	return mplayerlayer->isClearingBackgroundAllowed();
}

void MplayerWindow::showLogo( bool b)
{
	if (b) logo->show(); else logo->hide();
}


MplayerLayer * MplayerWindow::mplayerLayer()
{
    return mplayerlayer;
}

/*
void MplayerWindow::changePolicy() {
	setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Preferred  );
}
*/

void MplayerWindow::setResolution( int w, int h)
{
    video_width = w;
    video_height = h;
    
    //mplayerlayer->move(1,1);
    updateVideoWindow();
}


void MplayerWindow::resizeEvent( QResizeEvent * e)
{
   /*qDebug("MplayerWindow::resizeEvent: %d, %d",
	   e->size().width(), e->size().height() );*/

	offset_x = 0;
	offset_y = 0;

    updateVideoWindow();
	setZoom(zoom_factor);
}


void MplayerWindow::setMonitorAspect(double asp) {
	monitoraspect = asp;
}

void MplayerWindow::setAspect( double asp) {
    aspect = asp;
	if (monitoraspect!=0) {
		aspect = aspect / monitoraspect * DesktopInfo::desktop_aspectRatio(this);
	}
	updateVideoWindow();
}


void MplayerWindow::updateVideoWindow()
{
	/*
	mplayerlayer->resize(size());
	return;
	*/

	//qDebug("MplayerWindow::updateVideoWindow");

    //qDebug("aspect= %f", aspect);
    
    int w_width = size().width();
    int w_height = size().height();
    
    int pos1_w = w_width;
    int pos1_h = w_width / aspect + 0.5;
    
    int pos2_h = w_height;
    int pos2_w = w_height * aspect + 0.5;
    
    //qDebug("pos1_w: %d, pos1_h: %d", pos1_w, pos1_h);
    //qDebug("pos2_w: %d, pos2_h: %d", pos2_w, pos2_h);
    
    int w,h;
    int x=0;
    int y=0;
    
    if (pos1_h <= w_height) {
	//qDebug("Pos1!");
		w = pos1_w;
		h = pos1_h;
	
		y = (w_height - h) /2;
    } else {
	//qDebug("Pos2!");
		w = pos2_w;
		h = pos2_h;
	
		x = (w_width - w) /2;
    }

    mplayerlayer->move(x,y);
    mplayerlayer->resize(w, h);

	orig_x = x;
	orig_y = y;
	orig_width = w;
	orig_height = h;
    
    //qDebug( "w_width: %d, w_height: %d", w_width, w_height);
    //qDebug("w: %d, h: %d", w,h);
}


void MplayerWindow::mouseReleaseEvent( QMouseEvent * e) {
    qDebug( "MplayerWindow::mouseReleaseEvent" );

	if (e->button() == Qt::LeftButton) {
		e->accept();
		emit leftClicked();
	}
	else
    if (e->button() == Qt::RightButton) {
		e->accept();
		emit rightButtonReleased( e->globalPos() );
    } 
	else {
		e->ignore();
	}
}

void MplayerWindow::mouseDoubleClickEvent( QMouseEvent * e ) {
	e->accept();
	emit doubleClicked();
}

void MplayerWindow::wheelEvent( QWheelEvent * e ) {
    qDebug("MplayerWindow::wheelEvent: delta: %d", e->delta());
    e->accept();

    if (e->delta() >= 0)
        emit wheelUp();
    else
        emit wheelDown();
}


QSize MplayerWindow::sizeHint() const {
	//qDebug("MplayerWindow::sizeHint");
	return QSize( video_width, video_height );
}

QSize MplayerWindow::minimumSizeHint () const {
	return QSize(0,0);
}


void MplayerWindow::translatePos(QPoint p ) {
	int x = p.x() + mplayerlayer->x();
	int y = p.y() + mplayerlayer->y();

	emit mouseMoved( QPoint(x,y) );
}

void MplayerWindow::setOffsetX( int d) {
	offset_x = d;
	mplayerlayer->move( orig_x + offset_x, mplayerlayer->y() );
}

int MplayerWindow::offsetX() { return offset_x; }

void MplayerWindow::setOffsetY( int d) {
	offset_y = d;
	mplayerlayer->move( mplayerlayer->x(), orig_y + offset_y );
}

int MplayerWindow::offsetY() { return offset_y; }

void MplayerWindow::setZoom( double d) {
	zoom_factor = d;
	offset_x = 0;
	offset_y = 0;

	int x = orig_x;
	int y = orig_y;
	int w = orig_width;
	int h = orig_height;

	if (zoom_factor > 1.0) {
		w = w * zoom_factor;
		h = h * zoom_factor;

		// Center
		x = (width() - w) / 2;
		y = (height() -h) / 2;
	}

	mplayerlayer->move(x,y);
	mplayerlayer->resize(w,h);
}

double MplayerWindow::zoom() { return zoom_factor; }

void MplayerWindow::moveLayer( int offset_x, int offset_y ) {
	int x = mplayerlayer->x();
	int y = mplayerlayer->y();

	mplayerlayer->move( x + offset_x, y + offset_y );
}

void MplayerWindow::moveLeft() {
	if (mplayerlayer->x()+mplayerlayer->width() > width() )
		moveLayer( -16, 0 );
}

void MplayerWindow::moveRight() {
	if ( mplayerlayer->x() < 0 )
		moveLayer( +16, 0 );
}

void MplayerWindow::moveUp() {
	if (mplayerlayer->y()+mplayerlayer->height() > height() )
		moveLayer( 0, -16 );
}

void MplayerWindow::moveDown() {
	if ( mplayerlayer->y() < 0 )
		moveLayer( 0, +16 );
}

void MplayerWindow::incZoom() {
	setZoom( zoom_factor + 0.10 );
}

void MplayerWindow::decZoom() {
	double zoom = zoom_factor - 0.10;
	if (zoom < 1.0) zoom = 1.0;
	setZoom( zoom );
}