1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "videoplayer.h"
52
53#include <QtWidgets>
54#include <QGraphicsVideoItem>
55
56VideoPlayer::VideoPlayer(QWidget *parent)
57 : QWidget(parent)
58{
59 m_mediaPlayer = new QMediaPlayer(this, QMediaPlayer::VideoSurface);
60 const QRect screenGeometry = QApplication::desktop()->screenGeometry(widget: this);
61 m_videoItem = new QGraphicsVideoItem;
62 m_videoItem->setSize(QSizeF(screenGeometry.width() / 3, screenGeometry.height() / 2));
63
64 QGraphicsScene *scene = new QGraphicsScene(this);
65 QGraphicsView *graphicsView = new QGraphicsView(scene);
66
67 scene->addItem(item: m_videoItem);
68
69 QSlider *rotateSlider = new QSlider(Qt::Horizontal);
70 rotateSlider->setToolTip(tr(s: "Rotate Video"));
71 rotateSlider->setRange(min: -180, max: 180);
72 rotateSlider->setValue(0);
73
74 connect(sender: rotateSlider, signal: &QAbstractSlider::valueChanged,
75 receiver: this, slot: &VideoPlayer::rotateVideo);
76
77 QAbstractButton *openButton = new QPushButton(tr(s: "Open..."));
78 connect(sender: openButton, signal: &QAbstractButton::clicked, receiver: this, slot: &VideoPlayer::openFile);
79
80 m_playButton = new QPushButton;
81 m_playButton->setEnabled(false);
82 m_playButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaPlay));
83
84 connect(sender: m_playButton, signal: &QAbstractButton::clicked, receiver: this, slot: &VideoPlayer::play);
85
86 m_positionSlider = new QSlider(Qt::Horizontal);
87 m_positionSlider->setRange(min: 0, max: 0);
88
89 connect(sender: m_positionSlider, signal: &QAbstractSlider::sliderMoved,
90 receiver: this, slot: &VideoPlayer::setPosition);
91
92 QBoxLayout *controlLayout = new QHBoxLayout;
93 controlLayout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
94 controlLayout->addWidget(openButton);
95 controlLayout->addWidget(m_playButton);
96 controlLayout->addWidget(m_positionSlider);
97
98 QBoxLayout *layout = new QVBoxLayout(this);
99 layout->addWidget(graphicsView);
100 layout->addWidget(rotateSlider);
101 layout->addLayout(layout: controlLayout);
102
103 m_mediaPlayer->setVideoOutput(m_videoItem);
104 connect(sender: m_mediaPlayer, signal: &QMediaPlayer::stateChanged,
105 receiver: this, slot: &VideoPlayer::mediaStateChanged);
106 connect(sender: m_mediaPlayer, signal: &QMediaPlayer::positionChanged, receiver: this, slot: &VideoPlayer::positionChanged);
107 connect(sender: m_mediaPlayer, signal: &QMediaPlayer::durationChanged, receiver: this, slot: &VideoPlayer::durationChanged);
108}
109
110VideoPlayer::~VideoPlayer()
111{
112}
113
114QSize VideoPlayer::sizeHint() const
115{
116 return (m_videoItem->size() * qreal(3) / qreal(2)).toSize();
117}
118
119bool VideoPlayer::isPlayerAvailable() const
120{
121 return m_mediaPlayer->isAvailable();
122}
123
124void VideoPlayer::openFile()
125{
126 QFileDialog fileDialog(this);
127 fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
128 fileDialog.setWindowTitle(tr(s: "Open Movie"));
129 const QStringList supportedMimeTypes = m_mediaPlayer->supportedMimeTypes();
130 if (!supportedMimeTypes.isEmpty())
131 fileDialog.setMimeTypeFilters(supportedMimeTypes);
132 fileDialog.setDirectory(QStandardPaths::standardLocations(type: QStandardPaths::MoviesLocation).value(i: 0, defaultValue: QDir::homePath()));
133 if (fileDialog.exec() == QDialog::Accepted)
134 load(url: fileDialog.selectedUrls().constFirst());
135}
136
137void VideoPlayer::load(const QUrl &url)
138{
139 m_mediaPlayer->setMedia(media: url);
140 m_playButton->setEnabled(true);
141}
142
143void VideoPlayer::play()
144{
145 switch (m_mediaPlayer->state()) {
146 case QMediaPlayer::PlayingState:
147 m_mediaPlayer->pause();
148 break;
149 default:
150 m_mediaPlayer->play();
151 break;
152 }
153}
154
155void VideoPlayer::mediaStateChanged(QMediaPlayer::State state)
156{
157 switch(state) {
158 case QMediaPlayer::PlayingState:
159 m_playButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaPause));
160 break;
161 default:
162 m_playButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaPlay));
163 break;
164 }
165}
166
167void VideoPlayer::positionChanged(qint64 position)
168{
169 m_positionSlider->setValue(position);
170}
171
172void VideoPlayer::durationChanged(qint64 duration)
173{
174 m_positionSlider->setRange(min: 0, max: duration);
175}
176
177void VideoPlayer::setPosition(int position)
178{
179 m_mediaPlayer->setPosition(position);
180}
181
182
183void VideoPlayer::rotateVideo(int angle)
184{
185 //rotate around the center of video element
186 qreal x = m_videoItem->boundingRect().width() / 2.0;
187 qreal y = m_videoItem->boundingRect().height() / 2.0;
188 m_videoItem->setTransform(matrix: QTransform().translate(dx: x, dy: y).rotate(a: angle).translate(dx: -x, dy: -y));
189}
190

source code of qtmultimedia/examples/multimediawidgets/videographicsitem/videoplayer.cpp