~ubuntu-branches/debian/experimental/smplayer/experimental

« back to all changes in this revision

Viewing changes to src/mplayerwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Maia Kozheva
  • Date: 2009-01-03 17:08:06 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20090103170806-eodntb2slv6g2pb6
Tags: 0.6.6-0ubuntu1
* The "just before FF" release.
* New upstream release.
* debian/control: Bumped Standards-Version to 3.8.0.
* debian/copyright: Changed (C) to © to fix Lintian warning.

Show diffs side-by-side

added added

removed removed

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