author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
child 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include "videoplayer.h" |
|
42 |
||
43 |
#include "videowidget.h" |
|
44 |
||
45 |
#include <QtMultimedia> |
|
46 |
||
47 |
VideoPlayer::VideoPlayer(QWidget *parent) |
|
48 |
: QWidget(parent) |
|
49 |
, surface(0) |
|
50 |
, playButton(0) |
|
51 |
, positionSlider(0) |
|
52 |
{ |
|
53 |
connect(&movie, SIGNAL(stateChanged(QMovie::MovieState)), |
|
54 |
this, SLOT(movieStateChanged(QMovie::MovieState))); |
|
55 |
connect(&movie, SIGNAL(frameChanged(int)), |
|
56 |
this, SLOT(frameChanged(int))); |
|
57 |
||
58 |
VideoWidget *videoWidget = new VideoWidget; |
|
59 |
surface = videoWidget->videoSurface(); |
|
60 |
||
61 |
QAbstractButton *openButton = new QPushButton(tr("Open...")); |
|
62 |
connect(openButton, SIGNAL(clicked()), this, SLOT(openFile())); |
|
63 |
||
64 |
playButton = new QPushButton; |
|
65 |
playButton->setEnabled(false); |
|
66 |
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); |
|
67 |
||
68 |
connect(playButton, SIGNAL(clicked()), |
|
69 |
this, SLOT(play())); |
|
70 |
||
71 |
positionSlider = new QSlider(Qt::Horizontal); |
|
72 |
positionSlider->setRange(0, 0); |
|
73 |
||
74 |
connect(positionSlider, SIGNAL(sliderMoved(int)), |
|
75 |
this, SLOT(setPosition(int))); |
|
76 |
||
77 |
connect(&movie, SIGNAL(frameChanged(int)), |
|
78 |
positionSlider, SLOT(setValue(int))); |
|
79 |
||
80 |
QBoxLayout *controlLayout = new QHBoxLayout; |
|
81 |
controlLayout->setMargin(0); |
|
82 |
controlLayout->addWidget(openButton); |
|
83 |
controlLayout->addWidget(playButton); |
|
84 |
controlLayout->addWidget(positionSlider); |
|
85 |
||
86 |
QBoxLayout *layout = new QVBoxLayout; |
|
87 |
layout->addWidget(videoWidget); |
|
88 |
layout->addLayout(controlLayout); |
|
89 |
||
90 |
setLayout(layout); |
|
91 |
} |
|
92 |
||
93 |
VideoPlayer::~VideoPlayer() |
|
94 |
{ |
|
95 |
} |
|
96 |
||
97 |
void VideoPlayer::openFile() |
|
98 |
{ |
|
99 |
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie")); |
|
100 |
||
101 |
if (!fileName.isEmpty()) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
102 |
surface->stop(); |
0 | 103 |
|
104 |
movie.setFileName(fileName); |
|
105 |
||
106 |
playButton->setEnabled(true); |
|
107 |
positionSlider->setMaximum(movie.frameCount()); |
|
108 |
||
109 |
movie.jumpToFrame(0); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
void VideoPlayer::play() |
|
114 |
{ |
|
115 |
switch(movie.state()) { |
|
116 |
case QMovie::NotRunning: |
|
117 |
movie.start(); |
|
118 |
break; |
|
119 |
case QMovie::Paused: |
|
120 |
movie.setPaused(false); |
|
121 |
break; |
|
122 |
case QMovie::Running: |
|
123 |
movie.setPaused(true); |
|
124 |
break; |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
void VideoPlayer::movieStateChanged(QMovie::MovieState state) |
|
129 |
{ |
|
130 |
switch(state) { |
|
131 |
case QMovie::NotRunning: |
|
132 |
case QMovie::Paused: |
|
133 |
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); |
|
134 |
break; |
|
135 |
case QMovie::Running: |
|
136 |
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause)); |
|
137 |
break; |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
void VideoPlayer::frameChanged(int frame) |
|
142 |
{ |
|
143 |
if (!presentImage(movie.currentImage())) { |
|
144 |
movie.stop(); |
|
145 |
playButton->setEnabled(false); |
|
146 |
positionSlider->setMaximum(0); |
|
147 |
} else { |
|
148 |
positionSlider->setValue(frame); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
void VideoPlayer::setPosition(int frame) |
|
153 |
{ |
|
154 |
movie.jumpToFrame(frame); |
|
155 |
} |
|
156 |
||
157 |
bool VideoPlayer::presentImage(const QImage &image) |
|
158 |
{ |
|
159 |
QVideoFrame frame(image); |
|
160 |
||
161 |
if (!frame.isValid()) |
|
162 |
return false; |
|
163 |
||
164 |
QVideoSurfaceFormat currentFormat = surface->surfaceFormat(); |
|
165 |
||
166 |
if (frame.pixelFormat() != currentFormat.pixelFormat() |
|
167 |
|| frame.size() != currentFormat.frameSize()) { |
|
168 |
QVideoSurfaceFormat format(frame.size(), frame.pixelFormat()); |
|
169 |
||
170 |
if (!surface->start(format)) |
|
171 |
return false; |
|
172 |
} |
|
173 |
||
174 |
if (!surface->present(frame)) { |
|
175 |
surface->stop(); |
|
176 |
||
177 |
return false; |
|
178 |
} else { |
|
179 |
return true; |
|
180 |
} |
|
181 |
} |