22 #include "imagewidget.h"
24 #include <kabc/addressee.h>
25 #include <kfiledialog.h>
26 #include <kglobalsettings.h>
29 #include <kio/netaccess.h>
30 #include <klocalizedstring.h>
31 #include <kmessagebox.h>
32 #include <kpixmapregionselectordialog.h>
34 #include <QtCore/QMimeData>
36 #include <QDragEnterEvent>
46 ImageLoader( QWidget *parent = 0 );
48 QImage loadImage(
const KUrl &url,
bool *ok );
54 ImageLoader::ImageLoader( QWidget *parent )
59 QImage ImageLoader::loadImage(
const KUrl &url,
bool *ok )
64 if ( url.isEmpty() ) {
70 if ( url.isLocalFile() ) {
71 if ( image.load( url.toLocalFile() ) ) {
74 }
else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
75 if ( image.load( tempFile ) ) {
78 KIO::NetAccess::removeTempFile( tempFile );
83 KMessageBox::sorry( mParent, i18n(
"This contact's image cannot be found." ) );
87 QPixmap pixmap = QPixmap::fromImage( image );
89 image = KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 1, 1, mParent );
90 if ( image.isNull() ) {
95 if ( image.height() > 720 || image.width() > 720 ) {
96 if ( image.height() > image.width() )
97 image = image.scaledToHeight( 720 );
99 image = image.scaledToWidth( 720 );
107 ImageWidget::ImageWidget( Type type, QWidget *parent )
108 : QPushButton( parent ),
114 setAcceptDrops(
true );
116 setIconSize( QSize( 100, 100 ) );
117 setFixedSize( QSize( 120, 120 ) );
119 connect(
this, SIGNAL(clicked()), SLOT(changeImage()) );
121 if ( mType == Photo ) {
122 setToolTip( i18n(
"The photo of the contact (click to change)" ) );
124 setToolTip( i18n(
"The logo of the company (click to change)" ) );
130 ImageWidget::~ImageWidget()
135 void ImageWidget::loadContact(
const KABC::Addressee &contact )
137 mPicture = ( mType == Photo ? contact.photo() : contact.logo() );
138 if ( mPicture.isIntern() && !mPicture.data().isNull() ) {
145 void ImageWidget::storeContact( KABC::Addressee &contact )
const
147 if ( mType == Photo ) {
148 contact.setPhoto( mPicture );
150 contact.setLogo( mPicture );
154 void ImageWidget::setReadOnly(
bool readOnly )
156 mReadOnly = readOnly;
159 void ImageWidget::updateView()
162 setIcon( QPixmap::fromImage( mPicture.data() ) );
164 if ( mType == Photo ) {
165 setIcon( KIcon( QLatin1String(
"user-identity" ) ) );
167 setIcon( KIcon( QLatin1String(
"image-x-generic" ) ) );
172 void ImageWidget::dragEnterEvent( QDragEnterEvent *event )
174 const QMimeData *mimeData =
event->mimeData();
175 event->setAccepted( mimeData->hasImage() || mimeData->hasUrls() );
178 void ImageWidget::dropEvent( QDropEvent *event )
184 const QMimeData *mimeData =
event->mimeData();
185 if ( mimeData->hasImage() ) {
186 mPicture.setData( qvariant_cast<QImage>( mimeData->imageData() ) );
191 const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
192 if ( urls.isEmpty() ) {
193 event->setAccepted(
false );
196 const QImage image = imageLoader()->loadImage( urls.first(), &ok );
198 mPicture.setData( image );
205 void ImageWidget::mousePressEvent( QMouseEvent *event )
207 mDragStartPos =
event->pos();
208 QPushButton::mousePressEvent( event );
211 void ImageWidget::mouseMoveEvent( QMouseEvent *event )
213 if ( ( event->buttons() & Qt::LeftButton ) &&
214 ( event->pos() - mDragStartPos ).manhattanLength() > KGlobalSettings::dndEventDelay() ) {
217 QDrag *drag =
new QDrag(
this );
218 drag->setMimeData(
new QMimeData() );
219 drag->mimeData()->setImageData( mPicture.data() );
225 void ImageWidget::contextMenuEvent( QContextMenuEvent *event )
229 if ( mType == Photo ) {
231 menu.addAction( i18n(
"Change photo..." ),
this, SLOT(changeImage()) );
235 menu.addAction( i18n(
"Save photo..." ),
this, SLOT(saveImage()) );
238 menu.addAction( i18n(
"Remove photo" ),
this, SLOT(deleteImage()) );
243 menu.addAction( i18n(
"Change logo..." ),
this, SLOT(changeImage()) );
247 menu.addAction( i18n(
"Save logo..." ),
this, SLOT(saveImage()) );
250 menu.addAction( i18n(
"Remove logo" ),
this, SLOT(deleteImage()) );
255 menu.exec( event->globalPos() );
258 void ImageWidget::changeImage()
264 const KUrl url = KFileDialog::getOpenUrl( QUrl(), KImageIO::pattern(),
this );
265 if ( url.isValid() ) {
267 const QImage image = imageLoader()->loadImage( url, &ok );
269 mPicture.setData( image );
276 void ImageWidget::saveImage()
278 const QString fileName = KFileDialog::getSaveFileName( KUrl(), KImageIO::pattern(),
this );
279 if ( !fileName.isEmpty() ) {
280 mPicture.data().save( fileName );
284 void ImageWidget::deleteImage()
287 mPicture.setData( QImage() );
291 ImageLoader* ImageWidget::imageLoader()
293 if ( !mImageLoader ) {
294 mImageLoader =
new ImageLoader;