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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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 <klocalizedstring.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 ImageLoader::ImageLoader( QWidget *parent )
55  : mParent( parent )
56 {
57 }
58 
59 QImage ImageLoader::loadImage( const KUrl &url, bool *ok )
60 {
61  QImage image;
62  QString tempFile;
63 
64  if ( url.isEmpty() ) {
65  return image;
66  }
67 
68  ( *ok ) = false;
69 
70  if ( url.isLocalFile() ) {
71  if ( image.load( url.toLocalFile() ) ) {
72  ( *ok ) = true;
73  }
74  } else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
75  if ( image.load( tempFile ) ) {
76  ( *ok ) = true;
77  }
78  KIO::NetAccess::removeTempFile( tempFile );
79  }
80 
81  if ( !( *ok ) ) {
82  // image does not exist (any more)
83  KMessageBox::sorry( mParent, i18n( "This contact's image cannot be found." ) );
84  return image;
85  }
86 
87  QPixmap pixmap = QPixmap::fromImage( image );
88 
89  image = KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 1, 1, mParent );
90  if ( image.isNull() ) {
91  ( *ok ) = false;
92  return image;
93  }
94 
95  if ( image.height() > 720 || image.width() > 720 ) {
96  if ( image.height() > image.width() )
97  image = image.scaledToHeight( 720 );
98  else
99  image = image.scaledToWidth( 720 );
100  }
101 
102  ( *ok ) = true;
103 
104  return image;
105 }
106 
107 ImageWidget::ImageWidget( Type type, QWidget *parent )
108  : QPushButton( parent ),
109  mType( type ),
110  mHasImage( false ),
111  mReadOnly( false ),
112  mImageLoader( 0 )
113 {
114  setAcceptDrops( true );
115 
116  setIconSize( QSize( 100, 100 ) );
117  setFixedSize( QSize( 120, 120 ) );
118 
119  connect( this, SIGNAL(clicked()), SLOT(changeImage()) );
120 
121  if ( mType == Photo ) {
122  setToolTip( i18n( "The photo of the contact (click to change)" ) );
123  } else {
124  setToolTip( i18n( "The logo of the company (click to change)" ) );
125  }
126 
127  updateView();
128 }
129 
130 ImageWidget::~ImageWidget()
131 {
132  delete mImageLoader;
133 }
134 
135 void ImageWidget::loadContact( const KABC::Addressee &contact )
136 {
137  mPicture = ( mType == Photo ? contact.photo() : contact.logo() );
138  if ( mPicture.isIntern() && !mPicture.data().isNull() ) {
139  mHasImage = true;
140  }
141 
142  updateView();
143 }
144 
145 void ImageWidget::storeContact( KABC::Addressee &contact ) const
146 {
147  if ( mType == Photo ) {
148  contact.setPhoto( mPicture );
149  } else {
150  contact.setLogo( mPicture );
151  }
152 }
153 
154 void ImageWidget::setReadOnly( bool readOnly )
155 {
156  mReadOnly = readOnly;
157 }
158 
159 void ImageWidget::updateView()
160 {
161  if ( mHasImage ) {
162  setIcon( QPixmap::fromImage( mPicture.data() ) );
163  } else {
164  if ( mType == Photo ) {
165  setIcon( KIcon( QLatin1String( "user-identity" ) ) );
166  } else {
167  setIcon( KIcon( QLatin1String( "image-x-generic" ) ) );
168  }
169  }
170 }
171 
172 void ImageWidget::dragEnterEvent( QDragEnterEvent *event )
173 {
174  const QMimeData *mimeData = event->mimeData();
175  event->setAccepted( mimeData->hasImage() || mimeData->hasUrls() );
176 }
177 
178 void ImageWidget::dropEvent( QDropEvent *event )
179 {
180  if ( mReadOnly ) {
181  return;
182  }
183 
184  const QMimeData *mimeData = event->mimeData();
185  if ( mimeData->hasImage() ) {
186  mPicture.setData( qvariant_cast<QImage>( mimeData->imageData() ) );
187  mHasImage = true;
188  updateView();
189  }
190 
191  const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
192  if ( urls.isEmpty() ) { // oops, no data
193  event->setAccepted( false );
194  } else {
195  bool ok = false;
196  const QImage image = imageLoader()->loadImage( urls.first(), &ok );
197  if ( ok ) {
198  mPicture.setData( image );
199  mHasImage = true;
200  updateView();
201  }
202  }
203 }
204 
205 void ImageWidget::mousePressEvent( QMouseEvent *event )
206 {
207  mDragStartPos = event->pos();
208  QPushButton::mousePressEvent( event );
209 }
210 
211 void ImageWidget::mouseMoveEvent( QMouseEvent *event )
212 {
213  if ( ( event->buttons() & Qt::LeftButton ) &&
214  ( event->pos() - mDragStartPos ).manhattanLength() > KGlobalSettings::dndEventDelay() ) {
215 
216  if ( mHasImage ) {
217  QDrag *drag = new QDrag( this );
218  drag->setMimeData( new QMimeData() );
219  drag->mimeData()->setImageData( mPicture.data() );
220  drag->start();
221  }
222  }
223 }
224 
225 void ImageWidget::contextMenuEvent( QContextMenuEvent *event )
226 {
227  QMenu menu;
228 
229  if ( mType == Photo ) {
230  if ( !mReadOnly ) {
231  menu.addAction( i18n( "Change photo..." ), this, SLOT(changeImage()) );
232  }
233 
234  if ( mHasImage ) {
235  menu.addAction( i18n( "Save photo..." ), this, SLOT(saveImage()) );
236 
237  if ( !mReadOnly ) {
238  menu.addAction( i18n( "Remove photo" ), this, SLOT(deleteImage()) );
239  }
240  }
241  } else {
242  if ( !mReadOnly ) {
243  menu.addAction( i18n( "Change logo..." ), this, SLOT(changeImage()) );
244  }
245 
246  if ( mHasImage ) {
247  menu.addAction( i18n( "Save logo..." ), this, SLOT(saveImage()) );
248 
249  if ( !mReadOnly ) {
250  menu.addAction( i18n( "Remove logo" ), this, SLOT(deleteImage()) );
251  }
252  }
253  }
254 
255  menu.exec( event->globalPos() );
256 }
257 
258 void ImageWidget::changeImage()
259 {
260  if ( mReadOnly ) {
261  return;
262  }
263 
264  const KUrl url = KFileDialog::getOpenUrl( QUrl(), KImageIO::pattern(), this );
265  if ( url.isValid() ) {
266  bool ok = false;
267  const QImage image = imageLoader()->loadImage( url, &ok );
268  if ( ok ) {
269  mPicture.setData( image );
270  mHasImage = true;
271  updateView();
272  }
273  }
274 }
275 
276 void ImageWidget::saveImage()
277 {
278  const QString fileName = KFileDialog::getSaveFileName( KUrl(), KImageIO::pattern(), this );
279  if ( !fileName.isEmpty() ) {
280  mPicture.data().save( fileName );
281  }
282 }
283 
284 void ImageWidget::deleteImage()
285 {
286  mHasImage = false;
287  mPicture.setData( QImage() );
288  updateView();
289 }
290 
291 ImageLoader* ImageWidget::imageLoader()
292 {
293  if ( !mImageLoader ) {
294  mImageLoader = new ImageLoader;
295  }
296 
297  return mImageLoader;
298 }
299 
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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