]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/phononwidget.cpp
remove deprecated methods
[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 <KIconLoader>
24 #include <KLocalizedString>
25 #include <Phonon/AudioOutput>
26 #include <Phonon/MediaObject>
27 #include <Phonon/SeekSlider>
28 #include <Phonon/VideoWidget>
29
30 #include <QShowEvent>
31 #include <QToolButton>
32 #include <QVBoxLayout>
33
34 class EmbeddedVideoPlayer : public Phonon::VideoWidget
35 {
36 Q_OBJECT
37
38 public:
39 EmbeddedVideoPlayer(QWidget *parent = nullptr) :
40 Phonon::VideoWidget(parent)
41 {
42 }
43
44 void setSizeHint(const QSize& size)
45 {
46 m_sizeHint = size;
47 updateGeometry();
48 }
49
50 QSize sizeHint() const override
51 {
52 return m_sizeHint.isValid() ? m_sizeHint : Phonon::VideoWidget::sizeHint();
53 }
54
55 private:
56 QSize m_sizeHint;
57 };
58
59 PhononWidget::PhononWidget(QWidget *parent)
60 : QWidget(parent),
61 m_url(),
62 m_playButton(nullptr),
63 m_stopButton(nullptr),
64 m_topLayout(nullptr),
65 m_media(nullptr),
66 m_seekSlider(nullptr),
67 m_audioOutput(nullptr),
68 m_videoPlayer(nullptr)
69 {
70 }
71
72 void PhononWidget::setUrl(const QUrl &url)
73 {
74 if (m_url != url) {
75 stop(); // emits playingStopped() signal
76 m_url = url;
77 }
78 }
79
80 QUrl PhononWidget::url() const
81 {
82 return m_url;
83 }
84
85 void PhononWidget::setVideoSize(const QSize& size)
86 {
87 if (m_videoSize != size) {
88 m_videoSize = size;
89 applyVideoSize();
90 }
91 }
92
93 QSize PhononWidget::videoSize() const
94 {
95 return m_videoSize;
96 }
97
98 void PhononWidget::showEvent(QShowEvent *event)
99 {
100 if (event->spontaneous()) {
101 QWidget::showEvent(event);
102 return;
103 }
104
105 if (!m_topLayout) {
106 m_topLayout = new QVBoxLayout(this);
107 m_topLayout->setContentsMargins(0, 0, 0, 0);
108
109 QHBoxLayout *controlsLayout = new QHBoxLayout(this);
110 controlsLayout->setContentsMargins(0, 0, 0, 0);
111 controlsLayout->setSpacing(0);
112
113 m_playButton = new QToolButton(this);
114 m_stopButton = new QToolButton(this);
115 m_seekSlider = new Phonon::SeekSlider(this);
116
117 controlsLayout->addWidget(m_playButton);
118 controlsLayout->addWidget(m_stopButton);
119 controlsLayout->addWidget(m_seekSlider);
120
121 m_topLayout->addLayout(controlsLayout);
122
123 const int smallIconSize = IconSize(KIconLoader::Small);
124 const QSize buttonSize(smallIconSize, smallIconSize);
125
126 m_playButton->setToolTip(i18n("play"));
127 m_playButton->setIconSize(buttonSize);
128 m_playButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
129 m_playButton->setAutoRaise(true);
130 connect(m_playButton, &QToolButton::clicked, this, &PhononWidget::play);
131
132 m_stopButton->setToolTip(i18n("stop"));
133 m_stopButton->setIconSize(buttonSize);
134 m_stopButton->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-stop")));
135 m_stopButton->setAutoRaise(true);
136 m_stopButton->hide();
137 connect(m_stopButton, &QToolButton::clicked, this, &PhononWidget::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 case Phonon::StoppedState:
166 if (m_videoPlayer) {
167 m_videoPlayer->hide();
168 }
169 emit hasVideoChanged(false);
170 Q_FALLTHROUGH();
171 default:
172 m_stopButton->hide();
173 m_playButton->show();
174 break;
175 }
176 setUpdatesEnabled(true);
177 }
178
179 void PhononWidget::play()
180 {
181 if (!m_media) {
182 m_media = new Phonon::MediaObject(this);
183 connect(m_media, &Phonon::MediaObject::stateChanged,
184 this, &PhononWidget::stateChanged);
185 connect(m_media, &Phonon::MediaObject::hasVideoChanged,
186 this, &PhononWidget::slotHasVideoChanged);
187 m_seekSlider->setMediaObject(m_media);
188 }
189
190 if (!m_videoPlayer) {
191 m_videoPlayer = new EmbeddedVideoPlayer(this);
192 m_topLayout->insertWidget(0, m_videoPlayer);
193 Phonon::createPath(m_media, m_videoPlayer);
194 applyVideoSize();
195 }
196
197 if (!m_audioOutput) {
198 m_audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
199 Phonon::createPath(m_media, m_audioOutput);
200 }
201
202 emit hasVideoChanged(false);
203
204 m_media->setCurrentSource(m_url);
205 m_media->hasVideo();
206 m_media->play();
207 }
208
209 void PhononWidget::stop()
210 {
211 if (m_media) {
212 m_media->stop();
213 }
214 }
215
216 void PhononWidget::slotHasVideoChanged(bool hasVideo)
217 {
218 emit hasVideoChanged(hasVideo);
219
220 if (hasVideo) {
221 m_videoPlayer->show();
222 }
223 }
224
225 void PhononWidget::applyVideoSize()
226 {
227 if ((m_videoPlayer) && m_videoSize.isValid()) {
228 m_videoPlayer->setSizeHint(m_videoSize);
229 }
230 }
231
232 #include "phononwidget.moc"