~ubuntu-branches/ubuntu/hardy/krusader/hardy

« back to all changes in this revision

Viewing changes to krusader/KViewer/kimagefilepreview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Angel Ramos
  • Date: 2004-12-30 16:18:26 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20041230161826-wx99gkdypyalazpv
Tags: 1.51-1
* New upstream release (Closes: #280037, #287015).
* Moved from section utils to kde (Closes: #286748).
* Renamed dk.po to da.po (Closes: #269414).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* This file is part of the KDE project
 
3
* Copyright (C) 2001 Martin R. Jones <mjones@kde.org>
 
4
*               2001 Carsten Pfeiffer <pfeiffer@kde.org>
 
5
*
 
6
* You can Freely distribute this program under the GNU Library General Public
 
7
* License. See the file "COPYING" for the exact licensing terms.
 
8
*/
 
9
 
 
10
#include <qlayout.h>
 
11
#include <qlabel.h>
 
12
#include <qcombobox.h>
 
13
#include <qcheckbox.h>
 
14
#include <qwhatsthis.h>
 
15
#include <qtimer.h>
 
16
 
 
17
#include <kapplication.h>
 
18
#include <kglobal.h>
 
19
#include <kiconloader.h>
 
20
#include <kpushbutton.h>
 
21
#include <kstandarddirs.h>
 
22
#include <kdebug.h>
 
23
#include <klocale.h>
 
24
#include <kfiledialog.h>
 
25
#include <kfileitem.h>
 
26
#include <kio/previewjob.h>
 
27
 
 
28
#include "kimagefilepreview.h"
 
29
 
 
30
/**** KImageFilePreview ****/
 
31
 
 
32
KImageFilePreview::KImageFilePreview( QWidget *parent )
 
33
                : KPreviewWidgetBase( parent ),
 
34
m_job( 0L ) {
 
35
        QVBoxLayout *vb = new QVBoxLayout( this, KDialog::marginHint() );
 
36
 
 
37
        imageLabel = new QLabel( this );
 
38
        imageLabel->setFrameStyle( QFrame::Panel | QFrame::Sunken );
 
39
        imageLabel->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
 
40
        imageLabel->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Ignored ) );
 
41
        vb->addWidget( imageLabel, 1 );
 
42
 
 
43
        timer = new QTimer( this );
 
44
        connect( timer, SIGNAL( timeout() ), SLOT( showPreview() ) );
 
45
 
 
46
        setSupportedMimeTypes( KIO::PreviewJob::supportedMimeTypes() );
 
47
}
 
48
 
 
49
KImageFilePreview::~KImageFilePreview() {
 
50
        if ( m_job )
 
51
                m_job->kill();
 
52
}
 
53
 
 
54
void KImageFilePreview::showPreview() {
 
55
        // Pass a copy since clearPreview() will clear currentURL
 
56
        KURL url = currentURL;
 
57
        showPreview( url, true );
 
58
}
 
59
 
 
60
// called via KPreviewWidgetBase interface
 
61
void KImageFilePreview::showPreview( const KURL& url ) {
 
62
        showPreview( url, false );
 
63
}
 
64
 
 
65
void KImageFilePreview::showPreview( const KURL &url, bool force ) {
 
66
        if ( !url.isValid() ) {
 
67
                clearPreview();
 
68
                return ;
 
69
        }
 
70
 
 
71
        if ( url != currentURL || force ) {
 
72
                clearPreview();
 
73
                currentURL = url;
 
74
 
 
75
                int w = imageLabel->contentsRect().width() - 4;
 
76
                int h = imageLabel->contentsRect().height() - 4;
 
77
 
 
78
                m_job = createJob( url, w, h );
 
79
                connect( m_job, SIGNAL( result( KIO::Job * ) ),
 
80
                         this, SLOT( slotResult( KIO::Job * ) ) );
 
81
                connect( m_job, SIGNAL( gotPreview( const KFileItem*,
 
82
                                                    const QPixmap& ) ),
 
83
                         SLOT( gotPreview( const KFileItem*, const QPixmap& ) ) );
 
84
 
 
85
                connect( m_job, SIGNAL( failed( const KFileItem* ) ),
 
86
                         this, SLOT( slotFailed( const KFileItem* ) ) );
 
87
        }
 
88
}
 
89
 
 
90
void KImageFilePreview::resizeEvent( QResizeEvent * ) {
 
91
        timer->start( 100, true ); // forces a new preview
 
92
}
 
93
 
 
94
QSize KImageFilePreview::sizeHint() const {
 
95
        return QSize( 20, 200 ); // otherwise it ends up huge???
 
96
}
 
97
 
 
98
KIO::PreviewJob * KImageFilePreview::createJob( const KURL& url, int w, int h ) {
 
99
        KURL::List urls;
 
100
        urls.append( url );
 
101
        return KIO::filePreview( urls, w, h, 0, 0, true, false );
 
102
}
 
103
 
 
104
void KImageFilePreview::gotPreview( const KFileItem* item, const QPixmap& pm ) {
 
105
        if ( item->url() == currentURL )   // should always be the case
 
106
                imageLabel->setPixmap( pm );
 
107
}
 
108
 
 
109
void KImageFilePreview::slotFailed( const KFileItem* item ) {
 
110
        if ( item->isDir() )
 
111
                imageLabel->clear();
 
112
        else if ( item->url() == currentURL )   // should always be the case
 
113
                imageLabel->setPixmap( SmallIcon( "file_broken", KIcon::SizeLarge,
 
114
                                                  KIcon::DisabledState ) );
 
115
}
 
116
 
 
117
void KImageFilePreview::slotResult( KIO::Job *job ) {
 
118
        if ( job == m_job )
 
119
                m_job = 0L;
 
120
}
 
121
 
 
122
void KImageFilePreview::clearPreview() {
 
123
        if ( m_job ) {
 
124
                m_job->kill();
 
125
                m_job = 0L;
 
126
        }
 
127
 
 
128
        imageLabel->clear();
 
129
        currentURL = KURL();
 
130
}
 
131
 
 
132
void KImageFilePreview::virtual_hook( int id, void* data ) {
 
133
        KPreviewWidgetBase::virtual_hook( id, data );
 
134
}
 
135
 
 
136
#include "kimagefilepreview.moc"