source: smplayer/trunk/src/mplayerwindow.cpp

Last change on this file was 188, checked in by Silvan Scherrer, 8 years ago

SMPlayer: update trunk to version 17.1.0

  • Property svn:eol-style set to LF
File size: 15.1 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2017 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#include <QApplication>
36#include <QTimer>
37#include <QDebug>
38
39#if LOGO_ANIMATION
40#include <QPropertyAnimation>
41#endif
42
43//#define HANDLE_GESTURES
44
45#ifdef HANDLE_GESTURES
46#include <QGestureEvent>
47#include <QTapGesture>
48#endif
49
50Screen::Screen(QWidget* parent, Qt::WindowFlags f)
51 : QWidget(parent, f )
52 , check_mouse_timer(0)
53 , mouse_last_position(QPoint(0,0))
54 , autohide_cursor(false)
55 , autohide_interval(0)
56{
57 setMouseTracking(true);
58 setFocusPolicy( Qt::NoFocus );
59 setMinimumSize( QSize(0,0) );
60
61 check_mouse_timer = new QTimer(this);
62 connect( check_mouse_timer, SIGNAL(timeout()), this, SLOT(checkMousePos()) );
63
64 setAutoHideInterval(1000);
65 setAutoHideCursor(false);
66}
67
68Screen::~Screen() {
69}
70
71void Screen::setAutoHideCursor(bool b) {
72 qDebug("Screen::setAutoHideCursor: %d", b);
73
74 autohide_cursor = b;
75 if (autohide_cursor) {
76 check_mouse_timer->setInterval(autohide_interval);
77 check_mouse_timer->start();
78 } else {
79 check_mouse_timer->stop();
80 }
81}
82
83void Screen::checkMousePos() {
84 //qDebug("Screen::checkMousePos");
85
86 if (!autohide_cursor) {
87 setCursor(QCursor(Qt::ArrowCursor));
88 return;
89 }
90
91 QPoint pos = mapFromGlobal(QCursor::pos());
92
93 //qDebug("Screen::checkMousePos: x: %d, y: %d", pos.x(), pos.y());
94
95 if (mouse_last_position != pos) {
96 setCursor(QCursor(Qt::ArrowCursor));
97 } else {
98 setCursor(QCursor(Qt::BlankCursor));
99 }
100 mouse_last_position = pos;
101}
102
103void Screen::mouseMoveEvent( QMouseEvent * e ) {
104 //qDebug("Screen::mouseMoveEvent");
105 emit mouseMoved(e->pos());
106
107 if (cursor().shape() != Qt::ArrowCursor) {
108 //qDebug(" showing mouse cursor" );
109 setCursor(QCursor(Qt::ArrowCursor));
110 }
111}
112
113void Screen::playingStarted() {
114 qDebug("Screen::playingStarted");
115 setAutoHideCursor(true);
116}
117
118void Screen::playingStopped() {
119 qDebug("Screen::playingStopped");
120 setAutoHideCursor(false);
121}
122
123/* ---------------------------------------------------------------------- */
124
125MplayerLayer::MplayerLayer(QWidget* parent, Qt::WindowFlags f)
126 : Screen(parent, f)
127#if REPAINT_BACKGROUND_OPTION
128 , repaint_background(false)
129#endif
130 , playing(false)
131{
132}
133
134MplayerLayer::~MplayerLayer() {
135}
136
137#if REPAINT_BACKGROUND_OPTION
138void MplayerLayer::setRepaintBackground(bool b) {
139 qDebug("MplayerLayer::setRepaintBackground: %d", b);
140 repaint_background = b;
141}
142#endif
143
144void MplayerLayer::playingStarted() {
145 qDebug("MplayerLayer::playingStarted");
146// repaint();
147 playing = true;
148
149#ifndef Q_OS_WIN
150 if (!repaint_background) setUpdatesEnabled(false);
151#endif
152
153 Screen::playingStarted();
154}
155
156void MplayerLayer::playingStopped() {
157 qDebug("MplayerLayer::playingStopped");
158 playing = false;
159
160#ifndef Q_OS_WIN
161 setUpdatesEnabled(true);
162#endif
163
164// repaint();
165 Screen::playingStopped();
166}
167
168/* ---------------------------------------------------------------------- */
169
170MplayerWindow::MplayerWindow(QWidget* parent, Qt::WindowFlags f)
171 : Screen(parent, f)
172 , video_width(0)
173 , video_height(0)
174 , aspect((double) 4/3)
175 , monitoraspect(0)
176 , mplayerlayer(0)
177 , logo(0)
178 , offset_x(0)
179 , offset_y(0)
180 , zoom_factor(1.0)
181 , orig_x(0)
182 , orig_y(0)
183 , orig_width(0)
184 , orig_height(0)
185 , allow_video_movement(false)
186#if DELAYED_RESIZE
187 , resize_timer(0)
188#endif
189 , delay_left_click(false)
190 , left_click_timer(0)
191 , double_clicked(false)
192#if LOGO_ANIMATION
193 , animated_logo(false)
194#endif
195 , corner_widget(0)
196 , drag_state(NOT_DRAGGING)
197 , start_drag(QPoint(0,0))
198 , mouse_drag_tracking(false)
199{
200 mplayerlayer = new MplayerLayer(this);
201 mplayerlayer->setObjectName("mplayerlayer");
202
203 logo = new QLabel( mplayerlayer );
204 logo->setObjectName("mplayerwindowlogo");
205
206 // Set colors
207#ifdef CHANGE_WIDGET_COLOR
208 setAutoFillBackground(true);
209 ColorUtils::setBackgroundColor( this, QColor(0,0,0) );
210 mplayerlayer->setAutoFillBackground(true);
211 logo->setAutoFillBackground(true);
212 ColorUtils::setBackgroundColor( logo, QColor(0,0,0) );
213#else
214 setStyleSheet("MplayerWindow { background-color: black;}");
215 mplayerlayer->setStyleSheet("background-color: black;");
216#endif
217
218 QVBoxLayout * mplayerlayerLayout = new QVBoxLayout( mplayerlayer );
219 mplayerlayerLayout->addWidget( logo, 0, Qt::AlignHCenter | Qt::AlignVCenter );
220
221 setSizePolicy( QSizePolicy::Expanding , QSizePolicy::Expanding );
222 setFocusPolicy( Qt::StrongFocus );
223
224//#ifdef HANDLE_GESTURES
225 grabGesture(Qt::TapGesture);
226//#endif
227
228 installEventFilter(this);
229 mplayerlayer->installEventFilter(this);
230 //logo->installEventFilter(this);
231
232#if DELAYED_RESIZE
233 resize_timer = new QTimer(this);
234 resize_timer->setSingleShot(true);
235 resize_timer->setInterval(50);
236 connect( resize_timer, SIGNAL(timeout()), this, SLOT(resizeLater()) );
237#endif
238
239 left_click_timer = new QTimer(this);
240 left_click_timer->setSingleShot(true);
241 left_click_timer->setInterval(qApp->doubleClickInterval()+10);
242 connect(left_click_timer, SIGNAL(timeout()), this, SIGNAL(leftClicked()));
243
244 retranslateStrings();
245}
246
247MplayerWindow::~MplayerWindow() {
248}
249
250void MplayerWindow::setCornerWidget(QWidget * w) {
251 corner_widget = w;
252
253 QHBoxLayout * blayout = new QHBoxLayout;
254 blayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));
255 blayout->addWidget(corner_widget);
256
257 QVBoxLayout * layout = new QVBoxLayout(this);
258 layout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Expanding));
259 layout->addLayout(blayout);
260}
261
262#if USE_COLORKEY
263void MplayerWindow::setColorKey( QColor c ) {
264 #ifdef CHANGE_WIDGET_COLOR
265 ColorUtils::setBackgroundColor( mplayerlayer, c );
266 #endif
267}
268#endif
269
270void MplayerWindow::retranslateStrings() {
271 //qDebug("MplayerWindow::retranslateStrings");
272#ifndef MINILIB
273 logo->setPixmap( Images::icon("background") );
274#endif
275}
276
277void MplayerWindow::setLogoVisible( bool b) {
278 qDebug() << "MplayerWindow::setLogoVisible:" << b;
279
280#if REPAINT_BACKGROUND_OPTION
281 if (b) mplayerlayer->setUpdatesEnabled(true);
282#endif
283
284 if (corner_widget) {
285 corner_widget->setVisible(b);
286 }
287
288#if !LOGO_ANIMATION
289 logo->setVisible(b);
290#else
291 if (!animated_logo) {
292 logo->setVisible(b);
293 } else {
294 if (b) {
295 logo->show();
296 QPropertyAnimation * animation = new QPropertyAnimation(logo, "pos");
297 animation->setDuration(200);
298 animation->setEasingCurve(QEasingCurve::OutBounce);
299 animation->setStartValue(QPoint(logo->x(), 0 - logo->y()));
300 animation->setEndValue(logo->pos());
301 animation->start();
302 } else {
303 QPropertyAnimation * animation = new QPropertyAnimation(logo, "pos");
304 animation->setDuration(200);
305 animation->setEasingCurve(QEasingCurve::OutBounce);
306 animation->setEndValue(QPoint(width(), logo->y()));
307 animation->setStartValue(logo->pos());
308 animation->start();
309 connect(animation, SIGNAL(finished()), logo, SLOT(hide()));
310 //logo->hide();
311 }
312 }
313#endif
314}
315
316/*
317void MplayerWindow::changePolicy() {
318 setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Preferred );
319}
320*/
321
322void MplayerWindow::setResolution( int w, int h)
323{
324 video_width = w;
325 video_height = h;
326
327 //mplayerlayer->move(1,1);
328 updateVideoWindow();
329}
330
331
332void MplayerWindow::resizeEvent( QResizeEvent * /* e */)
333{
334 /*qDebug("MplayerWindow::resizeEvent: %d, %d",
335 e->size().width(), e->size().height() );*/
336
337#if !DELAYED_RESIZE
338 offset_x = 0;
339 offset_y = 0;
340
341 updateVideoWindow();
342 setZoom(zoom_factor);
343#else
344 resize_timer->start();
345#endif
346}
347
348#if DELAYED_RESIZE
349void MplayerWindow::resizeLater() {
350 offset_x = 0;
351 offset_y = 0;
352
353 updateVideoWindow();
354 setZoom(zoom_factor);
355}
356#endif
357
358void MplayerWindow::setMonitorAspect(double asp) {
359 monitoraspect = asp;
360}
361
362void MplayerWindow::setAspect( double asp) {
363 aspect = asp;
364 if (monitoraspect!=0) {
365 aspect = aspect / monitoraspect * DesktopInfo::desktop_aspectRatio(this);
366 }
367 updateVideoWindow();
368}
369
370
371void MplayerWindow::updateVideoWindow()
372{
373 //qDebug("MplayerWindow::updateVideoWindow");
374
375 //qDebug("aspect= %f", aspect);
376
377 int w_width = size().width();
378 int w_height = size().height();
379
380 int w = w_width;
381 int h = w_height;
382 int x = 0;
383 int y = 0;
384
385 if (aspect != 0) {
386 int pos1_w = w_width;
387 int pos1_h = w_width / aspect + 0.5;
388
389 int pos2_h = w_height;
390 int pos2_w = w_height * aspect + 0.5;
391
392 //qDebug("pos1_w: %d, pos1_h: %d", pos1_w, pos1_h);
393 //qDebug("pos2_w: %d, pos2_h: %d", pos2_w, pos2_h);
394
395 if (pos1_h <= w_height) {
396 //qDebug("Pos1!");
397 w = pos1_w;
398 h = pos1_h;
399
400 y = (w_height - h) /2;
401 } else {
402 //qDebug("Pos2!");
403 w = pos2_w;
404 h = pos2_h;
405
406 x = (w_width - w) /2;
407 }
408 }
409
410 mplayerlayer->move(x,y);
411 mplayerlayer->resize(w, h);
412
413 orig_x = x;
414 orig_y = y;
415 orig_width = w;
416 orig_height = h;
417
418 //qDebug( "w_width: %d, w_height: %d", w_width, w_height);
419 //qDebug("w: %d, h: %d", w,h);
420}
421
422
423void MplayerWindow::mouseReleaseEvent( QMouseEvent * e) {
424 qDebug( "MplayerWindow::mouseReleaseEvent" );
425
426 if (e->button() == Qt::LeftButton) {
427 e->accept();
428 if (delay_left_click) {
429 if (!double_clicked) left_click_timer->start();
430 double_clicked = false;
431 } else {
432 emit leftClicked();
433 }
434 }
435 else
436 if (e->button() == Qt::MidButton) {
437 e->accept();
438 emit middleClicked();
439 }
440 else
441 if (e->button() == Qt::XButton1) {
442 e->accept();
443 emit xbutton1Clicked();
444 }
445 else
446 if (e->button() == Qt::XButton2) {
447 e->accept();
448 emit xbutton2Clicked();
449 }
450 else
451 if (e->button() == Qt::RightButton) {
452 e->accept();
453 //emit rightButtonReleased( e->globalPos() );
454 emit rightClicked();
455 }
456 else {
457 e->ignore();
458 }
459}
460
461void MplayerWindow::mouseDoubleClickEvent( QMouseEvent * e ) {
462 if (e->button() == Qt::LeftButton) {
463 e->accept();
464 if (delay_left_click) {
465 left_click_timer->stop();
466 double_clicked = true;
467 }
468 emit doubleClicked();
469 } else {
470 e->ignore();
471 }
472}
473
474void MplayerWindow::wheelEvent( QWheelEvent * e ) {
475 qDebug("MplayerWindow::wheelEvent: delta: %d", e->delta());
476 e->accept();
477
478 if (e->orientation() == Qt::Vertical) {
479 if (e->delta() >= 0)
480 emit wheelUp();
481 else
482 emit wheelDown();
483 } else {
484 qDebug("MplayerWindow::wheelEvent: horizontal event received, doing nothing");
485 }
486}
487
488bool MplayerWindow::eventFilter( QObject * object, QEvent * event ) {
489#ifdef HANDLE_GESTURES
490 if (event->type() == QEvent::Gesture) {
491 qDebug() << "MplayerWindow::eventFilter: event:" << event;
492 QGestureEvent * ge = static_cast<QGestureEvent*>(event);
493 qDebug() << "MplayerWindow::eventFilter: ge:" << ge;
494 if (QGesture * tap = ge->gesture(Qt::TapGesture)) {
495 QTapGesture * tg = static_cast<QTapGesture *>(tap);
496 qDebug() << "MplayerWindow::eventFilter: tg:" << tg;
497 event->setAccepted(true);
498 return false;
499 }
500 }
501#endif
502
503 if (!mouse_drag_tracking)
504 return false;
505
506 QEvent::Type type = event->type();
507 if (type != QEvent::MouseButtonPress
508 && type != QEvent::MouseButtonRelease
509 && type != QEvent::MouseMove)
510 return false;
511
512 QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
513 if (!mouseEvent)
514 return false;
515
516 if (mouseEvent->modifiers() != Qt::NoModifier) {
517 drag_state = NOT_DRAGGING;
518 return false;
519 }
520
521 if (type == QEvent::MouseButtonPress) {
522 if (mouseEvent->button() != Qt::LeftButton) {
523 drag_state = NOT_DRAGGING;
524 return false;
525 }
526
527 drag_state = START_DRAGGING;
528 start_drag = mouseEvent->globalPos();
529 // Don't filter, so others can have a look at it too
530 return false;
531 }
532
533 if (type == QEvent::MouseButtonRelease) {
534 if (drag_state != DRAGGING || mouseEvent->button() != Qt::LeftButton) {
535 drag_state = NOT_DRAGGING;
536 return false;
537 }
538
539 // Stop dragging and eat event
540 drag_state = NOT_DRAGGING;
541 event->accept();
542 return true;
543 }
544
545 // type == QEvent::MouseMove
546 if (drag_state == NOT_DRAGGING)
547 return false;
548
549 // buttons() note the s
550 if (mouseEvent->buttons() != Qt::LeftButton) {
551 drag_state = NOT_DRAGGING;
552 return false;
553 }
554
555 QPoint pos = mouseEvent->globalPos();
556 QPoint diff = pos - start_drag;
557 if (drag_state == START_DRAGGING) {
558 // Don't start dragging before moving at least DRAG_THRESHOLD pixels
559 if (abs(diff.x()) < DRAG_THRESHOLD && abs(diff.y()) < DRAG_THRESHOLD)
560 return false;
561
562 drag_state = DRAGGING;
563 }
564
565 emit mouseMovedDiff(diff);
566 start_drag = pos;
567 event->accept();
568 return true;
569}
570
571QSize MplayerWindow::sizeHint() const {
572 //qDebug("MplayerWindow::sizeHint");
573 return QSize( video_width, video_height );
574}
575
576QSize MplayerWindow::minimumSizeHint () const {
577 return QSize(0,0);
578}
579
580void MplayerWindow::setOffsetX( int d) {
581 offset_x = d;
582 mplayerlayer->move( orig_x + offset_x, mplayerlayer->y() );
583}
584
585int MplayerWindow::offsetX() { return offset_x; }
586
587void MplayerWindow::setOffsetY( int d) {
588 offset_y = d;
589 mplayerlayer->move( mplayerlayer->x(), orig_y + offset_y );
590}
591
592int MplayerWindow::offsetY() { return offset_y; }
593
594void MplayerWindow::setZoom( double d) {
595 zoom_factor = d;
596 offset_x = 0;
597 offset_y = 0;
598
599 int x = orig_x;
600 int y = orig_y;
601 int w = orig_width;
602 int h = orig_height;
603
604 if (zoom_factor != 1.0) {
605 w = w * zoom_factor;
606 h = h * zoom_factor;
607
608 // Center
609 x = (width() - w) / 2;
610 y = (height() -h) / 2;
611 }
612
613 mplayerlayer->move(x,y);
614 mplayerlayer->resize(w,h);
615}
616
617double MplayerWindow::zoom() { return zoom_factor; }
618
619void MplayerWindow::moveLayer( int offset_x, int offset_y ) {
620 int x = mplayerlayer->x();
621 int y = mplayerlayer->y();
622
623 mplayerlayer->move( x + offset_x, y + offset_y );
624}
625
626void MplayerWindow::moveLeft() {
627 if ((allow_video_movement) || (mplayerlayer->x()+mplayerlayer->width() > width() ))
628 moveLayer( -16, 0 );
629}
630
631void MplayerWindow::moveRight() {
632 if ((allow_video_movement) || ( mplayerlayer->x() < 0 ))
633 moveLayer( +16, 0 );
634}
635
636void MplayerWindow::moveUp() {
637 if ((allow_video_movement) || (mplayerlayer->y()+mplayerlayer->height() > height() ))
638 moveLayer( 0, -16 );
639}
640
641void MplayerWindow::moveDown() {
642 if ((allow_video_movement) || ( mplayerlayer->y() < 0 ))
643 moveLayer( 0, +16 );
644}
645
646void MplayerWindow::incZoom() {
647 setZoom( zoom_factor + ZOOM_STEP );
648}
649
650void MplayerWindow::decZoom() {
651 double zoom = zoom_factor - ZOOM_STEP;
652 if (zoom < ZOOM_MIN) zoom = ZOOM_MIN;
653 setZoom( zoom );
654}
655
656// Language change stuff
657void MplayerWindow::changeEvent(QEvent *e) {
658 if (e->type() == QEvent::LanguageChange) {
659 retranslateStrings();
660 } else {
661 Screen::changeEvent(e);
662 }
663}
664
665#include "moc_mplayerwindow.cpp"
Note: See TracBrowser for help on using the repository browser.