summaryrefslogtreecommitdiffstats
path: root/krusader/KViewer/kimagefilepreview.cpp
blob: ebc428a070af0d67f0d19230f271746373d56dd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* This file is part of the KDE project
* Copyright (C) 2001 Martin R. Jones <mjones@kde.org>
*               2001 Carsten Pfeiffer <pfeiffer@kde.org>
*
* You can Freely distribute this program under the GNU Library General Public
* License. See the file "COPYING" for the exact licensing terms.
*/

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqcheckbox.h>
#include <tqwhatsthis.h>
#include <tqtimer.h>

#include <kapplication.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>
#include <kdebug.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <kfileitem.h>
#include <kio/previewjob.h>

#include "kimagefilepreview.h"

/**** KrusaderImageFilePreview ****/

KrusaderImageFilePreview::KrusaderImageFilePreview( TQWidget *parent )
		: KPreviewWidgetBase( parent ),
m_job( 0L ) {
	TQVBoxLayout *vb = new TQVBoxLayout( this, KDialog::marginHint() );

	imageLabel = new TQLabel( this );
	imageLabel->setFrameStyle( TQFrame::Panel | TQFrame::Sunken );
	imageLabel->setAlignment( TQt::AlignHCenter | TQt::AlignVCenter );
	imageLabel->setSizePolicy( TQSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Ignored ) );
	vb->addWidget( imageLabel, 1 );

	timer = new TQTimer( this );
	connect( timer, TQT_SIGNAL( timeout() ), TQT_SLOT( showPreview() ) );

	setSupportedMimeTypes( KIO::PreviewJob::supportedMimeTypes() );
}

KrusaderImageFilePreview::~KrusaderImageFilePreview() {
	if ( m_job )
		m_job->kill();
}

void KrusaderImageFilePreview::showPreview() {
	// Pass a copy since clearPreview() will clear currentURL
	KURL url = currentURL;
	showPreview( url, true );
}

// called via KPreviewWidgetBase interface
void KrusaderImageFilePreview::showPreview( const KURL& url ) {
	showPreview( url, false );
}

void KrusaderImageFilePreview::showPreview( const KURL &url, bool force ) {
	if ( !url.isValid() ) {
		clearPreview();
		return ;
	}

	if ( url != currentURL || force ) {
		clearPreview();
		currentURL = url;

		int w = imageLabel->contentsRect().width() - 4;
		int h = imageLabel->contentsRect().height() - 4;

		m_job = createJob( url, w, h );
		connect( m_job, TQT_SIGNAL( result( KIO::Job * ) ),
		         this, TQT_SLOT( slotResult( KIO::Job * ) ) );
		connect( m_job, TQT_SIGNAL( gotPreview( const KFileItem*,
		                                    const TQPixmap& ) ),
		         TQT_SLOT( gotPreview( const KFileItem*, const TQPixmap& ) ) );

		connect( m_job, TQT_SIGNAL( failed( const KFileItem* ) ),
		         this, TQT_SLOT( slotFailed( const KFileItem* ) ) );
	}
}

void KrusaderImageFilePreview::resizeEvent( TQResizeEvent * ) {
	timer->start( 100, true ); // forces a new preview
}

TQSize KrusaderImageFilePreview::sizeHint() const {
	return TQSize( 20, 200 ); // otherwise it ends up huge???
}

KIO::PreviewJob * KrusaderImageFilePreview::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 KrusaderImageFilePreview::gotPreview( const KFileItem* item, const TQPixmap& pm ) {
	if ( item->url() == currentURL )   // should always be the case
		imageLabel->setPixmap( pm );
}

void KrusaderImageFilePreview::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 KrusaderImageFilePreview::slotResult( KIO::Job *job ) {
	if ( job == m_job )
		m_job = 0L;
}

void KrusaderImageFilePreview::clearPreview() {
	if ( m_job ) {
		m_job->kill();
		m_job = 0L;
	}

	imageLabel->clear();
	currentURL = KURL();
}

void KrusaderImageFilePreview::virtual_hook( int id, void* data ) {
	KPreviewWidgetBase::virtual_hook( id, data );
}

#include "kimagefilepreview.moc"