File indexing completed on 2024-11-10 04:30:27

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2006-2007 Matthias Kretz <kretz@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 
0006 */
0007 
0008 #include "mediacontrols.h"
0009 #include "mediacontrols_p.h"
0010 
0011 #include <QBoxLayout>
0012 #include <QToolButton>
0013 #include <phonon/audiooutput.h>
0014 #include <phonon/mediaobject.h>
0015 #include <phonon/seekslider.h>
0016 #include <phonon/volumeslider.h>
0017 
0018 namespace Phonon
0019 {
0020 
0021 MediaControls::MediaControls(QWidget *parent)
0022     : QWidget(parent)
0023     , d_ptr(new MediaControlsPrivate(this))
0024 {
0025     setMaximumHeight(32);
0026 }
0027 
0028 MediaControls::~MediaControls()
0029 {
0030     delete d_ptr;
0031 }
0032 
0033 bool MediaControls::isSeekSliderVisible() const
0034 {
0035     Q_D(const MediaControls);
0036     return d->seekSlider.isVisible();
0037 }
0038 
0039 bool MediaControls::isVolumeControlVisible() const
0040 {
0041     Q_D(const MediaControls);
0042     return d->volumeSlider.isVisible();
0043 }
0044 
0045 void MediaControls::setMediaObject(MediaObject *media)
0046 {
0047     Q_D(MediaControls);
0048     if (d->media) {
0049         disconnect(d->media, SIGNAL(destroyed()), this, SLOT(_k_mediaDestroyed()));
0050         disconnect(d->media, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(_k_stateChanged(Phonon::State, Phonon::State)));
0051         disconnect(&d->playButton, SIGNAL(clicked()), d->media, SLOT(play()));
0052         disconnect(&d->pauseButton, SIGNAL(clicked()), d->media, SLOT(pause()));
0053     }
0054     d->media = media;
0055     if (media) {
0056         connect(media, SIGNAL(destroyed()), SLOT(_k_mediaDestroyed()));
0057         connect(media, SIGNAL(stateChanged(Phonon::State, Phonon::State)), SLOT(_k_stateChanged(Phonon::State, Phonon::State)));
0058         connect(&d->playButton, SIGNAL(clicked()), media, SLOT(play()));
0059         connect(&d->pauseButton, SIGNAL(clicked()), media, SLOT(pause()));
0060     }
0061 
0062     d->seekSlider.setMediaObject(media);
0063 }
0064 
0065 void MediaControls::setAudioOutput(AudioOutput *audioOutput)
0066 {
0067     Q_D(MediaControls);
0068     d->volumeSlider.setAudioOutput(audioOutput);
0069     d->updateVolumeSliderVisibility();
0070     d->volumeSlider.setVisible(audioOutput != nullptr);
0071 }
0072 
0073 void MediaControls::setSeekSliderVisible(bool vis)
0074 {
0075     Q_D(MediaControls);
0076     d->seekSlider.setVisible(vis);
0077 }
0078 
0079 void MediaControls::setVolumeControlVisible(bool vis)
0080 {
0081     Q_D(MediaControls);
0082     d->volumeSlider.setVisible(vis);
0083 }
0084 
0085 void MediaControls::resizeEvent(QResizeEvent *)
0086 {
0087     Q_D(MediaControls);
0088     d->updateVolumeSliderVisibility();
0089 }
0090 
0091 void MediaControlsPrivate::updateVolumeSliderVisibility()
0092 {
0093     bool isWide = q_ptr->width() > playButton.sizeHint().width() + seekSlider.sizeHint().width() + volumeSlider.sizeHint().width();
0094     bool hasAudio = volumeSlider.audioOutput() != nullptr;
0095     volumeSlider.setVisible(isWide && hasAudio);
0096 }
0097 
0098 void MediaControlsPrivate::_k_stateChanged(State newstate, State)
0099 {
0100     switch (newstate) {
0101     case Phonon::LoadingState:
0102     case Phonon::PausedState:
0103     case Phonon::StoppedState:
0104         playButton.show();
0105         pauseButton.hide();
0106         break;
0107     case Phonon::BufferingState:
0108     case Phonon::PlayingState:
0109         playButton.hide();
0110         pauseButton.show();
0111         break;
0112     case Phonon::ErrorState:
0113         return;
0114     }
0115 }
0116 
0117 void MediaControlsPrivate::_k_mediaDestroyed()
0118 {
0119     media = nullptr;
0120 }
0121 
0122 } // namespace Phonon
0123 
0124 #include "moc_mediacontrols.cpp"