qtmobility/examples/videographicsitem/videoplayer.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 "videoplayer.h"
       
    43 #include "videoitem.h"
       
    44 
       
    45 #include <QtGui>
       
    46 #include <qvideosurfaceformat.h>
       
    47 
       
    48 #ifndef QT_NO_OPENGL
       
    49 # include <QtOpenGL/QGLWidget>
       
    50 #endif
       
    51 
       
    52 VideoPlayer::VideoPlayer(QWidget *parent, Qt::WindowFlags flags)
       
    53     : QWidget(parent, flags)
       
    54     , videoItem(0)
       
    55     , playButton(0)
       
    56     , positionSlider(0)
       
    57 {
       
    58     connect(&movie, SIGNAL(stateChanged(QMovie::MovieState)),
       
    59             this, SLOT(movieStateChanged(QMovie::MovieState)));
       
    60     connect(&movie, SIGNAL(frameChanged(int)),
       
    61             this, SLOT(frameChanged(int)));
       
    62 
       
    63     videoItem = new VideoItem;
       
    64 
       
    65     QGraphicsScene *scene = new QGraphicsScene(this);
       
    66     QGraphicsView *graphicsView = new QGraphicsView(scene);
       
    67 
       
    68 #ifndef QT_NO_OPENGL
       
    69     graphicsView->setViewport(new QGLWidget);
       
    70 #endif
       
    71 
       
    72     scene->addItem(videoItem);
       
    73 
       
    74     QSlider *rotateSlider = new QSlider(Qt::Horizontal);
       
    75     rotateSlider->setRange(-180,  180);
       
    76     rotateSlider->setValue(0);
       
    77 
       
    78     connect(rotateSlider, SIGNAL(valueChanged(int)),
       
    79             this, SLOT(rotateVideo(int)));
       
    80 
       
    81     QAbstractButton *openButton = new QPushButton(tr("Open..."));
       
    82     connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
       
    83 
       
    84     playButton = new QPushButton;
       
    85     playButton->setEnabled(false);
       
    86     playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
    87 
       
    88     connect(playButton, SIGNAL(clicked()),
       
    89             this, SLOT(play()));
       
    90 
       
    91     positionSlider = new QSlider(Qt::Horizontal);
       
    92     positionSlider->setRange(0, 0);
       
    93 
       
    94     connect(positionSlider, SIGNAL(sliderMoved(int)),
       
    95             this, SLOT(setPosition(int)));
       
    96 
       
    97     connect(&movie, SIGNAL(frameChanged(int)),
       
    98             positionSlider, SLOT(setValue(int)));
       
    99 
       
   100     QBoxLayout *controlLayout = new QHBoxLayout;
       
   101     controlLayout->setMargin(0);
       
   102     controlLayout->addWidget(openButton);
       
   103     controlLayout->addWidget(playButton);
       
   104     controlLayout->addWidget(positionSlider);
       
   105 
       
   106     QBoxLayout *layout = new QVBoxLayout;
       
   107     layout->addWidget(graphicsView);
       
   108     layout->addWidget(rotateSlider);
       
   109     layout->addLayout(controlLayout);
       
   110 
       
   111     setLayout(layout);
       
   112 }
       
   113 
       
   114 VideoPlayer::~VideoPlayer()
       
   115 {
       
   116 }
       
   117 
       
   118 void VideoPlayer::openFile()
       
   119 {
       
   120     QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"));
       
   121 
       
   122     if (!fileName.isEmpty()) {
       
   123         videoItem->stop();
       
   124 
       
   125         movie.setFileName(fileName);
       
   126 
       
   127         playButton->setEnabled(true);
       
   128         positionSlider->setMaximum(movie.frameCount());
       
   129 
       
   130         movie.jumpToFrame(0);
       
   131     }
       
   132 }
       
   133 
       
   134 void VideoPlayer::play()
       
   135 {
       
   136     switch(movie.state()) {
       
   137     case QMovie::NotRunning:
       
   138         movie.start();
       
   139         break;
       
   140     case QMovie::Paused:
       
   141         movie.setPaused(false);
       
   142         break;
       
   143     case QMovie::Running:
       
   144         movie.setPaused(true);
       
   145         break;
       
   146     }
       
   147 }
       
   148 
       
   149 void VideoPlayer::movieStateChanged(QMovie::MovieState state)
       
   150 {
       
   151     switch(state) {
       
   152     case QMovie::NotRunning:
       
   153     case QMovie::Paused:
       
   154         playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
   155         break;
       
   156     case QMovie::Running:
       
   157         playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
       
   158         break;
       
   159     }
       
   160 }
       
   161 
       
   162 void VideoPlayer::frameChanged(int frame)
       
   163 {
       
   164     if (!presentImage(movie.currentImage())) {
       
   165         movie.stop();
       
   166         playButton->setEnabled(false);
       
   167         positionSlider->setMaximum(0);
       
   168     } else {
       
   169         positionSlider->setValue(frame);
       
   170     }
       
   171 }
       
   172 
       
   173 void VideoPlayer::setPosition(int frame)
       
   174 {
       
   175     movie.jumpToFrame(frame);
       
   176 }
       
   177 
       
   178 void VideoPlayer::rotateVideo(int angle)
       
   179 {
       
   180     //rotate around the center of video element
       
   181     qreal x = videoItem->boundingRect().width() / 2.0;
       
   182     qreal y = videoItem->boundingRect().height() / 2.0;
       
   183     videoItem->setTransform(QTransform().translate(x, y).rotate(angle).translate(-x, -y));
       
   184 }
       
   185 
       
   186 bool VideoPlayer::presentImage(const QImage &image)
       
   187 {
       
   188     QVideoFrame frame(image);
       
   189 
       
   190     if (!frame.isValid())
       
   191         return false;
       
   192 
       
   193     QVideoSurfaceFormat currentFormat = videoItem->surfaceFormat();
       
   194 
       
   195     if (frame.pixelFormat() != currentFormat.pixelFormat()
       
   196             || frame.size() != currentFormat.frameSize()) {
       
   197         QVideoSurfaceFormat format(frame.size(), frame.pixelFormat());
       
   198 
       
   199         if (!videoItem->start(format))
       
   200             return false;
       
   201     }
       
   202 
       
   203     if (!videoItem->present(frame)) {
       
   204         videoItem->stop();
       
   205 
       
   206         return false;
       
   207     } else {
       
   208         return true;
       
   209     }
       
   210 }