Diffstat (limited to 'kaddressbook/details/look_details.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kaddressbook/details/look_details.cpp | 419 |
1 files changed, 419 insertions, 0 deletions
diff --git a/kaddressbook/details/look_details.cpp b/kaddressbook/details/look_details.cpp new file mode 100644 index 0000000..51ec0c0 --- a/dev/null +++ b/kaddressbook/details/look_details.cpp | |||
@@ -0,0 +1,419 @@ | |||
1 | /* | ||
2 | This file is part of KAddressBook. | ||
3 | Copyright (c) 1996-2002 Mirko Boehm <mirko@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 <kconfig.h> | ||
25 | #include <kdebug.h> | ||
26 | #include <kglobalsettings.h> | ||
27 | #include <kinstance.h> | ||
28 | #include <klocale.h> | ||
29 | #include <kstandarddirs.h> | ||
30 | |||
31 | #include <qcursor.h> | ||
32 | #include <qdir.h> | ||
33 | #include <qpainter.h> | ||
34 | #include <qpopupmenu.h> | ||
35 | |||
36 | #include "global.h" | ||
37 | #include "kabentrypainter.h" | ||
38 | |||
39 | #include "look_details.h" | ||
40 | |||
41 | #define GRID 5 | ||
42 | |||
43 | const QString KABDetailedView::mBorderedBGDir = "kab3part/backgrounds/bordered/"; | ||
44 | const QString KABDetailedView::mTiledBGDir = "kab3part/backgrounds/tiled/"; | ||
45 | |||
46 | KABDetailedView::KABDetailedView( QWidget *parent, const char *name ) | ||
47 | : KABBasicLook( parent, name ), mPainter( 0 ), mBackgroundStyle( None ), | ||
48 | mDefaultBGColor( white ), mHeadLineBGColor( darkBlue ), | ||
49 | mHeadLineTextColor( yellow ), mGrid( 3 ), mMenuBorderedBG( 0 ), | ||
50 | mMenuTiledBG( 0 ) | ||
51 | { | ||
52 | KToggleAction** actions[] = { | ||
53 | &mActionShowAddresses, | ||
54 | &mActionShowEmails, | ||
55 | &mActionShowPhones, | ||
56 | &mActionShowURLs | ||
57 | }; | ||
58 | |||
59 | QString actionTexts[] = { | ||
60 | i18n( "Show Postal Addresses" ), | ||
61 | i18n( "Show Email Addresses" ), | ||
62 | i18n( "Show Telephone Numbers" ), | ||
63 | i18n( "Show Web Pages (URLs)" ) | ||
64 | }; | ||
65 | |||
66 | QFont general = KGlobalSettings::generalFont(); | ||
67 | QFont fixed = KGlobalSettings::fixedFont(); | ||
68 | QString gfont = general.family(); | ||
69 | QString ffont = fixed.family(); | ||
70 | |||
71 | int gpointsize = general.pixelSize(); | ||
72 | if ( gpointsize == -1 ) | ||
73 | gpointsize = general.pointSize(); | ||
74 | |||
75 | int fpointsize = fixed.pixelSize(); | ||
76 | if ( fpointsize == -1 ) | ||
77 | fpointsize = fixed.pointSize(); | ||
78 | |||
79 | mPainter = new KABEntryPainter; | ||
80 | |||
81 | mPainter->setForegroundColor( black ); | ||
82 | mPainter->setHeaderColor( mHeadLineTextColor ); | ||
83 | mPainter->setUseHeaderColor( mUseHeadLineBGColor ); | ||
84 | mPainter->setBackgroundColor( mHeadLineBGColor ); | ||
85 | |||
86 | mPainter->setHeaderFont( QFont( gfont, gpointsize + 4, QFont::Bold, true ) ); | ||
87 | mPainter->setHeadLineFont( QFont( gfont, gpointsize + 2, QFont::Bold, true ) ); | ||
88 | mPainter->setBodyFont( QFont( gfont, gpointsize, QFont::Normal, false ) ); | ||
89 | mPainter->setFixedFont( QFont( ffont, fpointsize, QFont::Normal, false ) ); | ||
90 | mPainter->setCommentFont( QFont( gfont, gpointsize, QFont::Normal, false ) ); | ||
91 | |||
92 | const int numActions = sizeof( actions ) / sizeof( actions[ 0 ] ); | ||
93 | |||
94 | for ( int count = 0; count < numActions; ++count ) { | ||
95 | *actions[ count ] = new KToggleAction( actionTexts[ count ] ); | ||
96 | (*actions[ count ])->setChecked( true ); | ||
97 | } | ||
98 | |||
99 | setMouseTracking( true ); | ||
100 | |||
101 | setBackgroundMode( NoBackground ); | ||
102 | } | ||
103 | |||
104 | KABDetailedView::~KABDetailedView() | ||
105 | { | ||
106 | delete mPainter; | ||
107 | mPainter = 0; | ||
108 | } | ||
109 | |||
110 | bool KABDetailedView::getBackground( QString path, QPixmap& image ) | ||
111 | { | ||
112 | QMap<QString, QPixmap>::iterator pos; | ||
113 | |||
114 | pos = mBackgroundMap.find( path ); | ||
115 | if ( pos == mBackgroundMap.end() ) { // the image has not been loaded previously | ||
116 | if ( image.load( path ) ) { | ||
117 | mBackgroundMap[ path ] = image; | ||
118 | return true; | ||
119 | } else | ||
120 | return false; | ||
121 | } else { // image found in cache | ||
122 | image = pos.data(); | ||
123 | return true; | ||
124 | } | ||
125 | } | ||
126 | |||
127 | void KABDetailedView::paintEvent( QPaintEvent* ) | ||
128 | { | ||
129 | const int BorderSpace = mGrid; | ||
130 | QPixmap pm( width(), height() ); | ||
131 | QPainter p; | ||
132 | |||
133 | QRect entryArea = QRect( BorderSpace, mGrid, width() - mGrid - BorderSpace, | ||
134 | height() - 2 * mGrid ); | ||
135 | p.begin( &pm ); | ||
136 | |||
137 | p.setPen( darkBlue ); | ||
138 | p.setBrush( mDefaultBGColor ); | ||
139 | p.drawRect( 0, 0, width(), height() ); | ||
140 | switch ( mBackgroundStyle ) { | ||
141 | case Tiled: | ||
142 | p.drawTiledPixmap( 1, 1, width() - 2, height() - 2, mCurrentBackground ); | ||
143 | break; | ||
144 | case Bordered: | ||
145 | p.drawTiledPixmap( 1, 1, QMIN( width() - 2, mCurrentBackground.width() ), | ||
146 | height() - 2, mCurrentBackground ); | ||
147 | break; | ||
148 | case None: // no BG image defined for this entry: | ||
149 | default: | ||
150 | if ( mUseDefaultBGImage ) | ||
151 | p.drawTiledPixmap( 1, 1, width() - 2, height() - 2, mDefaultBGImage ); | ||
152 | break; | ||
153 | }; | ||
154 | |||
155 | p.setViewport( entryArea ); | ||
156 | |||
157 | mPainter->setShowAddresses( mActionShowAddresses->isChecked() ); | ||
158 | mPainter->setShowEmails( mActionShowEmails->isChecked() ); | ||
159 | mPainter->setShowPhones( mActionShowPhones->isChecked() ); | ||
160 | mPainter->setShowURLs( mActionShowURLs->isChecked() ); | ||
161 | mPainter->printAddressee( addressee(), QRect( 0, 0, entryArea.width(), | ||
162 | entryArea.height() ), &p ); | ||
163 | p.end(); | ||
164 | bitBlt( this, 0, 0, &pm ); | ||
165 | } | ||
166 | |||
167 | void KABDetailedView::mouseMoveEvent( QMouseEvent *e ) | ||
168 | { | ||
169 | QPoint bias( mGrid, mGrid ); | ||
170 | int rc; | ||
171 | bool hit = false; | ||
172 | |||
173 | if ( ( rc = mPainter->hitsEmail( e->pos() - bias ) ) != -1 ) | ||
174 | hit = true; | ||
175 | else if ( ( rc = mPainter->hitsURL( e->pos() - bias ) ) != -1 ) | ||
176 | hit = true; | ||
177 | else if ( ( rc = mPainter->hitsPhone( e->pos() - bias ) ) != -1 ) | ||
178 | hit = true; | ||
179 | else if ( ( rc = mPainter->hitsTalk( e->pos() - bias ) ) != -1 ) | ||
180 | hit = true; | ||
181 | |||
182 | if ( hit ) { | ||
183 | if ( cursor().shape() != PointingHandCursor ) | ||
184 | setCursor( PointingHandCursor ); | ||
185 | else if( cursor().shape() != ArrowCursor ) | ||
186 | setCursor(ArrowCursor); | ||
187 | } | ||
188 | } | ||
189 | |||
190 | void KABDetailedView::mousePressEvent( QMouseEvent *e ) | ||
191 | { | ||
192 | QPopupMenu menu( this ); | ||
193 | QPopupMenu *menuBG = new QPopupMenu( &menu ); | ||
194 | mMenuBorderedBG = new QPopupMenu( &menu ); | ||
195 | mMenuTiledBG = new QPopupMenu( &menu ); | ||
196 | |||
197 | menu.insertItem( i18n( "Select Background" ), menuBG ); | ||
198 | menuBG->insertItem( i18n( "Bordered Backgrounds" ), mMenuBorderedBG ); | ||
199 | menuBG->insertItem( i18n( "Tiled Backgrounds" ), mMenuTiledBG ); | ||
200 | menu.insertSeparator(); | ||
201 | |||
202 | QPoint point = e->pos() - QPoint( mGrid, mGrid ); | ||
203 | int rc; | ||
204 | QStringList dirsBorderedBG, dirsTiledBG; | ||
205 | QDir dir; | ||
206 | |||
207 | switch( e->button() ) { | ||
208 | case QMouseEvent::RightButton: | ||
209 | if ( isReadOnly() ) | ||
210 | menu.setItemEnabled( menu.idAt( 0 ), false ); | ||
211 | else { | ||
212 | // TODO: settings need to be saved in view options | ||
213 | dirsBorderedBG = KGlobal::instance()->dirs()->findDirs( "data", mBorderedBGDir ); | ||
214 | if ( dirsBorderedBG.count() > 0 ) { | ||
215 | dir.setPath( dirsBorderedBG[ 0 ] ); | ||
216 | mBorders = dir.entryList( QDir::Files ); | ||
217 | for ( uint count = 0; count < mBorders.count(); ++count ) | ||
218 | mMenuBorderedBG->insertItem( mBorders[ count ], count ); | ||
219 | |||
220 | connect( mMenuBorderedBG, SIGNAL( activated( int ) ), | ||
221 | SLOT( slotBorderedBGSelected( int ) ) ); | ||
222 | } else | ||
223 | menuBG->setItemEnabled( menuBG->idAt( 0 ), false ); | ||
224 | |||
225 | dirsTiledBG = KGlobal::instance()->dirs()->findDirs( "data", mTiledBGDir ); | ||
226 | if ( dirsTiledBG.count() > 0 ) { | ||
227 | dir.setPath( dirsTiledBG[ 0 ] ); | ||
228 | mTiles = dir.entryList( QDir::Files ); | ||
229 | for ( uint count = 0; count < mTiles.count(); ++count ) | ||
230 | mMenuTiledBG->insertItem( mTiles[ count ], count ); | ||
231 | |||
232 | connect( mMenuTiledBG, SIGNAL( activated( int ) ), | ||
233 | SLOT( slotTiledBGSelected( int ) ) ); | ||
234 | } else | ||
235 | menuBG->setItemEnabled( menuBG->idAt( 1 ), false ); | ||
236 | } | ||
237 | |||
238 | mActionShowAddresses->plug( &menu ); | ||
239 | mActionShowEmails->plug( &menu ); | ||
240 | mActionShowPhones->plug( &menu ); | ||
241 | mActionShowURLs->plug( &menu ); | ||
242 | |||
243 | menu.exec( e->globalPos() ); | ||
244 | break; | ||
245 | |||
246 | case QMouseEvent::LeftButton: | ||
247 | // find whether the pointer touches an email address, URL, | ||
248 | // talk address or telephone number: | ||
249 | if ( ( rc = mPainter->hitsEmail( point ) ) != -1 ) { | ||
250 | emit sendEmail( addressee().emails()[ rc ] ); | ||
251 | break; | ||
252 | } | ||
253 | if ( ( rc = mPainter->hitsURL( point ) ) != -1 ) { | ||
254 | emit browse( addressee().url().prettyURL() ); | ||
255 | break; | ||
256 | } | ||
257 | if ( ( rc = mPainter->hitsPhone( point ) ) != -1 ) { | ||
258 | // not implemented yet | ||
259 | break; | ||
260 | } | ||
261 | if ( ( rc = mPainter->hitsTalk( point ) ) != -1 ) { | ||
262 | // not implemented yet | ||
263 | break; | ||
264 | } | ||
265 | break; | ||
266 | default: | ||
267 | break; | ||
268 | } | ||
269 | |||
270 | mMenuBorderedBG = 0; | ||
271 | mMenuTiledBG = 0; | ||
272 | } | ||
273 | |||
274 | void KABDetailedView::setAddressee( const KABC::Addressee &addr ) | ||
275 | { | ||
276 | BackgroundStyle style = None; | ||
277 | QString dir, file, styleSetting; | ||
278 | KABBasicLook::setAddressee( addr ); | ||
279 | |||
280 | // TODO: preload path and styleSetting with possible preference values | ||
281 | styleSetting = addressee().custom( "kab", "BackgroundStyle" ); | ||
282 | style = (BackgroundStyle)styleSetting.toInt(); | ||
283 | file = addressee().custom( "kab", "BackgroundImage" ); | ||
284 | if ( !file.isEmpty() ) { | ||
285 | switch ( style ) { | ||
286 | case Tiled: | ||
287 | dir = mTiledBGDir; | ||
288 | break; | ||
289 | case Bordered: | ||
290 | dir = mBorderedBGDir; | ||
291 | break; | ||
292 | case None: | ||
293 | default: | ||
294 | break; | ||
295 | } | ||
296 | |||
297 | QStringList dirs = KGlobal::instance()->dirs()->findDirs( "data", dir ); | ||
298 | mBackgroundStyle = None; | ||
299 | if ( !dirs.isEmpty() ) { | ||
300 | uint count = 0; | ||
301 | for ( ; count < dirs.count(); ++count ) { | ||
302 | QDir folder; | ||
303 | folder.setPath( dirs[ count ] ); | ||
304 | file = folder.absPath() + "/" + file; | ||
305 | if ( getBackground( file, mCurrentBackground ) ) { | ||
306 | mBackgroundStyle = style; | ||
307 | break; | ||
308 | } | ||
309 | } | ||
310 | |||
311 | if ( count == dirs.count() ) { | ||
312 | kdDebug(5720) << "KABDetailedView::setEntry: " << file | ||
313 | << " not locatable." << endl; | ||
314 | } | ||
315 | } | ||
316 | } else { // no background here | ||
317 | mBackgroundStyle = None; | ||
318 | mCurrentBackground.resize( 0, 0 ); | ||
319 | } | ||
320 | |||
321 | repaint( false ); | ||
322 | } | ||
323 | |||
324 | void KABDetailedView::slotBorderedBGSelected( int index ) | ||
325 | { | ||
326 | if ( index >= 0 && (uint)index < mBorders.count() && !isReadOnly() ) { | ||
327 | // get the selection and make it a full path | ||
328 | QString path = mBorders[ index ]; | ||
329 | mBackgroundStyle = Bordered; | ||
330 | addressee().insertCustom( "kab", "BackgroundStyle", | ||
331 | QString().setNum( mBackgroundStyle ) ); | ||
332 | addressee().insertCustom( "kab", "BackgroundImage", path ); | ||
333 | setAddressee( addressee() ); | ||
334 | } | ||
335 | } | ||
336 | |||
337 | void KABDetailedView::slotTiledBGSelected( int index ) | ||
338 | { | ||
339 | if ( index >= 0 && (uint)index < mTiles.count() && !isReadOnly() ) { | ||
340 | QString path = mTiles[ index ]; | ||
341 | mBackgroundStyle = Tiled; | ||
342 | addressee().insertCustom( "kab", "BackgroundStyle", | ||
343 | QString().setNum( mBackgroundStyle ) ); | ||
344 | addressee().insertCustom( "kab", "BackgroundImage", path ); | ||
345 | setAddressee( addressee() ); | ||
346 | } | ||
347 | } | ||
348 | |||
349 | void KABDetailedView::setReadOnly( bool state ) | ||
350 | { | ||
351 | KABBasicLook::setReadOnly( state ); | ||
352 | repaint( false ); | ||
353 | } | ||
354 | |||
355 | void KABDetailedView::restoreSettings( KConfig *config ) | ||
356 | { | ||
357 | QFont general = KGlobalSettings::generalFont(); | ||
358 | QFont fixed = KGlobalSettings::fixedFont(); | ||
359 | QString gfont = general.family(); | ||
360 | QString ffont = fixed.family(); | ||
361 | |||
362 | int gpointsize = general.pixelSize(); | ||
363 | if ( gpointsize == -1 ) | ||
364 | gpointsize = general.pointSize(); | ||
365 | |||
366 | int fpointsize = fixed.pixelSize(); | ||
367 | if ( fpointsize == -1 ) | ||
368 | fpointsize = fixed.pointSize(); | ||
369 | |||
370 | config->setGroup( ConfigView ); | ||
371 | |||
372 | // load the default background image: | ||
373 | QString bgImage; | ||
374 | mUseDefaultBGImage = config->readBoolEntry( ConfigView_UseDefaultBackground, true ); | ||
375 | mDefaultBGColor = config->readColorEntry( ConfigView_DefaultBackgroundColor, &white ); | ||
376 | bgImage = config->readEntry( ConfigView_DefaultBackgroundImage, "konqueror/tiles/kenwimer.png" ); | ||
377 | |||
378 | if ( mUseDefaultBGImage ) { | ||
379 | uint count = 0; | ||
380 | QStringList dirs = KGlobal::instance()->dirs()->findDirs( "data", "/" ); | ||
381 | if ( !dirs.isEmpty() ) { | ||
382 | for ( count = 0; count < dirs.count(); ++count ) { | ||
383 | if ( getBackground( dirs[ count ] + "/" + bgImage, mDefaultBGImage ) ) | ||
384 | break; | ||
385 | } | ||
386 | } | ||
387 | |||
388 | if ( count == dirs.count() ) { | ||
389 | mUseDefaultBGImage = getBackground( bgImage, mDefaultBGImage ); | ||
390 | if ( !mUseDefaultBGImage ) | ||
391 | kdDebug(5720) << "KABDetailedView::configure: " | ||
392 | << "default BG image selected, but could not be loaded." | ||
393 | << endl; | ||
394 | } | ||
395 | } | ||
396 | |||
397 | mDefaultBGColor = config->readColorEntry( ConfigView_DefaultBackgroundColor, &white ); | ||
398 | mHeadLineBGColor = config->readColorEntry( ConfigView_HeadlineBGColor, &darkBlue ); | ||
399 | mHeadLineTextColor = config->readColorEntry( ConfigView_HeadlineTextColor, &yellow ); | ||
400 | mUseHeadLineBGColor = config->readBoolEntry( ConfigView_UseHeadlineBGColor, true ); | ||
401 | |||
402 | if ( !mPainter ) | ||
403 | mPainter = new KABEntryPainter; | ||
404 | |||
405 | mPainter->setForegroundColor( black ); | ||
406 | mPainter->setHeaderColor( mHeadLineTextColor ); | ||
407 | mPainter->setUseHeaderColor( mUseHeadLineBGColor ); | ||
408 | mPainter->setBackgroundColor( mHeadLineBGColor ); | ||
409 | |||
410 | mPainter->setHeaderFont( QFont( gfont, gpointsize + 4, QFont::Bold, true ) ); | ||
411 | mPainter->setHeadLineFont( QFont( gfont, gpointsize + 2, QFont::Bold, true ) ); | ||
412 | mPainter->setBodyFont( QFont( gfont, gpointsize, QFont::Normal, false ) ); | ||
413 | mPainter->setFixedFont( QFont( ffont, fpointsize, QFont::Normal, false ) ); | ||
414 | mPainter->setCommentFont( QFont( gfont, gpointsize, QFont::Normal, false ) ); | ||
415 | } | ||
416 | |||
417 | #ifndef KAB_EMBEDDED | ||
418 | #include "look_details.moc" | ||
419 | #endif //KAB_EMBEDDED | ||