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

Last change on this file since 117 was 112, checked in by Silvan Scherrer, 15 years ago

Smplayer: eol-style

  • Property svn:eol-style set to LF
File size: 11.9 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2010 Ricardo Villalba <rvm@escomposlinux.org>
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::showLogo( bool b)
265{
266 if (b) logo->show(); else logo->hide();
267}
268
269/*
270void MplayerWindow::changePolicy() {
271 setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Preferred );
272}
273*/
274
275void MplayerWindow::setResolution( int w, int h)
276{
277 video_width = w;
278 video_height = h;
279
280 //mplayerlayer->move(1,1);
281 updateVideoWindow();
282}
283
284
285void MplayerWindow::resizeEvent( QResizeEvent * /* e */)
286{
287 /*qDebug("MplayerWindow::resizeEvent: %d, %d",
288 e->size().width(), e->size().height() );*/
289
290#if !DELAYED_RESIZE
291 offset_x = 0;
292 offset_y = 0;
293
294 updateVideoWindow();
295 setZoom(zoom_factor);
296#else
297 resize_timer->start();
298#endif
299}
300
301#if DELAYED_RESIZE
302void MplayerWindow::resizeLater() {
303 offset_x = 0;
304 offset_y = 0;
305
306 updateVideoWindow();
307 setZoom(zoom_factor);
308}
309#endif
310
311void MplayerWindow::setMonitorAspect(double asp) {
312 monitoraspect = asp;
313}
314
315void MplayerWindow::setAspect( double asp) {
316 aspect = asp;
317 if (monitoraspect!=0) {
318 aspect = aspect / monitoraspect * DesktopInfo::desktop_aspectRatio(this);
319 }
320 updateVideoWindow();
321}
322
323
324void MplayerWindow::updateVideoWindow()
325{
326 //qDebug("MplayerWindow::updateVideoWindow");
327
328 //qDebug("aspect= %f", aspect);
329
330 int w_width = size().width();
331 int w_height = size().height();
332
333 int w = w_width;
334 int h = w_height;
335 int x = 0;
336 int y = 0;
337
338 if (aspect != 0) {
339 int pos1_w = w_width;
340 int pos1_h = w_width / aspect + 0.5;
341
342 int pos2_h = w_height;
343 int pos2_w = w_height * aspect + 0.5;
344
345 //qDebug("pos1_w: %d, pos1_h: %d", pos1_w, pos1_h);
346 //qDebug("pos2_w: %d, pos2_h: %d", pos2_w, pos2_h);
347
348 if (pos1_h <= w_height) {
349 //qDebug("Pos1!");
350 w = pos1_w;
351 h = pos1_h;
352
353 y = (w_height - h) /2;
354 } else {
355 //qDebug("Pos2!");
356 w = pos2_w;
357 h = pos2_h;
358
359 x = (w_width - w) /2;
360 }
361 }
362
363 mplayerlayer->move(x,y);
364 mplayerlayer->resize(w, h);
365
366 orig_x = x;
367 orig_y = y;
368 orig_width = w;
369 orig_height = h;
370
371 //qDebug( "w_width: %d, w_height: %d", w_width, w_height);
372 //qDebug("w: %d, h: %d", w,h);
373}
374
375
376void MplayerWindow::mouseReleaseEvent( QMouseEvent * e) {
377 qDebug( "MplayerWindow::mouseReleaseEvent" );
378
379 if (e->button() == Qt::LeftButton) {
380 e->accept();
381 emit leftClicked();
382 }
383 else
384 if (e->button() == Qt::MidButton) {
385 e->accept();
386 emit middleClicked();
387 }
388 else
389 if (e->button() == Qt::XButton1) {
390 e->accept();
391 emit xbutton1Clicked();
392 }
393 else
394 if (e->button() == Qt::XButton2) {
395 e->accept();
396 emit xbutton2Clicked();
397 }
398 else
399 if (e->button() == Qt::RightButton) {
400 e->accept();
401 //emit rightButtonReleased( e->globalPos() );
402 emit rightClicked();
403 }
404 else {
405 e->ignore();
406 }
407}
408
409void MplayerWindow::mouseDoubleClickEvent( QMouseEvent * e ) {
410 if (e->button() == Qt::LeftButton) {
411 e->accept();
412 emit doubleClicked();
413 } else {
414 e->ignore();
415 }
416}
417
418void MplayerWindow::wheelEvent( QWheelEvent * e ) {
419 qDebug("MplayerWindow::wheelEvent: delta: %d", e->delta());
420 e->accept();
421
422 if (e->orientation() == Qt::Vertical) {
423 if (e->delta() >= 0)
424 emit wheelUp();
425 else
426 emit wheelDown();
427 } else {
428 qDebug("MplayerWindow::wheelEvent: horizontal event received, doing nothing");
429 }
430}
431
432bool MplayerWindow::eventFilter( QObject * /*watched*/, QEvent * event ) {
433 //qDebug("MplayerWindow::eventFilter");
434
435 if ( (event->type() == QEvent::MouseMove) ||
436 (event->type() == QEvent::MouseButtonRelease) )
437 {
438 QMouseEvent *mouse_event = static_cast<QMouseEvent *>(event);
439
440 if (event->type() == QEvent::MouseMove) {
441 emit mouseMoved(mouse_event->pos());
442 }
443 }
444
445 return false;
446}
447
448QSize MplayerWindow::sizeHint() const {
449 //qDebug("MplayerWindow::sizeHint");
450 return QSize( video_width, video_height );
451}
452
453QSize MplayerWindow::minimumSizeHint () const {
454 return QSize(0,0);
455}
456
457void MplayerWindow::setOffsetX( int d) {
458 offset_x = d;
459 mplayerlayer->move( orig_x + offset_x, mplayerlayer->y() );
460}
461
462int MplayerWindow::offsetX() { return offset_x; }
463
464void MplayerWindow::setOffsetY( int d) {
465 offset_y = d;
466 mplayerlayer->move( mplayerlayer->x(), orig_y + offset_y );
467}
468
469int MplayerWindow::offsetY() { return offset_y; }
470
471void MplayerWindow::setZoom( double d) {
472 zoom_factor = d;
473 offset_x = 0;
474 offset_y = 0;
475
476 int x = orig_x;
477 int y = orig_y;
478 int w = orig_width;
479 int h = orig_height;
480
481 if (zoom_factor != 1.0) {
482 w = w * zoom_factor;
483 h = h * zoom_factor;
484
485 // Center
486 x = (width() - w) / 2;
487 y = (height() -h) / 2;
488 }
489
490 mplayerlayer->move(x,y);
491 mplayerlayer->resize(w,h);
492}
493
494double MplayerWindow::zoom() { return zoom_factor; }
495
496void MplayerWindow::moveLayer( int offset_x, int offset_y ) {
497 int x = mplayerlayer->x();
498 int y = mplayerlayer->y();
499
500 mplayerlayer->move( x + offset_x, y + offset_y );
501}
502
503void MplayerWindow::moveLeft() {
504 if ((allow_video_movement) || (mplayerlayer->x()+mplayerlayer->width() > width() ))
505 moveLayer( -16, 0 );
506}
507
508void MplayerWindow::moveRight() {
509 if ((allow_video_movement) || ( mplayerlayer->x() < 0 ))
510 moveLayer( +16, 0 );
511}
512
513void MplayerWindow::moveUp() {
514 if ((allow_video_movement) || (mplayerlayer->y()+mplayerlayer->height() > height() ))
515 moveLayer( 0, -16 );
516}
517
518void MplayerWindow::moveDown() {
519 if ((allow_video_movement) || ( mplayerlayer->y() < 0 ))
520 moveLayer( 0, +16 );
521}
522
523void MplayerWindow::incZoom() {
524 setZoom( zoom_factor + ZOOM_STEP );
525}
526
527void MplayerWindow::decZoom() {
528 double zoom = zoom_factor - ZOOM_STEP;
529 if (zoom < ZOOM_MIN) zoom = ZOOM_MIN;
530 setZoom( zoom );
531}
532
533// Language change stuff
534void MplayerWindow::changeEvent(QEvent *e) {
535 if (e->type() == QEvent::LanguageChange) {
536 retranslateStrings();
537 } else {
538 QWidget::changeEvent(e);
539 }
540}
541
542#include "moc_mplayerwindow.cpp"
Note: See TracBrowser for help on using the repository browser.