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