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 <kfiledialog.h>
00028 #include <kglobalsettings.h>
00029 #include <kiconloader.h>
00030 #include <kimageio.h>
00031 #include <kio/netaccess.h>
00032 #include <klocale.h>
00033 #include <kmessagebox.h>
00034 #include <kurldrag.h>
00035 #include <libkdepim/kpixmapregionselectordialog.h>
00036
00037 #include <tqapplication.h>
00038 #include <tqdragobject.h>
00039 #include <tqeventloop.h>
00040 #include <tqgroupbox.h>
00041 #include <tqlabel.h>
00042 #include <tqlayout.h>
00043 #include <tqpixmap.h>
00044 #include <tqpopupmenu.h>
00045
00046 #include <unistd.h>
00047
00048 #include "imagewidget.h"
00049
00050 ImageLoader::ImageLoader( TQWidget *parent )
00051 : TQObject( 0, "ImageLoader" ), mParent( parent )
00052 {
00053 }
00054
00055 KABC::Picture ImageLoader::loadPicture( const KURL &url, bool *ok )
00056 {
00057 KABC::Picture picture;
00058 TQString tempFile;
00059
00060 if ( url.isEmpty() )
00061 return picture;
00062
00063 (*ok) = false;
00064
00065 TQImage image;
00066 if ( url.isLocalFile() ) {
00067 image.load( url.path() );
00068 picture.setData( image );
00069 (*ok) = true;
00070 } else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
00071 image.load( tempFile );
00072 picture.setData( image );
00073 (*ok) = true;
00074 KIO::NetAccess::removeTempFile( tempFile );
00075 }
00076
00077 if ( !(*ok) ) {
00078
00079 KMessageBox::sorry( mParent, i18n( "This contact's image cannot be found." ) );
00080 return picture;
00081 }
00082
00083 TQPixmap pixmap = picture.data();
00084
00085 TQPixmap selectedPixmap = KPIM::KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 100, 140, mParent );
00086 if ( selectedPixmap.isNull() ) {
00087 (*ok) = false;
00088 return picture;
00089 }
00090
00091 image = selectedPixmap;
00092 if ( image.height() != 140 || image.width() != 100 ) {
00093 if ( image.height() > image.width() )
00094 image = image.scaleHeight( 140 );
00095 else
00096 image = image.scaleWidth( 100 );
00097 }
00098
00099 picture.setData( image );
00100 (*ok) = true;
00101
00102 return picture;
00103 }
00104
00105
00106 ImageButton::ImageButton( const TQString &title, TQWidget *parent )
00107 : TQPushButton( title, parent ),
00108 mReadOnly( false ), mImageLoader( 0 )
00109 {
00110 setAcceptDrops( true );
00111
00112 connect( this, TQT_SIGNAL( clicked() ), TQT_SLOT( load() ) );
00113 }
00114
00115 void ImageButton::setReadOnly( bool readOnly )
00116 {
00117 mReadOnly = readOnly;
00118 }
00119
00120 void ImageButton::setPicture( const KABC::Picture &picture )
00121 {
00122 mPicture = picture;
00123 updateGUI();
00124 }
00125
00126 KABC::Picture ImageButton::picture() const
00127 {
00128 return mPicture;
00129 }
00130
00131 void ImageButton::setImageLoader( ImageLoader *loader )
00132 {
00133 mImageLoader = loader;
00134 }
00135
00136 void ImageButton::startDrag()
00137 {
00138 if ( !mPicture.data().isNull() ) {
00139 TQImageDrag *drag = new TQImageDrag( mPicture.data(), this );
00140 drag->dragCopy();
00141 }
00142 }
00143
00144 void ImageButton::updateGUI()
00145 {
00146 if ( mPicture.data().isNull() )
00147 setPixmap( KGlobal::iconLoader()->iconPath( "personal", KIcon::Desktop ) );
00148 else
00149 setPixmap( mPicture.data() );
00150 }
00151
00152 void ImageButton::dragEnterEvent( TQDragEnterEvent *event )
00153 {
00154 bool accepted = false;
00155
00156 if ( TQImageDrag::canDecode( event ) )
00157 accepted = true;
00158
00159 if ( TQUriDrag::canDecode( event ) )
00160 accepted = true;
00161
00162 event->accept( accepted );
00163 }
00164
00165 void ImageButton::dropEvent( TQDropEvent *event )
00166 {
00167 if ( mReadOnly )
00168 return;
00169
00170 if ( TQImageDrag::canDecode( event ) ) {
00171 TQPixmap pm;
00172
00173 if ( TQImageDrag::decode( event, pm ) ) {
00174 mPicture.setData( pm.convertToImage() );
00175 updateGUI();
00176 emit changed();
00177 }
00178 }
00179
00180 if ( TQUriDrag::canDecode( event ) ) {
00181 KURL::List urls;
00182 if ( KURLDrag::decode( event, urls ) ) {
00183 if ( urls.isEmpty() ) {
00184 event->accept( false );
00185 return;
00186 }
00187 }
00188
00189 if ( mImageLoader ) {
00190 bool ok = false;
00191 KABC::Picture pic = mImageLoader->loadPicture( urls[ 0 ], &ok );
00192 if ( ok ) {
00193 mPicture = pic;
00194 updateGUI();
00195 emit changed();
00196 }
00197 }
00198 }
00199 }
00200
00201 void ImageButton::mousePressEvent( TQMouseEvent *event )
00202 {
00203 mDragStartPos = event->pos();
00204 TQPushButton::mousePressEvent( event );
00205 }
00206
00207 void ImageButton::mouseMoveEvent( TQMouseEvent *event )
00208 {
00209 if ( (event->state() & Qt::LeftButton) &&
00210 (event->pos() - mDragStartPos).manhattanLength() >
00211 KGlobalSettings::dndEventDelay() ) {
00212 startDrag();
00213 }
00214 }
00215
00216 void ImageButton::contextMenuEvent( TQContextMenuEvent *event )
00217 {
00218 TQPopupMenu menu( this );
00219 menu.insertItem( i18n( "Reset" ), this, TQT_SLOT( clear() ) );
00220 menu.exec( event->globalPos() );
00221 }
00222
00223 void ImageButton::load()
00224 {
00225 if ( mReadOnly )
00226 return;
00227 KURL url = KFileDialog::getOpenURL( TQString(), KImageIO::pattern(), this );
00228 if ( url.isValid() ) {
00229 if ( mImageLoader ) {
00230 bool ok = false;
00231 KABC::Picture pic = mImageLoader->loadPicture( url, &ok );
00232 if ( ok ) {
00233 mPicture = pic;
00234 updateGUI();
00235 emit changed();
00236 }
00237 }
00238 }
00239 }
00240
00241 void ImageButton::clear()
00242 {
00243 mPicture = KABC::Picture();
00244 updateGUI();
00245
00246 emit changed();
00247 }
00248
00249 ImageBaseWidget::ImageBaseWidget( const TQString &title,
00250 TQWidget *parent, const char *name )
00251 : TQWidget( parent, name ), mReadOnly( false )
00252 {
00253 mImageLoader = new ImageLoader( this );
00254
00255 TQVBoxLayout *topLayout = new TQVBoxLayout( this, KDialog::marginHint(),
00256 KDialog::spacingHint() );
00257 TQGroupBox *box = new TQGroupBox( 0, Qt::Vertical, title, this );
00258 TQVBoxLayout *layout = new TQVBoxLayout( box->layout(), KDialog::spacingHint() );
00259
00260 mImageButton = new ImageButton( i18n( "Picture" ), box );
00261 mImageButton->setFixedSize( 100, 140 );
00262 mImageButton->setImageLoader( mImageLoader );
00263 layout->addWidget( mImageButton );
00264
00265 topLayout->addWidget( box );
00266
00267 connect( mImageButton, TQT_SIGNAL( changed() ), TQT_SIGNAL( changed() ) );
00268 }
00269
00270 ImageBaseWidget::~ImageBaseWidget()
00271 {
00272 delete mImageLoader;
00273 mImageLoader = 0;
00274 }
00275
00276 void ImageBaseWidget::setReadOnly( bool readOnly )
00277 {
00278 mReadOnly = readOnly;
00279 mImageButton->setReadOnly( mReadOnly );
00280 }
00281
00282 void ImageBaseWidget::setImage( const KABC::Picture &photo )
00283 {
00284 mImageButton->setPicture( photo );
00285 }
00286
00287 KABC::Picture ImageBaseWidget::image() const
00288 {
00289 return mImageButton->picture();
00290 }
00291
00292
00293 ImageWidget::ImageWidget( KABC::AddressBook *ab, TQWidget *parent, const char *name )
00294 : KAB::ContactEditorWidget( ab, parent, name )
00295 {
00296 TQHBoxLayout *layout = new TQHBoxLayout( this, KDialog::marginHint(),
00297 KDialog::spacingHint() );
00298
00299 mPhotoWidget = new ImageBaseWidget( KABC::Addressee::photoLabel(), this );
00300 layout->addWidget( mPhotoWidget );
00301
00302 mLogoWidget = new ImageBaseWidget( KABC::Addressee::logoLabel(), this );
00303 layout->addWidget( mLogoWidget );
00304
00305 connect( mPhotoWidget, TQT_SIGNAL( changed() ), TQT_SLOT( setModified() ) );
00306 connect( mLogoWidget, TQT_SIGNAL( changed() ), TQT_SLOT( setModified() ) );
00307 }
00308
00309 void ImageWidget::loadContact( KABC::Addressee *addr )
00310 {
00311 mPhotoWidget->setImage( addr->photo() );
00312 mLogoWidget->setImage( addr->logo() );
00313 }
00314
00315 void ImageWidget::storeContact( KABC::Addressee *addr )
00316 {
00317 addr->setPhoto( mPhotoWidget->image() );
00318 addr->setLogo( mLogoWidget->image() );
00319 }
00320
00321 void ImageWidget::setReadOnly( bool readOnly )
00322 {
00323 mPhotoWidget->setReadOnly( readOnly );
00324 mLogoWidget->setReadOnly( readOnly );
00325 }
00326
00327 #include "imagewidget.moc"