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

akonadi/contact

  • 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 <QtGui/QDrag>
36 #include <QtGui/QDragEnterEvent>
37 #include <QtGui/QDropEvent>
38 #include <QtGui/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  QImage mImage;
52  QWidget *mParent;
53 };
54 
55 
56 ImageLoader::ImageLoader( QWidget *parent )
57  : mParent( parent )
58 {
59 }
60 
61 QImage ImageLoader::loadImage( const KUrl &url, bool *ok )
62 {
63  QImage image;
64  QString tempFile;
65 
66  if ( url.isEmpty() )
67  return image;
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  updateView();
131 }
132 
133 ImageWidget::~ImageWidget()
134 {
135  delete mImageLoader;
136 }
137 
138 void ImageWidget::loadContact( const KABC::Addressee &contact )
139 {
140  const KABC::Picture picture = (mType == Photo ? contact.photo() : contact.logo());
141  if ( picture.isIntern() && !picture.data().isNull() ) {
142  mHasImage = true;
143  mImage = picture.data();
144  }
145 
146  updateView();
147 }
148 
149 void ImageWidget::storeContact( KABC::Addressee &contact ) const
150 {
151  if ( mType == Photo )
152  contact.setPhoto( mImage );
153  else
154  contact.setLogo( mImage );
155 }
156 
157 void ImageWidget::setReadOnly( bool readOnly )
158 {
159  mReadOnly = readOnly;
160 }
161 
162 void ImageWidget::updateView()
163 {
164  if ( mHasImage ) {
165  setIcon( QPixmap::fromImage( mImage ) );
166  } else {
167  if ( mType == Photo )
168  setIcon( KIcon( QLatin1String( "user-identity" ) ) );
169  else
170  setIcon( KIcon( QLatin1String( "image-x-generic" ) ) );
171  }
172 }
173 
174 void ImageWidget::dragEnterEvent( QDragEnterEvent *event )
175 {
176  const QMimeData *mimeData = event->mimeData();
177  event->setAccepted( mimeData->hasImage() || mimeData->hasUrls() );
178 }
179 
180 void ImageWidget::dropEvent( QDropEvent *event )
181 {
182  if ( mReadOnly )
183  return;
184 
185  const QMimeData *mimeData = event->mimeData();
186  if ( mimeData->hasImage() ) {
187  mImage = qvariant_cast<QImage>(mimeData->imageData());
188  mHasImage = true;
189  updateView();
190  }
191 
192  const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
193  if ( urls.isEmpty() ) { // oops, no data
194  event->setAccepted( false );
195  } else {
196  bool ok = false;
197  const QImage image = imageLoader()->loadImage( urls.first(), &ok );
198  if ( ok ) {
199  mImage = image;
200  mHasImage = true;
201  updateView();
202  }
203  }
204 }
205 
206 void ImageWidget::mousePressEvent( QMouseEvent *event )
207 {
208  mDragStartPos = event->pos();
209  QPushButton::mousePressEvent( event );
210 }
211 
212 void ImageWidget::mouseMoveEvent( QMouseEvent *event )
213 {
214  if ( (event->buttons() & Qt::LeftButton) &&
215  (event->pos() - mDragStartPos).manhattanLength() > KGlobalSettings::dndEventDelay() ) {
216 
217  if ( mHasImage ) {
218  QDrag *drag = new QDrag( this );
219  drag->setMimeData( new QMimeData() );
220  drag->mimeData()->setImageData( mImage );
221  drag->start();
222  }
223  }
224 }
225 
226 void ImageWidget::contextMenuEvent( QContextMenuEvent *event )
227 {
228  QMenu menu;
229 
230  if ( mType == Photo ) {
231  if ( !mReadOnly )
232  menu.addAction( i18n( "Change photo..." ), this, SLOT(changeImage()) );
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  } else {
241  if ( !mReadOnly )
242  menu.addAction( i18n( "Change logo..." ), this, SLOT(changeImage()) );
243 
244  if ( mHasImage ) {
245  menu.addAction( i18n( "Save logo..." ), this, SLOT(saveImage()) );
246 
247  if ( !mReadOnly )
248  menu.addAction( i18n( "Remove logo" ), this, SLOT(deleteImage()) );
249  }
250  }
251 
252  menu.exec( event->globalPos() );
253 }
254 
255 void ImageWidget::changeImage()
256 {
257  if ( mReadOnly )
258  return;
259 
260  const KUrl url = KFileDialog::getOpenUrl( QString(), KImageIO::pattern(), this );
261  if ( url.isValid() ) {
262  bool ok = false;
263  const QImage image = imageLoader()->loadImage( url, &ok );
264  if ( ok ) {
265  mImage = image;
266  mHasImage = true;
267  updateView();
268  }
269  }
270 }
271 
272 void ImageWidget::saveImage()
273 {
274  const QString fileName = KFileDialog::getSaveFileName( KUrl(), KImageIO::pattern(), this );
275  if ( !fileName.isEmpty() )
276  mImage.save( fileName );
277 }
278 
279 void ImageWidget::deleteImage()
280 {
281  mHasImage = false;
282  mImage = QImage();
283  updateView();
284 }
285 
286 ImageLoader* ImageWidget::imageLoader()
287 {
288  if ( !mImageLoader )
289  mImageLoader = new ImageLoader;
290 
291  return mImageLoader;
292 }
293 
294 #include "imagewidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:36:30 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

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

kdepimlibs-4.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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