]>
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 <Phonon/Global>
24 #include <Phonon/MediaObject>
25 #include <Phonon/SeekSlider>
26 #include <Phonon/VideoPlayer>
28 #include <QtGui/QVBoxLayout>
29 #include <QtGui/QHBoxLayout>
30 #include <QtGui/QShowEvent>
31 #include <QtGui/QToolButton>
38 class EmbeddedVideoPlayer
: public Phonon::VideoPlayer
41 EmbeddedVideoPlayer(Phonon::Category category
, QWidget
*parent
= 0) :
42 Phonon::VideoPlayer(category
, parent
)
44 setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
47 void setSizeHint(const QSize
& size
)
50 setFixedHeight(size
.height());
54 virtual QSize
sizeHint() const
56 return m_sizeHint
.isValid() ? m_sizeHint
: Phonon::VideoPlayer::sizeHint();
63 PhononWidget::PhononWidget(QWidget
*parent
)
77 void PhononWidget::setUrl(const KUrl
&url
)
80 stop(); // emits playingStopped() signal
85 KUrl
PhononWidget::url() const
90 void PhononWidget::setMode(Mode mode
)
93 stop(); // emits playingStopped() signal
98 PhononWidget::Mode
PhononWidget::mode() const
103 void PhononWidget::setVideoSize(const QSize
& size
)
105 if (m_videoSize
!= size
) {
111 QSize
PhononWidget::videoSize() const
116 void PhononWidget::showEvent(QShowEvent
*event
)
118 if (event
->spontaneous()) {
119 QWidget::showEvent(event
);
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);
131 m_playButton
= new QToolButton(this);
132 m_stopButton
= new QToolButton(this);
133 m_seekSlider
= new Phonon::SeekSlider(this);
135 controlsLayout
->addWidget(m_playButton
);
136 controlsLayout
->addWidget(m_stopButton
);
137 controlsLayout
->addWidget(m_seekSlider
);
139 m_topLayout
->addLayout(controlsLayout
);
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()));
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()));
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 noticable 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()
190 if (m_audioMedia
== 0) {
191 m_audioMedia
= Phonon::createPlayer(Phonon::MusicCategory
, m_url
);
192 m_audioMedia
->setParent(this);
194 m_media
= m_audioMedia
;
195 m_media
->setCurrentSource(m_url
);
199 if (m_videoPlayer
== 0) {
200 m_videoPlayer
= new EmbeddedVideoPlayer(Phonon::VideoCategory
, this);
201 m_topLayout
->insertWidget(0, m_videoPlayer
);
204 m_videoPlayer
->show();
205 m_videoPlayer
->play(m_url
);
206 m_media
= m_videoPlayer
->mediaObject();
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
);
218 emit
playingStarted();
221 void PhononWidget::stop()
225 disconnect(m_media
, SIGNAL(stateChanged(Phonon::State
, Phonon::State
)),
226 this, SLOT(stateChanged(Phonon::State
)));
227 emit
playingStopped();
229 m_stopButton
->hide();
230 m_playButton
->show();
233 if (m_videoPlayer
!= 0) {
234 m_videoPlayer
->hide();
238 void PhononWidget::applyVideoSize()
240 if ((m_videoPlayer
!= 0) && m_videoSize
.isValid()) {
241 m_videoPlayer
->setSizeHint(m_videoSize
);