• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi/contact

imagewidget.cpp

00001 /*
00002     This file is part of Akonadi Contact.
00003 
00004     Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or modify it
00007     under the terms of the GNU Library General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or (at your
00009     option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful, but WITHOUT
00012     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014     License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to the
00018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301, USA.
00020 */
00021 
00022 #include "imagewidget.h"
00023 
00024 #include <kabc/addressee.h>
00025 #include <kfiledialog.h>
00026 #include <kglobalsettings.h>
00027 #include <kicon.h>
00028 #include <kimageio.h>
00029 #include <kio/netaccess.h>
00030 #include <klocale.h>
00031 #include <kmessagebox.h>
00032 #include <kpixmapregionselectordialog.h>
00033 
00034 #include <QtCore/QMimeData>
00035 #include <QtGui/QDrag>
00036 #include <QtGui/QDragEnterEvent>
00037 #include <QtGui/QDropEvent>
00038 #include <QtGui/QMenu>
00039 
00043 class ImageLoader
00044 {
00045   public:
00046     ImageLoader( QWidget *parent = 0 );
00047 
00048     QImage loadImage( const KUrl &url, bool *ok );
00049 
00050   private:
00051     QImage mImage;
00052     QWidget *mParent;
00053 };
00054 
00055 
00056 ImageLoader::ImageLoader( QWidget *parent )
00057   : mParent( parent )
00058 {
00059 }
00060 
00061 QImage ImageLoader::loadImage( const KUrl &url, bool *ok )
00062 {
00063   QImage image;
00064   QString tempFile;
00065 
00066   if ( url.isEmpty() )
00067     return image;
00068 
00069   (*ok) = false;
00070 
00071   if ( url.isLocalFile() ) {
00072     if ( image.load( url.toLocalFile() ) ) {
00073       (*ok) = true;
00074     }
00075   } else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
00076     if ( image.load( tempFile ) ) {
00077       (*ok) = true;
00078     }
00079     KIO::NetAccess::removeTempFile( tempFile );
00080   }
00081 
00082   if ( !(*ok) ) {
00083     // image does not exist (any more)
00084     KMessageBox::sorry( mParent, i18n( "This contact's image cannot be found." ) );
00085     return image;
00086   }
00087 
00088   QPixmap pixmap = QPixmap::fromImage( image );
00089 
00090   image = KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 100, 140, mParent );
00091   if ( image.isNull() ) {
00092     (*ok) = false;
00093     return image;
00094   }
00095 
00096   if ( image.height() != 140 || image.width() != 100 ) {
00097     if ( image.height() > image.width() )
00098       image = image.scaledToHeight( 140 );
00099     else
00100       image = image.scaledToWidth( 100 );
00101   }
00102 
00103   (*ok) = true;
00104 
00105   return image;
00106 }
00107 
00108 
00109 
00110 
00111 ImageWidget::ImageWidget( Type type, QWidget *parent )
00112   : QPushButton( parent ),
00113     mType( type ),
00114     mHasImage( false ),
00115     mReadOnly( false ),
00116     mImageLoader( 0 )
00117 {
00118   setAcceptDrops( true );
00119 
00120   setIconSize( QSize( 100, 130 ) );
00121   setFixedSize( QSize( 120, 160 ) );
00122 
00123   connect( this, SIGNAL( clicked() ), SLOT( changeImage() ) );
00124 
00125   updateView();
00126 }
00127 
00128 ImageWidget::~ImageWidget()
00129 {
00130   delete mImageLoader;
00131 }
00132 
00133 void ImageWidget::loadContact( const KABC::Addressee &contact )
00134 {
00135   const KABC::Picture picture = (mType == Photo ? contact.photo() : contact.logo());
00136   if ( picture.isIntern() && !picture.data().isNull() ) {
00137     mHasImage = true;
00138     mImage = picture.data();
00139   }
00140 
00141   updateView();
00142 }
00143 
00144 void ImageWidget::storeContact( KABC::Addressee &contact ) const
00145 {
00146   if ( mType == Photo )
00147     contact.setPhoto( mImage );
00148   else
00149     contact.setLogo( mImage );
00150 }
00151 
00152 void ImageWidget::setReadOnly( bool readOnly )
00153 {
00154   mReadOnly = readOnly;
00155 }
00156 
00157 void ImageWidget::updateView()
00158 {
00159   if ( mHasImage ) {
00160     setIcon( QPixmap::fromImage( mImage ) );
00161   } else {
00162     setIcon( KIcon( QLatin1String( "user-identity" ) ) );
00163   }
00164 }
00165 
00166 void ImageWidget::dragEnterEvent( QDragEnterEvent *event )
00167 {
00168   const QMimeData *mimeData = event->mimeData();
00169   event->setAccepted( mimeData->hasImage() || mimeData->hasUrls() );
00170 }
00171 
00172 void ImageWidget::dropEvent( QDropEvent *event )
00173 {
00174   if ( mReadOnly )
00175     return;
00176 
00177   const QMimeData *mimeData = event->mimeData();
00178   if ( mimeData->hasImage() ) {
00179     mImage = qvariant_cast<QImage>(mimeData->imageData());
00180     mHasImage = true;
00181     updateView();
00182   }
00183 
00184   const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
00185   if ( urls.isEmpty() ) { // oops, no data
00186     event->setAccepted( false );
00187   } else {
00188     bool ok = false;
00189     const QImage image = imageLoader()->loadImage( urls.first(), &ok );
00190     if ( ok ) {
00191       mImage = image;
00192       mHasImage = true;
00193       updateView();
00194     }
00195   }
00196 }
00197 
00198 void ImageWidget::mousePressEvent( QMouseEvent *event )
00199 {
00200   mDragStartPos = event->pos();
00201   QPushButton::mousePressEvent( event );
00202 }
00203 
00204 void ImageWidget::mouseMoveEvent( QMouseEvent *event )
00205 {
00206   if ( (event->buttons() & Qt::LeftButton) &&
00207        (event->pos() - mDragStartPos).manhattanLength() > KGlobalSettings::dndEventDelay() ) {
00208 
00209     if ( mHasImage ) {
00210       QDrag *drag = new QDrag( this );
00211       drag->setMimeData( new QMimeData() );
00212       drag->mimeData()->setImageData( mImage );
00213       drag->start();
00214     }
00215   }
00216 }
00217 
00218 void ImageWidget::contextMenuEvent( QContextMenuEvent *event )
00219 {
00220   QMenu menu;
00221 
00222   if ( mType == Photo ) {
00223     if ( !mReadOnly )
00224       menu.addAction( i18n( "Change photo..." ), this, SLOT( changeImage() ) );
00225 
00226     if ( mHasImage ) {
00227       menu.addAction( i18n( "Save photo..." ), this, SLOT( saveImage() ) );
00228 
00229       if ( !mReadOnly )
00230         menu.addAction( i18n( "Remove photo" ), this, SLOT( deleteImage() ) );
00231     }
00232   } else {
00233     if ( !mReadOnly )
00234       menu.addAction( i18n( "Change logo..." ), this, SLOT( changeImage() ) );
00235 
00236     if ( mHasImage ) {
00237       menu.addAction( i18n( "Save logo..." ), this, SLOT( saveImage() ) );
00238 
00239       if ( !mReadOnly )
00240         menu.addAction( i18n( "Remove logo" ), this, SLOT( deleteImage() ) );
00241     }
00242   }
00243 
00244   menu.exec( event->globalPos() );
00245 }
00246 
00247 void ImageWidget::changeImage()
00248 {
00249   if ( mReadOnly )
00250     return;
00251 
00252   const KUrl url = KFileDialog::getOpenUrl( QString(), KImageIO::pattern(), this );
00253   if ( url.isValid() ) {
00254     bool ok = false;
00255     const QImage image = imageLoader()->loadImage( url, &ok );
00256     if ( ok ) {
00257       mImage = image;
00258       mHasImage = true;
00259       updateView();
00260     }
00261   }
00262 }
00263 
00264 void ImageWidget::saveImage()
00265 {
00266   const QString fileName = KFileDialog::getSaveFileName( KUrl(), KImageIO::pattern(), this );
00267   if ( !fileName.isEmpty() )
00268     mImage.save( fileName );
00269 }
00270 
00271 void ImageWidget::deleteImage()
00272 {
00273   mHasImage = false;
00274   mImage = QImage();
00275   updateView();
00276 }
00277 
00278 ImageLoader* ImageWidget::imageLoader()
00279 {
00280   if ( !mImageLoader )
00281     mImageLoader = new ImageLoader;
00282 
00283   return mImageLoader;
00284 }
00285 
00286 #include "imagewidget.moc"

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal