|
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 QStringList supportedFormats; |
|
121 foreach (QString fmt, QMovie::supportedFormats()) |
|
122 supportedFormats << fmt; |
|
123 foreach (QString fmt, QImageReader::supportedImageFormats()) |
|
124 supportedFormats << fmt; |
|
125 |
|
126 QString filter = "Images ("; |
|
127 foreach ( QString fmt, supportedFormats) { |
|
128 filter.append(QString("*.%1 ").arg(fmt)); |
|
129 } |
|
130 filter.append(")"); |
|
131 |
|
132 QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"), |
|
133 QDir::homePath(), filter); |
|
134 |
|
135 if (!fileName.isEmpty()) { |
|
136 videoItem->stop(); |
|
137 |
|
138 movie.setFileName(fileName); |
|
139 |
|
140 playButton->setEnabled(true); |
|
141 positionSlider->setMaximum(movie.frameCount()); |
|
142 |
|
143 movie.jumpToFrame(0); |
|
144 } |
|
145 } |
|
146 |
|
147 void VideoPlayer::play() |
|
148 { |
|
149 switch(movie.state()) { |
|
150 case QMovie::NotRunning: |
|
151 movie.start(); |
|
152 break; |
|
153 case QMovie::Paused: |
|
154 movie.setPaused(false); |
|
155 break; |
|
156 case QMovie::Running: |
|
157 movie.setPaused(true); |
|
158 break; |
|
159 } |
|
160 } |
|
161 |
|
162 void VideoPlayer::movieStateChanged(QMovie::MovieState state) |
|
163 { |
|
164 switch(state) { |
|
165 case QMovie::NotRunning: |
|
166 case QMovie::Paused: |
|
167 playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); |
|
168 break; |
|
169 case QMovie::Running: |
|
170 playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause)); |
|
171 break; |
|
172 } |
|
173 } |
|
174 |
|
175 void VideoPlayer::frameChanged(int frame) |
|
176 { |
|
177 if (!presentImage(movie.currentImage())) { |
|
178 movie.stop(); |
|
179 playButton->setEnabled(false); |
|
180 positionSlider->setMaximum(0); |
|
181 } else { |
|
182 positionSlider->setValue(frame); |
|
183 } |
|
184 } |
|
185 |
|
186 void VideoPlayer::setPosition(int frame) |
|
187 { |
|
188 movie.jumpToFrame(frame); |
|
189 } |
|
190 |
|
191 void VideoPlayer::rotateVideo(int angle) |
|
192 { |
|
193 //rotate around the center of video element |
|
194 qreal x = videoItem->boundingRect().width() / 2.0; |
|
195 qreal y = videoItem->boundingRect().height() / 2.0; |
|
196 videoItem->setTransform(QTransform().translate(x, y).rotate(angle).translate(-x, -y)); |
|
197 } |
|
198 |
|
199 bool VideoPlayer::presentImage(const QImage &image) |
|
200 { |
|
201 QVideoFrame frame(image); |
|
202 |
|
203 if (!frame.isValid()) |
|
204 return false; |
|
205 |
|
206 QVideoSurfaceFormat currentFormat = videoItem->surfaceFormat(); |
|
207 |
|
208 if (frame.pixelFormat() != currentFormat.pixelFormat() |
|
209 || frame.size() != currentFormat.frameSize()) { |
|
210 QVideoSurfaceFormat format(frame.size(), frame.pixelFormat()); |
|
211 |
|
212 if (!videoItem->start(format)) |
|
213 return false; |
|
214 } |
|
215 |
|
216 if (!videoItem->present(frame)) { |
|
217 videoItem->stop(); |
|
218 |
|
219 return false; |
|
220 } else { |
|
221 return true; |
|
222 } |
|
223 } |