Akonadi Contacts

imagewidget.cpp
1 /*
2  This file is part of Contact Editor.
3 
4  SPDX-FileCopyrightText: 2009 Tobias Koenig <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "imagewidget.h"
10 
11 #include <kcontacts/addressee.h>
12 
13 #include <KIO/TransferJob>
14 #include <KLocalizedString>
15 #include <KMessageBox>
16 #include <KPixmapRegionSelectorDialog>
17 #include <KUrlMimeData>
18 #include <QApplication>
19 #include <QDrag>
20 #include <QDragEnterEvent>
21 #include <QDropEvent>
22 #include <QFileDialog>
23 #include <QImageReader>
24 #include <QImageWriter>
25 #include <QInputDialog>
26 #include <QMenu>
27 #include <QMimeData>
28 #include <QUrl>
29 /**
30  * @short Small helper class to load image from network
31  */
32 class ImageLoader
33 {
34 public:
35  ImageLoader(QWidget *parent = nullptr);
36 
37  Q_REQUIRED_RESULT QImage loadImage(const QUrl &url, bool *ok, bool selectPictureSize = true);
38 
39 private:
40  QWidget *const mParent;
41 };
42 
43 ImageLoader::ImageLoader(QWidget *parent)
44  : mParent(parent)
45 {
46 }
47 
48 QImage ImageLoader::loadImage(const QUrl &url, bool *ok, bool selectPictureSize)
49 {
50  QImage image;
51 
52  if (url.isEmpty()) {
53  return image;
54  }
55 
56  (*ok) = false;
57 
58  if (url.isLocalFile()) {
59  if (image.load(url.toLocalFile())) {
60  (*ok) = true;
61  }
62  } else {
63  QByteArray imageData;
64  KIO::TransferJob *job = KIO::get(url, KIO::NoReload);
65  QObject::connect(job, &KIO::TransferJob::data, [&imageData](KIO::Job *, const QByteArray &data) {
66  imageData.append(data);
67  });
68  if (job->exec()) {
69  if (image.loadFromData(imageData)) {
70  (*ok) = true;
71  }
72  }
73  }
74 
75  if (!(*ok)) {
76  // image does not exist (any more)
77  KMessageBox::sorry(mParent, i18n("This contact's image cannot be found."));
78  return image;
79  }
80 
81  if (selectPictureSize) {
82  QPixmap pixmap = QPixmap::fromImage(image);
83  image = KPixmapRegionSelectorDialog::getSelectedImage(pixmap, 1, 1, mParent);
84  if (image.isNull()) {
85  (*ok) = false;
86  return image;
87  }
88  }
89 
90  if (image.height() > 720 || image.width() > 720) {
91  if (image.height() > image.width()) {
92  image = image.scaledToHeight(720);
93  } else {
94  image = image.scaledToWidth(720);
95  }
96  }
97 
98  (*ok) = true;
99 
100  return image;
101 }
102 
103 ImageWidget::ImageWidget(Type type, QWidget *parent)
104  : QPushButton(parent)
105  , mImageLoader(nullptr)
106  , mType(type)
107  , mHasImage(false)
108  , mReadOnly(false)
109 {
110  setAcceptDrops(true);
111 
112  setIconSize(QSize(100, 100));
113  setFixedSize(QSize(120, 120));
114 
115  connect(this, &ImageWidget::clicked, this, &ImageWidget::changeImage);
116 
117  if (mType == Photo) {
118  setToolTip(i18n("The photo of the contact (click to change)"));
119  } else {
120  setToolTip(i18n("The logo of the company (click to change)"));
121  }
122 
123  updateView();
124 }
125 
126 ImageWidget::~ImageWidget()
127 {
128  delete mImageLoader;
129 }
130 
131 void ImageWidget::loadContact(const KContacts::Addressee &contact)
132 {
133  mPicture = (mType == Photo) ? contact.photo() : contact.logo();
134  if (mPicture.isIntern() && !mPicture.data().isNull()) {
135  mHasImage = true;
136  } else if (!mPicture.isIntern() && !mPicture.url().isEmpty()) {
137  mHasImage = true;
138  }
139 
140  updateView();
141 }
142 
143 void ImageWidget::storeContact(KContacts::Addressee &contact) const
144 {
145  if (mType == Photo) {
146  contact.setPhoto(mPicture);
147  } else {
148  contact.setLogo(mPicture);
149  }
150 }
151 
152 void ImageWidget::setReadOnly(bool readOnly)
153 {
154  mReadOnly = readOnly;
155  setAcceptDrops(!mReadOnly);
156 }
157 
158 void ImageWidget::updateView()
159 {
160  if (mHasImage) {
161  if (mPicture.isIntern()) {
162  setIcon(QPixmap::fromImage(mPicture.data()));
163  } else {
164  bool ok = false;
165  const QPixmap pix = QPixmap::fromImage(imageLoader()->loadImage(QUrl(mPicture.url()), &ok, false));
166  if (ok) {
167  setIcon(pix);
168  }
169  }
170  } else {
171  if (mType == Photo) {
172  setIcon(QIcon::fromTheme(QStringLiteral("user-identity")));
173  } else {
174  setIcon(QIcon::fromTheme(QStringLiteral("image-x-generic")));
175  }
176  }
177 }
178 
179 void ImageWidget::dragEnterEvent(QDragEnterEvent *event)
180 {
181  const QMimeData *mimeData = event->mimeData();
182  event->setAccepted(mimeData->hasImage() || mimeData->hasUrls());
183 }
184 
185 void ImageWidget::dropEvent(QDropEvent *event)
186 {
187  if (mReadOnly) {
188  return;
189  }
190 
191  const QMimeData *mimeData = event->mimeData();
192  if (mimeData->hasImage()) {
193  mPicture.setData(qvariant_cast<QImage>(mimeData->imageData()));
194  mHasImage = true;
195  updateView();
196  }
197 
198  const QList<QUrl> urls = KUrlMimeData::urlsFromMimeData(mimeData);
199  if (urls.isEmpty()) { // oops, no data
200  event->setAccepted(false);
201  } else {
202  bool ok = false;
203  const QImage image = imageLoader()->loadImage(urls.first(), &ok);
204  if (ok) {
205  mPicture.setData(image);
206  mHasImage = true;
207  updateView();
208  }
209  }
210 }
211 
212 void ImageWidget::mousePressEvent(QMouseEvent *event)
213 {
214  mDragStartPos = event->pos();
216 }
217 
218 void ImageWidget::mouseMoveEvent(QMouseEvent *event)
219 {
220  if ((event->buttons() & Qt::LeftButton) && (event->pos() - mDragStartPos).manhattanLength() > QApplication::startDragDistance()) {
221  if (mHasImage) {
222  auto drag = new QDrag(this);
223  drag->setMimeData(new QMimeData());
224  drag->mimeData()->setImageData(mPicture.data());
225  drag->exec(Qt::CopyAction);
226  }
227  }
228 }
229 
230 void ImageWidget::contextMenuEvent(QContextMenuEvent *event)
231 {
232  QMenu menu;
233 
234  if (mType == Photo) {
235  if (!mReadOnly) {
236  menu.addAction(i18n("Change photo..."), this, &ImageWidget::changeImage);
237  menu.addAction(i18n("Change URL..."), this, &ImageWidget::changeUrl);
238  }
239 
240  if (mHasImage) {
241  menu.addAction(i18n("Save photo..."), this, &ImageWidget::saveImage);
242 
243  if (!mReadOnly) {
244  menu.addAction(i18n("Remove photo"), this, &ImageWidget::deleteImage);
245  }
246  }
247  } else {
248  if (!mReadOnly) {
249  menu.addAction(i18n("Change logo..."), this, &ImageWidget::changeImage);
250  menu.addAction(i18n("Change URL..."), this, &ImageWidget::changeUrl);
251  }
252 
253  if (mHasImage) {
254  menu.addAction(i18n("Save logo..."), this, &ImageWidget::saveImage);
255 
256  if (!mReadOnly) {
257  menu.addAction(i18n("Remove logo"), this, &ImageWidget::deleteImage);
258  }
259  }
260  }
261 
262  menu.exec(event->globalPos());
263 }
264 
265 void ImageWidget::changeUrl()
266 {
267  if (mReadOnly) {
268  return;
269  }
270  const QString path = QInputDialog::getText(this, i18n("Change image URL"), i18n("Image URL:"), QLineEdit::Normal, mPicture.url());
271  if (!path.isEmpty()) {
272  bool ok;
273  const QImage image = imageLoader()->loadImage(QUrl(path), &ok, false);
274  if (ok && !image.isNull()) {
275  mPicture.setUrl(path);
276  mHasImage = true;
277  updateView();
278  }
279  }
280 }
281 
282 void ImageWidget::changeImage()
283 {
284  if (mReadOnly) {
285  return;
286  }
287 
289  QString filter;
290  for (const QByteArray &ba : supportedImage) {
291  if (!filter.isEmpty()) {
292  filter += QLatin1Char(' ');
293  }
294  filter += QLatin1String("*.") + QString::fromLatin1(ba);
295  }
296 
297  const QUrl url = QFileDialog::getOpenFileUrl(this, QString(), QUrl(), i18n("Images (%1)", filter));
298  if (url.isValid()) {
299  bool ok = false;
300  const QImage image = imageLoader()->loadImage(url, &ok);
301  if (ok) {
302  mPicture.setData(image);
303  mHasImage = true;
304  updateView();
305  }
306  }
307 }
308 
309 void ImageWidget::saveImage()
310 {
312  QString filter;
313  for (const QByteArray &ba : supportedImage) {
314  if (!filter.isEmpty()) {
315  filter += QLatin1Char(' ');
316  }
317  filter += QLatin1String("*.") + QString::fromLatin1(ba);
318  }
319 
320  const QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), i18n("Images (%1)", filter));
321  if (!fileName.isEmpty()) {
322  mPicture.data().save(fileName);
323  }
324 }
325 
326 void ImageWidget::deleteImage()
327 {
328  mHasImage = false;
329  mPicture.setData(QImage());
330  mPicture.setUrl(QString());
331  updateView();
332 }
333 
334 ImageLoader *ImageWidget::imageLoader()
335 {
336  if (!mImageLoader) {
337  mImageLoader = new ImageLoader;
338  }
339 
340  return mImageLoader;
341 }
bool loadFromData(const uchar *data, int len, const char *format)
bool load(QIODevice *device, const char *format)
QImage scaledToWidth(int width, Qt::TransformationMode mode) const const
void setPhoto(const Picture &photo)
KCOREADDONS_EXPORT QList< QUrl > urlsFromMimeData(const QMimeData *mimeData, DecodeOptions decodeOptions=PreferKdeUrls, MetaDataMap *metaData=nullptr)
static QImage getSelectedImage(const QPixmap &pixmap, QWidget *parent=nullptr)
bool exec()
LeftButton
QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags)
QList< QByteArray > supportedImageFormats()
bool isNull() const const
Qt::MouseButtons buttons() const const
bool isEmpty() const const
const QPoint & globalPos() const const
QAction * addAction(const QString &text)
QUrl getOpenFileUrl(QWidget *parent, const QString &caption, const QUrl &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options, const QStringList &supportedSchemes)
QImage scaledToHeight(int height, Qt::TransformationMode mode) const const
int width() const const
bool isEmpty() const const
bool isEmpty() const const
T & first()
virtual void mousePressEvent(QMouseEvent *e) override
QAction * exec()
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
QString toLocalFile() const const
QByteArray & append(char ch)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
QString i18n(const char *text, const TYPE &arg...)
void data(KIO::Job *job, const QByteArray &data)
bool isValid() const const
bool hasUrls() const const
QList< QByteArray > supportedImageFormats()
bool hasImage() const const
void setLogo(const Picture &logo)
QVariant imageData() const const
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
Picture photo() const
QString fromLatin1(const char *str, int size)
int height() const const
QIcon fromTheme(const QString &name)
void setData(const QString &mimeType, const QByteArray &data)
QPoint pos() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
CopyAction
void sorry(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
int startDragDistance()
bool isLocalFile() const const
Picture logo() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.