22 #include "imagewidget.h"
24 #include <kabc/addressee.h>
25 #include <kfiledialog.h>
26 #include <kglobalsettings.h>
29 #include <kio/netaccess.h>
30 #include <klocalizedstring.h>
31 #include <kmessagebox.h>
32 #include <kpixmapregionselectordialog.h>
34 #include <QtCore/QMimeData>
36 #include <QDragEnterEvent>
46 ImageLoader(
QWidget *parent = 0);
48 QImage loadImage(
const KUrl &url,
bool *ok);
54 ImageLoader::ImageLoader(
QWidget *parent)
59 QImage ImageLoader::loadImage(
const KUrl &url,
bool *ok)
70 if (url.isLocalFile()) {
71 if (image.
load(url.toLocalFile())) {
74 }
else if (KIO::NetAccess::download(url, tempFile, mParent)) {
75 if (image.
load(tempFile)) {
78 KIO::NetAccess::removeTempFile(tempFile);
83 KMessageBox::sorry(mParent, i18n(
"This contact's image cannot be found."));
89 image = KPixmapRegionSelectorDialog::getSelectedImage(pixmap, 1, 1, mParent);
108 ImageWidget::ImageWidget(Type type,
QWidget *parent)
115 setAcceptDrops(
true);
117 setIconSize(
QSize(100, 100));
118 setFixedSize(
QSize(120, 120));
120 connect(
this, SIGNAL(clicked()), SLOT(changeImage()));
122 if (mType == Photo) {
123 setToolTip(i18n(
"The photo of the contact (click to change)"));
125 setToolTip(i18n(
"The logo of the company (click to change)"));
131 ImageWidget::~ImageWidget()
136 void ImageWidget::loadContact(
const KABC::Addressee &contact)
138 mPicture = (mType == Photo ? contact.photo() : contact.logo());
139 if (mPicture.isIntern() && !mPicture.data().isNull()) {
146 void ImageWidget::storeContact(KABC::Addressee &contact)
const
148 if (mType == Photo) {
149 contact.setPhoto(mPicture);
151 contact.setLogo(mPicture);
155 void ImageWidget::setReadOnly(
bool readOnly)
157 mReadOnly = readOnly;
160 void ImageWidget::updateView()
165 if (mType == Photo) {
175 const QMimeData *mimeData =
event->mimeData();
179 void ImageWidget::dropEvent(
QDropEvent *event)
185 const QMimeData *mimeData =
event->mimeData();
192 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
193 if (urls.isEmpty()) {
194 event->setAccepted(
false);
197 const QImage image = imageLoader()->loadImage(urls.first(), &ok);
199 mPicture.setData(image);
206 void ImageWidget::mousePressEvent(
QMouseEvent *event)
208 mDragStartPos =
event->pos();
212 void ImageWidget::mouseMoveEvent(
QMouseEvent *event)
214 if ((event->
buttons() & Qt::LeftButton) &&
215 (event->
pos() - mDragStartPos).manhattanLength() > KGlobalSettings::dndEventDelay()) {
230 if (mType == Photo) {
232 menu.
addAction(i18n(
"Change photo..."),
this, SLOT(changeImage()));
236 menu.
addAction(i18n(
"Save photo..."),
this, SLOT(saveImage()));
239 menu.
addAction(i18n(
"Remove photo"),
this, SLOT(deleteImage()));
244 menu.
addAction(i18n(
"Change logo..."),
this, SLOT(changeImage()));
248 menu.
addAction(i18n(
"Save logo..."),
this, SLOT(saveImage()));
251 menu.
addAction(i18n(
"Remove logo"),
this, SLOT(deleteImage()));
259 void ImageWidget::changeImage()
265 const KUrl url = KFileDialog::getOpenUrl(
QUrl(), KImageIO::pattern(),
this);
268 const QImage image = imageLoader()->loadImage(url, &ok);
270 mPicture.setData(image);
277 void ImageWidget::saveImage()
279 const QString fileName = KFileDialog::getSaveFileName(KUrl(), KImageIO::pattern(),
this,
QString(), KFileDialog::ConfirmOverwrite);
281 mPicture.data().save(fileName);
285 void ImageWidget::deleteImage()
288 mPicture.setData(
QImage());
292 ImageLoader *ImageWidget::imageLoader()
295 mImageLoader =
new ImageLoader;
bool load(QIODevice *device, const char *format)
QImage scaledToWidth(int width, Qt::TransformationMode mode) const
void setMimeData(QMimeData *data)
Qt::DropAction start(QFlags< Qt::DropAction > request)
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QMimeData * mimeData() const
QImage scaledToHeight(int height, Qt::TransformationMode mode) const
void setImageData(const QVariant &image)
QVariant imageData() const
void setData(const QString &mimeType, const QByteArray &data)
const QPoint & pos() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)