1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "videoplayer.h"
|
---|
43 | #include "videoitem.h"
|
---|
44 |
|
---|
45 | #include <QtMultimedia>
|
---|
46 |
|
---|
47 | #ifndef QT_NO_OPENGL
|
---|
48 | # include <QtOpenGL/QGLWidget>
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | VideoPlayer::VideoPlayer(QWidget *parent, Qt::WindowFlags flags)
|
---|
52 | : QWidget(parent, flags)
|
---|
53 | , videoItem(0)
|
---|
54 | , playButton(0)
|
---|
55 | , positionSlider(0)
|
---|
56 | {
|
---|
57 | connect(&movie, SIGNAL(stateChanged(QMovie::MovieState)),
|
---|
58 | this, SLOT(movieStateChanged(QMovie::MovieState)));
|
---|
59 | connect(&movie, SIGNAL(frameChanged(int)),
|
---|
60 | this, SLOT(frameChanged(int)));
|
---|
61 |
|
---|
62 | videoItem = new VideoItem;
|
---|
63 |
|
---|
64 | QGraphicsScene *scene = new QGraphicsScene(this);
|
---|
65 | QGraphicsView *graphicsView = new QGraphicsView(scene);
|
---|
66 |
|
---|
67 | #ifndef QT_NO_OPENGL
|
---|
68 | graphicsView->setViewport(new QGLWidget);
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | scene->addItem(videoItem);
|
---|
72 |
|
---|
73 | QSlider *rotateSlider = new QSlider(Qt::Horizontal);
|
---|
74 | rotateSlider->setRange(-180, 180);
|
---|
75 | rotateSlider->setValue(0);
|
---|
76 |
|
---|
77 | connect(rotateSlider, SIGNAL(valueChanged(int)),
|
---|
78 | this, SLOT(rotateVideo(int)));
|
---|
79 |
|
---|
80 | QAbstractButton *openButton = new QPushButton(tr("Open..."));
|
---|
81 | connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
|
---|
82 |
|
---|
83 | playButton = new QPushButton;
|
---|
84 | playButton->setEnabled(false);
|
---|
85 | playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
|
---|
86 |
|
---|
87 | connect(playButton, SIGNAL(clicked()),
|
---|
88 | this, SLOT(play()));
|
---|
89 |
|
---|
90 | positionSlider = new QSlider(Qt::Horizontal);
|
---|
91 | positionSlider->setRange(0, 0);
|
---|
92 |
|
---|
93 | connect(positionSlider, SIGNAL(sliderMoved(int)),
|
---|
94 | this, SLOT(setPosition(int)));
|
---|
95 |
|
---|
96 | connect(&movie, SIGNAL(frameChanged(int)),
|
---|
97 | positionSlider, SLOT(setValue(int)));
|
---|
98 |
|
---|
99 | QBoxLayout *controlLayout = new QHBoxLayout;
|
---|
100 | controlLayout->setMargin(0);
|
---|
101 | controlLayout->addWidget(openButton);
|
---|
102 | controlLayout->addWidget(playButton);
|
---|
103 | controlLayout->addWidget(positionSlider);
|
---|
104 |
|
---|
105 | QBoxLayout *layout = new QVBoxLayout;
|
---|
106 | layout->addWidget(graphicsView);
|
---|
107 | layout->addWidget(rotateSlider);
|
---|
108 | layout->addLayout(controlLayout);
|
---|
109 |
|
---|
110 | setLayout(layout);
|
---|
111 | }
|
---|
112 |
|
---|
113 | VideoPlayer::~VideoPlayer()
|
---|
114 | {
|
---|
115 | }
|
---|
116 |
|
---|
117 | void VideoPlayer::openFile()
|
---|
118 | {
|
---|
119 | QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"));
|
---|
120 |
|
---|
121 | if (!fileName.isEmpty()) {
|
---|
122 | videoItem->stop();
|
---|
123 |
|
---|
124 | movie.setFileName(fileName);
|
---|
125 |
|
---|
126 | playButton->setEnabled(true);
|
---|
127 | positionSlider->setMaximum(movie.frameCount());
|
---|
128 |
|
---|
129 | movie.jumpToFrame(0);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | void VideoPlayer::play()
|
---|
134 | {
|
---|
135 | switch(movie.state()) {
|
---|
136 | case QMovie::NotRunning:
|
---|
137 | movie.start();
|
---|
138 | break;
|
---|
139 | case QMovie::Paused:
|
---|
140 | movie.setPaused(false);
|
---|
141 | break;
|
---|
142 | case QMovie::Running:
|
---|
143 | movie.setPaused(true);
|
---|
144 | break;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | void VideoPlayer::movieStateChanged(QMovie::MovieState state)
|
---|
149 | {
|
---|
150 | switch(state) {
|
---|
151 | case QMovie::NotRunning:
|
---|
152 | case QMovie::Paused:
|
---|
153 | playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
|
---|
154 | break;
|
---|
155 | case QMovie::Running:
|
---|
156 | playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | void VideoPlayer::frameChanged(int frame)
|
---|
162 | {
|
---|
163 | if (!presentImage(movie.currentImage())) {
|
---|
164 | movie.stop();
|
---|
165 | playButton->setEnabled(false);
|
---|
166 | positionSlider->setMaximum(0);
|
---|
167 | } else {
|
---|
168 | positionSlider->setValue(frame);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | void VideoPlayer::setPosition(int frame)
|
---|
173 | {
|
---|
174 | movie.jumpToFrame(frame);
|
---|
175 | }
|
---|
176 |
|
---|
177 | void VideoPlayer::rotateVideo(int angle)
|
---|
178 | {
|
---|
179 | //rotate around the center of video element
|
---|
180 | qreal x = videoItem->boundingRect().width() / 2.0;
|
---|
181 | qreal y = videoItem->boundingRect().height() / 2.0;
|
---|
182 | videoItem->setTransform(QTransform().translate(x, y).rotate(angle).translate(-x, -y));
|
---|
183 | }
|
---|
184 |
|
---|
185 | bool VideoPlayer::presentImage(const QImage &image)
|
---|
186 | {
|
---|
187 | QVideoFrame frame(image);
|
---|
188 |
|
---|
189 | if (!frame.isValid())
|
---|
190 | return false;
|
---|
191 |
|
---|
192 | QVideoSurfaceFormat currentFormat = videoItem->surfaceFormat();
|
---|
193 |
|
---|
194 | if (frame.pixelFormat() != currentFormat.pixelFormat()
|
---|
195 | || frame.size() != currentFormat.frameSize()) {
|
---|
196 | QVideoSurfaceFormat format(frame.size(), frame.pixelFormat());
|
---|
197 |
|
---|
198 | if (!videoItem->start(format))
|
---|
199 | return false;
|
---|
200 | }
|
---|
201 |
|
---|
202 | if (!videoItem->present(frame)) {
|
---|
203 | videoItem->stop();
|
---|
204 |
|
---|
205 | return false;
|
---|
206 | } else {
|
---|
207 | return true;
|
---|
208 | }
|
---|
209 | }
|
---|