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
qgraphicsvideoitem.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
5#include "qvideosink.h"
6
7#include <qobject.h>
8#include <qvideoframe.h>
9#include <qvideoframeformat.h>
10
11#include <QtCore/qcoreevent.h>
12#include <QtCore/qpointer.h>
13
14QT_BEGIN_NAMESPACE
15
16class QGraphicsVideoItemPrivate
17{
18public:
19 QGraphicsVideoItemPrivate()
20 : rect(0.0, 0.0, 320, 240)
21 {
22 }
23
24 QGraphicsVideoItem *q_ptr = nullptr;
25
26 QVideoSink *sink = nullptr;
27 QRectF rect;
28 QRectF boundingRect;
29 QSizeF nativeSize;
30 QVideoFrame m_frame;
31 Qt::AspectRatioMode m_aspectRatioMode = Qt::KeepAspectRatio;
32
33 void updateRects();
34
35 void _q_present(const QVideoFrame &);
36};
37
38void QGraphicsVideoItemPrivate::updateRects()
39{
40 q_ptr->prepareGeometryChange();
41
42 boundingRect = rect;
43 if (nativeSize.isEmpty())
44 return;
45
46 if (m_aspectRatioMode == Qt::KeepAspectRatio) {
47 QSizeF size = nativeSize;
48 size.scale(rect.size(), Qt::KeepAspectRatio);
49
50 boundingRect = QRectF(0, 0, size.width(), size.height());
51 boundingRect.moveCenter(rect.center());
52 }
53}
54
55void QGraphicsVideoItemPrivate::_q_present(const QVideoFrame &frame)
56{
57 m_frame = frame;
58 q_ptr->update(boundingRect);
59
60 if (frame.isValid()) {
61 const QSize &size = frame.surfaceFormat().viewport().size();
62 if (nativeSize != size) {
63 nativeSize = size;
64
65 updateRects();
66 emit q_ptr->nativeSizeChanged(nativeSize);
67 }
68 }
69}
70
71/*!
72 \class QGraphicsVideoItem
73
74 \brief The QGraphicsVideoItem class provides a graphics item which display video produced by a QMediaPlayer or QCamera.
75
76 \inmodule QtMultimediaWidgets
77 \ingroup multimedia
78
79 Attaching a QGraphicsVideoItem to a QMediaPlayer or QCamera allows it to display
80 the video or image output of that media object.
81
82 \snippet multimedia-snippets/video.cpp Video graphics item
83
84 \b {Note}: Only a single display output can be attached to a media
85 object at one time.
86
87 \sa QMediaPlayer, QVideoWidget, QCamera
88*/
89
90/*!
91 Constructs a graphics item that displays video.
92
93 The \a parent is passed to QGraphicsItem.
94*/
95QGraphicsVideoItem::QGraphicsVideoItem(QGraphicsItem *parent)
96 : QGraphicsObject(parent)
97 , d_ptr(new QGraphicsVideoItemPrivate)
98{
99 d_ptr->q_ptr = this;
100 d_ptr->sink = new QVideoSink(this);
101
102 connect(d_ptr->sink, SIGNAL(videoFrameChanged(QVideoFrame)),
103 this, SLOT(_q_present(QVideoFrame)));
104}
105
106/*!
107 Destroys a video graphics item.
108*/
109QGraphicsVideoItem::~QGraphicsVideoItem()
110{
111 delete d_ptr;
112}
113
114/*!
115 \since 6.0
116 \property QGraphicsVideoItem::videoSink
117 \brief Returns the underlying video sink that can render video frames
118 to the current item.
119 This property is never \c nullptr.
120 Example of how to render video frames to QGraphicsVideoItem:
121 \snippet multimedia-snippets/video.cpp GraphicsVideoItem Surface
122 \sa QMediaPlayer::setVideoOutput
123*/
124
125QVideoSink *QGraphicsVideoItem::videoSink() const
126{
127 return d_func()->sink;
128}
129
130/*!
131 \property QGraphicsVideoItem::aspectRatioMode
132 \brief how a video is scaled to fit the graphics item's size.
133*/
134
135Qt::AspectRatioMode QGraphicsVideoItem::aspectRatioMode() const
136{
137 return d_func()->m_aspectRatioMode;
138}
139
140void QGraphicsVideoItem::setAspectRatioMode(Qt::AspectRatioMode mode)
141{
142 Q_D(QGraphicsVideoItem);
143 if (d->m_aspectRatioMode == mode)
144 return;
145
146 d->m_aspectRatioMode = mode;
147 d->updateRects();
148}
149
150/*!
151 \property QGraphicsVideoItem::offset
152 \brief the video item's offset.
153
154 QGraphicsVideoItem will draw video using the offset for its top left
155 corner.
156*/
157
158QPointF QGraphicsVideoItem::offset() const
159{
160 return d_func()->rect.topLeft();
161}
162
163void QGraphicsVideoItem::setOffset(const QPointF &offset)
164{
165 Q_D(QGraphicsVideoItem);
166
167 d->rect.moveTo(offset);
168 d->updateRects();
169}
170
171/*!
172 \property QGraphicsVideoItem::size
173 \brief the video item's size.
174
175 QGraphicsVideoItem will draw video scaled to fit size according to its
176 fillMode.
177*/
178
179QSizeF QGraphicsVideoItem::size() const
180{
181 return d_func()->rect.size();
182}
183
184void QGraphicsVideoItem::setSize(const QSizeF &size)
185{
186 Q_D(QGraphicsVideoItem);
187
188 d->rect.setSize(size.isValid() ? size : QSizeF(0, 0));
189 d->updateRects();
190}
191
192/*!
193 \property QGraphicsVideoItem::nativeSize
194 \brief the native size of the video.
195*/
196
197QSizeF QGraphicsVideoItem::nativeSize() const
198{
199 return d_func()->nativeSize;
200}
201
202/*!
203 \reimp
204*/
205QRectF QGraphicsVideoItem::boundingRect() const
206{
207 return d_func()->boundingRect;
208}
209
210/*!
211 \reimp
212*/
213void QGraphicsVideoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
214{
215 Q_D(QGraphicsVideoItem);
216
217 Q_UNUSED(option);
218 Q_UNUSED(widget);
219
220 d->m_frame.paint(painter, d->rect, { Qt::transparent, d->m_aspectRatioMode });
221}
222
223/*!
224 \fn int QGraphicsVideoItem::type() const
225 \reimp
226
227 Returns an int representing the type of the video item.
228*/
229/*!
230 \variable QGraphicsVideoItem::d_ptr
231 \internal
232*/
233/*!
234 \enum QGraphicsVideoItem::anonymous
235 \internal
236
237 \omitvalue Type
238*/
239/*!
240 \reimp
241
242 \internal
243*/
244QVariant QGraphicsVideoItem::itemChange(GraphicsItemChange change, const QVariant &value)
245{
246 return QGraphicsItem::itemChange(change, value);
247}
248
249/*!
250 \internal
251*/
252void QGraphicsVideoItem::timerEvent(QTimerEvent *event)
253{
254 QGraphicsObject::timerEvent(event);
255}
256
257QT_END_NAMESPACE
258
259#include "moc_qgraphicsvideoitem.cpp"