]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/phononwidget.cpp
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
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.
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.
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
21 #include "phononwidget.h"
23 #include <KIconLoader>
24 #include <KLocalizedString>
25 #include <Phonon/AudioOutput>
26 #include <Phonon/MediaObject>
27 #include <Phonon/SeekSlider>
28 #include <Phonon/VideoWidget>
31 #include <QToolButton>
32 #include <QVBoxLayout>
34 class EmbeddedVideoPlayer
: public Phonon::VideoWidget
39 EmbeddedVideoPlayer(QWidget
*parent
= nullptr) :
40 Phonon::VideoWidget(parent
)
44 void setSizeHint(const QSize
& size
)
50 QSize
sizeHint() const override
52 return m_sizeHint
.isValid() ? m_sizeHint
: Phonon::VideoWidget::sizeHint();
59 PhononWidget::PhononWidget(QWidget
*parent
)
62 m_playButton(nullptr),
63 m_stopButton(nullptr),
66 m_seekSlider(nullptr),
67 m_audioOutput(nullptr),
68 m_videoPlayer(nullptr)
72 void PhononWidget::setUrl(const QUrl
&url
, MediaKind kind
)
76 m_isVideo
= kind
== MediaKind::Video
;
85 void PhononWidget::setAutoPlay(bool autoPlay
)
87 m_autoPlay
= autoPlay
;
88 if (!m_url
.isEmpty() && (m_media
== nullptr || m_media
->state() != Phonon::State::PlayingState
) && m_autoPlay
&& isVisible()) {
93 QUrl
PhononWidget::url() const
98 void PhononWidget::setVideoSize(const QSize
& size
)
100 if (m_videoSize
!= size
) {
106 QSize
PhononWidget::videoSize() const
111 void PhononWidget::showEvent(QShowEvent
*event
)
113 if (event
->spontaneous()) {
114 QWidget::showEvent(event
);
119 m_topLayout
= new QVBoxLayout(this);
120 m_topLayout
->setContentsMargins(0, 0, 0, 0);
122 QHBoxLayout
*controlsLayout
= new QHBoxLayout();
123 controlsLayout
->setContentsMargins(0, 0, 0, 0);
124 controlsLayout
->setSpacing(0);
126 m_playButton
= new QToolButton(this);
127 m_stopButton
= new QToolButton(this);
128 m_seekSlider
= new Phonon::SeekSlider(this);
130 controlsLayout
->addWidget(m_playButton
);
131 controlsLayout
->addWidget(m_stopButton
);
132 controlsLayout
->addWidget(m_seekSlider
);
134 m_topLayout
->addLayout(controlsLayout
);
136 const int smallIconSize
= IconSize(KIconLoader::Small
);
137 const QSize
buttonSize(smallIconSize
, smallIconSize
);
139 m_playButton
->setToolTip(i18n("play"));
140 m_playButton
->setIconSize(buttonSize
);
141 m_playButton
->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
142 m_playButton
->setAutoRaise(true);
143 connect(m_playButton
, &QToolButton::clicked
, this, &PhononWidget::play
);
145 m_stopButton
->setToolTip(i18n("stop"));
146 m_stopButton
->setIconSize(buttonSize
);
147 m_stopButton
->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-stop")));
148 m_stopButton
->setAutoRaise(true);
149 m_stopButton
->hide();
150 connect(m_stopButton
, &QToolButton::clicked
, this, &PhononWidget::stop
);
152 m_seekSlider
->setIconVisible(false);
154 // Creating an audio player or video player instance might take up to
155 // 2 seconds when doing it the first time. To prevent that the user
156 // interface gets noticeable blocked, the creation is delayed until
157 // the play button has been pressed (see PhononWidget::play()).
161 void PhononWidget::hideEvent(QHideEvent
*event
)
163 QWidget::hideEvent(event
);
164 if (!event
->spontaneous()) {
169 void PhononWidget::stateChanged(Phonon::State newstate
)
171 setUpdatesEnabled(false);
173 case Phonon::PlayingState
:
174 case Phonon::BufferingState
:
175 m_stopButton
->show();
176 m_playButton
->hide();
179 m_stopButton
->hide();
180 m_playButton
->show();
183 setUpdatesEnabled(true);
186 void PhononWidget::play()
189 m_media
= new Phonon::MediaObject(this);
190 connect(m_media
, &Phonon::MediaObject::stateChanged
,
191 this, &PhononWidget::stateChanged
);
192 connect(m_media
, &Phonon::MediaObject::finished
,
193 this, &PhononWidget::finished
);
194 m_seekSlider
->setMediaObject(m_media
);
197 if (!m_videoPlayer
) {
198 m_videoPlayer
= new EmbeddedVideoPlayer(this);
199 m_videoPlayer
->installEventFilter(this);
200 m_topLayout
->insertWidget(0, m_videoPlayer
);
201 Phonon::createPath(m_media
, m_videoPlayer
);
205 if (!m_audioOutput
) {
206 m_audioOutput
= new Phonon::AudioOutput(Phonon::MusicCategory
, this);
207 Phonon::createPath(m_media
, m_audioOutput
);
211 emit
hasVideoChanged(true);
214 if (m_url
!= m_media
->currentSource().url()) {
215 m_media
->setCurrentSource(m_url
);
219 m_videoPlayer
->setVisible(m_isVideo
);
222 void PhononWidget::finished()
225 m_videoPlayer
->hide();
226 emit
hasVideoChanged(false);
230 void PhononWidget::stop()
234 m_videoPlayer
->hide();
235 emit
hasVideoChanged(false);
239 void PhononWidget::applyVideoSize()
241 if ((m_videoPlayer
) && m_videoSize
.isValid()) {
242 m_videoPlayer
->setSizeHint(m_videoSize
);
246 #include "phononwidget.moc"