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