interfaces Library API Documentation

kfileaudiopreview.cpp

00001 #include "kfileaudiopreview.h"
00002 
00003 #include <qcheckbox.h>
00004 #include <qhbox.h>
00005 #include <qlayout.h>
00006 #include <qvgroupbox.h>
00007 
00008 #include <kconfig.h>
00009 #include <klibloader.h>
00010 #include <klocale.h>
00011 #include <kmediaplayer/player.h>
00012 #include <kmimetype.h>
00013 #include <kparts/componentfactory.h>
00014 
00015 #include <kplayobjectfactory.h>
00016 
00017 #include <config-kfile.h>
00018 
00019 class KFileAudioPreviewFactory : public KLibFactory
00020 {
00021 protected:
00022     virtual QObject *createObject( QObject *parent, const char *name,
00023                            const char *, const QStringList & )
00024     {
00025         return new KFileAudioPreview( dynamic_cast<QWidget*>( parent ), name );
00026     }
00027 };
00028 
00029 K_EXPORT_COMPONENT_FACTORY( kfileaudiopreview, KFileAudioPreviewFactory )
00030 
00031 
00034 
00035 
00036 class KFileAudioPreview::KFileAudioPreviewPrivate
00037 {
00038 public:
00039     KFileAudioPreviewPrivate( QWidget *parent )
00040     {
00041         player = KParts::ComponentFactory::createInstanceFromQuery<KMediaPlayer::Player>( "KMediaPlayer/Player", QString::null, parent );
00042     }
00043 
00044     ~KFileAudioPreviewPrivate()
00045     {
00046         delete player;
00047     }
00048 
00049     KMediaPlayer::Player *player;
00050 };
00051 
00052 
00053 KFileAudioPreview::KFileAudioPreview( QWidget *parent, const char *name )
00054     : KPreviewWidgetBase( parent, name )
00055 {
00056     QStringList formats = KDE::PlayObjectFactory::mimeTypes();
00057     // ###
00058     QStringList::ConstIterator it = formats.begin();
00059     for ( ; it != formats.end(); ++it )
00060         m_supportedFormats.insert( *it, (void*) 1 );
00061 
00062     QVGroupBox *box = new QVGroupBox( i18n("Media Player"), this );
00063     QVBoxLayout *layout = new QVBoxLayout( this );
00064     layout->addWidget( box );
00065 
00066     (void) new QWidget( box ); // spacer
00067 
00068     d = new KFileAudioPreviewPrivate( 0L ); // not box -- being reparented anyway
00069     if ( d->player ) // only if there actually is a component...
00070     {
00071         setSupportedMimeTypes( formats );
00072         KMediaPlayer::View *view = d->player->view();
00073         view->setEnabled( false );
00074 
00075         // if we have access to the video widget, show it above the player
00076         // So, reparent first the video widget, then the view.
00077         if ( view->videoWidget() )
00078         {
00079             QHBox *frame = new QHBox( box );
00080             frame->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00081             frame->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00082             view->videoWidget()->reparent( frame, QPoint(0,0) );
00083         }
00084 
00085         view->reparent( box, QPoint(0,0) );
00086     }
00087 
00088     m_autoPlay = new QCheckBox( i18n("Play &automatically"), box );
00089     KConfigGroup config( KGlobal::config(), ConfigGroup );
00090     m_autoPlay->setChecked( config.readBoolEntry( "Autoplay sounds", true ) );
00091     connect( m_autoPlay, SIGNAL(toggled(bool)), SLOT(toggleAuto(bool)) );
00092 }
00093 
00094 KFileAudioPreview::~KFileAudioPreview()
00095 {
00096     KConfigGroup config( KGlobal::config(), ConfigGroup );
00097     config.writeEntry( "Autoplay sounds", m_autoPlay->isChecked() );
00098 
00099     delete d;
00100 }
00101 
00102 void KFileAudioPreview::showPreview( const KURL &url )
00103 {
00104     if ( !d->player || !url.isValid() )
00105         return;
00106 
00107     KMimeType::Ptr mt = KMimeType::findByURL( url );
00108     bool supported = m_supportedFormats.find( mt->name() );
00109     d->player->view()->setEnabled( supported );
00110     if ( !supported )
00111         return;
00112 
00113     static_cast<KParts::ReadOnlyPart*>(d->player)->openURL( url );
00114     if ( m_autoPlay->isChecked() )
00115         d->player->play();
00116 }
00117 
00118 void KFileAudioPreview::clearPreview()
00119 {
00120     if ( d->player )
00121     {
00122         d->player->stop();
00123         d->player->closeURL();
00124     }
00125 }
00126 
00127 void KFileAudioPreview::toggleAuto( bool on )
00128 {
00129     if ( !d->player )
00130         return;
00131 
00132     if ( on && m_currentURL.isValid() && d->player->view()->isEnabled() )
00133         d->player->play();
00134     else
00135         d->player->stop();
00136 }
00137 
00138 void KFileAudioPreview::virtual_hook( int, void* )
00139 {}
00140 
00141 #include "kfileaudiopreview.moc"
KDE Logo
This file is part of the documentation for interfaces Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun May 16 22:04:58 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003