00001
00002
00003
00004
00005
00006
00007
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
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 KPushButton( SmallIconSet("thumbnail"), 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 setSupportedMimeTypes( KIO::PreviewJob::supportedMimeTypes() );
00065 }
00066
00067 KImageFilePreview::~KImageFilePreview()
00068 {
00069 if ( m_job )
00070 m_job->kill();
00071
00072 KConfig *config = KGlobal::config();
00073 KConfigGroupSaver cs( config, ConfigGroup );
00074 config->writeEntry( "Automatic Preview", autoPreview->isChecked() );
00075 }
00076
00077 void KImageFilePreview::showPreview()
00078 {
00079
00080 KURL url = currentURL;
00081 showPreview( url, true );
00082 }
00083
00084
00085 void KImageFilePreview::showPreview( const KURL& url )
00086 {
00087 showPreview( url, false );
00088 }
00089
00090 void KImageFilePreview::showPreview( const KURL &url, bool force )
00091 {
00092 if ( !url.isValid() ) {
00093 clearPreview();
00094 return;
00095 }
00096
00097 if ( url != currentURL || force )
00098 {
00099 clearPreview();
00100 currentURL = url;
00101
00102 if ( autoMode || force )
00103 {
00104 int w = imageLabel->contentsRect().width() - 4;
00105 int h = imageLabel->contentsRect().height() - 4;
00106
00107 m_job = createJob( url, w, h );
00108 connect( m_job, SIGNAL( result( KIO::Job * )),
00109 this, SLOT( slotResult( KIO::Job * )));
00110 connect( m_job, SIGNAL( gotPreview( const KFileItem*,
00111 const QPixmap& )),
00112 SLOT( gotPreview( const KFileItem*, const QPixmap& ) ));
00113
00114 connect( m_job, SIGNAL( failed( const KFileItem* )),
00115 this, SLOT( slotFailed( const KFileItem* ) ));
00116 }
00117 }
00118 }
00119
00120 void KImageFilePreview::toggleAuto( bool a )
00121 {
00122 autoMode = a;
00123 if ( autoMode )
00124 {
00125
00126 KURL url = currentURL;
00127 showPreview( url, true );
00128 }
00129 }
00130
00131 void KImageFilePreview::resizeEvent( QResizeEvent * )
00132 {
00133 timer->start( 100, true );
00134 }
00135
00136 QSize KImageFilePreview::sizeHint() const
00137 {
00138 return QSize( 20, 200 );
00139 }
00140
00141 KIO::PreviewJob * KImageFilePreview::createJob( const KURL& url, int w, int h )
00142 {
00143 KURL::List urls;
00144 urls.append( url );
00145 return KIO::filePreview( urls, w, h, 0, 0, true, false );
00146 }
00147
00148 void KImageFilePreview::gotPreview( const KFileItem* item, const QPixmap& pm )
00149 {
00150 if ( item->url() == currentURL )
00151 imageLabel->setPixmap( pm );
00152 }
00153
00154 void KImageFilePreview::slotFailed( const KFileItem* item )
00155 {
00156 if ( item->isDir() )
00157 imageLabel->clear();
00158 else if ( item->url() == currentURL )
00159 imageLabel->setPixmap( SmallIcon( "file_broken", KIcon::SizeLarge,
00160 KIcon::DisabledState ));
00161 }
00162
00163 void KImageFilePreview::slotResult( KIO::Job *job )
00164 {
00165 if ( job == m_job )
00166 m_job = 0L;
00167 }
00168
00169 void KImageFilePreview::clearPreview()
00170 {
00171 if ( m_job ) {
00172 m_job->kill();
00173 m_job = 0L;
00174 }
00175
00176 imageLabel->clear();
00177 currentURL = KURL();
00178 }
00179
00180 void KImageFilePreview::virtual_hook( int id, void* data )
00181 { KPreviewWidgetBase::virtual_hook( id, data ); }
00182
00183 #include "kimagefilepreview.moc"