kaddressbook

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