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, 100, 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, 100 ) ); 00121 setFixedSize( QSize( 120, 120 ) ); 00122 00123 connect( this, SIGNAL(clicked()), SLOT(changeImage()) ); 00124 00125 if ( mType == Photo ) 00126 setToolTip( i18n( "The photo of the contact (click to change)" ) ); 00127 else 00128 setToolTip( i18n( "The logo of the company (click to change)" ) ); 00129 00130 updateView(); 00131 } 00132 00133 ImageWidget::~ImageWidget() 00134 { 00135 delete mImageLoader; 00136 } 00137 00138 void ImageWidget::loadContact( const KABC::Addressee &contact ) 00139 { 00140 const KABC::Picture picture = (mType == Photo ? contact.photo() : contact.logo()); 00141 if ( picture.isIntern() && !picture.data().isNull() ) { 00142 mHasImage = true; 00143 mImage = picture.data(); 00144 } 00145 00146 updateView(); 00147 } 00148 00149 void ImageWidget::storeContact( KABC::Addressee &contact ) const 00150 { 00151 if ( mType == Photo ) 00152 contact.setPhoto( mImage ); 00153 else 00154 contact.setLogo( mImage ); 00155 } 00156 00157 void ImageWidget::setReadOnly( bool readOnly ) 00158 { 00159 mReadOnly = readOnly; 00160 } 00161 00162 void ImageWidget::updateView() 00163 { 00164 if ( mHasImage ) { 00165 setIcon( QPixmap::fromImage( mImage ) ); 00166 } else { 00167 if ( mType == Photo ) 00168 setIcon( KIcon( QLatin1String( "user-identity" ) ) ); 00169 else 00170 setIcon( KIcon( QLatin1String( "image-x-generic" ) ) ); 00171 } 00172 } 00173 00174 void ImageWidget::dragEnterEvent( QDragEnterEvent *event ) 00175 { 00176 const QMimeData *mimeData = event->mimeData(); 00177 event->setAccepted( mimeData->hasImage() || mimeData->hasUrls() ); 00178 } 00179 00180 void ImageWidget::dropEvent( QDropEvent *event ) 00181 { 00182 if ( mReadOnly ) 00183 return; 00184 00185 const QMimeData *mimeData = event->mimeData(); 00186 if ( mimeData->hasImage() ) { 00187 mImage = qvariant_cast<QImage>(mimeData->imageData()); 00188 mHasImage = true; 00189 updateView(); 00190 } 00191 00192 const KUrl::List urls = KUrl::List::fromMimeData( mimeData ); 00193 if ( urls.isEmpty() ) { // oops, no data 00194 event->setAccepted( false ); 00195 } else { 00196 bool ok = false; 00197 const QImage image = imageLoader()->loadImage( urls.first(), &ok ); 00198 if ( ok ) { 00199 mImage = image; 00200 mHasImage = true; 00201 updateView(); 00202 } 00203 } 00204 } 00205 00206 void ImageWidget::mousePressEvent( QMouseEvent *event ) 00207 { 00208 mDragStartPos = event->pos(); 00209 QPushButton::mousePressEvent( event ); 00210 } 00211 00212 void ImageWidget::mouseMoveEvent( QMouseEvent *event ) 00213 { 00214 if ( (event->buttons() & Qt::LeftButton) && 00215 (event->pos() - mDragStartPos).manhattanLength() > KGlobalSettings::dndEventDelay() ) { 00216 00217 if ( mHasImage ) { 00218 QDrag *drag = new QDrag( this ); 00219 drag->setMimeData( new QMimeData() ); 00220 drag->mimeData()->setImageData( mImage ); 00221 drag->start(); 00222 } 00223 } 00224 } 00225 00226 void ImageWidget::contextMenuEvent( QContextMenuEvent *event ) 00227 { 00228 QMenu menu; 00229 00230 if ( mType == Photo ) { 00231 if ( !mReadOnly ) 00232 menu.addAction( i18n( "Change photo..." ), this, SLOT(changeImage()) ); 00233 00234 if ( mHasImage ) { 00235 menu.addAction( i18n( "Save photo..." ), this, SLOT(saveImage()) ); 00236 00237 if ( !mReadOnly ) 00238 menu.addAction( i18n( "Remove photo" ), this, SLOT(deleteImage()) ); 00239 } 00240 } else { 00241 if ( !mReadOnly ) 00242 menu.addAction( i18n( "Change logo..." ), this, SLOT(changeImage()) ); 00243 00244 if ( mHasImage ) { 00245 menu.addAction( i18n( "Save logo..." ), this, SLOT(saveImage()) ); 00246 00247 if ( !mReadOnly ) 00248 menu.addAction( i18n( "Remove logo" ), this, SLOT(deleteImage()) ); 00249 } 00250 } 00251 00252 menu.exec( event->globalPos() ); 00253 } 00254 00255 void ImageWidget::changeImage() 00256 { 00257 if ( mReadOnly ) 00258 return; 00259 00260 const KUrl url = KFileDialog::getOpenUrl( QString(), KImageIO::pattern(), this ); 00261 if ( url.isValid() ) { 00262 bool ok = false; 00263 const QImage image = imageLoader()->loadImage( url, &ok ); 00264 if ( ok ) { 00265 mImage = image; 00266 mHasImage = true; 00267 updateView(); 00268 } 00269 } 00270 } 00271 00272 void ImageWidget::saveImage() 00273 { 00274 const QString fileName = KFileDialog::getSaveFileName( KUrl(), KImageIO::pattern(), this ); 00275 if ( !fileName.isEmpty() ) 00276 mImage.save( fileName ); 00277 } 00278 00279 void ImageWidget::deleteImage() 00280 { 00281 mHasImage = false; 00282 mImage = QImage(); 00283 updateView(); 00284 } 00285 00286 ImageLoader* ImageWidget::imageLoader() 00287 { 00288 if ( !mImageLoader ) 00289 mImageLoader = new ImageLoader; 00290 00291 return mImageLoader; 00292 } 00293 00294 #include "imagewidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:09:52 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:09:52 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.