]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/phononwidget.cpp
Add click to play/pause feature on previews for audio/video
[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 <KIconLoader>
24 #include <KLocalizedString>
25 #include <Phonon/AudioOutput>
26 #include <Phonon/MediaObject>
27 #include <Phonon/SeekSlider>
28 #include <Phonon/VideoWidget>
29
30 #include <QShowEvent>
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_stopButton(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 bool PhononWidget::eventFilter(QObject *object, QEvent *event)
104 {
105 Q_UNUSED(object)
106 if (event->type() == QEvent::MouseButtonPress) {
107 const QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
108 if (mouseEvent->button() == Qt::LeftButton) {
109 // toggle playback
110 if (m_media && m_media->state() == Phonon::State::PlayingState) {
111 m_media->pause();
112 } else {
113 play();
114 }
115 return true;
116 }
117 }
118 return false;
119 }
120
121 void PhononWidget::setVideoSize(const QSize& size)
122 {
123 if (m_videoSize != size) {
124 m_videoSize = size;
125 applyVideoSize();
126 }
127 }
128
129 QSize PhononWidget::videoSize() const
130 {
131 return m_videoSize;
132 }
133
134 void PhononWidget::showEvent(QShowEvent *event)
135 {
136 if (event->spontaneous()) {
137 QWidget::showEvent(event);
138 return;
139 }
140
141 if (!m_topLayout) {
142 m_topLayout = new QVBoxLayout(this);
143 m_topLayout->setContentsMargins(0, 0, 0, 0);
144
145 QHBoxLayout *controlsLayout = new QHBoxLayout();
146 controlsLayout->setContentsMargins(0, 0, 0, 0);
147 controlsLayout->setSpacing(0);
148
149 m_playButton = new QToolButton(this);
150 m_stopButton = new QToolButton(this);
151 m_seekSlider = new Phonon::SeekSlider(this);
152
153 controlsLayout->addWidget(m_playButton);
154 controlsLayout->addWidget(m_stopButton);
155 controlsLayout->addWidget(m_seekSlider);
156
157 m_topLayout->addLayout(controlsLayout);
158
159 const int smallIconSize = IconSize(KIconLoader::Small);
160 const QSize buttonSize(smallIconSize, smallIconSize);
161
162 m_playButton->setToolTip(i18n("play"));
163 m_playButton->setIconSize(buttonSize);
164 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
165 m_playButton->setAutoRaise(true);
166 connect(m_playButton, &QToolButton::clicked, this, &PhononWidget::play);
167
168 m_stopButton->setToolTip(i18n("stop"));
169 m_stopButton->setIconSize(buttonSize);
170 m_stopButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-stop")));
171 m_stopButton->setAutoRaise(true);
172 m_stopButton->hide();
173 connect(m_stopButton, &QToolButton::clicked, this, &PhononWidget::stop);
174
175 m_seekSlider->setIconVisible(false);
176
177 // Creating an audio player or video player instance might take up to
178 // 2 seconds when doing it the first time. To prevent that the user
179 // interface gets noticeable blocked, the creation is delayed until
180 // the play button has been pressed (see PhononWidget::play()).
181 }
182 }
183
184 void PhononWidget::hideEvent(QHideEvent *event)
185 {
186 QWidget::hideEvent(event);
187 if (!event->spontaneous()) {
188 stop();
189 }
190 }
191
192 void PhononWidget::stateChanged(Phonon::State newstate)
193 {
194 setUpdatesEnabled(false);
195 switch (newstate) {
196 case Phonon::PlayingState:
197 case Phonon::BufferingState:
198 m_playButton->hide();
199 m_stopButton->show();
200 break;
201 default:
202 m_stopButton->hide();
203 m_playButton->show();
204 break;
205 }
206 setUpdatesEnabled(true);
207 }
208
209 void PhononWidget::play()
210 {
211 if (!m_media) {
212 m_media = new Phonon::MediaObject(this);
213 connect(m_media, &Phonon::MediaObject::stateChanged,
214 this, &PhononWidget::stateChanged);
215 connect(m_media, &Phonon::MediaObject::finished,
216 this, &PhononWidget::finished);
217 m_seekSlider->setMediaObject(m_media);
218 }
219
220 if (!m_videoPlayer) {
221 m_videoPlayer = new EmbeddedVideoPlayer(this);
222 m_videoPlayer->setCursor(Qt::PointingHandCursor);
223 m_videoPlayer->installEventFilter(this);
224 m_topLayout->insertWidget(0, m_videoPlayer);
225 Phonon::createPath(m_media, m_videoPlayer);
226 applyVideoSize();
227 }
228
229 if (!m_audioOutput) {
230 m_audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
231 Phonon::createPath(m_media, m_audioOutput);
232 }
233
234 if (m_isVideo) {
235 emit hasVideoChanged(true);
236 }
237
238 if (m_url != m_media->currentSource().url()) {
239 m_media->setCurrentSource(m_url);
240 }
241 m_media->play();
242
243 m_videoPlayer->setVisible(m_isVideo);
244 }
245
246 void PhononWidget::finished()
247 {
248 if (m_isVideo) {
249 m_videoPlayer->hide();
250 emit hasVideoChanged(false);
251 }
252 }
253
254 Phonon::State PhononWidget::state() const
255 {
256 return m_media == nullptr ? Phonon::State::StoppedState : m_media->state();
257 }
258
259 void PhononWidget::stop()
260 {
261 if (m_media) {
262 m_media->stop();
263 m_videoPlayer->hide();
264 emit hasVideoChanged(false);
265 }
266 }
267
268 void PhononWidget::applyVideoSize()
269 {
270 if ((m_videoPlayer) && m_videoSize.isValid()) {
271 m_videoPlayer->setSizeHint(m_videoSize);
272 }
273 }
274
275 #include "phononwidget.moc"