]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/phononwidget.cpp
The feature freeze is near: Add video support to the Information Panel. Phonon is...
[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_media(0),
42 m_seekSlider(0),
43 m_videoPlayer(0)
44 {
45 }
46
47 void PhononWidget::setUrl(const KUrl &url)
48 {
49 if (m_url != url) {
50 stop(); // emits playingStopped() signal
51 m_url = url;
52 m_videoPlayer->hide();
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
66 m_mode = mode;
67 if (m_mode == Audio) {
68 m_videoPlayer->hide();
69 m_media = 0;
70 }
71 }
72 }
73
74 PhononWidget::Mode PhononWidget::mode() const
75 {
76 return m_mode;
77 }
78
79 void PhononWidget::showEvent(QShowEvent *event)
80 {
81 if (event->spontaneous()) {
82 QWidget::showEvent(event);
83 return;
84 }
85
86 if (m_playButton == 0) {
87 QVBoxLayout *topLayout = new QVBoxLayout(this);
88 topLayout->setMargin(0);
89 topLayout->setSpacing(0);
90 QHBoxLayout *controlsLayout = new QHBoxLayout(this);
91 controlsLayout->setMargin(0);
92 controlsLayout->setSpacing(0);
93
94 m_playButton = new QToolButton(this);
95 m_stopButton = new QToolButton(this);
96 m_seekSlider = new Phonon::SeekSlider(this);
97 m_videoPlayer = new Phonon::VideoPlayer(Phonon::VideoCategory, this);
98 m_videoPlayer->hide();
99
100 controlsLayout->addWidget(m_playButton);
101 controlsLayout->addWidget(m_stopButton);
102 controlsLayout->addWidget(m_seekSlider);
103
104 topLayout->addWidget(m_videoPlayer);
105 topLayout->addLayout(controlsLayout);
106
107 m_playButton->setToolTip(i18n("play"));
108 m_playButton->setIconSize(QSize(16, 16));
109 m_playButton->setIcon(KIcon("media-playback-start"));
110 connect(m_playButton, SIGNAL(clicked()), this, SLOT(play()));
111
112 m_stopButton->setToolTip(i18n("stop"));
113 m_stopButton->setIconSize(QSize(16, 16));
114 m_stopButton->setIcon(KIcon("media-playback-stop"));
115 m_stopButton->hide();
116 connect(m_stopButton, SIGNAL(clicked()), this, SLOT(stop()));
117
118 m_seekSlider->setIconVisible(false);
119 }
120 }
121
122 void PhononWidget::hideEvent(QHideEvent *event)
123 {
124 QWidget::hideEvent(event);
125 if (!event->spontaneous()) {
126 stop();
127 if (m_videoPlayer != 0) {
128 m_videoPlayer->hide();
129 }
130 }
131 }
132
133 void PhononWidget::stateChanged(Phonon::State newstate)
134 {
135 setUpdatesEnabled(false);
136 switch (newstate) {
137 case Phonon::PlayingState:
138 case Phonon::BufferingState:
139 m_stopButton->show();
140 m_playButton->hide();
141 break;
142 default:
143 m_stopButton->hide();
144 m_playButton->show();
145 break;
146 }
147 setUpdatesEnabled(true);
148 }
149
150 void PhononWidget::play()
151 {
152 switch (m_mode) {
153 case Audio:
154 if (m_media == 0) {
155 m_media = Phonon::createPlayer(Phonon::MusicCategory, m_url);
156 m_media->setParent(this);
157 }
158 m_media->setCurrentSource(m_url);
159 break;
160
161 case Video:
162 m_videoPlayer->show();
163 m_videoPlayer->play(m_url);
164 m_media = m_videoPlayer->mediaObject();
165 break;
166
167 default:
168 break;
169 }
170
171 Q_ASSERT(m_media != 0);
172 connect(m_media, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
173 this, SLOT(stateChanged(Phonon::State)));
174 m_seekSlider->setMediaObject(m_media);
175
176 emit playingStarted();
177 }
178
179 void PhononWidget::stop()
180 {
181 if (m_media != 0) {
182 m_media->stop();
183 disconnect(m_media, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
184 this, SLOT(stateChanged(Phonon::State)));
185 emit playingStopped();
186
187 m_stopButton->hide();
188 m_playButton->show();
189 }
190 }