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 "playercontrols.h"
52
53#include <QBoxLayout>
54#include <QSlider>
55#include <QStyle>
56#include <QToolButton>
57#include <QComboBox>
58#include <QAudio>
59
60PlayerControls::PlayerControls(QWidget *parent)
61 : QWidget(parent)
62{
63 m_playButton = new QToolButton(this);
64 m_playButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaPlay));
65
66 connect(sender: m_playButton, signal: &QAbstractButton::clicked, receiver: this, slot: &PlayerControls::playClicked);
67
68 m_stopButton = new QToolButton(this);
69 m_stopButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaStop));
70 m_stopButton->setEnabled(false);
71
72 connect(sender: m_stopButton, signal: &QAbstractButton::clicked, receiver: this, slot: &PlayerControls::stop);
73
74 m_nextButton = new QToolButton(this);
75 m_nextButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaSkipForward));
76
77 connect(sender: m_nextButton, signal: &QAbstractButton::clicked, receiver: this, slot: &PlayerControls::next);
78
79 m_previousButton = new QToolButton(this);
80 m_previousButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaSkipBackward));
81
82 connect(sender: m_previousButton, signal: &QAbstractButton::clicked, receiver: this, slot: &PlayerControls::previous);
83
84 m_muteButton = new QToolButton(this);
85 m_muteButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaVolume));
86
87 connect(sender: m_muteButton, signal: &QAbstractButton::clicked, receiver: this, slot: &PlayerControls::muteClicked);
88
89 m_volumeSlider = new QSlider(Qt::Horizontal, this);
90 m_volumeSlider->setRange(min: 0, max: 100);
91
92 connect(sender: m_volumeSlider, signal: &QSlider::valueChanged, receiver: this, slot: &PlayerControls::onVolumeSliderValueChanged);
93
94 m_rateBox = new QComboBox(this);
95 m_rateBox->addItem(atext: "0.5x", auserData: QVariant(0.5));
96 m_rateBox->addItem(atext: "1.0x", auserData: QVariant(1.0));
97 m_rateBox->addItem(atext: "2.0x", auserData: QVariant(2.0));
98 m_rateBox->setCurrentIndex(1);
99
100 connect(sender: m_rateBox, signal: QOverload<int>::of(ptr: &QComboBox::activated), receiver: this, slot: &PlayerControls::updateRate);
101
102 QBoxLayout *layout = new QHBoxLayout;
103 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
104 layout->addWidget(m_stopButton);
105 layout->addWidget(m_previousButton);
106 layout->addWidget(m_playButton);
107 layout->addWidget(m_nextButton);
108 layout->addWidget(m_muteButton);
109 layout->addWidget(m_volumeSlider);
110 layout->addWidget(m_rateBox);
111 setLayout(layout);
112}
113
114QMediaPlayer::State PlayerControls::state() const
115{
116 return m_playerState;
117}
118
119void PlayerControls::setState(QMediaPlayer::State state)
120{
121 if (state != m_playerState) {
122 m_playerState = state;
123
124 switch (state) {
125 case QMediaPlayer::StoppedState:
126 m_stopButton->setEnabled(false);
127 m_playButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaPlay));
128 break;
129 case QMediaPlayer::PlayingState:
130 m_stopButton->setEnabled(true);
131 m_playButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaPause));
132 break;
133 case QMediaPlayer::PausedState:
134 m_stopButton->setEnabled(true);
135 m_playButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaPlay));
136 break;
137 }
138 }
139}
140
141int PlayerControls::volume() const
142{
143 qreal linearVolume = QAudio::convertVolume(volume: m_volumeSlider->value() / qreal(100),
144 from: QAudio::LogarithmicVolumeScale,
145 to: QAudio::LinearVolumeScale);
146
147 return qRound(d: linearVolume * 100);
148}
149
150void PlayerControls::setVolume(int volume)
151{
152 qreal logarithmicVolume = QAudio::convertVolume(volume: volume / qreal(100),
153 from: QAudio::LinearVolumeScale,
154 to: QAudio::LogarithmicVolumeScale);
155
156 m_volumeSlider->setValue(qRound(d: logarithmicVolume * 100));
157}
158
159bool PlayerControls::isMuted() const
160{
161 return m_playerMuted;
162}
163
164void PlayerControls::setMuted(bool muted)
165{
166 if (muted != m_playerMuted) {
167 m_playerMuted = muted;
168
169 m_muteButton->setIcon(style()->standardIcon(standardIcon: muted
170 ? QStyle::SP_MediaVolumeMuted
171 : QStyle::SP_MediaVolume));
172 }
173}
174
175void PlayerControls::playClicked()
176{
177 switch (m_playerState) {
178 case QMediaPlayer::StoppedState:
179 case QMediaPlayer::PausedState:
180 emit play();
181 break;
182 case QMediaPlayer::PlayingState:
183 emit pause();
184 break;
185 }
186}
187
188void PlayerControls::muteClicked()
189{
190 emit changeMuting(muting: !m_playerMuted);
191}
192
193qreal PlayerControls::playbackRate() const
194{
195 return m_rateBox->itemData(index: m_rateBox->currentIndex()).toDouble();
196}
197
198void PlayerControls::setPlaybackRate(float rate)
199{
200 for (int i = 0; i < m_rateBox->count(); ++i) {
201 if (qFuzzyCompare(p1: rate, p2: float(m_rateBox->itemData(index: i).toDouble()))) {
202 m_rateBox->setCurrentIndex(i);
203 return;
204 }
205 }
206
207 m_rateBox->addItem(atext: QString("%1x").arg(a: rate), auserData: QVariant(rate));
208 m_rateBox->setCurrentIndex(m_rateBox->count() - 1);
209}
210
211void PlayerControls::updateRate()
212{
213 emit changeRate(rate: playbackRate());
214}
215
216void PlayerControls::onVolumeSliderValueChanged()
217{
218 emit changeVolume(volume: volume());
219}
220

source code of qtmultimedia/examples/multimediawidgets/player/playercontrols.cpp