summaryrefslogtreecommitdiffabout
path: root/kaddressbook/imagewidget.cpp
Unidiff
Diffstat (limited to 'kaddressbook/imagewidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kaddressbook/imagewidget.cpp299
1 files changed, 299 insertions, 0 deletions
diff --git a/kaddressbook/imagewidget.cpp b/kaddressbook/imagewidget.cpp
new file mode 100644
index 0000000..49d456b
--- a/dev/null
+++ b/kaddressbook/imagewidget.cpp
@@ -0,0 +1,299 @@
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution.
22*/
23
24#include <kabc/picture.h>
25
26#ifndef KAB_EMBEDDED
27#include <kaccelmanager.h>
28#include <kio/netaccess.h>
29#include <kimageio.h>
30#endif //KAB_EMBEDDED
31
32#include <kdebug.h>
33#include <kdialog.h>
34#include <kiconloader.h>
35#include <klocale.h>
36#include <kurlrequester.h>
37#include <kurl.h>
38
39#include <qcheckbox.h>
40#include <qgroupbox.h>
41#include <qlabel.h>
42#include <qlayout.h>
43#include <qpixmap.h>
44#include <qapplication.h>
45
46#include "imagewidget.h"
47
48ImageWidget::ImageWidget( QWidget *parent, const char *name )
49 : QWidget( parent, name )
50{
51 QGridLayout *topLayout = new QGridLayout( this, 2, 1, KDialog::marginHint(),
52 KDialog::spacingHint() );
53
54 QGroupBox *photoBox = new QGroupBox( 0, Qt::Vertical, i18n( "Photo" ), this );
55 QGridLayout *boxLayout = new QGridLayout( photoBox->layout(), 3, 2,
56 KDialog::spacingHint() );
57 boxLayout->setRowStretch( 2, 1 );
58
59 mPhotoLabel = new QLabel( photoBox );
60 int fac = 9;
61 if ( QApplication::desktop()->width() > 320 )
62 fac = 6;
63 mPhotoLabel->setFixedSize( 50*9/fac, 70*9/fac );
64 mPhotoLabel->setScaledContents( true );
65 mPhotoLabel->setFrameStyle( QFrame::Panel | QFrame::Sunken );
66 boxLayout->addMultiCellWidget( mPhotoLabel, 0, 2, 0, 0 );
67
68 mPhotoUrl = new KURLRequester( photoBox );
69#ifndef KAB_EMBEDDED
70 mPhotoUrl->setFilter( KImageIO::pattern() );
71#else //KAB_EMBEDDED
72//US qDebug("ImageWidget::ImageWidget KImageIO not defined. Does this harm ???");
73#endif //KAB_EMBEDDED
74
75
76 boxLayout->addWidget( mPhotoUrl, 0, 1 );
77
78 mUsePhotoUrl = new QCheckBox( i18n( "Store as URL" ), photoBox );
79 mUsePhotoUrl->setEnabled( false );
80 boxLayout->addWidget( mUsePhotoUrl, 1, 1 );
81
82 topLayout->addWidget( photoBox, 0, 0 );
83
84 QGroupBox *logoBox = new QGroupBox( 0, Qt::Vertical, i18n( "Logo" ), this );
85 boxLayout = new QGridLayout( logoBox->layout(), 3, 2, KDialog::spacingHint() );
86 boxLayout->setRowStretch( 2, 1 );
87
88 mLogoLabel = new QLabel( logoBox );
89 mLogoLabel->setFixedSize( 50*9/fac, 70*9/fac );
90 mLogoLabel->setScaledContents( true );
91 mLogoLabel->setFrameStyle( QFrame::Panel | QFrame::Sunken );
92 boxLayout->addMultiCellWidget( mLogoLabel, 0, 2, 0, 0 );
93
94 mLogoUrl = new KURLRequester( logoBox );
95#ifndef KAB_EMBEDDED
96 mLogoUrl->setFilter( KImageIO::pattern() );
97#else //KAB_EMBEDDED
98//US qDebug("ImageWidget::ImageWidget KImageIO not defined 2");
99#endif //KAB_EMBEDDED
100 boxLayout->addWidget( mLogoUrl, 0, 1 );
101
102 mUseLogoUrl = new QCheckBox( i18n( "Store as URL" ), logoBox );
103 mUseLogoUrl->setEnabled( false );
104 boxLayout->addWidget( mUseLogoUrl, 1, 1 );
105
106 topLayout->addWidget( logoBox, 1, 0 );
107
108 connect( mPhotoUrl, SIGNAL( textChanged( const QString& ) ),
109 SIGNAL( changed() ) );
110 connect( mPhotoUrl, SIGNAL( urlSelected( const QString& ) ),
111 SLOT( loadPhoto() ) );
112 connect( mPhotoUrl, SIGNAL( urlSelected( const QString& ) ),
113 SIGNAL( changed() ) );
114 connect( mPhotoUrl, SIGNAL( urlSelected( const QString& ) ),
115 SLOT( updateGUI() ) );
116 connect( mUsePhotoUrl, SIGNAL( toggled( bool ) ),
117 SIGNAL( changed() ) );
118
119 connect( mLogoUrl, SIGNAL( textChanged( const QString& ) ),
120 SIGNAL( changed() ) );
121 connect( mLogoUrl, SIGNAL( urlSelected( const QString& ) ),
122 SLOT( loadLogo() ) );
123 connect( mLogoUrl, SIGNAL( urlSelected( const QString& ) ),
124 SIGNAL( changed() ) );
125 connect( mLogoUrl, SIGNAL( urlSelected( const QString& ) ),
126 SLOT( updateGUI() ) );
127 connect( mUseLogoUrl, SIGNAL( toggled( bool ) ),
128 SIGNAL( changed() ) );
129
130#ifndef KAB_EMBEDDED
131 KAcceleratorManager::manage( this );
132#endif //KAB_EMBEDDED
133
134#ifndef DESKTOP_VERSION
135 mUsePhotoUrl->setChecked( true );
136 mUseLogoUrl->setChecked( true );
137#endif
138}
139
140ImageWidget::~ImageWidget()
141{
142}
143
144void ImageWidget::setPhoto( const KABC::Picture &photo )
145{
146 bool blocked = signalsBlocked();
147 blockSignals( true );
148
149 if ( photo.isIntern() ) {
150//US
151//US mPhotoLabel->setPixmap( photo.data() );
152 if (photo.data().isNull() != true)
153 {
154 QPixmap pm;
155 pm.convertFromImage(photo.data());
156
157 mPhotoLabel->setPixmap( pm );
158 }
159
160 mUsePhotoUrl->setChecked( false );
161 } else {
162 mPhotoUrl->setURL( photo.url() );
163 if ( !photo.url().isEmpty() )
164 mUsePhotoUrl->setChecked( true );
165 loadPhoto();
166 }
167
168 blockSignals( blocked );
169}
170
171KABC::Picture ImageWidget::photo() const
172{
173 KABC::Picture photo;
174
175 if ( mUsePhotoUrl->isChecked() )
176 photo.setUrl( mPhotoUrl->url() );
177 else {
178 QPixmap *px = mPhotoLabel->pixmap();
179 if ( px ) {
180#ifndef KAB_EMBEDDED
181 if ( px->height() > px->width() )
182 photo.setData( px->convertToImage().scaleHeight( 140 ) );
183 else
184 photo.setData( px->convertToImage().scaleWidth( 100 ) );
185#else //KAB_EMBEDDED
186//US add teh nullcheck
187 if (px->isNull() != true )
188 photo.setData( px->convertToImage() );
189#endif //KAB_EMBEDDED
190
191 photo.setType( "PNG" );
192 }
193 }
194
195 return photo;
196}
197
198void ImageWidget::setLogo( const KABC::Picture &logo )
199{
200 bool blocked = signalsBlocked();
201 blockSignals( true );
202
203 if ( logo.isIntern() ) {
204//US
205//US mLogoLabel->setPixmap( logo.data() );
206 if (logo.data().isNull() != true)
207 {
208 QPixmap pm;
209 pm.convertFromImage(logo.data());
210 mLogoLabel->setPixmap( pm );
211 }
212 mUseLogoUrl->setChecked( false );
213 } else {
214 mLogoUrl->setURL( logo.url() );
215 if ( !logo.url().isEmpty() )
216 mUseLogoUrl->setChecked( true );
217 loadLogo();
218 }
219
220 blockSignals( blocked );
221}
222
223KABC::Picture ImageWidget::logo() const
224{
225 KABC::Picture logo;
226
227 if ( mUseLogoUrl->isChecked() )
228 logo.setUrl( mLogoUrl->url() );
229 else {
230 QPixmap *px = mLogoLabel->pixmap();
231 if ( px ) {
232#ifndef KAB_EMBEDDED
233 if ( px->height() > px->width() )
234 logo.setData( px->convertToImage().scaleHeight( 140 ) );
235 else
236 logo.setData( px->convertToImage().scaleWidth( 100 ) );
237#else //KAB_EMBEDDED
238 logo.setData( px->convertToImage() );
239#endif //KAB_EMBEDDED
240
241 logo.setType( "PNG" );
242
243 }
244 }
245
246 return logo;
247}
248
249void ImageWidget::loadPhoto()
250{
251 mPhotoLabel->setPixmap( loadPixmap( mPhotoUrl->url() ) );
252}
253
254void ImageWidget::loadLogo()
255{
256 mLogoLabel->setPixmap( loadPixmap( mLogoUrl->url() ) );
257}
258
259void ImageWidget::updateGUI()
260{
261 KURLRequester *ptr = (KURLRequester*)sender();
262
263#ifdef DESKTOP_VERSION
264 if ( ptr == mPhotoUrl )
265 mUsePhotoUrl->setEnabled( true );
266 else if ( ptr == mLogoUrl )
267 mUseLogoUrl->setEnabled( true );
268#endif
269}
270
271QPixmap ImageWidget::loadPixmap( const KURL &url )
272{
273 QString tempFile;
274 QPixmap pixmap;
275
276 if ( url.isEmpty() )
277 return pixmap;
278
279 if ( url.isLocalFile() )
280 pixmap = QPixmap( url.path() );
281 else
282 {
283#ifndef KAB_EMBEDDED
284 if ( KIO::NetAccess::download( url, tempFile ) ) {
285 pixmap = QPixmap( tempFile );
286 KIO::NetAccess::removeTempFile( tempFile );
287 }
288#else //KAB_EMBEDDED
289 qDebug("ImageWidget::loadPixmap : only local pixmaps are allowed");
290#endif //KAB_EMBEDDED
291
292 }
293
294 return pixmap;
295}
296
297#ifndef KAB_EMBEDDED
298#include "imagewidget.moc"
299#endif //KAB_EMBEDDED