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