TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
8.0KB

  1. /*
  2. This file is part of KAddressBook.
  3. Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. As a special exception, permission is given to link this program
  16. with any edition of TQt, and distribute the resulting executable,
  17. without including the source code for TQt in the source distribution.
  18. */
  19. #include <kabc/picture.h>
  20. #include <kdebug.h>
  21. #include <kdialog.h>
  22. #include <kfiledialog.h>
  23. #include <kglobalsettings.h>
  24. #include <kiconloader.h>
  25. #include <kimageio.h>
  26. #include <kio/netaccess.h>
  27. #include <klocale.h>
  28. #include <kmessagebox.h>
  29. #include <kurldrag.h>
  30. #include <libtdepim/kpixmapregionselectordialog.h>
  31. #include <tqapplication.h>
  32. #include <tqdragobject.h>
  33. #include <tqeventloop.h>
  34. #include <tqgroupbox.h>
  35. #include <tqlabel.h>
  36. #include <tqlayout.h>
  37. #include <tqpixmap.h>
  38. #include <tqpopupmenu.h>
  39. #include <unistd.h>
  40. #include "imagewidget.h"
  41. ImageLoader::ImageLoader( TQWidget *parent )
  42. : TQObject( 0, "ImageLoader" ), mParent( parent )
  43. {
  44. }
  45. KABC::Picture ImageLoader::loadPicture( const KURL &url, bool *ok )
  46. {
  47. KABC::Picture picture;
  48. TQString tempFile;
  49. if ( url.isEmpty() )
  50. return picture;
  51. (*ok) = false;
  52. TQImage image;
  53. if ( url.isLocalFile() ) {
  54. image.load( url.path() );
  55. picture.setData( image );
  56. (*ok) = true;
  57. } else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
  58. image.load( tempFile );
  59. picture.setData( image );
  60. (*ok) = true;
  61. KIO::NetAccess::removeTempFile( tempFile );
  62. }
  63. if ( !(*ok) ) {
  64. // image does not exist (any more)
  65. KMessageBox::sorry( mParent, i18n( "This contact's image cannot be found." ) );
  66. return picture;
  67. }
  68. TQPixmap pixmap = picture.data();
  69. TQPixmap selectedPixmap = KPIM::KPixmapRegionSelectorDialog::getSelectedImage( pixmap, 100, 140, mParent );
  70. if ( selectedPixmap.isNull() ) {
  71. (*ok) = false;
  72. return picture;
  73. }
  74. image = selectedPixmap;
  75. if ( image.height() != 140 || image.width() != 100 ) {
  76. if ( image.height() > image.width() )
  77. image = image.scaleHeight( 140 );
  78. else
  79. image = image.scaleWidth( 100 );
  80. }
  81. picture.setData( image );
  82. (*ok) = true;
  83. return picture;
  84. }
  85. ImageButton::ImageButton( const TQString &title, TQWidget *parent )
  86. : TQPushButton( title, parent ),
  87. mReadOnly( false ), mImageLoader( 0 )
  88. {
  89. setAcceptDrops( true );
  90. connect( this, TQT_SIGNAL( clicked() ), TQT_SLOT( load() ) );
  91. }
  92. void ImageButton::setReadOnly( bool readOnly )
  93. {
  94. mReadOnly = readOnly;
  95. }
  96. void ImageButton::setPicture( const KABC::Picture &picture )
  97. {
  98. mPicture = picture;
  99. updateGUI();
  100. }
  101. KABC::Picture ImageButton::picture() const
  102. {
  103. return mPicture;
  104. }
  105. void ImageButton::setImageLoader( ImageLoader *loader )
  106. {
  107. mImageLoader = loader;
  108. }
  109. void ImageButton::startDrag()
  110. {
  111. if ( !mPicture.data().isNull() ) {
  112. TQImageDrag *drag = new TQImageDrag( mPicture.data(), this );
  113. drag->dragCopy();
  114. }
  115. }
  116. void ImageButton::updateGUI()
  117. {
  118. if ( mPicture.data().isNull() )
  119. setPixmap( KGlobal::iconLoader()->iconPath( "personal", KIcon::Desktop ) );
  120. else
  121. setPixmap( mPicture.data() );
  122. }
  123. void ImageButton::dragEnterEvent( TQDragEnterEvent *event )
  124. {
  125. bool accepted = false;
  126. if ( TQImageDrag::canDecode( event ) )
  127. accepted = true;
  128. if ( TQUriDrag::canDecode( event ) )
  129. accepted = true;
  130. event->accept( accepted );
  131. }
  132. void ImageButton::dropEvent( TQDropEvent *event )
  133. {
  134. if ( mReadOnly )
  135. return;
  136. if ( TQImageDrag::canDecode( event ) ) {
  137. TQPixmap pm;
  138. if ( TQImageDrag::decode( event, pm ) ) {
  139. mPicture.setData( pm.convertToImage() );
  140. updateGUI();
  141. emit changed();
  142. }
  143. }
  144. if ( TQUriDrag::canDecode( event ) ) {
  145. KURL::List urls;
  146. if ( KURLDrag::decode( event, urls ) ) {
  147. if ( urls.isEmpty() ) { // oops, no data
  148. event->accept( false );
  149. return;
  150. }
  151. }
  152. if ( mImageLoader ) {
  153. bool ok = false;
  154. KABC::Picture pic = mImageLoader->loadPicture( urls[ 0 ], &ok );
  155. if ( ok ) {
  156. mPicture = pic;
  157. updateGUI();
  158. emit changed();
  159. }
  160. }
  161. }
  162. }
  163. void ImageButton::mousePressEvent( TQMouseEvent *event )
  164. {
  165. mDragStartPos = event->pos();
  166. TQPushButton::mousePressEvent( event );
  167. }
  168. void ImageButton::mouseMoveEvent( TQMouseEvent *event )
  169. {
  170. if ( (event->state() & Qt::LeftButton) &&
  171. (event->pos() - mDragStartPos).manhattanLength() >
  172. KGlobalSettings::dndEventDelay() ) {
  173. startDrag();
  174. }
  175. }
  176. void ImageButton::contextMenuEvent( TQContextMenuEvent *event )
  177. {
  178. TQPopupMenu menu( this );
  179. menu.insertItem( i18n( "Reset" ), this, TQT_SLOT( clear() ) );
  180. menu.exec( event->globalPos() );
  181. }
  182. void ImageButton::load()
  183. {
  184. if ( mReadOnly )
  185. return;
  186. KURL url = KFileDialog::getOpenURL( TQString(), KImageIO::pattern(), this );
  187. if ( url.isValid() ) {
  188. if ( mImageLoader ) {
  189. bool ok = false;
  190. KABC::Picture pic = mImageLoader->loadPicture( url, &ok );
  191. if ( ok ) {
  192. mPicture = pic;
  193. updateGUI();
  194. emit changed();
  195. }
  196. }
  197. }
  198. }
  199. void ImageButton::clear()
  200. {
  201. mPicture = KABC::Picture();
  202. updateGUI();
  203. emit changed();
  204. }
  205. ImageBaseWidget::ImageBaseWidget( const TQString &title,
  206. TQWidget *parent, const char *name )
  207. : TQWidget( parent, name ), mReadOnly( false )
  208. {
  209. mImageLoader = new ImageLoader( this );
  210. TQVBoxLayout *topLayout = new TQVBoxLayout( this, KDialog::marginHint(),
  211. KDialog::spacingHint() );
  212. TQGroupBox *box = new TQGroupBox( 0, Qt::Vertical, title, this );
  213. TQVBoxLayout *tqlayout = new TQVBoxLayout( box->tqlayout(), KDialog::spacingHint() );
  214. mImageButton = new ImageButton( i18n( "Picture" ), box );
  215. mImageButton->setFixedSize( 100, 140 );
  216. mImageButton->setImageLoader( mImageLoader );
  217. tqlayout->addWidget( mImageButton );
  218. topLayout->addWidget( box );
  219. connect( mImageButton, TQT_SIGNAL( changed() ), TQT_SIGNAL( changed() ) );
  220. }
  221. ImageBaseWidget::~ImageBaseWidget()
  222. {
  223. delete mImageLoader;
  224. mImageLoader = 0;
  225. }
  226. void ImageBaseWidget::setReadOnly( bool readOnly )
  227. {
  228. mReadOnly = readOnly;
  229. mImageButton->setReadOnly( mReadOnly );
  230. }
  231. void ImageBaseWidget::setImage( const KABC::Picture &photo )
  232. {
  233. mImageButton->setPicture( photo );
  234. }
  235. KABC::Picture ImageBaseWidget::image() const
  236. {
  237. return mImageButton->picture();
  238. }
  239. ImageWidget::ImageWidget( KABC::AddressBook *ab, TQWidget *parent, const char *name )
  240. : KAB::ContactEditorWidget( ab, parent, name )
  241. {
  242. TQHBoxLayout *tqlayout = new TQHBoxLayout( this, KDialog::marginHint(),
  243. KDialog::spacingHint() );
  244. mPhotoWidget = new ImageBaseWidget( KABC::Addressee::photoLabel(), this );
  245. tqlayout->addWidget( mPhotoWidget );
  246. mLogoWidget = new ImageBaseWidget( KABC::Addressee::logoLabel(), this );
  247. tqlayout->addWidget( mLogoWidget );
  248. connect( mPhotoWidget, TQT_SIGNAL( changed() ), TQT_SLOT( setModified() ) );
  249. connect( mLogoWidget, TQT_SIGNAL( changed() ), TQT_SLOT( setModified() ) );
  250. }
  251. void ImageWidget::loadContact( KABC::Addressee *addr )
  252. {
  253. mPhotoWidget->setImage( addr->photo() );
  254. mLogoWidget->setImage( addr->logo() );
  255. }
  256. void ImageWidget::storeContact( KABC::Addressee *addr )
  257. {
  258. addr->setPhoto( mPhotoWidget->image() );
  259. addr->setLogo( mLogoWidget->image() );
  260. }
  261. void ImageWidget::setReadOnly( bool readOnly )
  262. {
  263. mPhotoWidget->setReadOnly( readOnly );
  264. mLogoWidget->setReadOnly( readOnly );
  265. }
  266. #include "imagewidget.moc"