]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/phononwidget.cpp
- hide the video player in any case when the stop-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 #include <QtGui/QVBoxLayout>
28 #include <QtGui/QHBoxLayout>
29 #include <QtGui/QShowEvent>
30 #include <QtGui/QToolButton>
31 #include <kicon.h>
32 #include <kurl.h>
33 #include <klocale.h>
34
35 PhononWidget::PhononWidget(QWidget *parent)
36 : QWidget(parent),
37 m_mode(Audio),
38 m_url(),
39 m_playButton(0),
40 m_stopButton(0),
41 m_audioMedia(0),
42 m_media(0),
43 m_seekSlider(0),
44 m_videoPlayer(0)
45 {
46 }
47
48 void PhononWidget::setUrl(const KUrl &url)
49 {
50 if (m_url != url) {
51 stop(); // emits playingStopped() signal
52 m_url = url;
53 }
54 }
55
56 KUrl PhononWidget::url() const
57 {
58 return m_url;
59 }
60
61 void PhononWidget::setMode(Mode mode)
62 {
63 if (m_mode != mode) {
64 stop(); // emits playingStopped() signal
65 m_mode = mode;
66 }
67 }
68
69 PhononWidget::Mode PhononWidget::mode() const
70 {
71 return m_mode;
72 }
73
74 void PhononWidget::showEvent(QShowEvent *event)
75 {
76 if (event->spontaneous()) {
77 QWidget::showEvent(event);
78 return;
79 }
80
81 if (m_playButton == 0) {
82 QVBoxLayout *topLayout = new QVBoxLayout(this);
83 topLayout->setMargin(0);
84 topLayout->setSpacing(0);
85 QHBoxLayout *controlsLayout = new QHBoxLayout(this);
86 controlsLayout->setMargin(0);
87 controlsLayout->setSpacing(0);
88
89 m_playButton = new QToolButton(this);
90 m_stopButton = new QToolButton(this);
91 m_seekSlider = new Phonon::SeekSlider(this);
92 m_videoPlayer = new Phonon::VideoPlayer(Phonon::VideoCategory, this);
93 m_videoPlayer->hide();
94
95 controlsLayout->addWidget(m_playButton);
96 controlsLayout->addWidget(m_stopButton);
97 controlsLayout->addWidget(m_seekSlider);
98
99 topLayout->addWidget(m_videoPlayer);
100 topLayout->addLayout(controlsLayout);
101
102 m_playButton->setToolTip(i18n("play"));
103 m_playButton->setIconSize(QSize(16, 16));
104 m_playButton->setIcon(KIcon("media-playback-start"));
105 connect(m_playButton, SIGNAL(clicked()), this, SLOT(play()));
106
107 m_stopButton->setToolTip(i18n("stop"));
108 m_stopButton->setIconSize(QSize(16, 16));
109 m_stopButton->setIcon(KIcon("media-playback-stop"));
110 m_stopButton->hide();
111 connect(m_stopButton, SIGNAL(clicked()), this, SLOT(stop()));
112
113 m_seekSlider->setIconVisible(false);
114 }
115 }
116
117 void PhononWidget::hideEvent(QHideEvent *event)
118 {
119 QWidget::hideEvent(event);
120 if (!event->spontaneous()) {
121 stop();
122 }
123 }
124
125 void PhononWidget::stateChanged(Phonon::State newstate)
126 {
127 setUpdatesEnabled(false);
128 switch (newstate) {
129 case Phonon::PlayingState:
130 case Phonon::BufferingState:
131 m_stopButton->show();
132 m_playButton->hide();
133 break;
134 default:
135 m_stopButton->hide();
136 m_playButton->show();
137 break;
138 }
139 setUpdatesEnabled(true);
140 }
141
142 void PhononWidget::play()
143 {
144 switch (m_mode) {
145 case Audio:
146 if (m_audioMedia == 0) {
147 // Creating an audio player might take up to 2 seconds when doing
148 // it the first time. To prevent that the user interface gets
149 // noticable blocked, the creation is delayed until the play button
150 // has been pressed.
151 m_audioMedia = Phonon::createPlayer(Phonon::MusicCategory, m_url);
152 m_audioMedia->setParent(this);
153 }
154 m_media = m_audioMedia;
155 m_media->setCurrentSource(m_url);
156 break;
157
158 case Video:
159 m_videoPlayer->show();
160 m_videoPlayer->play(m_url);
161 m_media = m_videoPlayer->mediaObject();
162 break;
163
164 default:
165 break;
166 }
167
168 Q_ASSERT(m_media != 0);
169 connect(m_media, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
170 this, SLOT(stateChanged(Phonon::State)));
171 m_seekSlider->setMediaObject(m_media);
172
173 emit playingStarted();
174 }
175
176 void PhononWidget::stop()
177 {
178 if (m_media != 0) {
179 m_media->stop();
180 disconnect(m_media, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
181 this, SLOT(stateChanged(Phonon::State)));
182 emit playingStopped();
183
184 m_stopButton->hide();
185 m_playButton->show();
186 }
187
188 if (m_videoPlayer != 0) {
189 m_videoPlayer->hide();
190 }
191 }