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

Last change on this file since 149 was 142, checked in by Silvan Scherrer, 12 years ago

SMPlayer: update trunk to 0.8.5

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