kaddressbook

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <kfiledialog.h>
00028 #include <kglobalsettings.h>
00029 #include <kiconloader.h>
00030 #include <kimageio.h>
00031 #include <kio/netaccess.h>
00032 #include <klocale.h>
00033 #include <kmessagebox.h>
00034 #include <kurldrag.h>
00035 #include <libkdepim/kpixmapregionselectordialog.h>
00036 
00037 #include <qapplication.h>
00038 #include <qdragobject.h>
00039 #include <qeventloop.h>
00040 #include <qgroupbox.h>
00041 #include <qlabel.h>
00042 #include <qlayout.h>
00043 #include <qpixmap.h>
00044 #include <qpopupmenu.h>
00045 
00046 #include <unistd.h>
00047 
00048 #include "imagewidget.h"
00049 
00050 ImageLoader::ImageLoader( QWidget *parent )
00051   : QObject( 0, "ImageLoader" ), mParent( parent )
00052 {
00053 }
00054 
00055 KABC::Picture ImageLoader::loadPicture( const KURL &url, bool *ok )
00056 {
00057   KABC::Picture picture;
00058   QString tempFile;
00059 
00060   if ( url.isEmpty() )
00061     return picture;
00062 
00063   (*ok) = false;
00064 
00065   QImage image;
00066   if ( url.isLocalFile() ) {
00067     image.load( url.path() );
00068     picture.setData( image );
00069     (*ok) = true;
00070   } else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
00071     image.load( tempFile );
00072     picture.setData( image );
00073     (*ok) = true;
00074     KIO::NetAccess::removeTempFile( tempFile );
00075   }
00076 
00077   if ( !(*ok) ) {
00078     // image does not exist (any more)
00079     KMessageBox::sorry( mParent, i18n( "This contact's image cannot be found." ) );
00080     return picture;
00081   }
00082 
00083   QPixmap pixmap = picture.data();
00084 
00085   QPixmap selectedPixmap = KPIM::KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 100, 140, mParent );
00086   if ( selectedPixmap.isNull() ) {
00087     (*ok) = false;
00088     return picture;
00089   }
00090 
00091   image = selectedPixmap;
00092   if ( image.height() != 140 || image.width() != 100 ) {
00093     if ( image.height() > image.width() )
00094       image = image.scaleHeight( 140 );
00095     else
00096       image = image.scaleWidth( 100 );
00097   }
00098 
00099   picture.setData( image );
00100   (*ok) = true;
00101 
00102   return picture;
00103 }
00104 
00105 
00106 ImageButton::ImageButton( const QString &title, QWidget *parent )
00107   : QPushButton( title, parent ),
00108     mReadOnly( false ), mImageLoader( 0 )
00109 {
00110   setAcceptDrops( true );
00111 
00112   connect( this, SIGNAL( clicked() ), SLOT( load() ) );
00113 }
00114 
00115 void ImageButton::setReadOnly( bool readOnly )
00116 {
00117   mReadOnly = readOnly;
00118 }
00119 
00120 void ImageButton::setPicture( const KABC::Picture &picture )
00121 {
00122   mPicture = picture;
00123   updateGUI();
00124 }
00125 
00126 KABC::Picture ImageButton::picture() const
00127 {
00128   return mPicture;
00129 }
00130 
00131 void ImageButton::setImageLoader( ImageLoader *loader )
00132 {
00133   mImageLoader = loader;
00134 }
00135 
00136 void ImageButton::startDrag()
00137 {
00138   if ( !mPicture.data().isNull() ) {
00139     QImageDrag *drag = new QImageDrag( mPicture.data(), this );
00140     drag->dragCopy();
00141   }
00142 }
00143 
00144 void ImageButton::updateGUI()
00145 {
00146   if ( mPicture.data().isNull() )
00147     setPixmap( KGlobal::iconLoader()->iconPath( "personal", KIcon::Desktop ) );
00148   else
00149     setPixmap( mPicture.data() );
00150 }
00151 
00152 void ImageButton::dragEnterEvent( QDragEnterEvent *event )
00153 {
00154   bool accepted = false;
00155 
00156   if ( QImageDrag::canDecode( event ) )
00157     accepted = true;
00158 
00159   if ( QUriDrag::canDecode( event ) )
00160     accepted = true;
00161 
00162   event->accept( accepted );
00163 }
00164 
00165 void ImageButton::dropEvent( QDropEvent *event )
00166 {
00167   if ( mReadOnly )
00168     return;
00169 
00170   if ( QImageDrag::canDecode( event ) ) {
00171     QPixmap pm;
00172 
00173     if ( QImageDrag::decode( event, pm ) ) {
00174       mPicture.setData( pm.convertToImage() );
00175       updateGUI();
00176       emit changed();
00177     }
00178   }
00179 
00180   if ( QUriDrag::canDecode( event ) ) {
00181     KURL::List urls;
00182     if ( KURLDrag::decode( event, urls ) ) {
00183       if ( urls.isEmpty() ) { // oops, no data
00184         event->accept( false );
00185         return;
00186       }
00187     }
00188 
00189     if ( mImageLoader ) {
00190       bool ok = false;
00191       KABC::Picture pic = mImageLoader->loadPicture( urls[ 0 ], &ok );
00192       if ( ok ) {
00193         mPicture = pic;
00194         updateGUI();
00195         emit changed();
00196       }
00197     }
00198   }
00199 }
00200 
00201 void ImageButton::mousePressEvent( QMouseEvent *event )
00202 {
00203   mDragStartPos = event->pos();
00204   QPushButton::mousePressEvent( event );
00205 }
00206 
00207 void ImageButton::mouseMoveEvent( QMouseEvent *event )
00208 {
00209   if ( (event->state() & LeftButton) &&
00210        (event->pos() - mDragStartPos).manhattanLength() >
00211        KGlobalSettings::dndEventDelay() ) {
00212     startDrag();
00213   }
00214 }
00215 
00216 void ImageButton::contextMenuEvent( QContextMenuEvent *event )
00217 {
00218   QPopupMenu menu( this );
00219   menu.insertItem( i18n( "Reset" ), this, SLOT( clear() ) );
00220   menu.exec( event->globalPos() );
00221 }
00222 
00223 void ImageButton::load()
00224 {
00225   KURL url = KFileDialog::getOpenURL( QString(), KImageIO::pattern(), this );
00226   if ( url.isValid() ) {
00227     if ( mImageLoader ) {
00228       bool ok = false;
00229       KABC::Picture pic = mImageLoader->loadPicture( url, &ok );
00230       if ( ok ) {
00231         mPicture = pic;
00232         updateGUI();
00233         emit changed();
00234       }
00235     }
00236   }
00237 }
00238 
00239 void ImageButton::clear()
00240 {
00241   mPicture = KABC::Picture();
00242   updateGUI();
00243 
00244   emit changed();
00245 }
00246 
00247 ImageBaseWidget::ImageBaseWidget( const QString &title,
00248                                   QWidget *parent, const char *name )
00249   : QWidget( parent, name ), mReadOnly( false )
00250 {
00251   mImageLoader = new ImageLoader( this );
00252 
00253   QVBoxLayout *topLayout = new QVBoxLayout( this, KDialog::marginHint(),
00254                                             KDialog::spacingHint() );
00255   QGroupBox *box = new QGroupBox( 0, Qt::Vertical, title, this );
00256   QVBoxLayout *layout = new QVBoxLayout( box->layout(), KDialog::spacingHint() );
00257 
00258   mImageButton = new ImageButton( i18n( "Picture" ), box );
00259   mImageButton->setFixedSize( 100, 140 );
00260   mImageButton->setImageLoader( mImageLoader );
00261   layout->addWidget( mImageButton );
00262 
00263   topLayout->addWidget( box );
00264 
00265   connect( mImageButton, SIGNAL( changed() ), SIGNAL( changed() ) );
00266 }
00267 
00268 ImageBaseWidget::~ImageBaseWidget()
00269 {
00270   delete mImageLoader;
00271   mImageLoader = 0;
00272 }
00273 
00274 void ImageBaseWidget::setReadOnly( bool readOnly )
00275 {
00276   mReadOnly = readOnly;
00277   mImageButton->setReadOnly( mReadOnly );
00278 }
00279 
00280 void ImageBaseWidget::setImage( const KABC::Picture &photo )
00281 {
00282   mImageButton->setPicture( photo );
00283 }
00284 
00285 KABC::Picture ImageBaseWidget::image() const
00286 {
00287   return mImageButton->picture();
00288 }
00289 
00290 
00291 ImageWidget::ImageWidget( KABC::AddressBook *ab, QWidget *parent, const char *name )
00292   : KAB::ContactEditorWidget( ab, parent, name )
00293 {
00294   QHBoxLayout *layout = new QHBoxLayout( this, KDialog::marginHint(),
00295                                          KDialog::spacingHint() );
00296 
00297   mPhotoWidget = new ImageBaseWidget( KABC::Addressee::photoLabel(), this );
00298   layout->addWidget( mPhotoWidget );
00299 
00300   mLogoWidget = new ImageBaseWidget( KABC::Addressee::logoLabel(), this );
00301   layout->addWidget( mLogoWidget );
00302 
00303   connect( mPhotoWidget, SIGNAL( changed() ), SLOT( setModified() ) );
00304   connect( mLogoWidget, SIGNAL( changed() ), SLOT( setModified() ) );
00305 }
00306 
00307 void ImageWidget::loadContact( KABC::Addressee *addr )
00308 {
00309   mPhotoWidget->setImage( addr->photo() );
00310   mLogoWidget->setImage( addr->logo() );
00311 }
00312 
00313 void ImageWidget::storeContact( KABC::Addressee *addr )
00314 {
00315   addr->setPhoto( mPhotoWidget->image() );
00316   addr->setLogo( mLogoWidget->image() );
00317 }
00318 
00319 void ImageWidget::setReadOnly( bool readOnly )
00320 {
00321   mPhotoWidget->setReadOnly( readOnly );
00322   mLogoWidget->setReadOnly( readOnly );
00323 }
00324 
00325 #include "imagewidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys