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

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

SMPlayer: update trunk to 0.8.6

  • Property svn:eol-style set to LF
File size: 12.7 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->setObjectName("mplayerlayer");
185 mplayerlayer->setAutoFillBackground(TRUE);
186
187 logo = new QLabel( mplayerlayer );
188 logo->setAutoFillBackground(TRUE);
189#if QT_VERSION >= 0x040400
190 logo->setAttribute(Qt::WA_NativeWindow); // Otherwise the logo is not visible in Qt 4.4
191#else
192 logo->setAttribute(Qt::WA_PaintOnScreen); // Fixes the problem if compiled with Qt < 4.4
193#endif
194 ColorUtils::setBackgroundColor( logo, QColor(0,0,0) );
195
196 QVBoxLayout * mplayerlayerLayout = new QVBoxLayout( mplayerlayer );
197 mplayerlayerLayout->addWidget( logo, 0, Qt::AlignHCenter | Qt::AlignVCenter );
198
199 aspect = (double) 4 / 3;
200 monitoraspect = 0;
201
202 setSizePolicy( QSizePolicy::Expanding , QSizePolicy::Expanding );
203 setFocusPolicy( Qt::StrongFocus );
204
205 installEventFilter(this);
206 mplayerlayer->installEventFilter(this);
207 //logo->installEventFilter(this);
208
209#if DELAYED_RESIZE
210 resize_timer = new QTimer(this);
211 resize_timer->setSingleShot(true);
212 resize_timer->setInterval(50);
213 connect( resize_timer, SIGNAL(timeout()), this, SLOT(resizeLater()) );
214#endif
215
216 retranslateStrings();
217}
218
219MplayerWindow::~MplayerWindow() {
220}
221
222#if USE_COLORKEY
223void MplayerWindow::setColorKey( QColor c ) {
224 ColorUtils::setBackgroundColor( mplayerlayer, c );
225}
226#endif
227
228void MplayerWindow::retranslateStrings() {
229 //qDebug("MplayerWindow::retranslateStrings");
230#ifndef MINILIB
231 logo->setPixmap( Images::icon("background") );
232#endif
233}
234
235void MplayerWindow::setLogoVisible( bool b) {
236#if !LOGO_ANIMATION
237 logo->setVisible(b);
238#else
239 if (!animated_logo) {
240 logo->setVisible(b);
241 } else {
242 if (b) {
243 logo->show();
244 QPropertyAnimation * animation = new QPropertyAnimation(logo, "pos");
245 animation->setDuration(200);
246 animation->setEasingCurve(QEasingCurve::OutBounce);
247 animation->setStartValue(QPoint(logo->x(), 0 - logo->y()));
248 animation->setEndValue(logo->pos());
249 animation->start();
250 } else {
251 QPropertyAnimation * animation = new QPropertyAnimation(logo, "pos");
252 animation->setDuration(200);
253 animation->setEasingCurve(QEasingCurve::OutBounce);
254 animation->setEndValue(QPoint(width(), logo->y()));
255 animation->setStartValue(logo->pos());
256 animation->start();
257 connect(animation, SIGNAL(finished()), logo, SLOT(hide()));
258 //logo->hide();
259 }
260 }
261#endif
262}
263
264/*
265void MplayerWindow::changePolicy() {
266 setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Preferred );
267}
268*/
269
270void MplayerWindow::setResolution( int w, int h)
271{
272 video_width = w;
273 video_height = h;
274
275 //mplayerlayer->move(1,1);
276 updateVideoWindow();
277}
278
279
280void MplayerWindow::resizeEvent( QResizeEvent * /* e */)
281{
282 /*qDebug("MplayerWindow::resizeEvent: %d, %d",
283 e->size().width(), e->size().height() );*/
284
285#if !DELAYED_RESIZE
286 offset_x = 0;
287 offset_y = 0;
288
289 updateVideoWindow();
290 setZoom(zoom_factor);
291#else
292 resize_timer->start();
293#endif
294}
295
296#if DELAYED_RESIZE
297void MplayerWindow::resizeLater() {
298 offset_x = 0;
299 offset_y = 0;
300
301 updateVideoWindow();
302 setZoom(zoom_factor);
303}
304#endif
305
306void MplayerWindow::setMonitorAspect(double asp) {
307 monitoraspect = asp;
308}
309
310void MplayerWindow::setAspect( double asp) {
311 aspect = asp;
312 if (monitoraspect!=0) {
313 aspect = aspect / monitoraspect * DesktopInfo::desktop_aspectRatio(this);
314 }
315 updateVideoWindow();
316}
317
318
319void MplayerWindow::updateVideoWindow()
320{
321 //qDebug("MplayerWindow::updateVideoWindow");
322
323 //qDebug("aspect= %f", aspect);
324
325 int w_width = size().width();
326 int w_height = size().height();
327
328 int w = w_width;
329 int h = w_height;
330 int x = 0;
331 int y = 0;
332
333 if (aspect != 0) {
334 int pos1_w = w_width;
335 int pos1_h = w_width / aspect + 0.5;
336
337 int pos2_h = w_height;
338 int pos2_w = w_height * aspect + 0.5;
339
340 //qDebug("pos1_w: %d, pos1_h: %d", pos1_w, pos1_h);
341 //qDebug("pos2_w: %d, pos2_h: %d", pos2_w, pos2_h);
342
343 if (pos1_h <= w_height) {
344 //qDebug("Pos1!");
345 w = pos1_w;
346 h = pos1_h;
347
348 y = (w_height - h) /2;
349 } else {
350 //qDebug("Pos2!");
351 w = pos2_w;
352 h = pos2_h;
353
354 x = (w_width - w) /2;
355 }
356 }
357
358 mplayerlayer->move(x,y);
359 mplayerlayer->resize(w, h);
360
361 orig_x = x;
362 orig_y = y;
363 orig_width = w;
364 orig_height = h;
365
366 //qDebug( "w_width: %d, w_height: %d", w_width, w_height);
367 //qDebug("w: %d, h: %d", w,h);
368}
369
370
371void MplayerWindow::mouseReleaseEvent( QMouseEvent * e) {
372 qDebug( "MplayerWindow::mouseReleaseEvent" );
373
374 if (e->button() == Qt::LeftButton) {
375 e->accept();
376 emit leftClicked();
377 }
378 else
379 if (e->button() == Qt::MidButton) {
380 e->accept();
381 emit middleClicked();
382 }
383 else
384 if (e->button() == Qt::XButton1) {
385 e->accept();
386 emit xbutton1Clicked();
387 }
388 else
389 if (e->button() == Qt::XButton2) {
390 e->accept();
391 emit xbutton2Clicked();
392 }
393 else
394 if (e->button() == Qt::RightButton) {
395 e->accept();
396 //emit rightButtonReleased( e->globalPos() );
397 emit rightClicked();
398 }
399 else {
400 e->ignore();
401 }
402}
403
404void MplayerWindow::mouseDoubleClickEvent( QMouseEvent * e ) {
405 if (e->button() == Qt::LeftButton) {
406 e->accept();
407 emit doubleClicked();
408 } else {
409 e->ignore();
410 }
411}
412
413void MplayerWindow::wheelEvent( QWheelEvent * e ) {
414 qDebug("MplayerWindow::wheelEvent: delta: %d", e->delta());
415 e->accept();
416
417 if (e->orientation() == Qt::Vertical) {
418 if (e->delta() >= 0)
419 emit wheelUp();
420 else
421 emit wheelDown();
422 } else {
423 qDebug("MplayerWindow::wheelEvent: horizontal event received, doing nothing");
424 }
425}
426
427bool MplayerWindow::eventFilter( QObject * watched, QEvent * event ) {
428 //qDebug("MplayerWindow::eventFilter: watched: %s", watched->objectName().toUtf8().constData());
429
430 if ( (event->type() == QEvent::MouseMove) ||
431 (event->type() == QEvent::MouseButtonRelease) )
432 {
433 QMouseEvent *mouse_event = static_cast<QMouseEvent *>(event);
434
435 if (event->type() == QEvent::MouseMove) {
436 QPoint pos = mouse_event->pos();
437 if (watched->objectName()=="mplayerlayer") {
438 QWidget *widget = static_cast<QWidget *>(watched);
439 pos = widget->mapToParent(pos);
440 }
441 emit mouseMoved(pos);
442
443 if ( mouse_event->buttons().testFlag(Qt::LeftButton)) {
444 emit mouseMovedDiff( mouse_event->globalPos() - mouse_press_pos);
445 mouse_press_pos = mouse_event->globalPos();
446 /* qDebug("MplayerWindow::eventFilter: mouse_press_pos: x: %d y: %d", mouse_press_pos.x(), mouse_press_pos.y()); */
447 }
448 }
449 }
450
451 if (event->type() == QEvent::MouseButtonPress) {
452 QMouseEvent *mouse_event = static_cast<QMouseEvent *>(event);
453 mouse_press_pos = mouse_event->globalPos();
454 }
455
456 return false;
457}
458
459QSize MplayerWindow::sizeHint() const {
460 //qDebug("MplayerWindow::sizeHint");
461 return QSize( video_width, video_height );
462}
463
464QSize MplayerWindow::minimumSizeHint () const {
465 return QSize(0,0);
466}
467
468void MplayerWindow::setOffsetX( int d) {
469 offset_x = d;
470 mplayerlayer->move( orig_x + offset_x, mplayerlayer->y() );
471}
472
473int MplayerWindow::offsetX() { return offset_x; }
474
475void MplayerWindow::setOffsetY( int d) {
476 offset_y = d;
477 mplayerlayer->move( mplayerlayer->x(), orig_y + offset_y );
478}
479
480int MplayerWindow::offsetY() { return offset_y; }
481
482void MplayerWindow::setZoom( double d) {
483 zoom_factor = d;
484 offset_x = 0;
485 offset_y = 0;
486
487 int x = orig_x;
488 int y = orig_y;
489 int w = orig_width;
490 int h = orig_height;
491
492 if (zoom_factor != 1.0) {
493 w = w * zoom_factor;
494 h = h * zoom_factor;
495
496 // Center
497 x = (width() - w) / 2;
498 y = (height() -h) / 2;
499 }
500
501 mplayerlayer->move(x,y);
502 mplayerlayer->resize(w,h);
503}
504
505double MplayerWindow::zoom() { return zoom_factor; }
506
507void MplayerWindow::moveLayer( int offset_x, int offset_y ) {
508 int x = mplayerlayer->x();
509 int y = mplayerlayer->y();
510
511 mplayerlayer->move( x + offset_x, y + offset_y );
512}
513
514void MplayerWindow::moveLeft() {
515 if ((allow_video_movement) || (mplayerlayer->x()+mplayerlayer->width() > width() ))
516 moveLayer( -16, 0 );
517}
518
519void MplayerWindow::moveRight() {
520 if ((allow_video_movement) || ( mplayerlayer->x() < 0 ))
521 moveLayer( +16, 0 );
522}
523
524void MplayerWindow::moveUp() {
525 if ((allow_video_movement) || (mplayerlayer->y()+mplayerlayer->height() > height() ))
526 moveLayer( 0, -16 );
527}
528
529void MplayerWindow::moveDown() {
530 if ((allow_video_movement) || ( mplayerlayer->y() < 0 ))
531 moveLayer( 0, +16 );
532}
533
534void MplayerWindow::incZoom() {
535 setZoom( zoom_factor + ZOOM_STEP );
536}
537
538void MplayerWindow::decZoom() {
539 double zoom = zoom_factor - ZOOM_STEP;
540 if (zoom < ZOOM_MIN) zoom = ZOOM_MIN;
541 setZoom( zoom );
542}
543
544// Language change stuff
545void MplayerWindow::changeEvent(QEvent *e) {
546 if (e->type() == QEvent::LanguageChange) {
547 retranslateStrings();
548 } else {
549 QWidget::changeEvent(e);
550 }
551}
552
553#include "moc_mplayerwindow.cpp"
Note: See TracBrowser for help on using the repository browser.