]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/phononwidget.cpp
Merge remote-tracking branch 'origin/Applications/16.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 <Phonon/AudioOutput>
24 #include <Phonon/Global>
25 #include <Phonon/MediaObject>
26 #include <Phonon/SeekSlider>
27 #include <Phonon/VideoWidget>
28
29 #include <QVBoxLayout>
30 #include <QHBoxLayout>
31 #include <QShowEvent>
32 #include <QToolButton>
33 #include <QDialog>
34 #include <QIcon>
35 #include <KIconLoader>
36 #include <QUrl>
37 #include <KLocalizedString>
38
39 class EmbeddedVideoPlayer : public Phonon::VideoWidget
40 {
41 Q_OBJECT
42
43 public:
44 EmbeddedVideoPlayer(QWidget *parent = 0) :
45 Phonon::VideoWidget(parent)
46 {
47 }
48
49 void setSizeHint(const QSize& size)
50 {
51 m_sizeHint = size;
52 updateGeometry();
53 }
54
55 QSize sizeHint() const Q_DECL_OVERRIDE
56 {
57 return m_sizeHint.isValid() ? m_sizeHint : Phonon::VideoWidget::sizeHint();
58 }
59
60 private:
61 QSize m_sizeHint;
62 };
63
64 PhononWidget::PhononWidget(QWidget *parent)
65 : QWidget(parent),
66 m_url(),
67 m_playButton(0),
68 m_stopButton(0),
69 m_topLayout(0),
70 m_media(0),
71 m_seekSlider(0),
72 m_audioOutput(0),
73 m_videoPlayer(0)
74 {
75 }
76
77 void PhononWidget::setUrl(const QUrl &url)
78 {
79 if (m_url != url) {
80 stop(); // emits playingStopped() signal
81 m_url = url;
82 }
83 }
84
85 QUrl PhononWidget::url() const
86 {
87 return m_url;
88 }
89
90 void PhononWidget::setVideoSize(const QSize& size)
91 {
92 if (m_videoSize != size) {
93 m_videoSize = size;
94 applyVideoSize();
95 }
96 }
97
98 QSize PhononWidget::videoSize() const
99 {
100 return m_videoSize;
101 }
102
103 void PhononWidget::showEvent(QShowEvent *event)
104 {
105 if (event->spontaneous()) {
106 QWidget::showEvent(event);
107 return;
108 }
109
110 if (!m_topLayout) {
111 m_topLayout = new QVBoxLayout(this);
112 m_topLayout->setMargin(0);
113
114 QHBoxLayout *controlsLayout = new QHBoxLayout(this);
115 controlsLayout->setMargin(0);
116 controlsLayout->setSpacing(0);
117
118 m_playButton = new QToolButton(this);
119 m_stopButton = new QToolButton(this);
120 m_seekSlider = new Phonon::SeekSlider(this);
121
122 controlsLayout->addWidget(m_playButton);
123 controlsLayout->addWidget(m_stopButton);
124 controlsLayout->addWidget(m_seekSlider);
125
126 m_topLayout->addLayout(controlsLayout);
127
128 const int smallIconSize = IconSize(KIconLoader::Small);
129 const QSize buttonSize(smallIconSize, smallIconSize);
130
131 m_playButton->setToolTip(i18n("play"));
132 m_playButton->setIconSize(buttonSize);
133 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
134 m_playButton->setAutoRaise(true);
135 connect(m_playButton, &QToolButton::clicked, this, &PhononWidget::play);
136
137 m_stopButton->setToolTip(i18n("stop"));
138 m_stopButton->setIconSize(buttonSize);
139 m_stopButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-stop")));
140 m_stopButton->setAutoRaise(true);
141 m_stopButton->hide();
142 connect(m_stopButton, &QToolButton::clicked, this, &PhononWidget::stop);
143
144 m_seekSlider->setIconVisible(false);
145
146 // Creating an audio player or video player instance might take up to
147 // 2 seconds when doing it the first time. To prevent that the user
148 // interface gets noticeable blocked, the creation is delayed until
149 // the play button has been pressed (see PhononWidget::play()).
150 }
151 }
152
153 void PhononWidget::hideEvent(QHideEvent *event)
154 {
155 QWidget::hideEvent(event);
156 if (!event->spontaneous()) {
157 stop();
158 }
159 }
160
161 void PhononWidget::stateChanged(Phonon::State newstate)
162 {
163 setUpdatesEnabled(false);
164 switch (newstate) {
165 case Phonon::PlayingState:
166 case Phonon::BufferingState:
167 m_stopButton->show();
168 m_playButton->hide();
169 break;
170 case Phonon::StoppedState:
171 if (m_videoPlayer) {
172 m_videoPlayer->hide();
173 }
174 emit hasVideoChanged(false);
175 // fall through
176 default:
177 m_stopButton->hide();
178 m_playButton->show();
179 break;
180 }
181 setUpdatesEnabled(true);
182 }
183
184 void PhononWidget::play()
185 {
186 if (!m_media) {
187 m_media = new Phonon::MediaObject(this);
188 connect(m_media, &Phonon::MediaObject::stateChanged,
189 this, &PhononWidget::stateChanged);
190 connect(m_media, &Phonon::MediaObject::hasVideoChanged,
191 this, &PhononWidget::slotHasVideoChanged);
192 m_seekSlider->setMediaObject(m_media);
193 }
194
195 if (!m_videoPlayer) {
196 m_videoPlayer = new EmbeddedVideoPlayer(this);
197 m_topLayout->insertWidget(0, m_videoPlayer);
198 Phonon::createPath(m_media, m_videoPlayer);
199 applyVideoSize();
200 }
201
202 if (!m_audioOutput) {
203 m_audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
204 Phonon::createPath(m_media, m_audioOutput);
205 }
206
207 emit hasVideoChanged(false);
208
209 m_media->setCurrentSource(m_url);
210 m_media->hasVideo();
211 m_media->play();
212 }
213
214 void PhononWidget::stop()
215 {
216 if (m_media) {
217 m_media->stop();
218 }
219 }
220
221 void PhononWidget::slotHasVideoChanged(bool hasVideo)
222 {
223 emit hasVideoChanged(hasVideo);
224
225 if (hasVideo) {
226 m_videoPlayer->show();
227 }
228 }
229
230 void PhononWidget::applyVideoSize()
231 {
232 if ((m_videoPlayer) && m_videoSize.isValid()) {
233 m_videoPlayer->setSizeHint(m_videoSize);
234 }
235 }
236
237 #include "phononwidget.moc"