Commit 8260b783 authored by Tobias Koenig's avatar Tobias Koenig

Bring back the image widget for setting photo and logo of a contact

svn path=/trunk/KDE/kdepim/kcontactmanager/editor/; revision=912118
parent 68dedb5e
......@@ -21,6 +21,7 @@
#include "contacteditor.h"
#include "addresseditwidget.h"
#include "imagewidget.h"
#include <klineedit.h>
#include <klocale.h>
......@@ -51,7 +52,7 @@ class ContactEditor::Private
// widgets from name group
KLineEdit *mNameWidget;
QLabel *mPhotoWidget;
ImageWidget *mPhotoWidget;
KLineEdit *mDisplayNameWidget;
KLineEdit *mNickNameWidget;
QLabel *mSoundWidget;
......@@ -72,6 +73,7 @@ class ContactEditor::Private
QWidget *mCoordinatesWidget;
// widgets from general group
ImageWidget *mLogoWidget;
KLineEdit *mProfessionWidget;
KLineEdit *mTitleWidget;
KLineEdit *mDepartmentWidget;
......@@ -137,7 +139,7 @@ void ContactEditor::Private::initGuiContactTab()
label->setBuddy( mNameWidget );
nameLayout->addWidget( mNameWidget, 0, 1 );
mPhotoWidget = new QLabel;
mPhotoWidget = new ImageWidget( ImageWidget::Photo );
mPhotoWidget->setMinimumSize( QSize( 100, 140 ) );
nameLayout->addWidget( mPhotoWidget, 0, 2, 4, 1 );
......@@ -259,6 +261,9 @@ void ContactEditor::Private::initGuiBusinessTab()
QLabel *label = 0;
// setup general group box
mLogoWidget = new ImageWidget( ImageWidget::Logo );
generalLayout->addWidget( mLogoWidget, 0, 2, 6, 1, Qt::AlignTop );
label = new QLabel( i18n( "Profession:" ) );
label->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
generalLayout->addWidget( label, 0, 0 );
......@@ -387,10 +392,14 @@ ContactEditor::~ContactEditor()
void ContactEditor::loadContact( const KABC::Addressee &contact )
{
d->mPhotoWidget->loadContact( contact );
d->mAddressesWidget->loadContact( contact );
d->mLogoWidget->loadContact( contact );
}
void ContactEditor::storeContact( KABC::Addressee &contact ) const
{
d->mPhotoWidget->storeContact( contact );
d->mAddressesWidget->storeContact( contact );
d->mLogoWidget->storeContact( contact );
}
/*
This file is part of KContactManager.
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "imagewidget.h"
#include <kabc/addressee.h>
#include <kfiledialog.h>
#include <kglobalsettings.h>
#include <kicon.h>
#include <kimageio.h>
#include <kio/netaccess.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpixmapregionselectordialog.h>
#include <QtCore/QMimeData>
#include <QtGui/QDrag>
#include <QtGui/QDragEnterEvent>
#include <QtGui/QDropEvent>
#include <QtGui/QMenu>
#include <QtGui/QPainter>
/**
* @short Small helper class to load image from network
*/
class ImageLoader
{
public:
ImageLoader( QWidget *parent = 0 );
QImage loadImage( const KUrl &url, bool *ok );
private:
QImage mImage;
QWidget *mParent;
};
ImageLoader::ImageLoader( QWidget *parent )
: mParent( parent )
{
}
QImage ImageLoader::loadImage( const KUrl &url, bool *ok )
{
QImage image;
QString tempFile;
if ( url.isEmpty() )
return image;
(*ok) = false;
if ( url.isLocalFile() ) {
if ( image.load( url.path() ) ) {
(*ok) = true;
}
} else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
if ( image.load( tempFile ) ) {
(*ok) = true;
}
KIO::NetAccess::removeTempFile( tempFile );
}
if ( !(*ok) ) {
// image does not exist (any more)
KMessageBox::sorry( mParent, i18n( "This contact's image cannot be found." ) );
return image;
}
QPixmap pixmap = QPixmap::fromImage( image );
image = KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 100, 140, mParent );
if ( image.isNull() ) {
(*ok) = false;
return image;
}
if ( image.height() != 140 || image.width() != 100 ) {
if ( image.height() > image.width() )
image = image.scaledToHeight( 140 );
else
image = image.scaledToWidth( 100 );
}
(*ok) = true;
return image;
}
ImageWidget::ImageWidget( Type type, QWidget *parent )
: QPushButton( parent ),
mType( type ),
mHasImage( false ),
mReadOnly( false ),
mImageLoader( 0 )
{
setAcceptDrops( true );
setIconSize( QSize( 100, 130 ) );
setFixedSize( QSize( 120, 160 ) );
connect( this, SIGNAL( clicked() ), SLOT( changeImage() ) );
}
ImageWidget::~ImageWidget()
{
delete mImageLoader;
}
void ImageWidget::loadContact( const KABC::Addressee &contact )
{
const KABC::Picture picture = (mType == Photo ? contact.photo() : contact.logo());
if ( picture.isIntern() && !picture.data().isNull() ) {
mHasImage = true;
mImage = picture.data();
}
updateView();
}
void ImageWidget::storeContact( KABC::Addressee &contact ) const
{
if ( mType == Photo )
contact.setPhoto( mImage );
else
contact.setLogo( mImage );
}
void ImageWidget::setReadOnly( bool readOnly )
{
mReadOnly = readOnly;
}
void ImageWidget::updateView()
{
if ( mHasImage ) {
setIcon( QPixmap::fromImage( mImage ) );
} else {
setIcon( KIcon( "user-identity" ) );
}
}
void ImageWidget::dragEnterEvent( QDragEnterEvent *event )
{
const QMimeData *mimeData = event->mimeData();
event->setAccepted( mimeData->hasImage() || mimeData->hasUrls() );
}
void ImageWidget::dropEvent( QDropEvent *event )
{
if ( mReadOnly )
return;
const QMimeData *mimeData = event->mimeData();
if ( mimeData->hasImage() ) {
mImage = qvariant_cast<QImage>(mimeData->imageData());
mHasImage = true;
updateView();
}
const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
if ( urls.isEmpty() ) { // oops, no data
event->setAccepted( false );
} else {
bool ok = false;
const QImage image = imageLoader()->loadImage( urls.first(), &ok );
if ( ok ) {
mImage = image;
mHasImage = true;
updateView();
}
}
}
void ImageWidget::mousePressEvent( QMouseEvent *event )
{
mDragStartPos = event->pos();
QPushButton::mousePressEvent( event );
}
void ImageWidget::mouseMoveEvent( QMouseEvent *event )
{
if ( (event->buttons() & Qt::LeftButton) &&
(event->pos() - mDragStartPos).manhattanLength() > KGlobalSettings::dndEventDelay() ) {
if ( mHasImage ) {
QDrag *drag = new QDrag( this );
drag->setMimeData( new QMimeData() );
drag->mimeData()->setImageData( mImage );
drag->start();
}
}
}
void ImageWidget::contextMenuEvent( QContextMenuEvent *event )
{
QMenu menu;
if ( mType == Photo ) {
if ( !mReadOnly )
menu.addAction( i18n( "Change photo..." ), this, SLOT( changeImage() ) );
if ( mHasImage ) {
menu.addAction( i18n( "Save photo..." ), this, SLOT( saveImage() ) );
if ( !mReadOnly )
menu.addAction( i18n( "Remove photo" ), this, SLOT( deleteImage() ) );
}
} else {
if ( !mReadOnly )
menu.addAction( i18n( "Change logo..." ), this, SLOT( changeImage() ) );
if ( mHasImage ) {
menu.addAction( i18n( "Save logo..." ), this, SLOT( saveImage() ) );
if ( !mReadOnly )
menu.addAction( i18n( "Remove logo" ), this, SLOT( deleteImage() ) );
}
}
menu.exec( event->globalPos() );
}
void ImageWidget::changeImage()
{
const KUrl url = KFileDialog::getOpenUrl( QString(), KImageIO::pattern(), this );
if ( url.isValid() ) {
bool ok = false;
const QImage image = imageLoader()->loadImage( url, &ok );
if ( ok ) {
mImage = image;
mHasImage = true;
updateView();
}
}
}
void ImageWidget::saveImage()
{
const QString fileName = KFileDialog::getSaveFileName( KUrl(), KImageIO::pattern(), this );
if ( !fileName.isEmpty() )
mImage.save( fileName );
}
void ImageWidget::deleteImage()
{
mHasImage = false;
updateView();
}
ImageLoader* ImageWidget::imageLoader()
{
if ( !mImageLoader )
mImageLoader = new ImageLoader;
return mImageLoader;
}
/*
This file is part of KContactManager.
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H
#include <QtCore/QPoint>
#include <QtGui/QPushButton>
namespace KABC
{
class Addressee;
}
class ImageLoader;
class ImageWidget : public QPushButton
{
Q_OBJECT
public:
enum Type {
Photo,
Logo
};
ImageWidget( Type type, QWidget *parent = 0 );
~ImageWidget();
void loadContact( const KABC::Addressee &contact );
void storeContact( KABC::Addressee &contact ) const;
void setReadOnly( bool readOnly );
protected:
// image drop handling
virtual void dragEnterEvent( QDragEnterEvent* );
virtual void dropEvent( QDropEvent* );
// image drag handling
virtual void mousePressEvent( QMouseEvent* );
virtual void mouseMoveEvent( QMouseEvent* );
// context menu handling
virtual void contextMenuEvent( QContextMenuEvent* );
private Q_SLOTS:
void updateView();
void changeImage();
void saveImage();
void deleteImage();
private:
ImageLoader *imageLoader();
Type mType;
QImage mImage;
bool mHasImage;
bool mReadOnly;
QPoint mDragStartPos;
ImageLoader *mImageLoader;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment