kword

KWInsertPicDia.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C)  2001 David Faure <faure@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 <qpainter.h>
00022 #include <qpushbutton.h>
00023 #include <qbitmap.h>
00024 #include <qlayout.h>
00025 #include <qcheckbox.h>
00026 #include <qscrollview.h>
00027 
00028 #include <kdebug.h>
00029 #include <klocale.h>
00030 #include <kfiledialog.h>
00031 #include <kimageio.h>
00032 #include <kio/netaccess.h>
00033 
00034 #include <KoPicture.h>
00035 #include <KoPictureFilePreview.h>
00036 
00037 #include "KWDocument.h"
00038 #include "KWInsertPicDia.h"
00039 #include <qtimer.h>
00040 
00046 class KWInsertPicPreview : public QScrollView
00047 {
00048 public:
00049     KWInsertPicPreview( QWidget *parent )
00050         : QScrollView( parent )
00051     {
00052         viewport()->setBackgroundMode( PaletteBase );
00053         setMinimumSize( 300, 200 );
00054     }
00055 
00056     virtual ~KWInsertPicPreview() {}
00057 
00058     bool setPicture( const KoPicture & picture )
00059     {
00060         if (!picture.isNull())
00061         {
00062             m_size = picture.getOriginalSize();
00063             m_picture = picture;
00064             resizeContents( m_size.width(), m_size.height() );
00065             repaint( false );
00066             return true;
00067         }
00068         else
00069             return false;
00070     }
00071 
00072     void drawContents( QPainter *p, int, int, int, int )
00073     {
00074         p->setBackgroundColor( Qt::white );
00075         // Be sure that the background is white (for transparency)
00076         p->fillRect(0, 0, m_size.width(), m_size.height(), QBrush( Qt::white ));
00077         m_picture.draw( *p, 0 ,0, m_size.width(), m_size.height());
00078     }
00079 
00080 private:
00081     KoPicture m_picture;
00082     QSize m_size;
00083 };
00084 
00086 
00087 KWInsertPicDia::KWInsertPicDia( QWidget *parent, bool _inline, bool _keepRatio, KWDocument *_doc, const char *name )
00088     : KDialogBase( Plain, i18n("Insert Picture"), Ok|Cancel, Ok, parent, name, true ),
00089     m_bFirst ( true ), m_doc ( _doc )
00090 {
00091     setInitialSize( QSize(400, 300) );
00092     QWidget *page = plainPage();
00093     QGridLayout *grid = new QGridLayout( page, 4, 2, KDialog::marginHint(), KDialog::spacingHint() );
00094 
00095     QPushButton *pbImage = new QPushButton( i18n( "Choose &Picture..." ), page );
00096     grid->addWidget( pbImage, 0, 0 );
00097     connect( pbImage, SIGNAL( clicked() ), SLOT( slotChooseImage() ) );
00098 
00099     m_cbInline = new QCheckBox( i18n( "Insert picture inline" ), page );
00100     grid->addWidget( m_cbInline, 1, 0 );
00101 
00102     m_cbKeepRatio= new QCheckBox( i18n("Retain original aspect ratio"),page);
00103     grid->addWidget( m_cbKeepRatio, 2, 0);
00104 
00105     m_preview = new KWInsertPicPreview( page );
00106     grid->addMultiCellWidget( m_preview, 0, 3, 1, 1 );
00107 
00108     // Stretch the buttons and checkboxes a little, but stretch the preview much more
00109     grid->setRowStretch( 0, 1 );
00110     grid->setRowStretch( 1, 1 );
00111     grid->setRowStretch( 2, 1 );
00112     grid->setRowStretch( 3, 10 );
00113     grid->setColStretch( 0, 1 );
00114     grid->setColStretch( 1, 10 );
00115     m_cbKeepRatio->setChecked(_keepRatio);
00116     m_cbInline->setChecked( _inline );
00117     enableButtonOK( false );
00118     setFocus();
00119     slotChooseImage(); // save the user time, directly open the dialog
00120 }
00121 
00122 bool KWInsertPicDia::makeInline() const
00123 {
00124     return m_cbInline->isChecked();
00125 }
00126 
00127 bool KWInsertPicDia::keepRatio() const
00128 {
00129     return m_cbKeepRatio->isChecked();
00130 }
00131 
00132 void KWInsertPicDia::slotChooseImage()
00133 {
00134     KoPicture tmppicture = KWInsertPicDia::selectPictureDia( ":picture", this );
00135     if ( !tmppicture.isNull() ) // if canceled, keep current picture (#72827)
00136         m_picture = tmppicture;
00137     if ( m_picture.isNull() && m_bFirst)
00138     {
00139         kdDebug() << "KWInsertPicDia::slotChooseImage cancelled by user." << endl;
00140         // Close, but delayed, otherwise it won't work (we only return from the ctor)
00141         QTimer::singleShot( 0, this, SLOT( cancel() ) );
00142         return;
00143     }
00144     enableButtonOK ( m_preview->setPicture( m_picture ) );
00145     m_bFirst = false;
00146 }
00147 
00148 KoPicture KWInsertPicDia::selectPictureDia( const QString & _path, QWidget* parent )
00149 {
00150     QStringList mimetypes ( KImageIO::mimeTypes( KImageIO::Reading ) );
00151     mimetypes += KoPictureFilePreview::clipartMimeTypes();
00152 
00153     KFileDialog fd( _path, QString::null, parent, 0, TRUE );
00154     fd.setMimeFilter( mimetypes );
00155     fd.setCaption(i18n("Choose Picture"));
00156     return selectPicture( fd, parent );
00157 }
00158 
00159 KoPicture KWInsertPicDia::selectPicture( KFileDialog & fd, QWidget *parent )
00160 {
00161     KoPicture picture;
00162 
00163     fd.setPreviewWidget( new KoPictureFilePreview( &fd ) );
00164     KURL url;
00165     if ( fd.exec() == QDialog::Accepted )
00166         url = fd.selectedURL();
00167 
00168     if( !url.isEmpty() )
00169         picture.setKeyAndDownloadPicture( url, parent );
00170 
00171     return picture;
00172 }
00173 
00174 KoPicture KWInsertPicDia::picture ( void ) const
00175 {
00176     kdDebug() << m_picture.getKey().toString() << " selected in KWInsertPicDia" << endl;
00177     return m_picture;
00178 }
00179 
00180 #include "KWInsertPicDia.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys