From ce4a32fe52ef09d8f5ff1dd22c001110902b60a2 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdelibs@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kio/kfile/kimagefilepreview.cpp | 187 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 kio/kfile/kimagefilepreview.cpp (limited to 'kio/kfile/kimagefilepreview.cpp') diff --git a/kio/kfile/kimagefilepreview.cpp b/kio/kfile/kimagefilepreview.cpp new file mode 100644 index 000000000..d973c6e1b --- /dev/null +++ b/kio/kfile/kimagefilepreview.cpp @@ -0,0 +1,187 @@ +/* + * This file is part of the KDE project + * Copyright (C) 2001 Martin R. Jones + * 2001 Carsten Pfeiffer + * + * You can Freely distribute this program under the GNU Library General Public + * License. See the file "COPYING" for the exact licensing terms. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kimagefilepreview.h" +#include "config-kfile.h" + +/**** KImageFilePreview ****/ + +KImageFilePreview::KImageFilePreview( QWidget *parent ) + : KPreviewWidgetBase( parent ), + m_job( 0L ) +{ + KConfig *config = KGlobal::config(); + KConfigGroupSaver cs( config, ConfigGroup ); + autoMode = config->readBoolEntry( "Automatic Preview", true ); + + QVBoxLayout *vb = new QVBoxLayout( this, 0, KDialog::spacingHint() ); + + imageLabel = new QLabel( this ); + imageLabel->setFrameStyle( QFrame::NoFrame ); + imageLabel->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter ); + imageLabel->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding) ); + vb->addWidget( imageLabel ); + + QHBoxLayout *hb = new QHBoxLayout( 0 ); + vb->addLayout( hb ); + + autoPreview = new QCheckBox( i18n("&Automatic preview"), this ); + autoPreview->setChecked( autoMode ); + hb->addWidget( autoPreview ); + connect( autoPreview, SIGNAL(toggled(bool)), SLOT(toggleAuto(bool)) ); + + previewButton = new KPushButton( SmallIconSet("thumbnail"), i18n("&Preview"), this ); + hb->addWidget( previewButton ); + connect( previewButton, SIGNAL(clicked()), SLOT(showPreview()) ); + + timer = new QTimer( this ); + connect( timer, SIGNAL(timeout()), SLOT(showPreview()) ); + + setSupportedMimeTypes( KIO::PreviewJob::supportedMimeTypes() ); +} + +KImageFilePreview::~KImageFilePreview() +{ + if ( m_job ) + m_job->kill(); + + KConfig *config = KGlobal::config(); + KConfigGroupSaver cs( config, ConfigGroup ); + config->writeEntry( "Automatic Preview", autoPreview->isChecked() ); +} + +void KImageFilePreview::showPreview() +{ + // Pass a copy since clearPreview() will clear currentURL + KURL url = currentURL; + showPreview( url, true ); +} + +// called via KPreviewWidgetBase interface +void KImageFilePreview::showPreview( const KURL& url ) +{ + showPreview( url, false ); +} + +void KImageFilePreview::showPreview( const KURL &url, bool force ) +{ + if ( !url.isValid() ) { + clearPreview(); + return; + } + + if ( url != currentURL || force ) + { + clearPreview(); + currentURL = url; + + if ( autoMode || force ) + { + int w = imageLabel->contentsRect().width() - 4; + int h = imageLabel->contentsRect().height() - 4; + + m_job = createJob( url, w, h ); + if ( force ) // explicitly requested previews shall always be generated! + m_job->setIgnoreMaximumSize( true ); + + connect( m_job, SIGNAL( result( KIO::Job * )), + this, SLOT( slotResult( KIO::Job * ))); + connect( m_job, SIGNAL( gotPreview( const KFileItem*, + const QPixmap& )), + SLOT( gotPreview( const KFileItem*, const QPixmap& ) )); + + connect( m_job, SIGNAL( failed( const KFileItem* )), + this, SLOT( slotFailed( const KFileItem* ) )); + } + } +} + +void KImageFilePreview::toggleAuto( bool a ) +{ + autoMode = a; + if ( autoMode ) + { + // Pass a copy since clearPreview() will clear currentURL + KURL url = currentURL; + showPreview( url, true ); + } +} + +void KImageFilePreview::resizeEvent( QResizeEvent * ) +{ + timer->start( 100, true ); // forces a new preview +} + +QSize KImageFilePreview::sizeHint() const +{ + return QSize( 20, 200 ); // otherwise it ends up huge??? +} + +KIO::PreviewJob * KImageFilePreview::createJob( const KURL& url, int w, int h ) +{ + KURL::List urls; + urls.append( url ); + return KIO::filePreview( urls, w, h, 0, 0, true, false ); +} + +void KImageFilePreview::gotPreview( const KFileItem* item, const QPixmap& pm ) +{ + if ( item->url() == currentURL ) // should always be the case + imageLabel->setPixmap( pm ); +} + +void KImageFilePreview::slotFailed( const KFileItem* item ) +{ + if ( item->isDir() ) + imageLabel->clear(); + else if ( item->url() == currentURL ) // should always be the case + imageLabel->setPixmap( SmallIcon( "file_broken", KIcon::SizeLarge, + KIcon::DisabledState )); +} + +void KImageFilePreview::slotResult( KIO::Job *job ) +{ + if ( job == m_job ) + m_job = 0L; +} + +void KImageFilePreview::clearPreview() +{ + if ( m_job ) { + m_job->kill(); + m_job = 0L; + } + + imageLabel->clear(); + currentURL = KURL(); +} + +void KImageFilePreview::virtual_hook( int id, void* data ) +{ KPreviewWidgetBase::virtual_hook( id, data ); } + +#include "kimagefilepreview.moc" -- cgit v1.2.1