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

Last change on this file since 168 was 165, checked in by Silvan Scherrer, 11 years ago

SMPlayer: update trunk to latest 0.8.7

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