kaddressbook Library API Documentation

imagewidget.cpp

00001 /*                                                                      
00002     This file is part of KAddressBook.                                  
00003     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>                   
00004                                                                         
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or   
00008     (at your option) any later version.                                 
00009                                                                         
00010     This program 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        
00013     GNU General Public License for more details.                        
00014                                                                         
00015     You should have received a copy of the GNU General Public License   
00016     along with this program; if not, write to the Free Software         
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           
00018                                                                         
00019     As a special exception, permission is given to link this program    
00020     with any edition of Qt, and distribute the resulting executable,    
00021     without including the source code for Qt in the source distribution.
00022 */                                                                      
00023 
00024 #include <kabc/picture.h>
00025 #include <kdebug.h>
00026 #include <kdialog.h>
00027 #include <kglobalsettings.h>
00028 #include <kiconloader.h>
00029 #include <kio/netaccess.h>
00030 #include <klocale.h>
00031 #include <kurlrequester.h>
00032 #include <kimageio.h>
00033 
00034 #include <qcheckbox.h>
00035 #include <qdragobject.h>
00036 #include <qgroupbox.h>
00037 #include <qlabel.h>
00038 #include <qlayout.h>
00039 #include <qpixmap.h>
00040 
00041 #include "imagewidget.h"
00042 
00043 ImageLabel::ImageLabel( const QString &title, QWidget *parent )
00044   : QLabel( title, parent ), mReadOnly( false )
00045 {
00046   setAcceptDrops( true );
00047 }
00048 
00049 void ImageLabel::setReadOnly( bool readOnly )
00050 {
00051   mReadOnly = readOnly;
00052 }
00053 
00054 void ImageLabel::startDrag()
00055 {
00056   if ( pixmap() && !pixmap()->isNull() ) {
00057     QImageDrag *drag = new QImageDrag( pixmap()->convertToImage(), this );
00058     drag->dragCopy();
00059   }
00060 }
00061 
00062 void ImageLabel::dragEnterEvent( QDragEnterEvent *event )
00063 {
00064   event->accept( QImageDrag::canDecode( event ) );
00065 }
00066 
00067 void ImageLabel::dropEvent( QDropEvent *event )
00068 {
00069   QPixmap pm;
00070   if ( QImageDrag::decode( event, pm ) && !mReadOnly ) {
00071     setPixmap( pm );
00072     emit changed();
00073   }
00074 }
00075 
00076 void ImageLabel::mousePressEvent( QMouseEvent *event )
00077 {
00078   mDragStartPos = event->pos();
00079   QLabel::mousePressEvent( event );
00080 }
00081 
00082 void ImageLabel::mouseMoveEvent( QMouseEvent *event )
00083 {
00084   if ( (event->state() & LeftButton) &&
00085        (event->pos() - mDragStartPos).manhattanLength() >
00086        KGlobalSettings::dndEventDelay() ) {
00087     startDrag();
00088   }
00089 }
00090 
00091 
00092 ImageWidget::ImageWidget( const QString &title, QWidget *parent, const char *name )
00093   : QWidget( parent, name ), mReadOnly( false )
00094 {
00095   QHBoxLayout *topLayout = new QHBoxLayout( this, KDialog::marginHint(),
00096                                             KDialog::spacingHint() );
00097   QGroupBox *box = new QGroupBox( 0, Qt::Vertical, title, this );
00098   QGridLayout *boxLayout = new QGridLayout( box->layout(), 4, 2,
00099                                             KDialog::spacingHint() );
00100   boxLayout->setRowStretch( 3, 1 );
00101 
00102   mImageLabel = new ImageLabel( i18n( "Picture" ), box );
00103   mImageLabel->setFixedSize( 50, 70 );
00104   mImageLabel->setScaledContents( true );
00105   mImageLabel->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00106   boxLayout->addMultiCellWidget( mImageLabel, 0, 2, 0, 0, AlignTop );
00107 
00108   mImageUrl = new KURLRequester( box );
00109   mImageUrl->setFilter( KImageIO::pattern() );
00110   mImageUrl->setMode( KFile::File );
00111   boxLayout->addWidget( mImageUrl, 0, 1 );
00112 
00113   mUseImageUrl = new QCheckBox( i18n( "Store as URL" ), box );
00114   mUseImageUrl->setEnabled( false );
00115   boxLayout->addWidget( mUseImageUrl, 1, 1 );
00116 
00117   mClearButton = new QPushButton( i18n( "Clear" ), box );
00118   mClearButton->setEnabled( false );
00119   boxLayout->addMultiCellWidget( mClearButton, 3, 3, 0, 1 );
00120 
00121   topLayout->addWidget( box );
00122 
00123   connect( mImageLabel, SIGNAL( changed() ),
00124            SLOT( imageChanged() ) );
00125   connect( mImageUrl, SIGNAL( textChanged( const QString& ) ),
00126            SIGNAL( changed() ) );
00127   connect( mImageUrl, SIGNAL( urlSelected( const QString& ) ),
00128            SLOT( loadImage() ) );
00129   connect( mImageUrl, SIGNAL( urlSelected( const QString& ) ),
00130            SIGNAL( changed() ) );
00131   connect( mImageUrl, SIGNAL( urlSelected( const QString& ) ),
00132            SLOT( updateGUI() ) );
00133   connect( mUseImageUrl, SIGNAL( toggled( bool ) ),
00134            SIGNAL( changed() ) );
00135   connect( mClearButton, SIGNAL( clicked() ),
00136            SLOT( clear() ) );
00137 }
00138 
00139 ImageWidget::~ImageWidget()
00140 {
00141 }
00142 
00143 void ImageWidget::setReadOnly( bool readOnly )
00144 {
00145   mReadOnly = readOnly;
00146   mImageLabel->setReadOnly( mReadOnly );
00147   mImageUrl->setEnabled( !mReadOnly );
00148 }
00149 
00150 void ImageWidget::setImage( const KABC::Picture &photo )
00151 {
00152   bool blocked = signalsBlocked();
00153   blockSignals( true );
00154 
00155   if ( photo.isIntern() ) {
00156     mImageLabel->setPixmap( photo.data() );
00157     mUseImageUrl->setChecked( false );
00158   } else {
00159     mImageUrl->setURL( photo.url() );
00160     if ( !photo.url().isEmpty() )
00161       mUseImageUrl->setChecked( true );
00162     loadImage();
00163   }
00164 
00165   blockSignals( blocked );
00166 
00167   updateGUI();
00168 }
00169 
00170 KABC::Picture ImageWidget::image() const
00171 {
00172   KABC::Picture photo;
00173 
00174   if ( mUseImageUrl->isChecked() )
00175     photo.setUrl( mImageUrl->url() );
00176   else {
00177     QPixmap *px = mImageLabel->pixmap();
00178     if ( px ) {
00179       if ( px->height() > px->width() )
00180         photo.setData( px->convertToImage().scaleHeight( 140 ) );
00181       else
00182         photo.setData( px->convertToImage().scaleWidth( 100 ) );
00183 
00184       photo.setType( "PNG" );
00185     }
00186   }
00187 
00188   return photo;
00189 }
00190 
00191 void ImageWidget::loadImage()
00192 {
00193   mImageLabel->setPixmap( loadPixmap( KURL( mImageUrl->url() ) ) );
00194 }
00195 
00196 void ImageWidget::updateGUI()
00197 {
00198   if ( !mReadOnly ) {
00199     mUseImageUrl->setEnabled( !mImageUrl->url().isEmpty() );
00200     mClearButton->setEnabled( !mImageUrl->url().isEmpty() || ( mImageLabel->pixmap() && !mImageLabel->pixmap()->isNull() ) );
00201   }
00202 }
00203 
00204 void ImageWidget::clear()
00205 {
00206   mImageLabel->clear();
00207   mImageUrl->clear();
00208   mUseImageUrl->setChecked( false );
00209 
00210   updateGUI();
00211 
00212   emit changed();
00213 }
00214 
00215 void ImageWidget::imageChanged()
00216 {
00217   updateGUI();
00218   
00219   emit changed();
00220 }
00221 
00222 QPixmap ImageWidget::loadPixmap( const KURL &url )
00223 {
00224   QString tempFile;
00225   QPixmap pixmap;
00226 
00227   if ( url.isEmpty() )
00228     return pixmap;
00229 
00230   if ( url.isLocalFile() )
00231     pixmap = QPixmap( url.path() );
00232   else if ( KIO::NetAccess::download( url, tempFile, this ) ) {
00233     pixmap = QPixmap( tempFile );
00234     KIO::NetAccess::removeTempFile( tempFile );
00235   }
00236 
00237   return pixmap;
00238 }
00239 
00240 #include "imagewidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:51 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003