qtmobility/examples/videographicsitem/videoitem.cpp
changeset 14 6fbed849b4f4
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 <QtGui>
       
    45 #include <qvideosurfaceformat.h>
       
    46 
       
    47 VideoItem::VideoItem(QGraphicsItem *parent)
       
    48     : QGraphicsItem(parent)
       
    49     , imageFormat(QImage::Format_Invalid)
       
    50     , framePainted(false)
       
    51 {
       
    52 }
       
    53 
       
    54 VideoItem::~VideoItem()
       
    55 {
       
    56 }
       
    57 
       
    58 QRectF VideoItem::boundingRect() const
       
    59 {
       
    60     return QRectF(QPointF(0,0), surfaceFormat().sizeHint());
       
    61 }
       
    62 
       
    63 void VideoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
    64 {
       
    65     Q_UNUSED(option);
       
    66     Q_UNUSED(widget);
       
    67 
       
    68     if (currentFrame.map(QAbstractVideoBuffer::ReadOnly)) {
       
    69         const QTransform oldTransform = painter->transform();
       
    70 
       
    71         if (surfaceFormat().scanLineDirection() == QVideoSurfaceFormat::BottomToTop) {
       
    72            painter->scale(1, -1);
       
    73            painter->translate(0, -boundingRect().height());
       
    74         }
       
    75 
       
    76         painter->drawImage(boundingRect(), QImage(
       
    77                 currentFrame.bits(),
       
    78                 imageSize.width(),
       
    79                 imageSize.height(),
       
    80                 imageFormat));
       
    81 
       
    82         painter->setTransform(oldTransform);
       
    83 
       
    84         framePainted = true;
       
    85 
       
    86         currentFrame.unmap();
       
    87     }
       
    88 }
       
    89 
       
    90 QList<QVideoFrame::PixelFormat> VideoItem::supportedPixelFormats(
       
    91         QAbstractVideoBuffer::HandleType handleType) const
       
    92 {
       
    93     if (handleType == QAbstractVideoBuffer::NoHandle) {
       
    94         return QList<QVideoFrame::PixelFormat>()
       
    95                 << QVideoFrame::Format_RGB32
       
    96                 << QVideoFrame::Format_ARGB32
       
    97                 << QVideoFrame::Format_ARGB32_Premultiplied
       
    98                 << QVideoFrame::Format_RGB565
       
    99                 << QVideoFrame::Format_RGB555;
       
   100     } else {
       
   101         return QList<QVideoFrame::PixelFormat>();
       
   102     }
       
   103 }
       
   104 
       
   105 bool VideoItem::start(const QVideoSurfaceFormat &format)
       
   106 {
       
   107     if (isFormatSupported(format)) {
       
   108         imageFormat = QVideoFrame::imageFormatFromPixelFormat(format.pixelFormat());
       
   109         imageSize = format.frameSize();
       
   110         framePainted = true;
       
   111 
       
   112         QAbstractVideoSurface::start(format);
       
   113 
       
   114         prepareGeometryChange();
       
   115 
       
   116         return true;
       
   117     } else {
       
   118         return false;
       
   119     }
       
   120 }
       
   121 
       
   122 void VideoItem::stop()
       
   123 {
       
   124     currentFrame = QVideoFrame();
       
   125     framePainted = false;
       
   126 
       
   127     QAbstractVideoSurface::stop();
       
   128 }
       
   129 
       
   130 bool VideoItem::present(const QVideoFrame &frame)
       
   131 {
       
   132     if (!framePainted) {
       
   133         if (!QAbstractVideoSurface::isActive())
       
   134             setError(StoppedError);
       
   135 
       
   136         return false;
       
   137     } else {
       
   138         currentFrame = frame;
       
   139         framePainted = false;
       
   140 
       
   141         update();
       
   142 
       
   143         return true;
       
   144     }
       
   145 }