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, 0, KDialog::spacingHint() );
00043
00044 imageLabel = new QLabel( this );
00045 imageLabel->setFrameStyle( QFrame::NoFrame );
00046 imageLabel->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
00047 imageLabel->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding) );
00048 vb->addWidget( imageLabel );
00049
00050 QHBoxLayout *hb = new QHBoxLayout( 0 );
00051 vb->addLayout( hb );
00052
00053 autoPreview = new QCheckBox( i18n("&Automatic preview"), this );
00054 autoPreview->setChecked( autoMode );
00055 hb->addWidget( autoPreview );
00056 connect( autoPreview, SIGNAL(toggled(bool)), SLOT(toggleAuto(bool)) );
00057
00058 previewButton = new KPushButton( SmallIconSet("thumbnail"), i18n("&Preview"), this );
00059 hb->addWidget( previewButton );
00060 connect( previewButton, SIGNAL(clicked()), SLOT(showPreview()) );
00061
00062 timer = new QTimer( this );
00063 connect( timer, SIGNAL(timeout()), SLOT(showPreview()) );
00064
00065 setSupportedMimeTypes( KIO::PreviewJob::supportedMimeTypes() );
00066 }
00067
00068 KImageFilePreview::~KImageFilePreview()
00069 {
00070 if ( m_job )
00071 m_job->kill();
00072
00073 KConfig *config = KGlobal::config();
00074 KConfigGroupSaver cs( config, ConfigGroup );
00075 config->writeEntry( "Automatic Preview", autoPreview->isChecked() );
00076 }
00077
00078 void KImageFilePreview::showPreview()
00079 {
00080
00081 KURL url = currentURL;
00082 showPreview( url, true );
00083 }
00084
00085
00086 void KImageFilePreview::showPreview( const KURL& url )
00087 {
00088 showPreview( url, false );
00089 }
00090
00091 void KImageFilePreview::showPreview( const KURL &url, bool force )
00092 {
00093 if ( !url.isValid() ) {
00094 clearPreview();
00095 return;
00096 }
00097
00098 if ( url != currentURL || force )
00099 {
00100 clearPreview();
00101 currentURL = url;
00102
00103 if ( autoMode || force )
00104 {
00105 int w = imageLabel->contentsRect().width() - 4;
00106 int h = imageLabel->contentsRect().height() - 4;
00107
00108 m_job = createJob( url, w, h );
00109 if ( force )
00110 m_job->setIgnoreMaximumSize( true );
00111
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
00130 KURL url = currentURL;
00131 showPreview( url, true );
00132 }
00133 }
00134
00135 void KImageFilePreview::resizeEvent( QResizeEvent * )
00136 {
00137 timer->start( 100, true );
00138 }
00139
00140 QSize KImageFilePreview::sizeHint() const
00141 {
00142 return QSize( 20, 200 );
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 )
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 )
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"