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 <qwhatsthis.h>
00015 #include <qtimer.h>
00016 
00017 #include <kapplication.h>
00018 #include <kconfig.h>
00019 #include <kglobal.h>
00020 #include <kiconloader.h>
00021 #include <kpushbutton.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     QGridLayout *vb = new QGridLayout( this, 2, 2, 0, KDialog::spacingHint() );
00043     vb->addItem( new QSpacerItem(0, 0, QSizePolicy::Expanding), 0, 0 );
00044 
00045     imageLabel = new QLabel( this );
00046     imageLabel->setFrameStyle( QFrame::NoFrame );
00047     imageLabel->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
00048     imageLabel->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) );
00049     vb->addWidget( imageLabel, 0, 1 );
00050 
00051     vb->addItem( new QSpacerItem(0, 0, QSizePolicy::Expanding), 0, 2 );
00052 
00053     QHBoxLayout *hb = new QHBoxLayout( 0 );
00054     vb->addLayout( hb, 1, 1 );
00055 
00056     autoPreview = new QCheckBox( i18n("&Automatic preview"), this );
00057     autoPreview->setChecked( autoMode );
00058     hb->addWidget( autoPreview );
00059     connect( autoPreview, SIGNAL(toggled(bool)), SLOT(toggleAuto(bool)) );
00060 
00061     previewButton = new KPushButton( SmallIconSet("thumbnail"), i18n("&Preview"), this );
00062     hb->addWidget( previewButton );
00063     connect( previewButton, SIGNAL(clicked()), SLOT(showPreview()) );
00064 
00065     timer = new QTimer( this );
00066     connect( timer, SIGNAL(timeout()), SLOT(showPreview()) );
00067 
00068     setSupportedMimeTypes( KIO::PreviewJob::supportedMimeTypes() );
00069 }
00070 
00071 KImageFilePreview::~KImageFilePreview()
00072 {
00073     if ( m_job )
00074         m_job->kill();
00075 
00076     KConfig *config = KGlobal::config();
00077     KConfigGroupSaver cs( config, ConfigGroup );
00078     config->writeEntry( "Automatic Preview", autoPreview->isChecked() );
00079 }
00080 
00081 void KImageFilePreview::showPreview()
00082 {
00083     // Pass a copy since clearPreview() will clear currentURL
00084     KURL url = currentURL;
00085     showPreview( url, true );
00086 }
00087 
00088 // called via KPreviewWidgetBase interface
00089 void KImageFilePreview::showPreview( const KURL& url )
00090 {
00091     showPreview( url, false );
00092 }
00093 
00094 void KImageFilePreview::showPreview( const KURL &url, bool force )
00095 {
00096     if ( !url.isValid() ) {
00097         clearPreview();
00098         return;
00099     }
00100 
00101     if ( url != currentURL || force )
00102     {
00103         clearPreview();
00104     currentURL = url;
00105 
00106     if ( autoMode || force )
00107     {
00108             int w = imageLabel->contentsRect().width() - 4;
00109             int h = imageLabel->contentsRect().height() - 4;
00110 
00111             m_job =  createJob( url, w, h );
00112             connect( m_job, SIGNAL( result( KIO::Job * )),
00113                      this, SLOT( slotResult( KIO::Job * )));
00114             connect( m_job, SIGNAL( gotPreview( const KFileItem*,
00115                                                 const QPixmap& )),
00116                      SLOT( gotPreview( const KFileItem*, const QPixmap& ) ));
00117 
00118             connect( m_job, SIGNAL( failed( const KFileItem* )),
00119                      this, SLOT( slotFailed( const KFileItem* ) ));
00120     }
00121     }
00122 }
00123 
00124 void KImageFilePreview::toggleAuto( bool a )
00125 {
00126     autoMode = a;
00127     if ( autoMode )
00128     {
00129         // Pass a copy since clearPreview() will clear currentURL
00130         KURL url = currentURL;
00131         showPreview( url, true );
00132     }
00133 }
00134 
00135 void KImageFilePreview::resizeEvent( QResizeEvent * )
00136 {
00137     timer->start( 100, true ); // forces a new preview
00138 }
00139 
00140 QSize KImageFilePreview::sizeHint() const
00141 {
00142     return QSize( 20, 200 ); // otherwise it ends up huge???
00143 }
00144 
00145 KIO::PreviewJob * KImageFilePreview::createJob( const KURL& url, int w, int h )
00146 {
00147     KURL::List urls;
00148     urls.append( url );
00149     return KIO::filePreview( urls, w, h, 0, 0, true, false );
00150 }
00151 
00152 void KImageFilePreview::gotPreview( const KFileItem* item, const QPixmap& pm )
00153 {
00154     if ( item->url() == currentURL ) // should always be the case
00155         imageLabel->setPixmap( pm );
00156 }
00157 
00158 void KImageFilePreview::slotFailed( const KFileItem* item )
00159 {
00160     if ( item->isDir() )
00161         imageLabel->clear();
00162     else if ( item->url() == currentURL ) // should always be the case
00163         imageLabel->setPixmap( SmallIcon( "file_broken", KIcon::SizeLarge,
00164                                           KIcon::DisabledState ));
00165 }
00166 
00167 void KImageFilePreview::slotResult( KIO::Job *job )
00168 {
00169     if ( job == m_job )
00170         m_job = 0L;
00171 }
00172 
00173 void KImageFilePreview::clearPreview()
00174 {
00175     if ( m_job ) {
00176         m_job->kill();
00177         m_job = 0L;
00178     }
00179 
00180     imageLabel->clear();
00181     currentURL = KURL();
00182 }
00183 
00184 void KImageFilePreview::virtual_hook( int id, void* data )
00185 { KPreviewWidgetBase::virtual_hook( id, data ); }
00186 
00187 #include "kimagefilepreview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys