[Qt] Eliminate first set of QtWidgets dependencies from WebCore
[WebKit-https.git] / Source / WebCore / platform / graphics / gstreamer / PlatformVideoWindowQt.cpp
1 /*
2     Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2 of the License, or (at your option) any later version.
8
9     This library 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 GNU
12     Library General Public License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include "PlatformVideoWindow.h"
22
23 #include "HTMLVideoElement.h"
24 #include "PlatformVideoWindowPrivate.h"
25
26 #include <QCursor>
27 #include <QGuiApplication>
28 #include <QKeyEvent>
29 #include <QPalette>
30
31 using namespace WebCore;
32
33 #ifndef QT_NO_CURSOR
34 static const int gHideMouseCursorDelay = 3000;
35 #endif
36
37 FullScreenVideoWindow::FullScreenVideoWindow()
38     : m_mediaElement(0)
39 {
40 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
41     setAttribute(Qt::WA_NativeWindow);
42     setAttribute(Qt::WA_NoSystemBackground, true);
43     setAttribute(Qt::WA_PaintOnScreen, true);
44 #endif
45     setWindowModality(Qt::ApplicationModal);
46
47 #ifndef QT_NO_CURSOR
48     m_cursorTimer.setSingleShot(true);
49     connect(&m_cursorTimer, SIGNAL(timeout()), this, SLOT(hideCursor()));
50 #endif
51 }
52
53 void FullScreenVideoWindow::setVideoElement(HTMLVideoElement* element)
54 {
55     m_mediaElement = element;
56 }
57
58 void FullScreenVideoWindow::keyPressEvent(QKeyEvent* ev)
59 {
60     if (m_mediaElement && ev->key() == Qt::Key_Space) {
61         if (!m_mediaElement->paused())
62             m_mediaElement->pause();
63         else
64             m_mediaElement->play();
65     } else if (ev->key() == Qt::Key_Escape)
66         emit closed();
67     Base::keyPressEvent(ev);
68 }
69
70 bool FullScreenVideoWindow::event(QEvent* ev)
71 {
72     switch (ev->type()) {
73     case QEvent::MouseMove:
74         showCursor();
75         ev->accept();
76         return true;
77     case QEvent::MouseButtonDblClick:
78         emit closed();
79         ev->accept();
80         return true;
81     case QEvent::Close:
82 #ifndef QT_NO_CURSOR
83         m_cursorTimer.stop();
84 #endif
85 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
86         setMouseTracking(false);
87         releaseMouse();
88 #endif
89 #ifndef QT_NO_CURSOR
90         QGuiApplication::restoreOverrideCursor();
91 #endif
92         break;
93     default:
94         break;
95     }
96     return Base::event(ev);
97 }
98
99 void FullScreenVideoWindow::showFullScreen()
100 {
101     Base::showFullScreen();
102 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
103     setMouseTracking(true);
104 #endif
105     raise();
106 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
107     setFocus();
108 #endif
109     hideCursor();
110 }
111
112 void FullScreenVideoWindow::hideCursor()
113 {
114 #ifndef QT_NO_CURSOR
115     QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
116 #endif
117 }
118
119 void FullScreenVideoWindow::showCursor()
120 {
121 #ifndef QT_NO_CURSOR
122     QGuiApplication::restoreOverrideCursor();
123     m_cursorTimer.start(gHideMouseCursorDelay);
124 #endif
125 }
126
127
128 PlatformVideoWindow::PlatformVideoWindow()
129 {
130     Base* win = new FullScreenVideoWindow();
131     m_window = win;
132     win->setWindowFlags(win->windowFlags() | Qt::FramelessWindowHint);
133     // FIXME: Port to Qt 5.
134 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
135     QPalette p;
136     p.setColor(QPalette::Base, Qt::black);
137     p.setColor(QPalette::Window, Qt::black);
138     win->setPalette(p);
139 #endif
140     win->showFullScreen();
141     m_videoWindowId = win->winId();
142 }
143
144 PlatformVideoWindow::~PlatformVideoWindow()
145 {
146     delete m_window;
147     m_videoWindowId = 0;
148 }
149
150 void PlatformVideoWindow::prepareForOverlay(GstMessage*)
151 {
152 }