37 ** |
37 ** |
38 ** $QT_END_LICENSE$ |
38 ** $QT_END_LICENSE$ |
39 ** |
39 ** |
40 ****************************************************************************/ |
40 ****************************************************************************/ |
41 |
41 |
42 #include <qvideowidget_p.h> |
42 #include "qvideowidget_p.h" |
43 |
43 |
44 #include <qmediaobject.h> |
44 #include "qmediaobject.h" |
45 #include <qmediaservice.h> |
45 #include "qmediaservice.h" |
46 #include <qvideooutputcontrol.h> |
46 #include "qvideooutputcontrol.h" |
47 #include <qvideowindowcontrol.h> |
47 #include "qvideowindowcontrol.h" |
48 #include <qvideowidgetcontrol.h> |
48 #include "qvideowidgetcontrol.h" |
49 |
49 |
50 #include <qpaintervideosurface_p.h> |
50 #include "qpaintervideosurface_p.h" |
51 #include <qvideorenderercontrol.h> |
51 #include "qvideorenderercontrol.h" |
52 #include <QtMultimedia/qvideosurfaceformat.h> |
52 #include <QtMultimedia/qvideosurfaceformat.h> |
53 #include <qpainter.h> |
53 #include <qpainter.h> |
54 |
54 |
55 #include <qapplication.h> |
55 #include <qapplication.h> |
56 #include <qevent.h> |
56 #include <qevent.h> |
104 { |
104 { |
105 m_widgetControl->setFullScreen(fullScreen); |
105 m_widgetControl->setFullScreen(fullScreen); |
106 } |
106 } |
107 |
107 |
108 |
108 |
109 QVideoWidget::AspectRatioMode QVideoWidgetControlBackend::aspectRatioMode() const |
109 Qt::AspectRatioMode QVideoWidgetControlBackend::aspectRatioMode() const |
110 { |
110 { |
111 return m_widgetControl->aspectRatioMode(); |
111 return m_widgetControl->aspectRatioMode(); |
112 } |
112 } |
113 |
113 |
114 void QVideoWidgetControlBackend::setAspectRatioMode(QVideoWidget::AspectRatioMode mode) |
114 void QVideoWidgetControlBackend::setAspectRatioMode(Qt::AspectRatioMode mode) |
115 { |
115 { |
116 m_widgetControl->setAspectRatioMode(mode); |
116 m_widgetControl->setAspectRatioMode(mode); |
117 } |
117 } |
118 |
118 |
119 QRendererVideoWidgetBackend::QRendererVideoWidgetBackend( |
119 QRendererVideoWidgetBackend::QRendererVideoWidgetBackend( |
120 QVideoRendererControl *control, QWidget *widget) |
120 QVideoRendererControl *control, QWidget *widget) |
121 : m_rendererControl(control) |
121 : m_rendererControl(control) |
122 , m_widget(widget) |
122 , m_widget(widget) |
123 , m_surface(new QPainterVideoSurface) |
123 , m_surface(new QPainterVideoSurface) |
124 , m_aspectRatioMode(QVideoWidget::KeepAspectRatio) |
124 , m_aspectRatioMode(Qt::KeepAspectRatio) |
125 , m_updatePaintDevice(true) |
125 , m_updatePaintDevice(true) |
126 { |
126 { |
127 connect(this, SIGNAL(brightnessChanged(int)), m_widget, SLOT(_q_brightnessChanged(int))); |
127 connect(this, SIGNAL(brightnessChanged(int)), m_widget, SLOT(_q_brightnessChanged(int))); |
128 connect(this, SIGNAL(contrastChanged(int)), m_widget, SLOT(_q_contrastChanged(int))); |
128 connect(this, SIGNAL(contrastChanged(int)), m_widget, SLOT(_q_contrastChanged(int))); |
129 connect(this, SIGNAL(hueChanged(int)), m_widget, SLOT(_q_hueChanged(int))); |
129 connect(this, SIGNAL(hueChanged(int)), m_widget, SLOT(_q_hueChanged(int))); |
130 connect(this, SIGNAL(saturationChanged(int)), m_widget, SLOT(_q_saturationChanged(int))); |
130 connect(this, SIGNAL(saturationChanged(int)), m_widget, SLOT(_q_saturationChanged(int))); |
131 connect(m_surface, SIGNAL(frameChanged()), m_widget, SLOT(update())); |
131 connect(m_surface, SIGNAL(frameChanged()), this, SLOT(frameChanged())); |
132 connect(m_surface, SIGNAL(surfaceFormatChanged(QVideoSurfaceFormat)), |
132 connect(m_surface, SIGNAL(surfaceFormatChanged(QVideoSurfaceFormat)), |
133 m_widget, SLOT(_q_dimensionsChanged())); |
133 this, SLOT(formatChanged(QVideoSurfaceFormat))); |
134 |
134 |
135 m_rendererControl->setSurface(m_surface); |
135 m_rendererControl->setSurface(m_surface); |
136 } |
136 } |
137 |
137 |
138 QRendererVideoWidgetBackend::~QRendererVideoWidgetBackend() |
138 QRendererVideoWidgetBackend::~QRendererVideoWidgetBackend() |
206 #endif |
206 #endif |
207 } |
207 } |
208 |
208 |
209 void QRendererVideoWidgetBackend::resizeEvent(QResizeEvent *) |
209 void QRendererVideoWidgetBackend::resizeEvent(QResizeEvent *) |
210 { |
210 { |
|
211 updateRects(); |
211 } |
212 } |
212 |
213 |
213 void QRendererVideoWidgetBackend::moveEvent(QMoveEvent *) |
214 void QRendererVideoWidgetBackend::moveEvent(QMoveEvent *) |
214 { |
215 { |
215 } |
216 } |
216 |
217 |
217 void QRendererVideoWidgetBackend::paintEvent(QPaintEvent *event) |
218 void QRendererVideoWidgetBackend::paintEvent(QPaintEvent *event) |
218 { |
219 { |
219 QPainter painter(m_widget); |
220 QPainter painter(m_widget); |
220 |
221 |
221 if (m_surface->isActive()) { |
222 if (m_widget->testAttribute(Qt::WA_OpaquePaintEvent)) { |
222 m_surface->paint(&painter, displayRect()); |
223 QRegion borderRegion = event->region(); |
|
224 borderRegion = borderRegion.subtracted(m_boundingRect); |
|
225 |
|
226 QBrush brush = m_widget->palette().window(); |
|
227 |
|
228 QVector<QRect> rects = borderRegion.rects(); |
|
229 for (QVector<QRect>::iterator it = rects.begin(), end = rects.end(); it != end; ++it) { |
|
230 painter.fillRect(*it, brush); |
|
231 } |
|
232 } |
|
233 |
|
234 if (m_surface->isActive() && m_boundingRect.intersects(event->rect())) { |
|
235 m_surface->paint(&painter, m_boundingRect, m_sourceRect); |
223 |
236 |
224 m_surface->setReady(true); |
237 m_surface->setReady(true); |
225 } else { |
238 } else { |
226 painter.fillRect(event->rect(), m_widget->palette().background()); |
|
227 |
|
228 #if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES_1_CL) && !defined(QT_OPENGL_ES_1) |
239 #if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES_1_CL) && !defined(QT_OPENGL_ES_1) |
229 if (m_updatePaintDevice && (painter.paintEngine()->type() == QPaintEngine::OpenGL |
240 if (m_updatePaintDevice && (painter.paintEngine()->type() == QPaintEngine::OpenGL |
230 || painter.paintEngine()->type() == QPaintEngine::OpenGL2)) { |
241 || painter.paintEngine()->type() == QPaintEngine::OpenGL2)) { |
231 m_updatePaintDevice = false; |
242 m_updatePaintDevice = false; |
232 |
243 |
237 m_surface->setShaderType(QPainterVideoSurface::FragmentProgramShader); |
248 m_surface->setShaderType(QPainterVideoSurface::FragmentProgramShader); |
238 } |
249 } |
239 } |
250 } |
240 #endif |
251 #endif |
241 } |
252 } |
242 } |
253 |
243 |
254 } |
244 QRect QRendererVideoWidgetBackend::displayRect() const |
255 |
245 { |
256 void QRendererVideoWidgetBackend::formatChanged(const QVideoSurfaceFormat &format) |
246 QRect displayRect = m_widget->rect(); |
257 { |
247 |
258 m_nativeSize = format.sizeHint(); |
248 if (m_aspectRatioMode != QVideoWidget::IgnoreAspectRatio) { |
259 |
249 QVideoSurfaceFormat format = m_surface->surfaceFormat(); |
260 updateRects(); |
250 |
261 |
251 QSize aspectRatio = format.pixelAspectRatio(); |
262 m_widget->updateGeometry(); |
252 |
263 m_widget->update(); |
253 QSize size = format.viewport().size(); |
264 } |
254 size.rwidth() *= aspectRatio.width(); |
265 |
255 size.rheight() *= aspectRatio.height(); |
266 void QRendererVideoWidgetBackend::frameChanged() |
256 size.scale(displayRect.size(), Qt::KeepAspectRatio); |
267 { |
257 |
268 m_widget->update(m_boundingRect); |
258 QPoint center = displayRect.center(); |
269 } |
259 |
270 |
260 displayRect = QRect(QPoint(0, 0), size); |
271 void QRendererVideoWidgetBackend::updateRects() |
261 displayRect.moveCenter(center); |
272 { |
262 } |
273 QRect rect = m_widget->rect(); |
263 |
274 |
264 return displayRect; |
275 if (m_nativeSize.isEmpty()) { |
|
276 m_boundingRect = QRect(); |
|
277 } else if (m_aspectRatioMode == Qt::IgnoreAspectRatio) { |
|
278 m_boundingRect = rect; |
|
279 m_sourceRect = QRectF(0, 0, 1, 1); |
|
280 } else if (m_aspectRatioMode == Qt::KeepAspectRatio) { |
|
281 QSize size = m_nativeSize; |
|
282 size.scale(rect.size(), Qt::KeepAspectRatio); |
|
283 |
|
284 m_boundingRect = QRect(0, 0, size.width(), size.height()); |
|
285 m_boundingRect.moveCenter(rect.center()); |
|
286 |
|
287 m_sourceRect = QRectF(0, 0, 1, 1); |
|
288 } else if (m_aspectRatioMode == Qt::KeepAspectRatioByExpanding) { |
|
289 m_boundingRect = rect; |
|
290 |
|
291 QSizeF size = rect.size(); |
|
292 size.scale(m_nativeSize, Qt::KeepAspectRatio); |
|
293 |
|
294 m_sourceRect = QRectF( |
|
295 0, 0, size.width() / m_nativeSize.width(), size.height() / m_nativeSize.height()); |
|
296 m_sourceRect.moveCenter(QPointF(0.5, 0.5)); |
|
297 } |
265 } |
298 } |
266 |
299 |
267 QWindowVideoWidgetBackend::QWindowVideoWidgetBackend(QVideoWindowControl *control, QWidget *widget) |
300 QWindowVideoWidgetBackend::QWindowVideoWidgetBackend(QVideoWindowControl *control, QWidget *widget) |
268 : m_windowControl(control) |
301 : m_windowControl(control) |
269 , m_widget(widget) |
302 , m_widget(widget) |
270 , m_aspectRatioMode(QVideoWidget::KeepAspectRatio) |
303 , m_aspectRatioMode(Qt::KeepAspectRatio) |
271 { |
304 { |
272 connect(control, SIGNAL(brightnessChanged(int)), m_widget, SLOT(_q_brightnessChanged(int))); |
305 connect(control, SIGNAL(brightnessChanged(int)), m_widget, SLOT(_q_brightnessChanged(int))); |
273 connect(control, SIGNAL(contrastChanged(int)), m_widget, SLOT(_q_contrastChanged(int))); |
306 connect(control, SIGNAL(contrastChanged(int)), m_widget, SLOT(_q_contrastChanged(int))); |
274 connect(control, SIGNAL(hueChanged(int)), m_widget, SLOT(_q_hueChanged(int))); |
307 connect(control, SIGNAL(hueChanged(int)), m_widget, SLOT(_q_hueChanged(int))); |
275 connect(control, SIGNAL(saturationChanged(int)), m_widget, SLOT(_q_saturationChanged(int))); |
308 connect(control, SIGNAL(saturationChanged(int)), m_widget, SLOT(_q_saturationChanged(int))); |
514 |
554 |
515 \sa QMediaObject, QMediaPlayer, QGraphicsVideoItem |
555 \sa QMediaObject, QMediaPlayer, QGraphicsVideoItem |
516 */ |
556 */ |
517 |
557 |
518 /*! |
558 /*! |
519 \enum QVideoWidget::AspectRatioMode |
|
520 |
|
521 Specfies how video is scaled with respect to its aspect ratio. |
|
522 |
|
523 \value IgnoreAspectRatio The video is scaled to fill the widget ignoring its aspect ratio. |
|
524 \value KeepAspectRatio The video is scaled to the largest rectangle that will fit within the |
|
525 widget's dimensions while still retaining its original aspect ratio. |
|
526 */ |
|
527 |
|
528 /*! |
|
529 Constructs a new video widget. |
559 Constructs a new video widget. |
530 |
560 |
531 The \a parent is passed to QWidget. |
561 The \a parent is passed to QWidget. |
532 */ |
562 */ |
533 QVideoWidget::QVideoWidget(QWidget *parent) |
563 QVideoWidget::QVideoWidget(QWidget *parent) |
534 : QWidget(parent, 0) |
564 : QWidget(parent, 0) |
535 , d_ptr(new QVideoWidgetPrivate) |
565 , d_ptr(new QVideoWidgetPrivate) |
536 { |
566 { |
537 d_ptr->q_ptr = this; |
567 d_ptr->q_ptr = this; |
538 |
|
539 QPalette palette = QWidget::palette(); |
|
540 palette.setColor(QPalette::Background, Qt::black); |
|
541 setPalette(palette); |
|
542 } |
568 } |
543 |
569 |
544 /*! |
570 /*! |
545 Destroys a video widget. |
571 Destroys a video widget. |
546 */ |
572 */ |