Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qvideowidget.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <private/qtmultimediaglobal_p.h>
5
6#include <QtMultimediaWidgets/private/qvideowidget_p.h>
7
8#include <QtMultimedia/qtmultimediaglobal.h>
9#include <QtMultimedia/qvideoframeformat.h>
10#include <QtMultimedia/qvideosink.h>
11#include <QtMultimedia/private/qvideowindow_p.h>
12
13#include <QtGui/qevent.h>
14#include <QtGui/qguiapplication.h>
15#include <QtCore/qobject.h>
16
17QT_BEGIN_NAMESPACE
18
19using namespace Qt::Literals;
20
21/*!
22 \class QVideoWidget
23
24
25 \brief The QVideoWidget class provides a widget which presents video
26 produced by a media object.
27 \ingroup multimedia
28 \ingroup multimedia_video
29 \inmodule QtMultimediaWidgets
30
31 Attaching a QVideoWidget to a QMediaPlayer or QCamera allows it to display the
32 video or image output of that object.
33
34 \snippet multimedia-snippets/video.cpp Video widget
35
36 \b {Note}: Only a single display output can be attached to a media
37 object at one time.
38
39 \warning QVideoWidget is not supported on the \c eglfs platform plugin.
40
41 \sa QCamera, QMediaPlayer, QGraphicsVideoItem
42*/
43/*!
44 \variable QVideoWidget::d_ptr
45 \internal
46*/
47/*!
48 Constructs a new video widget.
49
50 The \a parent is passed to QWidget.
51*/
52QVideoWidget::QVideoWidget(QWidget *parent)
53 : QWidget(parent, {})
54 , d_ptr(new QVideoWidgetPrivate)
55{
56 d_ptr->q_ptr = this;
57 d_ptr->isEglfs = QGuiApplication::platformName().startsWith("eglfs"_L1, Qt::CaseInsensitive);
58
59 if (d_ptr->isEglfs) {
60 qWarning("QVideoWidget is not supported on eglfs");
61 d_ptr->fakeVideoSink = new QVideoSink(this);
62 } else {
63 d_ptr->videoWindow = new QVideoWindow;
64 d_ptr->videoWindow->setFlag(Qt::WindowTransparentForInput, true);
65 d_ptr->windowContainer = QWidget::createWindowContainer(d_ptr->videoWindow, this, Qt::WindowTransparentForInput);
66 d_ptr->windowContainer->move(0, 0);
67 d_ptr->windowContainer->resize(size());
68 connect(d_ptr->videoWindow, &QVideoWindow::aspectRatioModeChanged, this,
69 &QVideoWidget::aspectRatioModeChanged);
70 }
71}
72
73/*!
74 Destroys a video widget.
75*/
76QVideoWidget::~QVideoWidget()
77{
78 delete d_ptr->videoWindow;
79 delete d_ptr;
80}
81
82/*!
83 Returns the QVideoSink instance.
84*/
85QVideoSink *QVideoWidget::videoSink() const
86{
87 return d_ptr->videoWindow ? d_ptr->videoWindow->videoSink() : d_ptr->fakeVideoSink;
88}
89
90/*!
91 \property QVideoWidget::aspectRatioMode
92 \brief how video is scaled with respect to its aspect ratio.
93*/
94
95Qt::AspectRatioMode QVideoWidget::aspectRatioMode() const
96{
97 return d_ptr->videoWindow ? d_ptr->videoWindow->aspectRatioMode() : Qt::KeepAspectRatio;
98}
99
100void QVideoWidget::setAspectRatioMode(Qt::AspectRatioMode mode)
101{
102 if (d_ptr->videoWindow)
103 d_ptr->videoWindow->setAspectRatioMode(mode);
104}
105
106/*!
107 \property QVideoWidget::fullScreen
108 \brief whether video display is confined to a window or is fullScreen.
109*/
110
111void QVideoWidget::setFullScreen(bool fullScreen)
112{
113 Q_D(QVideoWidget);
114 if (isFullScreen() == fullScreen)
115 return;
116
117 if (d_ptr->isEglfs)
118 return;
119
120 Qt::WindowFlags flags = windowFlags();
121
122 if (fullScreen) {
123 // get the position of the widget in global coordinates
124 QPoint position = mapToGlobal(QPoint(0,0));
125 d->nonFullScreenFlags = flags & (Qt::Window | Qt::SubWindow);
126 d_ptr->nonFullscreenPos = pos();
127 flags |= Qt::Window;
128 flags &= ~Qt::SubWindow;
129 setWindowFlags(flags);
130 // move the widget to the position it had on screen, so that showFullScreen() will
131 // place it on the correct screen
132 move(position);
133
134 showFullScreen();
135 } else {
136 flags &= ~(Qt::Window | Qt::SubWindow); //clear the flags...
137 flags |= d->nonFullScreenFlags; //then we reset the flags (window and subwindow)
138 setWindowFlags(flags);
139
140 showNormal();
141 move(d_ptr->nonFullscreenPos);
142 d_ptr->nonFullscreenPos = {};
143 }
144}
145
146/*!
147 Returns the size hint for the current back end,
148 if there is one, or else the size hint from QWidget.
149 */
150QSize QVideoWidget::sizeHint() const
151{
152 auto size = videoSink()->videoSize();
153 if (size.isValid())
154 return size;
155
156 return QWidget::sizeHint();
157}
158
159/*!
160 \reimp
161 Current event \a event.
162 Returns the value of the base class QWidget::event(QEvent *event) function.
163*/
164bool QVideoWidget::event(QEvent *event)
165{
166 Q_D(QVideoWidget);
167
168 if (event->type() == QEvent::WindowStateChange) {
169 bool fullScreen = bool(windowState() & Qt::WindowFullScreen);
170 if (fullScreen != d->wasFullScreen) {
171 emit fullScreenChanged(fullScreen);
172 d->wasFullScreen = fullScreen;
173 }
174 }
175
176 return QWidget::event(event);
177}
178
179/*!
180 \reimp
181 Handles the show \a event.
182 */
183void QVideoWidget::showEvent(QShowEvent *event)
184{
185 QWidget::showEvent(event);
186}
187
188/*!
189 \reimp
190 Handles the hide \a event.
191*/
192void QVideoWidget::hideEvent(QHideEvent *event)
193{
194 QWidget::hideEvent(event);
195}
196
197/*!
198 \reimp
199 Handles the resize \a event.
200 */
201void QVideoWidget::resizeEvent(QResizeEvent *event)
202{
203 if (!d_ptr->isEglfs)
204 d_ptr->windowContainer->resize(event->size());
205 QWidget::resizeEvent(event);
206}
207
208/*!
209 \reimp
210 Handles the move \a event.
211 */
212void QVideoWidget::moveEvent(QMoveEvent *event)
213{
214 if (!d_ptr->isEglfs)
215 QWidget::moveEvent(event);
216}
217
218QT_END_NAMESPACE
219
220#include "moc_qvideowidget.cpp"