kio Library API Documentation

kimagefilepreview.cpp

00001 /*
00002  * This file is part of the KDE project
00003  * Copyright (C) 2001 Martin R. Jones <mjones@kde.org>
00004  *               2001 Carsten Pfeiffer <pfeiffer@kde.org>
00005  *
00006  * You can Freely distribute this program under the GNU Library General Public
00007  * License. See the file "COPYING" for the exact licensing terms.
00008  */
00009 
00010 #include <qlayout.h>
00011 #include <qlabel.h>
00012 #include <qcombobox.h>
00013 #include <qcheckbox.h>
00014 #include <qpushbutton.h>
00015 #include <qwhatsthis.h>
00016 #include <qtimer.h>
00017 
00018 #include <kapplication.h>
00019 #include <kconfig.h>
00020 #include <kglobal.h>
00021 #include <kiconloader.h>
00022 #include <kstandarddirs.h>
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025 #include <kfiledialog.h>
00026 #include <kfileitem.h>
00027 #include <kio/previewjob.h>
00028 
00029 #include "kimagefilepreview.h"
00030 #include "config-kfile.h"
00031 
00032 /**** KImageFilePreview ****/
00033 
00034 KImageFilePreview::KImageFilePreview( QWidget *parent )
00035     : KPreviewWidgetBase( parent ),
00036       m_job( 0L )
00037 {
00038     KConfig *config = KGlobal::config();
00039     KConfigGroupSaver cs( config, ConfigGroup );
00040     autoMode = config->readBoolEntry( "Automatic Preview", true );
00041 
00042     QVBoxLayout *vb = new QVBoxLayout( this, KDialog::marginHint() );
00043 
00044     imageLabel = new QLabel( this );
00045     imageLabel->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00046     imageLabel->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
00047     imageLabel->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) );
00048     vb->addWidget( imageLabel, 1 );
00049 
00050     QHBoxLayout *hb = new QHBoxLayout( vb );
00051 
00052     autoPreview = new QCheckBox( i18n("&Automatic preview"), this );
00053     autoPreview->setChecked( autoMode );
00054     hb->addWidget( autoPreview );
00055     connect( autoPreview, SIGNAL(toggled(bool)), SLOT(toggleAuto(bool)) );
00056 
00057     previewButton = new QPushButton( i18n("&Preview"), this );
00058     hb->addWidget( previewButton );
00059     connect( previewButton, SIGNAL(clicked()), SLOT(showPreview()) );
00060 
00061     timer = new QTimer( this );
00062     connect( timer, SIGNAL(timeout()), SLOT(showPreview()) );
00063 }
00064 
00065 KImageFilePreview::~KImageFilePreview()
00066 {
00067     if ( m_job )
00068         m_job->kill();
00069 
00070     KConfig *config = KGlobal::config();
00071     KConfigGroupSaver cs( config, ConfigGroup );
00072     config->writeEntry( "Automatic Preview", autoPreview->isChecked() );
00073 }
00074 
00075 // called via KPreviewWidgetBase interface
00076 void KImageFilePreview::showPreview( const KURL& url )
00077 {
00078     showPreview( url, false );
00079 }
00080 
00081 void KImageFilePreview::showPreview( const KURL &url, bool force )
00082 {
00083     if ( url.isMalformed() ) {
00084         clearPreview();
00085         return;
00086     }
00087 
00088     if ( url != currentURL || force )
00089     {
00090         clearPreview();
00091         currentURL = url;
00092 
00093         if ( autoMode || force )
00094         {
00095             int w = imageLabel->contentsRect().width() - 4;
00096             int h = imageLabel->contentsRect().height() - 4;
00097 
00098             m_job =  createJob( url, w, h );
00099             connect( m_job, SIGNAL( result( KIO::Job * )),
00100                      this, SLOT( slotResult( KIO::Job * )));
00101             connect( m_job, SIGNAL( gotPreview( const KFileItem*,
00102                                                 const QPixmap& )),
00103                      SLOT( gotPreview( const KFileItem*, const QPixmap& ) ));
00104 
00105             connect( m_job, SIGNAL( failed( const KFileItem* )),
00106                      this, SLOT( slotFailed( const KFileItem* ) ));
00107         }
00108     }
00109 }
00110 
00111 void KImageFilePreview::toggleAuto( bool a )
00112 {
00113     autoMode = a;
00114     if ( autoMode )
00115     {
00116         showPreview( currentURL, true );
00117     }
00118 }
00119 
00120 void KImageFilePreview::resizeEvent( QResizeEvent * )
00121 {
00122     timer->start( 100, true ); // forces a new preview
00123 }
00124 
00125 QSize KImageFilePreview::sizeHint() const
00126 {
00127     return QSize( 20, 200 ); // otherwise it ends up huge???
00128 }
00129 
00130 KIO::PreviewJob * KImageFilePreview::createJob( const KURL& url, int w, int h )
00131 {
00132     KURL::List urls;
00133     urls.append( url );
00134     return KIO::filePreview( urls, w, h, 0, 0, true, false );
00135 }
00136 
00137 void KImageFilePreview::gotPreview( const KFileItem* item, const QPixmap& pm )
00138 {
00139     if ( item->url() == currentURL ) // should always be the case
00140         imageLabel->setPixmap( pm );
00141 }
00142 
00143 void KImageFilePreview::slotFailed( const KFileItem* item )
00144 {
00145     if ( item->isDir() )
00146         imageLabel->clear();
00147     else if ( item->url() == currentURL ) // should always be the case
00148         imageLabel->setPixmap( SmallIcon( "file_broken", KIcon::SizeLarge,
00149                                           KIcon::DisabledState ));
00150 }
00151 
00152 void KImageFilePreview::slotResult( KIO::Job *job )
00153 {
00154     if ( job == m_job )
00155         m_job = 0L;
00156 }
00157 
00158 void KImageFilePreview::clearPreview()
00159 {
00160     if ( m_job ) {
00161         m_job->kill();
00162         m_job = 0L;
00163     }
00164 
00165     imageLabel->clear();
00166 //     currentURL = KURL();
00167 }
00168 
00169 void KImageFilePreview::virtual_hook( int id, void* data )
00170 { KPreviewWidgetBase::virtual_hook( id, data ); }
00171 
00172 #include "kimagefilepreview.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:13:37 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001