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 <tqlayout.h> 00011 #include <tqlabel.h> 00012 #include <tqcombobox.h> 00013 #include <tqcheckbox.h> 00014 #include <tqwhatsthis.h> 00015 #include <tqtimer.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( TQWidget *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 TQVBoxLayout *vb = new TQVBoxLayout( this, 0, KDialog::spacingHint() ); 00043 00044 imageLabel = new TQLabel( this ); 00045 imageLabel->setFrameStyle( TQFrame::NoFrame ); 00046 imageLabel->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter ); 00047 imageLabel->setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding) ); 00048 vb->addWidget( imageLabel ); 00049 00050 TQHBoxLayout *hb = new TQHBoxLayout( 0 ); 00051 vb->addLayout( hb ); 00052 00053 autoPreview = new TQCheckBox( i18n("&Automatic preview"), this ); 00054 autoPreview->setChecked( autoMode ); 00055 hb->addWidget( autoPreview ); 00056 connect( autoPreview, TQT_SIGNAL(toggled(bool)), TQT_SLOT(toggleAuto(bool)) ); 00057 00058 previewButton = new KPushButton( SmallIconSet("thumbnail"), i18n("&Preview"), this ); 00059 hb->addWidget( previewButton ); 00060 connect( previewButton, TQT_SIGNAL(clicked()), TQT_SLOT(showPreview()) ); 00061 00062 timer = new TQTimer( this ); 00063 connect( timer, TQT_SIGNAL(timeout()), TQT_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 // Pass a copy since clearPreview() will clear currentURL 00081 KURL url = currentURL; 00082 showPreview( url, true ); 00083 } 00084 00085 // called via KPreviewWidgetBase interface 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 ) // explicitly requested previews shall always be generated! 00110 m_job->setIgnoreMaximumSize( true ); 00111 00112 connect( m_job, TQT_SIGNAL( result( KIO::Job * )), 00113 this, TQT_SLOT( slotResult( KIO::Job * ))); 00114 connect( m_job, TQT_SIGNAL( gotPreview( const KFileItem*, 00115 const TQPixmap& )), 00116 TQT_SLOT( gotPreview( const KFileItem*, const TQPixmap& ) )); 00117 00118 connect( m_job, TQT_SIGNAL( failed( const KFileItem* )), 00119 this, TQT_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( TQResizeEvent * ) 00136 { 00137 timer->start( 100, true ); // forces a new preview 00138 } 00139 00140 TQSize KImageFilePreview::sizeHint() const 00141 { 00142 return TQSize( 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 TQPixmap& 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"