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