source: trunk/examples/multimedia/videowidget/videoplayer.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
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** 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 qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "videoplayer.h"
43
44#include "videowidget.h"
45
46#include <QtMultimedia>
47
48VideoPlayer::VideoPlayer(QWidget *parent)
49 : QWidget(parent)
50 , surface(0)
51 , playButton(0)
52 , positionSlider(0)
53{
54 connect(&movie, SIGNAL(stateChanged(QMovie::MovieState)),
55 this, SLOT(movieStateChanged(QMovie::MovieState)));
56 connect(&movie, SIGNAL(frameChanged(int)),
57 this, SLOT(frameChanged(int)));
58
59 VideoWidget *videoWidget = new VideoWidget;
60 surface = videoWidget->videoSurface();
61
62 QAbstractButton *openButton = new QPushButton(tr("Open..."));
63 connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
64
65 playButton = new QPushButton;
66 playButton->setEnabled(false);
67 playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
68
69 connect(playButton, SIGNAL(clicked()),
70 this, SLOT(play()));
71
72 positionSlider = new QSlider(Qt::Horizontal);
73 positionSlider->setRange(0, 0);
74
75 connect(positionSlider, SIGNAL(sliderMoved(int)),
76 this, SLOT(setPosition(int)));
77
78 connect(&movie, SIGNAL(frameChanged(int)),
79 positionSlider, SLOT(setValue(int)));
80
81 QBoxLayout *controlLayout = new QHBoxLayout;
82 controlLayout->setMargin(0);
83 controlLayout->addWidget(openButton);
84 controlLayout->addWidget(playButton);
85 controlLayout->addWidget(positionSlider);
86
87 QBoxLayout *layout = new QVBoxLayout;
88 layout->addWidget(videoWidget);
89 layout->addLayout(controlLayout);
90
91 setLayout(layout);
92}
93
94VideoPlayer::~VideoPlayer()
95{
96}
97
98void VideoPlayer::openFile()
99{
100 QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"));
101
102 if (!fileName.isEmpty()) {
103 surface->stop();
104
105 movie.setFileName(fileName);
106
107 playButton->setEnabled(true);
108 positionSlider->setMaximum(movie.frameCount());
109
110 movie.jumpToFrame(0);
111 }
112}
113
114void VideoPlayer::play()
115{
116 switch(movie.state()) {
117 case QMovie::NotRunning:
118 movie.start();
119 break;
120 case QMovie::Paused:
121 movie.setPaused(false);
122 break;
123 case QMovie::Running:
124 movie.setPaused(true);
125 break;
126 }
127}
128
129void VideoPlayer::movieStateChanged(QMovie::MovieState state)
130{
131 switch(state) {
132 case QMovie::NotRunning:
133 case QMovie::Paused:
134 playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
135 break;
136 case QMovie::Running:
137 playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
138 break;
139 }
140}
141
142void VideoPlayer::frameChanged(int frame)
143{
144 if (!presentImage(movie.currentImage())) {
145 movie.stop();
146 playButton->setEnabled(false);
147 positionSlider->setMaximum(0);
148 } else {
149 positionSlider->setValue(frame);
150 }
151}
152
153void VideoPlayer::setPosition(int frame)
154{
155 movie.jumpToFrame(frame);
156}
157
158bool VideoPlayer::presentImage(const QImage &image)
159{
160 QVideoFrame frame(image);
161
162 if (!frame.isValid())
163 return false;
164
165 QVideoSurfaceFormat currentFormat = surface->surfaceFormat();
166
167 if (frame.pixelFormat() != currentFormat.pixelFormat()
168 || frame.size() != currentFormat.frameSize()) {
169 QVideoSurfaceFormat format(frame.size(), frame.pixelFormat());
170
171 if (!surface->start(format))
172 return false;
173 }
174
175 if (!surface->present(frame)) {
176 surface->stop();
177
178 return false;
179 } else {
180 return true;
181 }
182}
Note: See TracBrowser for help on using the repository browser.