1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the examples of the Qt Toolkit.
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
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.
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.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
40
****************************************************************************/
42
#include "videoplayer.h"
43
#include "videoitem.h"
45
#include <QtMultimedia>
48
# include <QtOpenGL/QGLWidget>
51
VideoPlayer::VideoPlayer(QWidget *parent, Qt::WindowFlags flags)
52
: QWidget(parent, flags)
57
connect(&movie, SIGNAL(stateChanged(QMovie::MovieState)),
58
this, SLOT(movieStateChanged(QMovie::MovieState)));
59
connect(&movie, SIGNAL(frameChanged(int)),
60
this, SLOT(frameChanged(int)));
62
videoItem = new VideoItem;
64
QGraphicsScene *scene = new QGraphicsScene(this);
65
QGraphicsView *graphicsView = new QGraphicsView(scene);
68
graphicsView->setViewport(new QGLWidget);
71
scene->addItem(videoItem);
73
QSlider *rotateSlider = new QSlider(Qt::Horizontal);
74
rotateSlider->setRange(-180, 180);
75
rotateSlider->setValue(0);
77
connect(rotateSlider, SIGNAL(valueChanged(int)),
78
this, SLOT(rotateVideo(int)));
80
QAbstractButton *openButton = new QPushButton(tr("Open..."));
81
connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
83
playButton = new QPushButton;
84
playButton->setEnabled(false);
85
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
87
connect(playButton, SIGNAL(clicked()),
90
positionSlider = new QSlider(Qt::Horizontal);
91
positionSlider->setRange(0, 0);
93
connect(positionSlider, SIGNAL(sliderMoved(int)),
94
this, SLOT(setPosition(int)));
96
connect(&movie, SIGNAL(frameChanged(int)),
97
positionSlider, SLOT(setValue(int)));
99
QBoxLayout *controlLayout = new QHBoxLayout;
100
controlLayout->setMargin(0);
101
controlLayout->addWidget(openButton);
102
controlLayout->addWidget(playButton);
103
controlLayout->addWidget(positionSlider);
105
QBoxLayout *layout = new QVBoxLayout;
106
layout->addWidget(graphicsView);
107
layout->addWidget(rotateSlider);
108
layout->addLayout(controlLayout);
113
VideoPlayer::~VideoPlayer()
117
void VideoPlayer::openFile()
119
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"));
121
if (!fileName.isEmpty()) {
122
if (videoItem->isStarted())
125
movie.setFileName(fileName);
127
playButton->setEnabled(true);
128
positionSlider->setMaximum(movie.frameCount());
130
movie.jumpToFrame(0);
134
void VideoPlayer::play()
136
switch(movie.state()) {
137
case QMovie::NotRunning:
141
movie.setPaused(false);
143
case QMovie::Running:
144
movie.setPaused(true);
149
void VideoPlayer::movieStateChanged(QMovie::MovieState state)
152
case QMovie::NotRunning:
154
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
156
case QMovie::Running:
157
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
162
void VideoPlayer::frameChanged(int frame)
164
if (!presentImage(movie.currentImage())) {
166
playButton->setEnabled(false);
167
positionSlider->setMaximum(0);
169
positionSlider->setValue(frame);
173
void VideoPlayer::setPosition(int frame)
175
movie.jumpToFrame(frame);
178
void VideoPlayer::rotateVideo(int angle)
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));
186
bool VideoPlayer::presentImage(const QImage &image)
188
QVideoFrame frame(image);
190
if (!frame.isValid())
193
QVideoSurfaceFormat currentFormat = videoItem->surfaceFormat();
195
if (frame.pixelFormat() != currentFormat.pixelFormat()
196
|| frame.size() != currentFormat.frameSize()) {
197
QVideoSurfaceFormat format(frame.size(), frame.pixelFormat());
199
if (!videoItem->start(format))
203
if (!videoItem->present(frame)) {