]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/information/phononwidget.cpp
Show video previews according to file content instead of the mimetype-string
[dolphin.git] / src / panels / information / phononwidget.cpp
index 405172b8d4301b38d5dfa073c2498c1ed7f9785a..1419f68be9b80e425b08b59285458db22817a93a 100644 (file)
 
 #include "phononwidget.h"
 
+#include <Phonon/AudioOutput>
 #include <Phonon/Global>
 #include <Phonon/MediaObject>
 #include <Phonon/SeekSlider>
-#include <Phonon/VideoPlayer>
-#include <QtGui/QVBoxLayout>
-#include <QtGui/QHBoxLayout>
-#include <QtGui/QShowEvent>
-#include <QtGui/QToolButton>
-#include <kicon.h>
-#include <kurl.h>
-#include <klocale.h>
+#include <Phonon/VideoWidget>
+
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+#include <QShowEvent>
+#include <QToolButton>
+
+#include <KDialog>
+#include <KIcon>
+#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_mode(Audio),
     m_url(),
     m_playButton(0),
     m_stopButton(0),
-    m_audioMedia(0),
+    m_topLayout(0),
     m_media(0),
     m_seekSlider(0),
+    m_audioOutput(0),
     m_videoPlayer(0)
 {
 }
@@ -58,17 +85,17 @@ KUrl PhononWidget::url() const
     return m_url;
 }
 
-void PhononWidget::setMode(Mode mode)
+void PhononWidget::setVideoSize(const QSize& size)
 {
-    if (m_mode != mode) {
-        stop(); // emits playingStopped() signal
-        m_mode = mode;
+    if (m_videoSize != size) {
+        m_videoSize = size;
+        applyVideoSize();
     }
 }
 
-PhononWidget::Mode PhononWidget::mode() const
+QSize PhononWidget::videoSize() const
 {
-    return m_mode;
+    return m_videoSize;
 }
 
 void PhononWidget::showEvent(QShowEvent *event)
@@ -78,10 +105,10 @@ void PhononWidget::showEvent(QShowEvent *event)
         return;
     }
 
-    if (m_playButton == 0) {
-        QVBoxLayout *topLayout = new QVBoxLayout(this);
-        topLayout->setMargin(0);
-        topLayout->setSpacing(0);
+    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);
@@ -89,15 +116,12 @@ void PhononWidget::showEvent(QShowEvent *event)
         m_playButton = new QToolButton(this);
         m_stopButton = new QToolButton(this);
         m_seekSlider = new Phonon::SeekSlider(this);
-        m_videoPlayer = new Phonon::VideoPlayer(Phonon::VideoCategory, this);
-        m_videoPlayer->hide();
 
         controlsLayout->addWidget(m_playButton);
         controlsLayout->addWidget(m_stopButton);
         controlsLayout->addWidget(m_seekSlider);
 
-        topLayout->addWidget(m_videoPlayer);
-        topLayout->addLayout(controlsLayout);
+        m_topLayout->addLayout(controlsLayout);
 
         m_playButton->setToolTip(i18n("play"));
         m_playButton->setIconSize(QSize(16, 16));
@@ -111,6 +135,11 @@ void PhononWidget::showEvent(QShowEvent *event)
         connect(m_stopButton, SIGNAL(clicked()), this, SLOT(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()).
     }
 }
 
@@ -141,51 +170,64 @@ void PhononWidget::stateChanged(Phonon::State newstate)
 
 void PhononWidget::play()
 {
-    switch (m_mode) {
-    case Audio:
-        if (m_audioMedia == 0) {
-            // Creating an audio player might take up to 2 seconds when doing
-            // it the first time. To prevent that the user interface gets
-            // noticable blocked, the creation is delayed until the play button
-            // has been pressed.
-            m_audioMedia = Phonon::createPlayer(Phonon::MusicCategory, m_url);
-            m_audioMedia->setParent(this);
-        }
-        m_media = m_audioMedia;
-        m_media->setCurrentSource(m_url);
-        break;
-
-    case Video:
-        m_videoPlayer->show();
-        m_videoPlayer->play(m_url);
-        m_media = m_videoPlayer->mediaObject();
-        break;
+    if (!m_media) {
+        m_media = new Phonon::MediaObject(this);
+        connect(m_media, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
+                this, SLOT(stateChanged(Phonon::State)));
+        connect(m_media, SIGNAL(hasVideoChanged(bool)),
+                this, SLOT(slotHasVideoChanged(bool)));
+        m_seekSlider->setMediaObject(m_media);
+    }
 
-    default:
-        break;
+    if (!m_audioOutput) {
+        m_audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
+        Phonon::createPath(m_media, m_audioOutput);
     }
 
-    Q_ASSERT(m_media != 0);
-    connect(m_media, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
-            this, SLOT(stateChanged(Phonon::State)));
-    m_seekSlider->setMediaObject(m_media);
+    emit hasVideoChanged(false);
 
-    emit playingStarted();
+    m_media->setCurrentSource(m_url);
+    m_media->hasVideo();
+    m_media->play();
 }
 
 void PhononWidget::stop()
 {
-    if (m_media != 0) {
+    if (m_media) {
         m_media->stop();
-        disconnect(m_media, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
-                   this, SLOT(stateChanged(Phonon::State)));
-        emit playingStopped();
 
         m_stopButton->hide();
         m_playButton->show();
     }
 
-    if (m_videoPlayer != 0) {
+    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);
+    }
 }