24 #include <tdeabc/picture.h>
27 #include <tdefiledialog.h>
28 #include <tdeglobalsettings.h>
29 #include <kiconloader.h>
31 #include <tdeio/netaccess.h>
32 #include <tdelocale.h>
33 #include <tdemessagebox.h>
35 #include <libtdepim/kpixmapregionselectordialog.h>
37 #include <tqapplication.h>
38 #include <tqdragobject.h>
39 #include <tqeventloop.h>
40 #include <tqgroupbox.h>
44 #include <tqpopupmenu.h>
48 #include "imagewidget.h"
50 ImageLoader::ImageLoader( TQWidget *parent )
51 : TQObject( 0,
"ImageLoader" ), mParent( parent )
55 TDEABC::Picture ImageLoader::loadPicture(
const KURL &url,
bool *ok )
57 TDEABC::Picture picture;
66 if ( url.isLocalFile() ) {
67 image.load( url.path() );
68 picture.setData( image );
70 }
else if ( TDEIO::NetAccess::download( url, tempFile, mParent ) ) {
71 image.load( tempFile );
72 picture.setData( image );
74 TDEIO::NetAccess::removeTempFile( tempFile );
79 KMessageBox::sorry( mParent, i18n(
"This contact's image cannot be found." ) );
83 TQPixmap pixmap = picture.data();
85 TQPixmap selectedPixmap = KPIM::KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 100, 140, mParent );
86 if ( selectedPixmap.isNull() ) {
91 image = selectedPixmap;
92 if ( image.height() != 140 || image.width() != 100 ) {
93 if ( image.height() > image.width() )
94 image = image.scaleHeight( 140 );
96 image = image.scaleWidth( 100 );
99 picture.setData( image );
106 ImageButton::ImageButton(
const TQString &title, TQWidget *parent )
107 : TQPushButton( title, parent ),
108 mReadOnly( false ), mImageLoader( 0 )
110 setAcceptDrops(
true );
112 connect(
this, TQ_SIGNAL( clicked() ), TQ_SLOT( load() ) );
115 void ImageButton::setReadOnly(
bool readOnly )
117 mReadOnly = readOnly;
120 void ImageButton::setPicture(
const TDEABC::Picture &picture )
126 TDEABC::Picture ImageButton::picture()
const
131 void ImageButton::setImageLoader(
ImageLoader *loader )
133 mImageLoader = loader;
136 void ImageButton::startDrag()
138 if ( !mPicture.data().isNull() ) {
139 TQImageDrag *drag =
new TQImageDrag( mPicture.data(),
this );
144 void ImageButton::updateGUI()
146 if ( mPicture.data().isNull() )
147 setPixmap( TDEGlobal::iconLoader()->iconPath(
"preferences-desktop-personal", TDEIcon::Desktop ) );
149 setPixmap( mPicture.data() );
152 void ImageButton::dragEnterEvent( TQDragEnterEvent *event )
154 bool accepted =
false;
156 if ( TQImageDrag::canDecode( event ) )
159 if ( TQUriDrag::canDecode( event ) )
162 event->accept( accepted );
165 void ImageButton::dropEvent( TQDropEvent *event )
170 if ( TQImageDrag::canDecode( event ) ) {
173 if ( TQImageDrag::decode( event, pm ) ) {
174 mPicture.setData( pm.convertToImage() );
180 if ( TQUriDrag::canDecode( event ) ) {
182 if ( KURLDrag::decode( event, urls ) ) {
183 if ( urls.isEmpty() ) {
184 event->accept(
false );
189 if ( mImageLoader ) {
191 TDEABC::Picture pic = mImageLoader->loadPicture( urls[ 0 ], &ok );
201 void ImageButton::mousePressEvent( TQMouseEvent *event )
203 mDragStartPos =
event->pos();
204 TQPushButton::mousePressEvent( event );
207 void ImageButton::mouseMoveEvent( TQMouseEvent *event )
209 if ( (event->state() & TQt::LeftButton) &&
210 (event->pos() - mDragStartPos).manhattanLength() >
211 TDEGlobalSettings::dndEventDelay() ) {
216 void ImageButton::contextMenuEvent( TQContextMenuEvent *event )
218 TQPopupMenu menu(
this );
219 menu.insertItem( i18n(
"Reset" ),
this, TQ_SLOT( clear() ) );
220 menu.exec( event->globalPos() );
223 void ImageButton::load()
227 KURL url = KFileDialog::getOpenURL( TQString(), KImageIO::pattern(),
this );
228 if ( url.isValid() ) {
229 if ( mImageLoader ) {
231 TDEABC::Picture pic = mImageLoader->loadPicture( url, &ok );
241 void ImageButton::clear()
243 mPicture = TDEABC::Picture();
249 ImageBaseWidget::ImageBaseWidget(
const TQString &title,
250 TQWidget *parent,
const char *name )
251 : TQWidget( parent, name ), mReadOnly( false )
255 TQVBoxLayout *topLayout =
new TQVBoxLayout(
this, KDialog::marginHint(),
256 KDialog::spacingHint() );
257 TQGroupBox *box =
new TQGroupBox( 0, TQt::Vertical, title,
this );
258 TQVBoxLayout *layout =
new TQVBoxLayout( box->layout(), KDialog::spacingHint() );
260 mImageButton =
new ImageButton( i18n(
"Picture" ), box );
261 mImageButton->setFixedSize( 100, 140 );
262 mImageButton->setImageLoader( mImageLoader );
263 layout->addWidget( mImageButton );
265 topLayout->addWidget( box );
267 connect( mImageButton, TQ_SIGNAL( changed() ), TQ_SIGNAL( changed() ) );
270 ImageBaseWidget::~ImageBaseWidget()
276 void ImageBaseWidget::setReadOnly(
bool readOnly )
278 mReadOnly = readOnly;
279 mImageButton->setReadOnly( mReadOnly );
282 void ImageBaseWidget::setImage(
const TDEABC::Picture &photo )
284 mImageButton->setPicture( photo );
287 TDEABC::Picture ImageBaseWidget::image()
const
289 return mImageButton->picture();
293 ImageWidget::ImageWidget( TDEABC::AddressBook *ab, TQWidget *parent,
const char *name )
294 : KAB::ContactEditorWidget( ab, parent, name )
296 TQHBoxLayout *layout =
new TQHBoxLayout(
this, KDialog::marginHint(),
297 KDialog::spacingHint() );
299 mPhotoWidget =
new ImageBaseWidget( TDEABC::Addressee::photoLabel(),
this );
300 layout->addWidget( mPhotoWidget );
302 mLogoWidget =
new ImageBaseWidget( TDEABC::Addressee::logoLabel(),
this );
303 layout->addWidget( mLogoWidget );
305 connect( mPhotoWidget, TQ_SIGNAL( changed() ), TQ_SLOT( setModified() ) );
306 connect( mLogoWidget, TQ_SIGNAL( changed() ), TQ_SLOT( setModified() ) );
309 void ImageWidget::loadContact( TDEABC::Addressee *addr )
311 mPhotoWidget->setImage( addr->photo() );
312 mLogoWidget->setImage( addr->logo() );
315 void ImageWidget::storeContact( TDEABC::Addressee *addr )
317 addr->setPhoto( mPhotoWidget->image() );
318 addr->setLogo( mLogoWidget->image() );
321 void ImageWidget::setReadOnly(
bool readOnly )
323 mPhotoWidget->setReadOnly( readOnly );
324 mLogoWidget->setReadOnly( readOnly );
327 #include "imagewidget.moc"