00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <kabc/picture.h>
00025 #include <kdebug.h>
00026 #include <kdialog.h>
00027 #include <kglobalsettings.h>
00028 #include <kiconloader.h>
00029 #include <kio/netaccess.h>
00030 #include <klocale.h>
00031 #include <kurlrequester.h>
00032 #include <kimageio.h>
00033
00034 #include <qcheckbox.h>
00035 #include <qdragobject.h>
00036 #include <qgroupbox.h>
00037 #include <qlabel.h>
00038 #include <qlayout.h>
00039 #include <qpixmap.h>
00040 #include <qtooltip.h>
00041
00042 #include "imagewidget.h"
00043
00044 ImageLabel::ImageLabel( const QString &title, QWidget *parent )
00045 : QLabel( title, parent ), mReadOnly( false )
00046 {
00047 setAcceptDrops( true );
00048 }
00049
00050 void ImageLabel::setReadOnly( bool readOnly )
00051 {
00052 mReadOnly = readOnly;
00053 }
00054
00055 void ImageLabel::startDrag()
00056 {
00057 if ( pixmap() && !pixmap()->isNull() ) {
00058 QImageDrag *drag = new QImageDrag( pixmap()->convertToImage(), this );
00059 drag->dragCopy();
00060 }
00061 }
00062
00063 void ImageLabel::dragEnterEvent( QDragEnterEvent *event )
00064 {
00065 event->accept( QImageDrag::canDecode( event ) );
00066 }
00067
00068 void ImageLabel::dropEvent( QDropEvent *event )
00069 {
00070 QPixmap pm;
00071 if ( QImageDrag::decode( event, pm ) && !mReadOnly ) {
00072 setPixmap( pm );
00073 emit changed();
00074 }
00075 }
00076
00077 void ImageLabel::mousePressEvent( QMouseEvent *event )
00078 {
00079 mDragStartPos = event->pos();
00080 QLabel::mousePressEvent( event );
00081 }
00082
00083 void ImageLabel::mouseMoveEvent( QMouseEvent *event )
00084 {
00085 if ( (event->state() & LeftButton) &&
00086 (event->pos() - mDragStartPos).manhattanLength() >
00087 KGlobalSettings::dndEventDelay() ) {
00088 startDrag();
00089 }
00090 }
00091
00092
00093 ImageBaseWidget::ImageBaseWidget( const QString &title, QWidget *parent, const char *name )
00094 : QWidget( parent, name ), mReadOnly( false )
00095 {
00096 QHBoxLayout *topLayout = new QHBoxLayout( this, KDialog::marginHint(),
00097 KDialog::spacingHint() );
00098 QGroupBox *box = new QGroupBox( 0, Qt::Vertical, title, this );
00099 QGridLayout *boxLayout = new QGridLayout( box->layout(), 3, 3,
00100 KDialog::spacingHint() );
00101 boxLayout->setRowStretch( 2, 1 );
00102
00103 mImageLabel = new ImageLabel( i18n( "Picture" ), box );
00104 mImageLabel->setFixedSize( 100, 140 );
00105 mImageLabel->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00106 boxLayout->addMultiCellWidget( mImageLabel, 0, 2, 0, 0, AlignTop );
00107
00108 mImageUrl = new KURLRequester( box );
00109 mImageUrl->setFilter( KImageIO::pattern() );
00110 mImageUrl->setMode( KFile::File );
00111 boxLayout->addWidget( mImageUrl, 0, 1 );
00112
00113 mClearButton = new QPushButton( box );
00114 mClearButton->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) );
00115 mClearButton->setPixmap( SmallIcon( "clear_left" ) );
00116 mClearButton->setEnabled( false );
00117 boxLayout->addWidget( mClearButton, 0, 2 );
00118
00119 mUseImageUrl = new QCheckBox( i18n( "Store as URL" ), box );
00120 mUseImageUrl->setEnabled( false );
00121 boxLayout->addMultiCellWidget( mUseImageUrl, 1, 1, 1, 2 );
00122
00123 topLayout->addWidget( box );
00124
00125 QToolTip::add( mClearButton, i18n( "Reset" ) );
00126
00127 connect( mImageLabel, SIGNAL( changed() ),
00128 SIGNAL( changed() ) );
00129 connect( mImageUrl, SIGNAL( textChanged( const QString& ) ),
00130 SIGNAL( changed() ) );
00131 connect( mImageUrl, SIGNAL( urlSelected( const QString& ) ),
00132 SLOT( loadImage() ) );
00133 connect( mImageUrl, SIGNAL( urlSelected( const QString& ) ),
00134 SIGNAL( changed() ) );
00135 connect( mImageUrl, SIGNAL( urlSelected( const QString& ) ),
00136 SLOT( updateGUI() ) );
00137 connect( mUseImageUrl, SIGNAL( toggled( bool ) ),
00138 SIGNAL( changed() ) );
00139 connect( mClearButton, SIGNAL( clicked() ),
00140 SLOT( clear() ) );
00141 }
00142
00143 ImageBaseWidget::~ImageBaseWidget()
00144 {
00145 }
00146
00147 void ImageBaseWidget::setReadOnly( bool readOnly )
00148 {
00149 mReadOnly = readOnly;
00150 mImageLabel->setReadOnly( mReadOnly );
00151 mImageUrl->setEnabled( !mReadOnly );
00152 }
00153
00154 void ImageBaseWidget::setImage( const KABC::Picture &photo )
00155 {
00156 bool blocked = signalsBlocked();
00157 blockSignals( true );
00158
00159 if ( photo.isIntern() ) {
00160 QPixmap px = photo.data();
00161
00162 if ( px.height() != 140 || px.width() != 100 ) {
00163 if ( px.height() > px.width() )
00164 px = px.convertToImage().scaleHeight( 140 );
00165 else
00166 px = px.convertToImage().scaleWidth( 100 );
00167 }
00168
00169 mImageLabel->setPixmap( px );
00170 mUseImageUrl->setChecked( false );
00171 } else {
00172 mImageUrl->setURL( photo.url() );
00173 if ( !photo.url().isEmpty() )
00174 mUseImageUrl->setChecked( true );
00175 loadImage();
00176 }
00177
00178 blockSignals( blocked );
00179
00180 updateGUI();
00181 }
00182
00183 KABC::Picture ImageBaseWidget::image() const
00184 {
00185 KABC::Picture photo;
00186
00187 if ( mUseImageUrl->isChecked() )
00188 photo.setUrl( mImageUrl->url() );
00189 else {
00190 if ( mImageLabel->pixmap() )
00191 photo.setData( mImageLabel->pixmap()->convertToImage() );
00192 }
00193
00194 return photo;
00195 }
00196
00197 void ImageBaseWidget::loadImage()
00198 {
00199 mImageLabel->setPixmap( loadPixmap( KURL( mImageUrl->url() ) ) );
00200 }
00201
00202 void ImageBaseWidget::updateGUI()
00203 {
00204 if ( !mReadOnly ) {
00205 mUseImageUrl->setEnabled( !mImageUrl->url().isEmpty() );
00206 mClearButton->setEnabled( !mImageUrl->url().isEmpty() || ( mImageLabel->pixmap() && !mImageLabel->pixmap()->isNull() ) );
00207 }
00208 }
00209
00210 void ImageBaseWidget::clear()
00211 {
00212 mImageLabel->clear();
00213 mImageUrl->clear();
00214 mUseImageUrl->setChecked( false );
00215
00216 updateGUI();
00217
00218 emit changed();
00219 }
00220
00221 void ImageBaseWidget::imageChanged()
00222 {
00223 updateGUI();
00224
00225 emit changed();
00226 }
00227
00228 QPixmap ImageBaseWidget::loadPixmap( const KURL &url )
00229 {
00230 QString tempFile;
00231 QPixmap pixmap;
00232
00233 if ( url.isEmpty() )
00234 return pixmap;
00235
00236 if ( url.isLocalFile() )
00237 pixmap = QPixmap( url.path() );
00238 else if ( KIO::NetAccess::download( url, tempFile, this ) ) {
00239 pixmap = QPixmap( tempFile );
00240 KIO::NetAccess::removeTempFile( tempFile );
00241 }
00242
00243 if ( pixmap.height() != 140 || pixmap.width() != 100 ) {
00244 if ( pixmap.height() > pixmap.width() )
00245 pixmap = pixmap.convertToImage().scaleHeight( 140 );
00246 else
00247 pixmap = pixmap.convertToImage().scaleWidth( 100 );
00248 }
00249
00250 return pixmap;
00251 }
00252
00253 ImageWidget::ImageWidget( KABC::AddressBook *ab, QWidget *parent, const char *name )
00254 : KAB::ContactEditorWidget( ab, parent, name )
00255 {
00256 QHBoxLayout *layout = new QHBoxLayout( this, KDialog::marginHint(),
00257 KDialog::spacingHint() );
00258
00259 mPhotoWidget = new ImageBaseWidget( KABC::Addressee::photoLabel(), this );
00260 layout->addWidget( mPhotoWidget );
00261
00262 mLogoWidget = new ImageBaseWidget( KABC::Addressee::logoLabel(), this );
00263 layout->addWidget( mLogoWidget );
00264
00265 connect( mPhotoWidget, SIGNAL( changed() ), SLOT( setModified() ) );
00266 connect( mLogoWidget, SIGNAL( changed() ), SLOT( setModified() ) );
00267 }
00268
00269 void ImageWidget::loadContact( KABC::Addressee *addr )
00270 {
00271 mPhotoWidget->setImage( addr->photo() );
00272 mLogoWidget->setImage( addr->logo() );
00273 }
00274
00275 void ImageWidget::storeContact( KABC::Addressee *addr )
00276 {
00277 addr->setPhoto( mPhotoWidget->image() );
00278 addr->setLogo( mLogoWidget->image() );
00279 }
00280
00281 void ImageWidget::setReadOnly( bool readOnly )
00282 {
00283 mPhotoWidget->setReadOnly( readOnly );
00284 mLogoWidget->setReadOnly( readOnly );
00285 }
00286
00287 #include "imagewidget.moc"