┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/panels/information/phononwidget.cpp
blob: 16dbabfed26d241ecec860fea081d1b9ab35a674 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*  This file is part of the KDE project
    Copyright (C) 2007 Matthias Kretz <[email protected]>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of
    the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.

*/

#include "phononwidget.h"

#include <Phonon/AudioOutput>
#include <Phonon/Global>
#include <Phonon/MediaObject>
#include <Phonon/SeekSlider>
#include <Phonon/VideoWidget>

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QShowEvent>
#include <QToolButton>

#include <KDialog>
#include <QIcon>
#include <KIconLoader>
#include <KUrl>
#include <KLocale>

class EmbeddedVideoPlayer : public Phonon::VideoWidget
{
    public:
        EmbeddedVideoPlayer(QWidget *parent = 0) :
            Phonon::VideoWidget(parent)
        {
        }

        void setSizeHint(const QSize& size)
        {
            m_sizeHint = size;
            updateGeometry();
        }

        virtual QSize sizeHint() const
        {
            return m_sizeHint.isValid() ? m_sizeHint : Phonon::VideoWidget::sizeHint();
        }

    private:
        QSize m_sizeHint;
};

PhononWidget::PhononWidget(QWidget *parent)
    : QWidget(parent),
    m_url(),
    m_playButton(0),
    m_stopButton(0),
    m_topLayout(0),
    m_media(0),
    m_seekSlider(0),
    m_audioOutput(0),
    m_videoPlayer(0)
{
}

void PhononWidget::setUrl(const KUrl &url)
{
    if (m_url != url) {
        stop(); // emits playingStopped() signal
        m_url = url;
    }
}

KUrl PhononWidget::url() const
{
    return m_url;
}

void PhononWidget::setVideoSize(const QSize& size)
{
    if (m_videoSize != size) {
        m_videoSize = size;
        applyVideoSize();
    }
}

QSize PhononWidget::videoSize() const
{
    return m_videoSize;
}

void PhononWidget::showEvent(QShowEvent *event)
{
    if (event->spontaneous()) {
        QWidget::showEvent(event);
        return;
    }

    if (!m_topLayout) {
        m_topLayout = new QVBoxLayout(this);
        m_topLayout->setMargin(0);
        m_topLayout->setSpacing(KDialog::spacingHint());
        QHBoxLayout *controlsLayout = new QHBoxLayout(this);
        controlsLayout->setMargin(0);
        controlsLayout->setSpacing(0);

        m_playButton = new QToolButton(this);
        m_stopButton = new QToolButton(this);
        m_seekSlider = new Phonon::SeekSlider(this);

        controlsLayout->addWidget(m_playButton);
        controlsLayout->addWidget(m_stopButton);
        controlsLayout->addWidget(m_seekSlider);

        m_topLayout->addLayout(controlsLayout);

        const int smallIconSize = IconSize(KIconLoader::Small);
        const QSize buttonSize(smallIconSize, smallIconSize);

        m_playButton->setToolTip(i18n("play"));
        m_playButton->setIconSize(buttonSize);
        m_playButton->setIcon(QIcon::fromTheme("media-playback-start"));
        m_playButton->setAutoRaise(true);
        connect(m_playButton, &QToolButton::clicked, this, &PhononWidget::play);

        m_stopButton->setToolTip(i18n("stop"));
        m_stopButton->setIconSize(buttonSize);
        m_stopButton->setIcon(QIcon::fromTheme("media-playback-stop"));
        m_stopButton->setAutoRaise(true);
        m_stopButton->hide();
        connect(m_stopButton, &QToolButton::clicked, this, &PhononWidget::stop);

        m_seekSlider->setIconVisible(false);

        // Creating an audio player or video player instance might take up to
        // 2 seconds when doing it the first time. To prevent that the user
        // interface gets noticeable blocked, the creation is delayed until
        // the play button has been pressed (see PhononWidget::play()).
    }
}

void PhononWidget::hideEvent(QHideEvent *event)
{
    QWidget::hideEvent(event);
    if (!event->spontaneous()) {
        stop();
    }
}

void PhononWidget::stateChanged(Phonon::State newstate)
{
    setUpdatesEnabled(false);
    switch (newstate) {
    case Phonon::PlayingState:
    case Phonon::BufferingState:
        m_stopButton->show();
        m_playButton->hide();
        break;
    default:
        m_stopButton->hide();
        m_playButton->show();
        break;
    }
    setUpdatesEnabled(true);
}

void PhononWidget::play()
{
    if (!m_media) {
        m_media = new Phonon::MediaObject(this);
        connect(m_media, &Phonon::MediaObject::stateChanged,
                this, &PhononWidget::stateChanged);
        connect(m_media, &Phonon::MediaObject::hasVideoChanged,
                this, &PhononWidget::slotHasVideoChanged);
        m_seekSlider->setMediaObject(m_media);
    }

    if (!m_audioOutput) {
        m_audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
        Phonon::createPath(m_media, m_audioOutput);
    }

    emit hasVideoChanged(false);

    m_media->setCurrentSource(m_url);
    m_media->hasVideo();
    m_media->play();
}

void PhononWidget::stop()
{
    if (m_media) {
        m_media->stop();

        m_stopButton->hide();
        m_playButton->show();
    }

    if (m_videoPlayer) {
        m_videoPlayer->hide();
    }

    emit hasVideoChanged(false);
}

void PhononWidget::slotHasVideoChanged(bool hasVideo)
{
    emit hasVideoChanged(hasVideo);

    if (hasVideo) {
        if (!m_videoPlayer) {
            // Replay the media to apply path changes
            m_media->stop();
            m_videoPlayer = new EmbeddedVideoPlayer(this);
            m_topLayout->insertWidget(0, m_videoPlayer);
            Phonon::createPath(m_media, m_videoPlayer);
            m_media->play();
        }
        applyVideoSize();
        m_videoPlayer->show();
    }
}

void PhononWidget::applyVideoSize()
{
    if ((m_videoPlayer) && m_videoSize.isValid()) {
        m_videoPlayer->setSizeHint(m_videoSize);
    }
}