summaryrefslogtreecommitdiffstats
path: root/lib/kofficeui/KoPictureFilePreview.cpp
blob: 7f27b94028af5f7e3fcc3e190396cb6164b0f03f (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
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
   Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KoPictureFilePreview.h"
#include "KoPictureFilePreview.moc"
#include <kdialog.h>
#include <klocale.h>
#include <kurl.h>
#include <qbitmap.h>
#include <qlayout.h>
#include <qfileinfo.h>
#include <qpainter.h>
#include <qscrollview.h>

#include <kdebug.h>

#include <KoPicture.h>

/**
 * This class implements the actual widget that shows the image.
 * It is a scrollview, to have scrollbars if the image is big,
 * and it supports both pixmaps and cliparts
 */
class KoPictureFilePreviewWidget : public QScrollView
{
public:
    KoPictureFilePreviewWidget( QWidget *parent )
        : QScrollView( parent ) { viewport()->setBackgroundMode( PaletteBase ); }

    bool setPicture( const KURL& url )
    {
        KoPicture picture;
	if ( url.isLocalFile() )
	{
            if ( !picture.loadFromFile( url.path() ) )
	    {
	        return false;
	    }
	}
	else
	{
	    // ### TODO: find a way to avoid to download the file again later
            if ( !picture.setKeyAndDownloadPicture( url, this ) )
	    {
	        return false;
	    }
	}
        m_size = picture.getOriginalSize();
        m_picture = picture;
        resizeContents( m_size.width(), m_size.height() );
        repaintContents();
        return true;
    }

    void setNullPicture(void)
    {
        m_picture=KoPicture();
        m_size=QSize();
    }

    void drawContents( QPainter *p, int, int, int, int )
    {
        p->setBackgroundColor( Qt::white );
        // Be sure that the background is white (for transparency)
        p->fillRect(0, 0, m_size.width(), m_size.height(), QBrush( Qt::white ));
        m_picture.draw( *p, 0 ,0, m_size.width(), m_size.height());
    }

private:
    KoPicture m_picture;
    QSize m_size;
};

KoPictureFilePreview::KoPictureFilePreview( QWidget *parent )
    : KPreviewWidgetBase( parent )
{
    QVBoxLayout *vb = new QVBoxLayout( this, KDialog::marginHint() );
    m_widget = new KoPictureFilePreviewWidget( this );
    vb->addWidget( m_widget, 1 );
}

void KoPictureFilePreview::showPreview( const KURL &u )
{
    m_widget->setPicture( u );
}

void KoPictureFilePreview::clearPreview()
{
    m_widget->setNullPicture();
}

QString KoPictureFilePreview::clipartPattern()
{
    return i18n( "*.svg *.wmf *.qpic|Clipart (*.svg *.wmf *.qpic)" );
}

QStringList KoPictureFilePreview::clipartMimeTypes()
{
    QStringList lst;
    lst << "image/svg+xml";
    lst << "image/x-wmf";
    lst << "image/x-vnd.trolltech.qpicture";
    return lst;
}