KoPictureFilePreview.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
3 Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "KoPictureFilePreview.h"
22#include "KoPictureFilePreview.moc"
23#include <kdialog.h>
24#include <tdelocale.h>
25#include <kurl.h>
26#include <tqbitmap.h>
27#include <tqlayout.h>
28#include <tqfileinfo.h>
29#include <tqpainter.h>
30#include <tqscrollview.h>
31
32#include <kdebug.h>
33
34#include <KoPicture.h>
35
41class KoPictureFilePreviewWidget : public TQScrollView
42{
43public:
44 KoPictureFilePreviewWidget( TQWidget *parent )
45 : TQScrollView( parent ) { viewport()->setBackgroundMode( PaletteBase ); }
46
47 bool setPicture( const KURL& url )
48 {
49 KoPicture picture;
50 if ( url.isLocalFile() )
51 {
52 if ( !picture.loadFromFile( url.path() ) )
53 {
54 return false;
55 }
56 }
57 else
58 {
59 // ### TODO: find a way to avoid to download the file again later
60 if ( !picture.setKeyAndDownloadPicture( url, this ) )
61 {
62 return false;
63 }
64 }
65 m_size = picture.getOriginalSize();
66 m_picture = picture;
67 resizeContents( m_size.width(), m_size.height() );
68 repaintContents();
69 return true;
70 }
71
72 void setNullPicture(void)
73 {
74 m_picture=KoPicture();
75 m_size=TQSize();
76 }
77
78 void drawContents( TQPainter *p, int, int, int, int )
79 {
80 p->setBackgroundColor( TQt::white );
81 // Be sure that the background is white (for transparency)
82 p->fillRect(0, 0, m_size.width(), m_size.height(), TQBrush( TQt::white ));
83 m_picture.draw( *p, 0 ,0, m_size.width(), m_size.height());
84 }
85
86private:
87 KoPicture m_picture;
88 TQSize m_size;
89};
90
91KoPictureFilePreview::KoPictureFilePreview( TQWidget *parent )
92 : KPreviewWidgetBase( parent )
93{
94 TQVBoxLayout *vb = new TQVBoxLayout( this, KDialog::marginHint() );
95 m_widget = new KoPictureFilePreviewWidget( this );
96 vb->addWidget( m_widget, 1 );
97}
98
99void KoPictureFilePreview::showPreview( const KURL &u )
100{
101 m_widget->setPicture( u );
102}
103
104void KoPictureFilePreview::clearPreview()
105{
106 m_widget->setNullPicture();
107}
108
110{
111 return i18n( "*.svg *.wmf *.qpic|Clipart (*.svg *.wmf *.qpic)" );
112}
113
115{
116 TQStringList lst;
117 lst << "image/svg+xml";
118 lst << "image/x-wmf";
119 lst << "image/x-vnd.trolltech.qpicture";
120 return lst;
121}
static int marginHint()
static TQStringList clipartMimeTypes()
static TQString clipartPattern()
KoPicture is a container class for various types of pictures supported by KOffice.
Definition KoPicture.h:44
bool setKeyAndDownloadPicture(const KURL &url, TQWidget *window)
Download and set the key for a possibly remote file.
bool loadFromFile(const TQString &fileName)
Load the picture from a file named fileName.
TQSize getOriginalSize(void) const