examples/multimedia/videographicsitem/videoitem.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "videoitem.h"
       
    43 
       
    44 #include <QtMultimedia>
       
    45 
       
    46 VideoItem::VideoItem(QGraphicsItem *parent)
       
    47     : QGraphicsItem(parent)
       
    48     , imageFormat(QImage::Format_Invalid)
       
    49     , framePainted(false)
       
    50 {
       
    51 }
       
    52 
       
    53 VideoItem::~VideoItem()
       
    54 {
       
    55 }
       
    56 
       
    57 QRectF VideoItem::boundingRect() const
       
    58 {
       
    59     return QRectF(QPointF(0,0), surfaceFormat().sizeHint());
       
    60 }
       
    61 
       
    62 void VideoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
    63 {
       
    64     Q_UNUSED(option);
       
    65     Q_UNUSED(widget);
       
    66 
       
    67     if (currentFrame.map(QAbstractVideoBuffer::ReadOnly)) {
       
    68         const QTransform oldTransform = painter->transform();
       
    69 
       
    70         if (surfaceFormat().scanLineDirection() == QVideoSurfaceFormat::BottomToTop) {
       
    71            painter->scale(1, -1);
       
    72            painter->translate(0, -boundingRect().height());
       
    73         }
       
    74 
       
    75         painter->drawImage(boundingRect(), QImage(
       
    76                 currentFrame.bits(),
       
    77                 imageSize.width(),
       
    78                 imageSize.height(),
       
    79                 imageFormat));
       
    80 
       
    81         painter->setTransform(oldTransform);
       
    82 
       
    83         framePainted = true;
       
    84 
       
    85         currentFrame.unmap();
       
    86     }
       
    87 }
       
    88 
       
    89 QList<QVideoFrame::PixelFormat> VideoItem::supportedPixelFormats(
       
    90         QAbstractVideoBuffer::HandleType handleType) const
       
    91 {
       
    92     if (handleType == QAbstractVideoBuffer::NoHandle) {
       
    93         return QList<QVideoFrame::PixelFormat>()
       
    94                 << QVideoFrame::Format_RGB32
       
    95                 << QVideoFrame::Format_ARGB32
       
    96                 << QVideoFrame::Format_ARGB32_Premultiplied
       
    97                 << QVideoFrame::Format_RGB565
       
    98                 << QVideoFrame::Format_RGB555;
       
    99     } else {
       
   100         return QList<QVideoFrame::PixelFormat>();
       
   101     }
       
   102 }
       
   103 
       
   104 bool VideoItem::start(const QVideoSurfaceFormat &format)
       
   105 {
       
   106     if (isFormatSupported(format)) {
       
   107         imageFormat = QVideoFrame::equivalentImageFormat(format.pixelFormat());
       
   108         imageSize = format.frameSize();
       
   109         framePainted = true;
       
   110 
       
   111         QAbstractVideoSurface::start(format);
       
   112 
       
   113         prepareGeometryChange();
       
   114 
       
   115         return true;
       
   116     } else {
       
   117         return false;
       
   118     }
       
   119 }
       
   120 
       
   121 void VideoItem::stop()
       
   122 {
       
   123     currentFrame = QVideoFrame();
       
   124     framePainted = false;
       
   125 
       
   126     QAbstractVideoSurface::stop();
       
   127 }
       
   128 
       
   129 bool VideoItem::present(const QVideoFrame &frame)
       
   130 {
       
   131     if (!framePainted) {
       
   132         if (!isStarted())
       
   133             setError(StoppedError);
       
   134 
       
   135         return false;
       
   136     } else {
       
   137         currentFrame = frame;
       
   138         framePainted = false;
       
   139 
       
   140         update();
       
   141 
       
   142         return true;
       
   143     }
       
   144 }