qtmobility/examples/videowidget/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 
       
    44 #include "videowidget.h"
       
    45 
       
    46 #include <QtGui>
       
    47 #include <qvideosurfaceformat.h>
       
    48 
       
    49 VideoPlayer::VideoPlayer(QWidget *parent)
       
    50     : QWidget(parent)
       
    51     , surface(0)
       
    52     , playButton(0)
       
    53     , positionSlider(0)
       
    54 {
       
    55     connect(&movie, SIGNAL(stateChanged(QMovie::MovieState)),
       
    56             this, SLOT(movieStateChanged(QMovie::MovieState)));
       
    57     connect(&movie, SIGNAL(frameChanged(int)),
       
    58             this, SLOT(frameChanged(int)));
       
    59 
       
    60     VideoWidget *videoWidget = new VideoWidget;
       
    61     surface = videoWidget->videoSurface();
       
    62 
       
    63     QAbstractButton *openButton = new QPushButton(tr("Open..."));
       
    64     connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
       
    65 
       
    66     playButton = new QPushButton;
       
    67     playButton->setEnabled(false);
       
    68     playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
    69 
       
    70     connect(playButton, SIGNAL(clicked()),
       
    71             this, SLOT(play()));
       
    72 
       
    73     positionSlider = new QSlider(Qt::Horizontal);
       
    74     positionSlider->setRange(0, 0);
       
    75 
       
    76     connect(positionSlider, SIGNAL(sliderMoved(int)),
       
    77             this, SLOT(setPosition(int)));
       
    78 
       
    79     connect(&movie, SIGNAL(frameChanged(int)),
       
    80             positionSlider, SLOT(setValue(int)));
       
    81 
       
    82     QBoxLayout *controlLayout = new QHBoxLayout;
       
    83     controlLayout->setMargin(0);
       
    84     controlLayout->addWidget(openButton);
       
    85     controlLayout->addWidget(playButton);
       
    86     controlLayout->addWidget(positionSlider);
       
    87 
       
    88     QBoxLayout *layout = new QVBoxLayout;
       
    89     layout->addWidget(videoWidget);
       
    90     layout->addLayout(controlLayout);
       
    91 
       
    92     setLayout(layout);
       
    93 }
       
    94 
       
    95 VideoPlayer::~VideoPlayer()
       
    96 {
       
    97 }
       
    98 
       
    99 void VideoPlayer::openFile()
       
   100 {
       
   101     QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"));
       
   102 
       
   103     if (!fileName.isEmpty()) {
       
   104         surface->stop();
       
   105 
       
   106         movie.setFileName(fileName);
       
   107 
       
   108         playButton->setEnabled(true);
       
   109         positionSlider->setMaximum(movie.frameCount());
       
   110 
       
   111         movie.jumpToFrame(0);
       
   112     }
       
   113 }
       
   114 
       
   115 void VideoPlayer::play()
       
   116 {
       
   117     switch(movie.state()) {
       
   118     case QMovie::NotRunning:
       
   119         movie.start();
       
   120         break;
       
   121     case QMovie::Paused:
       
   122         movie.setPaused(false);
       
   123         break;
       
   124     case QMovie::Running:
       
   125         movie.setPaused(true);
       
   126         break;
       
   127     }
       
   128 }
       
   129 
       
   130 void VideoPlayer::movieStateChanged(QMovie::MovieState state)
       
   131 {
       
   132     switch(state) {
       
   133     case QMovie::NotRunning:
       
   134     case QMovie::Paused:
       
   135         playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
       
   136         break;
       
   137     case QMovie::Running:
       
   138         playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
       
   139         break;
       
   140     }
       
   141 }
       
   142 
       
   143 void VideoPlayer::frameChanged(int frame)
       
   144 {
       
   145     if (!presentImage(movie.currentImage())) {
       
   146         movie.stop();
       
   147         playButton->setEnabled(false);
       
   148         positionSlider->setMaximum(0);
       
   149     } else {
       
   150         positionSlider->setValue(frame);
       
   151     }
       
   152 }
       
   153 
       
   154 void VideoPlayer::setPosition(int frame)
       
   155 {
       
   156     movie.jumpToFrame(frame);
       
   157 }
       
   158 
       
   159 bool VideoPlayer::presentImage(const QImage &image)
       
   160 {
       
   161     QVideoFrame frame(image);
       
   162 
       
   163     if (!frame.isValid())
       
   164         return false;
       
   165 
       
   166     QVideoSurfaceFormat currentFormat = surface->surfaceFormat();
       
   167 
       
   168     if (frame.pixelFormat() != currentFormat.pixelFormat()
       
   169             || frame.size() != currentFormat.frameSize()) {
       
   170         QVideoSurfaceFormat format(frame.size(), frame.pixelFormat());
       
   171 
       
   172         if (!surface->start(format))
       
   173             return false;
       
   174     }
       
   175 
       
   176     if (!surface->present(frame)) {
       
   177         surface->stop();
       
   178 
       
   179         return false;
       
   180     } else {
       
   181         return true;
       
   182     }
       
   183 }