• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
  • contact
  • editor
imagewidget.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "imagewidget.h"
23 
24 #include <kabc/addressee.h>
25 #include <kfiledialog.h>
26 #include <kglobalsettings.h>
27 #include <kicon.h>
28 #include <kimageio.h>
29 #include <kio/netaccess.h>
30 #include <klocale.h>
31 #include <kmessagebox.h>
32 #include <kpixmapregionselectordialog.h>
33 
34 #include <QtCore/QMimeData>
35 #include <QDrag>
36 #include <QDragEnterEvent>
37 #include <QDropEvent>
38 #include <QMenu>
39 
43 class ImageLoader
44 {
45  public:
46  ImageLoader( QWidget *parent = 0 );
47 
48  QImage loadImage( const KUrl &url, bool *ok );
49 
50  private:
51  QWidget *mParent;
52 };
53 
54 
55 ImageLoader::ImageLoader( QWidget *parent )
56  : mParent( parent )
57 {
58 }
59 
60 QImage ImageLoader::loadImage( const KUrl &url, bool *ok )
61 {
62  QImage image;
63  QString tempFile;
64 
65  if ( url.isEmpty() ) {
66  return image;
67  }
68 
69  ( *ok ) = false;
70 
71  if ( url.isLocalFile() ) {
72  if ( image.load( url.toLocalFile() ) ) {
73  ( *ok ) = true;
74  }
75  } else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
76  if ( image.load( tempFile ) ) {
77  ( *ok ) = true;
78  }
79  KIO::NetAccess::removeTempFile( tempFile );
80  }
81 
82  if ( !( *ok ) ) {
83  // image does not exist (any more)
84  KMessageBox::sorry( mParent, i18n( "This contact's image cannot be found." ) );
85  return image;
86  }
87 
88  QPixmap pixmap = QPixmap::fromImage( image );
89 
90  image = KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 1, 1, mParent );
91  if ( image.isNull() ) {
92  ( *ok ) = false;
93  return image;
94  }
95 
96  if ( image.height() > 720 || image.width() > 720 ) {
97  if ( image.height() > image.width() )
98  image = image.scaledToHeight( 720 );
99  else
100  image = image.scaledToWidth( 720 );
101  }
102 
103  ( *ok ) = true;
104 
105  return image;
106 }
107 
108 
109 
110 
111 ImageWidget::ImageWidget( Type type, QWidget *parent )
112  : QPushButton( parent ),
113  mType( type ),
114  mHasImage( false ),
115  mReadOnly( false ),
116  mImageLoader( 0 )
117 {
118  setAcceptDrops( true );
119 
120  setIconSize( QSize( 100, 100 ) );
121  setFixedSize( QSize( 120, 120 ) );
122 
123  connect( this, SIGNAL(clicked()), SLOT(changeImage()) );
124 
125  if ( mType == Photo ) {
126  setToolTip( i18n( "The photo of the contact (click to change)" ) );
127  } else {
128  setToolTip( i18n( "The logo of the company (click to change)" ) );
129  }
130 
131  updateView();
132 }
133 
134 ImageWidget::~ImageWidget()
135 {
136  delete mImageLoader;
137 }
138 
139 void ImageWidget::loadContact( const KABC::Addressee &contact )
140 {
141  mPicture = ( mType == Photo ? contact.photo() : contact.logo() );
142  if ( mPicture.isIntern() && !mPicture.data().isNull() ) {
143  mHasImage = true;
144  }
145 
146  updateView();
147 }
148 
149 void ImageWidget::storeContact( KABC::Addressee &contact ) const
150 {
151  if ( mType == Photo ) {
152  contact.setPhoto( mPicture );
153  } else {
154  contact.setLogo( mPicture );
155  }
156 }
157 
158 void ImageWidget::setReadOnly( bool readOnly )
159 {
160  mReadOnly = readOnly;
161 }
162 
163 void ImageWidget::updateView()
164 {
165  if ( mHasImage ) {
166  setIcon( QPixmap::fromImage( mPicture.data() ) );
167  } else {
168  if ( mType == Photo ) {
169  setIcon( KIcon( QLatin1String( "user-identity" ) ) );
170  } else {
171  setIcon( KIcon( QLatin1String( "image-x-generic" ) ) );
172  }
173  }
174 }
175 
176 void ImageWidget::dragEnterEvent( QDragEnterEvent *event )
177 {
178  const QMimeData *mimeData = event->mimeData();
179  event->setAccepted( mimeData->hasImage() || mimeData->hasUrls() );
180 }
181 
182 void ImageWidget::dropEvent( QDropEvent *event )
183 {
184  if ( mReadOnly ) {
185  return;
186  }
187 
188  const QMimeData *mimeData = event->mimeData();
189  if ( mimeData->hasImage() ) {
190  mPicture.setData( qvariant_cast<QImage>( mimeData->imageData() ) );
191  mHasImage = true;
192  updateView();
193  }
194 
195  const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
196  if ( urls.isEmpty() ) { // oops, no data
197  event->setAccepted( false );
198  } else {
199  bool ok = false;
200  const QImage image = imageLoader()->loadImage( urls.first(), &ok );
201  if ( ok ) {
202  mPicture.setData( image );
203  mHasImage = true;
204  updateView();
205  }
206  }
207 }
208 
209 void ImageWidget::mousePressEvent( QMouseEvent *event )
210 {
211  mDragStartPos = event->pos();
212  QPushButton::mousePressEvent( event );
213 }
214 
215 void ImageWidget::mouseMoveEvent( QMouseEvent *event )
216 {
217  if ( ( event->buttons() & Qt::LeftButton ) &&
218  ( event->pos() - mDragStartPos ).manhattanLength() > KGlobalSettings::dndEventDelay() ) {
219 
220  if ( mHasImage ) {
221  QDrag *drag = new QDrag( this );
222  drag->setMimeData( new QMimeData() );
223  drag->mimeData()->setImageData( mPicture.data() );
224  drag->start();
225  }
226  }
227 }
228 
229 void ImageWidget::contextMenuEvent( QContextMenuEvent *event )
230 {
231  QMenu menu;
232 
233  if ( mType == Photo ) {
234  if ( !mReadOnly ) {
235  menu.addAction( i18n( "Change photo..." ), this, SLOT(changeImage()) );
236  }
237 
238  if ( mHasImage ) {
239  menu.addAction( i18n( "Save photo..." ), this, SLOT(saveImage()) );
240 
241  if ( !mReadOnly ) {
242  menu.addAction( i18n( "Remove photo" ), this, SLOT(deleteImage()) );
243  }
244  }
245  } else {
246  if ( !mReadOnly ) {
247  menu.addAction( i18n( "Change logo..." ), this, SLOT(changeImage()) );
248  }
249 
250  if ( mHasImage ) {
251  menu.addAction( i18n( "Save logo..." ), this, SLOT(saveImage()) );
252 
253  if ( !mReadOnly ) {
254  menu.addAction( i18n( "Remove logo" ), this, SLOT(deleteImage()) );
255  }
256  }
257  }
258 
259  menu.exec( event->globalPos() );
260 }
261 
262 void ImageWidget::changeImage()
263 {
264  if ( mReadOnly ) {
265  return;
266  }
267 
268  const KUrl url = KFileDialog::getOpenUrl( QUrl(), KImageIO::pattern(), this );
269  if ( url.isValid() ) {
270  bool ok = false;
271  const QImage image = imageLoader()->loadImage( url, &ok );
272  if ( ok ) {
273  mPicture.setData( image );
274  mHasImage = true;
275  updateView();
276  }
277  }
278 }
279 
280 void ImageWidget::saveImage()
281 {
282  const QString fileName = KFileDialog::getSaveFileName( KUrl(), KImageIO::pattern(), this );
283  if ( !fileName.isEmpty() ) {
284  mPicture.data().save( fileName );
285  }
286 }
287 
288 void ImageWidget::deleteImage()
289 {
290  mHasImage = false;
291  mPicture.setData( QImage() );
292  updateView();
293 }
294 
295 ImageLoader* ImageWidget::imageLoader()
296 {
297  if ( !mImageLoader ) {
298  mImageLoader = new ImageLoader;
299  }
300 
301  return mImageLoader;
302 }
303 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:37 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal