source: smplayer/trunk/src/mplayerwindow.cpp@ 136

Last change on this file since 136 was 135, checked in by Silvan Scherrer, 13 years ago

SMplayer: update trunk to 0.8.1

  • Property svn:eol-style set to LF
File size: 12.7 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2012 Ricardo Villalba <rvm@users.sourceforge.net>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "mplayerwindow.h"
20#include "global.h"
21#include "desktopinfo.h"
22#include "colorutils.h"
23
24#ifndef MINILIB
25#include "images.h"
26#endif
27
28#include <QLabel>
29#include <QTimer>
30#include <QCursor>
31#include <QEvent>
32#include <QLayout>
33#include <QPixmap>
34#include <QPainter>
35
36#if DELAYED_RESIZE
37#include <QTimer>
38#endif
39
40#if LOGO_ANIMATION
41#include <QPropertyAnimation>
42#endif
43
44Screen::Screen(QWidget* parent, Qt::WindowFlags f) : QWidget(parent, f )
45{
46 setMouseTracking(TRUE);
47 setFocusPolicy( Qt::NoFocus );
48 setMinimumSize( QSize(0,0) );
49
50#if NEW_MOUSE_CHECK_POS
51 mouse_last_position = QPoint(0,0);
52#else
53 cursor_pos = QPoint(0,0);
54 last_cursor_pos = QPoint(0,0);
55#endif
56
57 check_mouse_timer = new QTimer(this);
58 connect( check_mouse_timer, SIGNAL(timeout()), this, SLOT(checkMousePos()) );
59#if !NEW_MOUSE_CHECK_POS
60 check_mouse_timer->start(2000);
61#endif
62
63 // Change attributes
64 setAttribute(Qt::WA_NoSystemBackground);
65 //setAttribute(Qt::WA_StaticContents);
66 //setAttribute( Qt::WA_OpaquePaintEvent );
67 setAttribute(Qt::WA_PaintOnScreen);
68 setAttribute(Qt::WA_PaintUnclipped);
69 //setAttribute(Qt::WA_PaintOutsidePaintEvent);
70
71#if NEW_MOUSE_CHECK_POS
72 setAutoHideInterval(1000);
73 setAutoHideCursor(false);
74#endif
75}
76
77Screen::~Screen() {
78}
79
80void Screen::paintEvent( QPaintEvent * e ) {
81 //qDebug("Screen::paintEvent");
82 QPainter painter(this);
83 painter.eraseRect( e->rect() );
84 //painter.fillRect( e->rect(), QColor(255,0,0) );
85}
86
87#if NEW_MOUSE_CHECK_POS
88void Screen::setAutoHideCursor(bool b) {
89 qDebug("Screen::setAutoHideCursor: %d", b);
90
91 autohide_cursor = b;
92 if (autohide_cursor) {
93 check_mouse_timer->setInterval(autohide_interval);
94 check_mouse_timer->start();
95 } else {
96 check_mouse_timer->stop();
97 }
98}
99
100void Screen::checkMousePos() {
101 //qDebug("Screen::checkMousePos");
102
103 if (!autohide_cursor) {
104 setCursor(QCursor(Qt::ArrowCursor));
105 return;
106 }
107
108 QPoint pos = mapFromGlobal(QCursor::pos());
109
110 //qDebug("Screen::checkMousePos: x: %d, y: %d", pos.x(), pos.y());
111
112 if (mouse_last_position != pos) {
113 setCursor(QCursor(Qt::ArrowCursor));
114 } else {
115 setCursor(QCursor(Qt::BlankCursor));
116 }
117 mouse_last_position = pos;
118}
119#else
120void Screen::checkMousePos() {
121 //qDebug("Screen::checkMousePos");
122
123 if ( cursor_pos == last_cursor_pos ) {
124 //qDebug(" same pos");
125 if (cursor().shape() != Qt::BlankCursor) {
126 //qDebug(" hiding mouse cursor");
127 setCursor(QCursor(Qt::BlankCursor));
128 }
129 } else {
130 last_cursor_pos = cursor_pos;
131 }
132}
133
134void Screen::mouseMoveEvent( QMouseEvent * e ) {
135 //qDebug("Screen::mouseMoveEvent");
136 //qDebug(" pos: x: %d y: %d", e->pos().x(), e->pos().y() );
137 cursor_pos = e->pos();
138
139 if (cursor().shape() != Qt::ArrowCursor) {
140 //qDebug(" showing mouse cursor" );
141 setCursor(QCursor(Qt::ArrowCursor));
142 }
143}
144#endif
145
146void Screen::playingStarted() {
147#if NEW_MOUSE_CHECK_POS
148 qDebug("Screen::playingStarted");
149 setAutoHideCursor(true);
150#endif
151}
152
153void Screen::playingStopped() {
154#if NEW_MOUSE_CHECK_POS
155 qDebug("Screen::playingStopped");
156 setAutoHideCursor(false);
157#endif
158}
159
160/* ---------------------------------------------------------------------- */
161
162MplayerLayer::MplayerLayer(QWidget* parent, Qt::WindowFlags f)
163 : Screen(parent, f)
164{
165#if REPAINT_BACKGROUND_OPTION
166 repaint_background = true;
167#endif
168 playing = false;
169}
170
171MplayerLayer::~MplayerLayer() {
172}
173
174#if REPAINT_BACKGROUND_OPTION
175void MplayerLayer::setRepaintBackground(bool b) {
176 qDebug("MplayerLayer::setRepaintBackground: %d", b);
177 repaint_background = b;
178}
179
180void MplayerLayer::paintEvent( QPaintEvent * e ) {
181 //qDebug("MplayerLayer::paintEvent: allow_clearing: %d", allow_clearing);
182 if (repaint_background || !playing) {
183 //qDebug("MplayerLayer::paintEvent: painting");
184 Screen::paintEvent(e);
185 }
186}
187#endif
188
189void MplayerLayer::playingStarted() {
190 qDebug("MplayerLayer::playingStarted");
191 repaint();
192 playing = true;
193
194 Screen::playingStarted();
195}
196
197void MplayerLayer::playingStopped() {
198 qDebug("MplayerLayer::playingStopped");
199 playing = false;
200 repaint();
201
202 Screen::playingStopped();
203}
204
205/* ---------------------------------------------------------------------- */
206
207MplayerWindow::MplayerWindow(QWidget* parent, Qt::WindowFlags f)
208 : Screen(parent, f) , allow_video_movement(false)
209{
210 offset_x = 0;
211 offset_y = 0;
212 zoom_factor = 1.0;
213
214 setAutoFillBackground(true);
215 ColorUtils::setBackgroundColor( this, QColor(0,0,0) );
216
217 mplayerlayer = new MplayerLayer( this );
218 mplayerlayer->setAutoFillBackground(TRUE);
219
220 logo = new QLabel( mplayerlayer );
221 logo->setAutoFillBackground(TRUE);
222#if QT_VERSION >= 0x040400
223 logo->setAttribute(Qt::WA_NativeWindow); // Otherwise the logo is not visible in Qt 4.4
224#else
225 logo->setAttribute(Qt::WA_PaintOnScreen); // Fixes the problem if compiled with Qt < 4.4
226#endif
227 ColorUtils::setBackgroundColor( logo, QColor(0,0,0) );
228
229 QVBoxLayout * mplayerlayerLayout = new QVBoxLayout( mplayerlayer );
230 mplayerlayerLayout->addWidget( logo, 0, Qt::AlignHCenter | Qt::AlignVCenter );
231
232 aspect = (double) 4 / 3;
233 monitoraspect = 0;
234
235 setSizePolicy( QSizePolicy::Expanding , QSizePolicy::Expanding );
236 setFocusPolicy( Qt::StrongFocus );
237
238 installEventFilter(this);
239 mplayerlayer->installEventFilter(this);
240 //logo->installEventFilter(this);
241
242#if DELAYED_RESIZE
243 resize_timer = new QTimer(this);
244 resize_timer->setSingleShot(true);
245 resize_timer->setInterval(50);
246 connect( resize_timer, SIGNAL(timeout()), this, SLOT(resizeLater()) );
247#endif
248
249 retranslateStrings();
250}
251
252MplayerWindow::~MplayerWindow() {
253}
254
255#if USE_COLORKEY
256void MplayerWindow::setColorKey( QColor c ) {
257 ColorUtils::setBackgroundColor( mplayerlayer, c );
258}
259#endif
260
261void MplayerWindow::retranslateStrings() {
262 //qDebug("MplayerWindow::retranslateStrings");
263#ifndef MINILIB
264 logo->setPixmap( Images::icon("background") );
265#endif
266}
267
268void MplayerWindow::setLogoVisible( bool b) {
269#if !LOGO_ANIMATION
270 logo->setVisible(b);
271#else
272 if (!animated_logo) {
273 logo->setVisible(b);
274 } else {
275 if (b) {
276 logo->show();
277 QPropertyAnimation * animation = new QPropertyAnimation(logo, "pos");
278 animation->setDuration(200);
279 animation->setEasingCurve(QEasingCurve::OutBounce);
280 animation->setStartValue(QPoint(logo->x(), 0 - logo->y()));
281 animation->setEndValue(logo->pos());
282 animation->start();
283 } else {
284 QPropertyAnimation * animation = new QPropertyAnimation(logo, "pos");
285 animation->setDuration(200);
286 animation->setEasingCurve(QEasingCurve::OutBounce);
287 animation->setEndValue(QPoint(width(), logo->y()));
288 animation->setStartValue(logo->pos());
289 animation->start();
290 connect(animation, SIGNAL(finished()), logo, SLOT(hide()));
291 //logo->hide();
292 }
293 }
294#endif
295}
296
297/*
298void MplayerWindow::changePolicy() {
299 setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Preferred );
300}
301*/
302
303void MplayerWindow::setResolution( int w, int h)
304{
305 video_width = w;
306 video_height = h;
307
308 //mplayerlayer->move(1,1);
309 updateVideoWindow();
310}
311
312
313void MplayerWindow::resizeEvent( QResizeEvent * /* e */)
314{
315 /*qDebug("MplayerWindow::resizeEvent: %d, %d",
316 e->size().width(), e->size().height() );*/
317
318#if !DELAYED_RESIZE
319 offset_x = 0;
320 offset_y = 0;
321
322 updateVideoWindow();
323 setZoom(zoom_factor);
324#else
325 resize_timer->start();
326#endif
327}
328
329#if DELAYED_RESIZE
330void MplayerWindow::resizeLater() {
331 offset_x = 0;
332 offset_y = 0;
333
334 updateVideoWindow();
335 setZoom(zoom_factor);
336}
337#endif
338
339void MplayerWindow::setMonitorAspect(double asp) {
340 monitoraspect = asp;
341}
342
343void MplayerWindow::setAspect( double asp) {
344 aspect = asp;
345 if (monitoraspect!=0) {
346 aspect = aspect / monitoraspect * DesktopInfo::desktop_aspectRatio(this);
347 }
348 updateVideoWindow();
349}
350
351
352void MplayerWindow::updateVideoWindow()
353{
354 //qDebug("MplayerWindow::updateVideoWindow");
355
356 //qDebug("aspect= %f", aspect);
357
358 int w_width = size().width();
359 int w_height = size().height();
360
361 int w = w_width;
362 int h = w_height;
363 int x = 0;
364 int y = 0;
365
366 if (aspect != 0) {
367 int pos1_w = w_width;
368 int pos1_h = w_width / aspect + 0.5;
369
370 int pos2_h = w_height;
371 int pos2_w = w_height * aspect + 0.5;
372
373 //qDebug("pos1_w: %d, pos1_h: %d", pos1_w, pos1_h);
374 //qDebug("pos2_w: %d, pos2_h: %d", pos2_w, pos2_h);
375
376 if (pos1_h <= w_height) {
377 //qDebug("Pos1!");
378 w = pos1_w;
379 h = pos1_h;
380
381 y = (w_height - h) /2;
382 } else {
383 //qDebug("Pos2!");
384 w = pos2_w;
385 h = pos2_h;
386
387 x = (w_width - w) /2;
388 }
389 }
390
391 mplayerlayer->move(x,y);
392 mplayerlayer->resize(w, h);
393
394 orig_x = x;
395 orig_y = y;
396 orig_width = w;
397 orig_height = h;
398
399 //qDebug( "w_width: %d, w_height: %d", w_width, w_height);
400 //qDebug("w: %d, h: %d", w,h);
401}
402
403
404void MplayerWindow::mouseReleaseEvent( QMouseEvent * e) {
405 qDebug( "MplayerWindow::mouseReleaseEvent" );
406
407 if (e->button() == Qt::LeftButton) {
408 e->accept();
409 emit leftClicked();
410 }
411 else
412 if (e->button() == Qt::MidButton) {
413 e->accept();
414 emit middleClicked();
415 }
416 else
417 if (e->button() == Qt::XButton1) {
418 e->accept();
419 emit xbutton1Clicked();
420 }
421 else
422 if (e->button() == Qt::XButton2) {
423 e->accept();
424 emit xbutton2Clicked();
425 }
426 else
427 if (e->button() == Qt::RightButton) {
428 e->accept();
429 //emit rightButtonReleased( e->globalPos() );
430 emit rightClicked();
431 }
432 else {
433 e->ignore();
434 }
435}
436
437void MplayerWindow::mouseDoubleClickEvent( QMouseEvent * e ) {
438 if (e->button() == Qt::LeftButton) {
439 e->accept();
440 emit doubleClicked();
441 } else {
442 e->ignore();
443 }
444}
445
446void MplayerWindow::wheelEvent( QWheelEvent * e ) {
447 qDebug("MplayerWindow::wheelEvent: delta: %d", e->delta());
448 e->accept();
449
450 if (e->orientation() == Qt::Vertical) {
451 if (e->delta() >= 0)
452 emit wheelUp();
453 else
454 emit wheelDown();
455 } else {
456 qDebug("MplayerWindow::wheelEvent: horizontal event received, doing nothing");
457 }
458}
459
460bool MplayerWindow::eventFilter( QObject * /*watched*/, QEvent * event ) {
461 //qDebug("MplayerWindow::eventFilter");
462
463 if ( (event->type() == QEvent::MouseMove) ||
464 (event->type() == QEvent::MouseButtonRelease) )
465 {
466 QMouseEvent *mouse_event = static_cast<QMouseEvent *>(event);
467
468 if (event->type() == QEvent::MouseMove) {
469 emit mouseMoved(mouse_event->pos());
470 }
471 }
472
473 return false;
474}
475
476QSize MplayerWindow::sizeHint() const {
477 //qDebug("MplayerWindow::sizeHint");
478 return QSize( video_width, video_height );
479}
480
481QSize MplayerWindow::minimumSizeHint () const {
482 return QSize(0,0);
483}
484
485void MplayerWindow::setOffsetX( int d) {
486 offset_x = d;
487 mplayerlayer->move( orig_x + offset_x, mplayerlayer->y() );
488}
489
490int MplayerWindow::offsetX() { return offset_x; }
491
492void MplayerWindow::setOffsetY( int d) {
493 offset_y = d;
494 mplayerlayer->move( mplayerlayer->x(), orig_y + offset_y );
495}
496
497int MplayerWindow::offsetY() { return offset_y; }
498
499void MplayerWindow::setZoom( double d) {
500 zoom_factor = d;
501 offset_x = 0;
502 offset_y = 0;
503
504 int x = orig_x;
505 int y = orig_y;
506 int w = orig_width;
507 int h = orig_height;
508
509 if (zoom_factor != 1.0) {
510 w = w * zoom_factor;
511 h = h * zoom_factor;
512
513 // Center
514 x = (width() - w) / 2;
515 y = (height() -h) / 2;
516 }
517
518 mplayerlayer->move(x,y);
519 mplayerlayer->resize(w,h);
520}
521
522double MplayerWindow::zoom() { return zoom_factor; }
523
524void MplayerWindow::moveLayer( int offset_x, int offset_y ) {
525 int x = mplayerlayer->x();
526 int y = mplayerlayer->y();
527
528 mplayerlayer->move( x + offset_x, y + offset_y );
529}
530
531void MplayerWindow::moveLeft() {
532 if ((allow_video_movement) || (mplayerlayer->x()+mplayerlayer->width() > width() ))
533 moveLayer( -16, 0 );
534}
535
536void MplayerWindow::moveRight() {
537 if ((allow_video_movement) || ( mplayerlayer->x() < 0 ))
538 moveLayer( +16, 0 );
539}
540
541void MplayerWindow::moveUp() {
542 if ((allow_video_movement) || (mplayerlayer->y()+mplayerlayer->height() > height() ))
543 moveLayer( 0, -16 );
544}
545
546void MplayerWindow::moveDown() {
547 if ((allow_video_movement) || ( mplayerlayer->y() < 0 ))
548 moveLayer( 0, +16 );
549}
550
551void MplayerWindow::incZoom() {
552 setZoom( zoom_factor + ZOOM_STEP );
553}
554
555void MplayerWindow::decZoom() {
556 double zoom = zoom_factor - ZOOM_STEP;
557 if (zoom < ZOOM_MIN) zoom = ZOOM_MIN;
558 setZoom( zoom );
559}
560
561// Language change stuff
562void MplayerWindow::changeEvent(QEvent *e) {
563 if (e->type() == QEvent::LanguageChange) {
564 retranslateStrings();
565 } else {
566 QWidget::changeEvent(e);
567 }
568}
569
570#include "moc_mplayerwindow.cpp"
Note: See TracBrowser for help on using the repository browser.