Home | History | Annotate | Download | only in WebCoreSupport
      1 /*
      2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
      3  *
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include "FullScreenVideoWidget.h"
     29 
     30 #include <QApplication>
     31 #include <QCloseEvent>
     32 #include <QDesktopWidget>
     33 #include <QKeyEvent>
     34 #include <QMediaPlayer>
     35 
     36 static const int gHideMouseCursorDelay = 3000;
     37 
     38 namespace WebCore {
     39 
     40 FullScreenVideoWidget::FullScreenVideoWidget()
     41     : QVideoWidget()
     42     , m_mediaPlayer(0)
     43 {
     44     QPalette palette(this->palette());
     45     palette.setColor(QPalette::Base, Qt::black);
     46     palette.setColor(QPalette::Background, Qt::black);
     47     setPalette(palette);
     48 
     49     setWindowModality(Qt::ApplicationModal);
     50     setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
     51 
     52     setGeometry(QApplication::desktop()->screenGeometry());
     53 
     54     m_cursorTimer.setSingleShot(true);
     55     connect(&m_cursorTimer, SIGNAL(timeout()), this, SLOT(hideCursor()));
     56 }
     57 
     58 FullScreenVideoWidget::~FullScreenVideoWidget()
     59 {
     60 }
     61 
     62 void FullScreenVideoWidget::show(QMediaPlayer* player)
     63 {
     64     m_mediaPlayer = player;
     65     showFullScreen();
     66     setMouseTracking(true);
     67     raise();
     68     m_mediaPlayer->setVideoOutput(this);
     69     setFocus();
     70     grabMouse();
     71     hideCursor();
     72 }
     73 
     74 void FullScreenVideoWidget::closeEvent(QCloseEvent* event)
     75 {
     76     m_mediaPlayer = 0;
     77     m_cursorTimer.stop();
     78     setMouseTracking(false);
     79     releaseMouse();
     80     QApplication::restoreOverrideCursor();
     81     event->ignore();
     82     hide();
     83     emit didExitFullScreen();
     84 }
     85 
     86 bool FullScreenVideoWidget::event(QEvent* ev)
     87 {
     88     switch (ev->type()) {
     89     case QEvent::MouseMove:
     90         showCursor();
     91         ev->accept();
     92         return true;
     93     case QEvent::MouseButtonDblClick:
     94         close();
     95         ev->accept();
     96         return true;
     97     default:
     98         return QVideoWidget::event(ev);
     99     }
    100 }
    101 
    102 void FullScreenVideoWidget::keyPressEvent(QKeyEvent* ev)
    103 {
    104     if (m_mediaPlayer && ev->key() == Qt::Key_Space) {
    105         if (m_mediaPlayer->state() == QMediaPlayer::PlayingState)
    106             m_mediaPlayer->pause();
    107         else
    108             m_mediaPlayer->play();
    109     } else if (ev->key() == Qt::Key_Escape)
    110         close();
    111 }
    112 
    113 void FullScreenVideoWidget::hideCursor()
    114 {
    115     QApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
    116 }
    117 
    118 void FullScreenVideoWidget::showCursor()
    119 {
    120     QApplication::restoreOverrideCursor();
    121     m_cursorTimer.start(gHideMouseCursorDelay);
    122 }
    123 
    124 }
    125