qtmobility/src/multimedia/qvideowidget.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    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()
   171     m_surface->setSaturation(saturation);
   171     m_surface->setSaturation(saturation);
   172 
   172 
   173     emit saturationChanged(saturation);
   173     emit saturationChanged(saturation);
   174 }
   174 }
   175 
   175 
   176 QVideoWidget::AspectRatioMode QRendererVideoWidgetBackend::aspectRatioMode() const
   176 Qt::AspectRatioMode QRendererVideoWidgetBackend::aspectRatioMode() const
   177 {
   177 {
   178     return m_aspectRatioMode;
   178     return m_aspectRatioMode;
   179 }
   179 }
   180 
   180 
   181 void QRendererVideoWidgetBackend::setAspectRatioMode(QVideoWidget::AspectRatioMode mode)
   181 void QRendererVideoWidgetBackend::setAspectRatioMode(Qt::AspectRatioMode mode)
   182 {
   182 {
   183     m_aspectRatioMode = mode;
   183     m_aspectRatioMode = mode;
   184 
   184 
   185     m_widget->updateGeometry();
   185     m_widget->updateGeometry();
   186 }
   186 }
   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)));
   304 void QWindowVideoWidgetBackend::setFullScreen(bool fullScreen)
   337 void QWindowVideoWidgetBackend::setFullScreen(bool fullScreen)
   305 {
   338 {
   306     m_windowControl->setFullScreen(fullScreen);
   339     m_windowControl->setFullScreen(fullScreen);
   307 }
   340 }
   308 
   341 
   309 QVideoWidget::AspectRatioMode QWindowVideoWidgetBackend::aspectRatioMode() const
   342 Qt::AspectRatioMode QWindowVideoWidgetBackend::aspectRatioMode() const
   310 {
   343 {
   311     return m_windowControl->aspectRatioMode();
   344     return m_windowControl->aspectRatioMode();
   312 }
   345 }
   313 
   346 
   314 void QWindowVideoWidgetBackend::setAspectRatioMode(QVideoWidget::AspectRatioMode mode)
   347 void QWindowVideoWidgetBackend::setAspectRatioMode(Qt::AspectRatioMode mode)
   315 {
   348 {
   316     m_windowControl->setAspectRatioMode(mode);
   349     m_windowControl->setAspectRatioMode(mode);
   317 }
   350 }
   318 
   351 
   319 QSize QWindowVideoWidgetBackend::sizeHint() const
   352 QSize QWindowVideoWidgetBackend::sizeHint() const
   342     m_windowControl->setDisplayRect(m_widget->rect());
   375     m_windowControl->setDisplayRect(m_widget->rect());
   343 }
   376 }
   344 
   377 
   345 void QWindowVideoWidgetBackend::paintEvent(QPaintEvent *event)
   378 void QWindowVideoWidgetBackend::paintEvent(QPaintEvent *event)
   346 {
   379 {
       
   380     if (m_widget->testAttribute(Qt::WA_OpaquePaintEvent)) {
       
   381         QPainter painter(m_widget);
       
   382 
       
   383         painter.fillRect(event->rect(), m_widget->palette().window());
       
   384     }
       
   385 
   347     m_windowControl->repaint();
   386     m_windowControl->repaint();
   348 
   387 
   349     event->accept();
   388     event->accept();
   350 }
   389 }
   351 
   390 
   482 }
   521 }
   483 
   522 
   484 void QVideoWidgetPrivate::_q_dimensionsChanged()
   523 void QVideoWidgetPrivate::_q_dimensionsChanged()
   485 {
   524 {
   486     q_func()->updateGeometry();
   525     q_func()->updateGeometry();
       
   526     q_func()->update();
   487 }
   527 }
   488 
   528 
   489 /*!
   529 /*!
   490     \class QVideoWidget
   530     \class QVideoWidget
   491     \preliminary
   531     \preliminary
   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 */
   616 /*!
   642 /*!
   617     \property QVideoWidget::aspectRatioMode
   643     \property QVideoWidget::aspectRatioMode
   618     \brief how video is scaled with respect to its aspect ratio.
   644     \brief how video is scaled with respect to its aspect ratio.
   619 */
   645 */
   620 
   646 
   621 QVideoWidget::AspectRatioMode QVideoWidget::aspectRatioMode() const
   647 Qt::AspectRatioMode QVideoWidget::aspectRatioMode() const
   622 {
   648 {
   623     return d_func()->aspectRatioMode;
   649     return d_func()->aspectRatioMode;
   624 }
   650 }
   625 
   651 
   626 void QVideoWidget::setAspectRatioMode(QVideoWidget::AspectRatioMode mode)
   652 void QVideoWidget::setAspectRatioMode(Qt::AspectRatioMode mode)
   627 {
   653 {
   628     Q_D(QVideoWidget);
   654     Q_D(QVideoWidget);
   629 
   655 
   630     if (d->currentControl) {
   656     if (d->currentControl) {
   631         d->currentControl->setAspectRatioMode(mode);
   657         d->currentControl->setAspectRatioMode(mode);
   867     Q_D(QVideoWidget);
   893     Q_D(QVideoWidget);
   868 
   894 
   869     if (d->currentBackend)
   895     if (d->currentBackend)
   870         d->currentBackend->hideEvent(event);
   896         d->currentBackend->hideEvent(event);
   871 
   897 
   872     if (d->outputControl)
       
   873         d->outputControl->setOutput(QVideoOutputControl::NoOutput);
       
   874 
       
   875     QWidget::hideEvent(event);
   898     QWidget::hideEvent(event);
   876 }
   899 }
   877 
   900 
   878 /*!
   901 /*!
   879   Handles the resize \a event.
   902   Handles the resize \a event.
   904  */
   927  */
   905 void QVideoWidget::paintEvent(QPaintEvent *event)
   928 void QVideoWidget::paintEvent(QPaintEvent *event)
   906 {
   929 {
   907     Q_D(QVideoWidget);
   930     Q_D(QVideoWidget);
   908 
   931 
   909     if (d->currentBackend)
   932     if (d->currentBackend) {
   910         d->currentBackend->paintEvent(event);
   933         d->currentBackend->paintEvent(event);
       
   934     } else if (testAttribute(Qt::WA_OpaquePaintEvent)) {
       
   935         QPainter painter(this);
       
   936 
       
   937         painter.fillRect(event->rect(), palette().window());
       
   938     }
   911 }
   939 }
   912 
   940 
   913 #include "moc_qvideowidget.cpp"
   941 #include "moc_qvideowidget.cpp"
   914 #include "moc_qvideowidget_p.cpp"
   942 #include "moc_qvideowidget_p.cpp"
   915 QTM_END_NAMESPACE
   943 QTM_END_NAMESPACE