lib

KoPictureFilePreview.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
00003    Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "KoPictureFilePreview.h"
00022 #include "KoPictureFilePreview.moc"
00023 #include <kdialog.h>
00024 #include <klocale.h>
00025 #include <kurl.h>
00026 #include <qbitmap.h>
00027 #include <qlayout.h>
00028 #include <qfileinfo.h>
00029 #include <qpainter.h>
00030 #include <qscrollview.h>
00031 
00032 #include <kdebug.h>
00033 
00034 #include <KoPicture.h>
00035 
00041 class KoPictureFilePreviewWidget : public QScrollView
00042 {
00043 public:
00044     KoPictureFilePreviewWidget( QWidget *parent )
00045         : QScrollView( parent ) { viewport()->setBackgroundMode( PaletteBase ); }
00046 
00047     bool setPicture( const KURL& url )
00048     {
00049         KoPicture picture;
00050     if ( url.isLocalFile() )
00051     {
00052             if ( !picture.loadFromFile( url.path() ) )
00053         {
00054             return false;
00055         }
00056     }
00057     else
00058     {
00059         // ### TODO: find a way to avoid to download the file again later
00060             if ( !picture.setKeyAndDownloadPicture( url, this ) )
00061         {
00062             return false;
00063         }
00064     }
00065         m_size = picture.getOriginalSize();
00066         m_picture = picture;
00067         resizeContents( m_size.width(), m_size.height() );
00068         repaintContents();
00069         return true;
00070     }
00071 
00072     void setNullPicture(void)
00073     {
00074         m_picture=KoPicture();
00075         m_size=QSize();
00076     }
00077 
00078     void drawContents( QPainter *p, int, int, int, int )
00079     {
00080         p->setBackgroundColor( Qt::white );
00081         // Be sure that the background is white (for transparency)
00082         p->fillRect(0, 0, m_size.width(), m_size.height(), QBrush( Qt::white ));
00083         m_picture.draw( *p, 0 ,0, m_size.width(), m_size.height());
00084     }
00085 
00086 private:
00087     KoPicture m_picture;
00088     QSize m_size;
00089 };
00090 
00091 KoPictureFilePreview::KoPictureFilePreview( QWidget *parent )
00092     : KPreviewWidgetBase( parent )
00093 {
00094     QVBoxLayout *vb = new QVBoxLayout( this, KDialog::marginHint() );
00095     m_widget = new KoPictureFilePreviewWidget( this );
00096     vb->addWidget( m_widget, 1 );
00097 }
00098 
00099 void KoPictureFilePreview::showPreview( const KURL &u )
00100 {
00101     m_widget->setPicture( u );
00102 }
00103 
00104 void KoPictureFilePreview::clearPreview()
00105 {
00106     m_widget->setNullPicture();
00107 }
00108 
00109 QString KoPictureFilePreview::clipartPattern()
00110 {
00111     return i18n( "*.svg *.wmf *.qpic|Clipart (*.svg *.wmf *.qpic)" );
00112 }
00113 
00114 QStringList KoPictureFilePreview::clipartMimeTypes()
00115 {
00116     QStringList lst;
00117     lst << "image/svg+xml";
00118     lst << "image/x-wmf";
00119     lst << "image/x-vnd.trolltech.qpicture";
00120     return lst;
00121 }
KDE Home | KDE Accessibility Home | Description of Access Keys