]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/phononwidget.cpp
Merge branch 'release/19.12'
[dolphin.git] / src / panels / information / phononwidget.cpp
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18
19 */
20
21 #include "phononwidget.h"
22
23 #include <KLocalizedString>
24 #include <Phonon/AudioOutput>
25 #include <Phonon/MediaObject>
26 #include <Phonon/SeekSlider>
27 #include <Phonon/VideoWidget>
28
29 #include <QShowEvent>
30 #include <QStyle>
31 #include <QToolButton>
32 #include <QVBoxLayout>
33
34 class EmbeddedVideoPlayer : public Phonon::VideoWidget
35 {
36 Q_OBJECT
37
38 public:
39 EmbeddedVideoPlayer(QWidget *parent = nullptr) :
40 Phonon::VideoWidget(parent)
41 {
42 }
43
44 void setSizeHint(const QSize& size)
45 {
46 m_sizeHint = size;
47 updateGeometry();
48 }
49
50 QSize sizeHint() const override
51 {
52 return m_sizeHint.isValid() ? m_sizeHint : Phonon::VideoWidget::sizeHint();
53 }
54
55 private:
56 QSize m_sizeHint;
57 };
58
59 PhononWidget::PhononWidget(QWidget *parent)
60 : QWidget(parent),
61 m_url(),
62 m_playButton(nullptr),
63 m_pauseButton(nullptr),
64 m_topLayout(nullptr),
65 m_media(nullptr),
66 m_seekSlider(nullptr),
67 m_audioOutput(nullptr),
68 m_videoPlayer(nullptr)
69 {
70 }
71
72 void PhononWidget::setUrl(const QUrl &url, MediaKind kind)
73 {
74 if (m_url != url) {
75 m_url = url;
76 m_isVideo = kind == MediaKind::Video;
77 }
78 if (m_autoPlay) {
79 play();
80 } else {
81 stop();
82 }
83 }
84
85 void PhononWidget::setAutoPlay(bool autoPlay)
86 {
87 m_autoPlay = autoPlay;
88 if (!m_url.isEmpty() && (m_media == nullptr || m_media->state() != Phonon::State::PlayingState) && m_autoPlay && isVisible()) {
89 play();
90 }
91 }
92
93 QUrl PhononWidget::url() const
94 {
95 return m_url;
96 }
97
98 void PhononWidget::clearUrl()
99 {
100 m_url.clear();
101 }
102
103 void PhononWidget::togglePlayback()
104 {
105 if (m_media && m_media->state() == Phonon::State::PlayingState) {
106 m_media->pause();
107 } else {
108 play();
109 }
110 }
111
112 bool PhononWidget::eventFilter(QObject *object, QEvent *event)
113 {
114 Q_UNUSED(object)
115 if (event->type() == QEvent::MouseButtonPress) {
116 const QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
117 if (mouseEvent->button() == Qt::LeftButton) {
118 // toggle playback
119 togglePlayback();
120 return true;
121 }
122 }
123 return false;
124 }
125
126 void PhononWidget::setVideoSize(const QSize& size)
127 {
128 if (m_videoSize != size) {
129 m_videoSize = size;
130 applyVideoSize();
131 }
132 }
133
134 QSize PhononWidget::videoSize() const
135 {
136 return m_videoSize;
137 }
138
139 void PhononWidget::showEvent(QShowEvent *event)
140 {
141 if (event->spontaneous()) {
142 QWidget::showEvent(event);
143 return;
144 }
145
146 if (!m_topLayout) {
147 m_topLayout = new QVBoxLayout(this);
148 m_topLayout->setContentsMargins(0, 0, 0, 0);
149
150 QHBoxLayout *controlsLayout = new QHBoxLayout();
151 controlsLayout->setContentsMargins(0, 0, 0, 0);
152 controlsLayout->setSpacing(0);
153
154 m_playButton = new QToolButton(this);
155 m_pauseButton = new QToolButton(this);
156 m_seekSlider = new Phonon::SeekSlider(this);
157
158 controlsLayout->addWidget(m_playButton);
159 controlsLayout->addWidget(m_pauseButton);
160 controlsLayout->addWidget(m_seekSlider);
161
162 m_topLayout->addLayout(controlsLayout);
163
164 const int smallIconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
165 const QSize buttonSize(smallIconSize, smallIconSize);
166
167 m_playButton->setToolTip(i18n("play"));
168 m_playButton->setIconSize(buttonSize);
169 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
170 m_playButton->setAutoRaise(true);
171 connect(m_playButton, &QToolButton::clicked, this, &PhononWidget::play);
172
173 m_pauseButton->setToolTip(i18n("pause"));
174 m_pauseButton->setIconSize(buttonSize);
175 m_pauseButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-pause")));
176 m_pauseButton->setAutoRaise(true);
177 m_pauseButton->hide();
178 connect(m_pauseButton, &QToolButton::clicked, this, &PhononWidget::togglePlayback);
179
180 m_seekSlider->setIconVisible(false);
181
182 // Creating an audio player or video player instance might take up to
183 // 2 seconds when doing it the first time. To prevent that the user
184 // interface gets noticeable blocked, the creation is delayed until
185 // the play button has been pressed (see PhononWidget::play()).
186 }
187 }
188
189 void PhononWidget::hideEvent(QHideEvent *event)
190 {
191 QWidget::hideEvent(event);
192 if (!event->spontaneous()) {
193 stop();
194 }
195 }
196
197 void PhononWidget::stateChanged(Phonon::State newstate)
198 {
199 setUpdatesEnabled(false);
200 switch (newstate) {
201 case Phonon::PlayingState:
202 case Phonon::BufferingState:
203 m_playButton->hide();
204 m_pauseButton->show();
205 break;
206 default:
207 m_pauseButton->hide();
208 m_playButton->show();
209 break;
210 }
211 setUpdatesEnabled(true);
212 }
213
214 void PhononWidget::play()
215 {
216 if (!m_media) {
217 m_media = new Phonon::MediaObject(this);
218 connect(m_media, &Phonon::MediaObject::stateChanged,
219 this, &PhononWidget::stateChanged);
220 connect(m_media, &Phonon::MediaObject::finished,
221 this, &PhononWidget::finished);
222 m_seekSlider->setMediaObject(m_media);
223 }
224
225 if (!m_videoPlayer) {
226 m_videoPlayer = new EmbeddedVideoPlayer(this);
227 m_videoPlayer->setCursor(Qt::PointingHandCursor);
228 m_videoPlayer->installEventFilter(this);
229 m_topLayout->insertWidget(0, m_videoPlayer);
230 Phonon::createPath(m_media, m_videoPlayer);
231 applyVideoSize();
232 }
233
234 if (!m_audioOutput) {
235 m_audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
236 Phonon::createPath(m_media, m_audioOutput);
237 }
238
239 if (m_isVideo) {
240 emit hasVideoChanged(true);
241 }
242
243 if (m_url != m_media->currentSource().url()) {
244 m_media->setCurrentSource(m_url);
245 }
246 m_media->play();
247
248 m_videoPlayer->setVisible(m_isVideo);
249 }
250
251 void PhononWidget::finished()
252 {
253 if (m_isVideo) {
254 m_videoPlayer->hide();
255 emit hasVideoChanged(false);
256 }
257 }
258
259 Phonon::State PhononWidget::state() const
260 {
261 return m_media == nullptr ? Phonon::State::StoppedState : m_media->state();
262 }
263
264 void PhononWidget::stop()
265 {
266 if (m_media) {
267 m_media->stop();
268 m_videoPlayer->hide();
269 emit hasVideoChanged(false);
270 }
271 }
272
273 void PhononWidget::applyVideoSize()
274 {
275 if ((m_videoPlayer) && m_videoSize.isValid()) {
276 m_videoPlayer->setSizeHint(m_videoSize);
277 }
278 }
279
280 #include "phononwidget.moc"