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

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

SMPlayer: 0.7.1 trunk update

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