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

akonadi/contact

  • sources
  • kde-4.14
  • 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 
103  (*ok) = true;
104 
105  return image;
106 }
107 
108 ImageWidget::ImageWidget(Type type, QWidget *parent)
109  : QPushButton(parent)
110  , mImageLoader(0)
111  , mType(type)
112  , mHasImage(false)
113  , mReadOnly(false)
114 {
115  setAcceptDrops(true);
116 
117  setIconSize(QSize(100, 100));
118  setFixedSize(QSize(120, 120));
119 
120  connect(this, SIGNAL(clicked()), SLOT(changeImage()));
121 
122  if (mType == Photo) {
123  setToolTip(i18n("The photo of the contact (click to change)"));
124  } else {
125  setToolTip(i18n("The logo of the company (click to change)"));
126  }
127 
128  updateView();
129 }
130 
131 ImageWidget::~ImageWidget()
132 {
133  delete mImageLoader;
134 }
135 
136 void ImageWidget::loadContact(const KABC::Addressee &contact)
137 {
138  mPicture = (mType == Photo ? contact.photo() : contact.logo());
139  if (mPicture.isIntern() && !mPicture.data().isNull()) {
140  mHasImage = true;
141  }
142 
143  updateView();
144 }
145 
146 void ImageWidget::storeContact(KABC::Addressee &contact) const
147 {
148  if (mType == Photo) {
149  contact.setPhoto(mPicture);
150  } else {
151  contact.setLogo(mPicture);
152  }
153 }
154 
155 void ImageWidget::setReadOnly(bool readOnly)
156 {
157  mReadOnly = readOnly;
158 }
159 
160 void ImageWidget::updateView()
161 {
162  if (mHasImage) {
163  setIcon(QPixmap::fromImage(mPicture.data()));
164  } else {
165  if (mType == Photo) {
166  setIcon(KIcon(QLatin1String("user-identity")));
167  } else {
168  setIcon(KIcon(QLatin1String("image-x-generic")));
169  }
170  }
171 }
172 
173 void ImageWidget::dragEnterEvent(QDragEnterEvent *event)
174 {
175  const QMimeData *mimeData = event->mimeData();
176  event->setAccepted(mimeData->hasImage() || mimeData->hasUrls());
177 }
178 
179 void ImageWidget::dropEvent(QDropEvent *event)
180 {
181  if (mReadOnly) {
182  return;
183  }
184 
185  const QMimeData *mimeData = event->mimeData();
186  if (mimeData->hasImage()) {
187  mPicture.setData(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  mPicture.setData(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(mPicture.data());
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 
235  if (mHasImage) {
236  menu.addAction(i18n("Save photo..."), this, SLOT(saveImage()));
237 
238  if (!mReadOnly) {
239  menu.addAction(i18n("Remove photo"), this, SLOT(deleteImage()));
240  }
241  }
242  } else {
243  if (!mReadOnly) {
244  menu.addAction(i18n("Change logo..."), this, SLOT(changeImage()));
245  }
246 
247  if (mHasImage) {
248  menu.addAction(i18n("Save logo..."), this, SLOT(saveImage()));
249 
250  if (!mReadOnly) {
251  menu.addAction(i18n("Remove logo"), this, SLOT(deleteImage()));
252  }
253  }
254  }
255 
256  menu.exec(event->globalPos());
257 }
258 
259 void ImageWidget::changeImage()
260 {
261  if (mReadOnly) {
262  return;
263  }
264 
265  const KUrl url = KFileDialog::getOpenUrl(QUrl(), KImageIO::pattern(), this);
266  if (url.isValid()) {
267  bool ok = false;
268  const QImage image = imageLoader()->loadImage(url, &ok);
269  if (ok) {
270  mPicture.setData(image);
271  mHasImage = true;
272  updateView();
273  }
274  }
275 }
276 
277 void ImageWidget::saveImage()
278 {
279  const QString fileName = KFileDialog::getSaveFileName(KUrl(), KImageIO::pattern(), this, QString(), KFileDialog::ConfirmOverwrite);
280  if (!fileName.isEmpty()) {
281  mPicture.data().save(fileName);
282  }
283 }
284 
285 void ImageWidget::deleteImage()
286 {
287  mHasImage = false;
288  mPicture.setData(QImage());
289  updateView();
290 }
291 
292 ImageLoader *ImageWidget::imageLoader()
293 {
294  if (!mImageLoader) {
295  mImageLoader = new ImageLoader;
296  }
297 
298  return mImageLoader;
299 }
QWidget
QImage::load
bool load(QIODevice *device, const char *format)
QImage::scaledToWidth
QImage scaledToWidth(int width, Qt::TransformationMode mode) const
QDrag::setMimeData
void setMimeData(QMimeData *data)
QDrag::start
Qt::DropAction start(QFlags< Qt::DropAction > request)
QMenu::addAction
void addAction(QAction *action)
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QMouseEvent
QImage::isNull
bool isNull() const
QMouseEvent::buttons
Qt::MouseButtons buttons() const
QDrag::mimeData
QMimeData * mimeData() const
QContextMenuEvent::globalPos
const QPoint & globalPos() const
QMimeData
QImage::scaledToHeight
QImage scaledToHeight(int height, Qt::TransformationMode mode) const
QContextMenuEvent
QImage::width
int width() const
QDropEvent
QDrag
QString::isEmpty
bool isEmpty() const
QString
QAbstractButton::mousePressEvent
virtual void mousePressEvent(QMouseEvent *e)
QMenu::exec
QAction * exec()
QMimeData::setImageData
void setImageData(const QVariant &image)
QPixmap
QWidget::setAcceptDrops
void setAcceptDrops(bool on)
QMenu
QSize
QUrl
QWidget::setFixedSize
void setFixedSize(const QSize &s)
QImage
QMimeData::hasUrls
bool hasUrls() const
QDragEnterEvent
QLatin1String
QMimeData::hasImage
bool hasImage() const
QMimeData::imageData
QVariant imageData() const
QImage::height
int height() const
QPushButton
QMimeData::setData
void setData(const QString &mimeType, const QByteArray &data)
QMouseEvent::pos
const QPoint & pos() const
QWidget::setToolTip
void setToolTip(const QString &)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:38:19 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs 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

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