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