|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 <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 if (videoItem->isStarted()) |
|
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 } |