summaryrefslogtreecommitdiffabout
path: root/kaddressbook/details/look_details.cpp
blob: 2c3a79db46572a2ee60ad6da6b03745fb55324e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*                                                                      
    This file is part of KAddressBook.
    Copyright (c) 1996-2002 Mirko Boehm <mirko@kde.org>
                                                                        
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or   
    (at your option) any later version.                                 
                                                                        
    This program is distributed in the hope that it will be useful,     
    but WITHOUT ANY WARRANTY; without even the implied warranty of      
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        
    GNU General Public License for more details.                        
                                                                        
    You should have received a copy of the GNU General Public License   
    along with this program; if not, write to the Free Software         
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           
                                                                        
    As a special exception, permission is given to link this program    
    with any edition of Qt, and distribute the resulting executable,    
    without including the source code for Qt in the source distribution.
*/                                                                      

#include <kconfig.h>
#include <kdebug.h>
#include <kglobalsettings.h>
#include <kinstance.h>
#include <klocale.h>
#include <kstandarddirs.h>

#include <qcursor.h>
#include <qdir.h>
#include <qpainter.h>
#include <qpopupmenu.h>

#include "global.h"
#include "kabentrypainter.h"

#include "look_details.h"

#define GRID 5

const QString KABDetailedView::mBorderedBGDir = "kab3part/backgrounds/bordered/";
const QString KABDetailedView::mTiledBGDir = "kab3part/backgrounds/tiled/";

KABDetailedView::KABDetailedView( QWidget *parent, const char *name )
  : KABBasicLook( parent, name ), mPainter( 0 ), mBackgroundStyle( None ),
    mDefaultBGColor( white ), mHeadLineBGColor( darkBlue ),
    mHeadLineTextColor( yellow ), mGrid( 3 ), mMenuBorderedBG( 0 ),
    mMenuTiledBG( 0 )
{
  KToggleAction** actions[] = {
    &mActionShowAddresses,
    &mActionShowEmails,
    &mActionShowPhones,
    &mActionShowURLs
  };

  QString actionTexts[] = {
    i18n( "Show Postal Addresses" ),
    i18n( "Show Email Addresses" ),
    i18n( "Show Telephone Numbers" ),
    i18n( "Show Web Pages (URLs)" )
  };

  QFont general = KGlobalSettings::generalMaxFont();
  QFont fixed = KGlobalSettings::fixedFont();
  QString gfont = general.family();
  QString ffont = fixed.family();

  int gpointsize = general.pixelSize();
  if ( gpointsize == -1 )
    gpointsize = general.pointSize();

  int fpointsize = fixed.pixelSize();
  if ( fpointsize == -1 )
    fpointsize = fixed.pointSize();

  mPainter = new KABEntryPainter;

  mPainter->setForegroundColor( black );
  mPainter->setHeaderColor( mHeadLineTextColor );
  mPainter->setUseHeaderColor( mUseHeadLineBGColor );
  mPainter->setBackgroundColor( mHeadLineBGColor );

  mPainter->setHeaderFont( QFont( gfont, gpointsize + 4, QFont::Bold, true ) );
  mPainter->setHeadLineFont( QFont( gfont, gpointsize + 2, QFont::Bold, true ) );
  mPainter->setBodyFont( QFont( gfont, gpointsize, QFont::Normal, false ) );
  mPainter->setFixedFont( QFont( ffont, fpointsize, QFont::Normal, false ) );
  mPainter->setCommentFont( QFont( gfont, gpointsize, QFont::Normal, false ) );

  const int numActions = sizeof( actions ) / sizeof( actions[ 0 ] );

  for ( int count = 0; count < numActions; ++count ) {
    *actions[ count ] = new KToggleAction( actionTexts[ count ] );
    (*actions[ count ])->setChecked( true );
  }

  setMouseTracking( true );

  setBackgroundMode( NoBackground );
}

KABDetailedView::~KABDetailedView()
{
  delete mPainter;
  mPainter = 0;
}

bool KABDetailedView::getBackground( QString path, QPixmap& image )
{
  QMap<QString, QPixmap>::iterator pos;

  pos = mBackgroundMap.find( path );
  if ( pos == mBackgroundMap.end() ) { // the image has not been loaded previously
    if ( image.load( path ) ) {
      mBackgroundMap[ path ] = image;
      return true;
    } else
      return false;
  } else { // image found in cache
    image = pos.data();
    return true;
  }
}

void KABDetailedView::paintEvent( QPaintEvent* )
{
  const int BorderSpace = mGrid;
  QPixmap pm( width(), height() );
  QPainter p;

  QRect entryArea = QRect( BorderSpace, mGrid, width() - mGrid - BorderSpace,
                           height() - 2 * mGrid );
  p.begin( &pm );

  p.setPen( darkBlue );
  p.setBrush( mDefaultBGColor );
  p.drawRect( 0, 0, width(), height() );
  switch ( mBackgroundStyle ) {
    case Tiled:
      p.drawTiledPixmap( 1, 1, width() - 2, height() - 2, mCurrentBackground );
      break;
    case Bordered:
      p.drawTiledPixmap( 1, 1, QMIN( width() - 2, mCurrentBackground.width() ),
                         height() - 2, mCurrentBackground );
      break;
    case None: // no BG image defined for this entry:
    default:
      if ( mUseDefaultBGImage )
        p.drawTiledPixmap( 1, 1, width() - 2, height() - 2, mDefaultBGImage );
      break;
  };

  p.setViewport( entryArea );

  mPainter->setShowAddresses( mActionShowAddresses->isChecked() );
  mPainter->setShowEmails( mActionShowEmails->isChecked() );
  mPainter->setShowPhones( mActionShowPhones->isChecked() );
  mPainter->setShowURLs( mActionShowURLs->isChecked() );
  mPainter->printAddressee( addressee(), QRect( 0, 0, entryArea.width(),
                            entryArea.height() ), &p );
  p.end();
  bitBlt( this, 0, 0, &pm );
}

void KABDetailedView::mouseMoveEvent( QMouseEvent *e )
{
  QPoint bias( mGrid, mGrid );
  int rc;
  bool hit = false;

  if ( ( rc = mPainter->hitsEmail( e->pos() - bias ) ) != -1 )
    hit = true;
  else if ( ( rc = mPainter->hitsURL( e->pos() - bias ) ) != -1 )
    hit = true;
  else if ( ( rc = mPainter->hitsPhone( e->pos() - bias ) ) != -1 )
    hit = true;
  else if ( ( rc = mPainter->hitsTalk( e->pos() - bias ) ) != -1 )
    hit = true;

  if ( hit ) {
    if ( cursor().shape() != PointingHandCursor )
      setCursor( PointingHandCursor );
    else if( cursor().shape() != ArrowCursor )
      setCursor(ArrowCursor);
  }
}

void KABDetailedView::mousePressEvent( QMouseEvent *e )
{
  Q3PopupMenu menu( this );
  Q3PopupMenu *menuBG = new Q3PopupMenu( &menu );
  mMenuBorderedBG = new Q3PopupMenu( &menu );
  mMenuTiledBG = new Q3PopupMenu( &menu );

  menu.insertItem( i18n( "Select Background" ), menuBG );
  menuBG->insertItem( i18n( "Bordered Backgrounds" ), mMenuBorderedBG );
  menuBG->insertItem( i18n( "Tiled Backgrounds" ), mMenuTiledBG );
  menu.insertSeparator();

  QPoint point = e->pos() - QPoint( mGrid, mGrid );
  int rc;
  QStringList dirsBorderedBG, dirsTiledBG;
  QDir dir;

  switch( e->button() ) {
    case QMouseEvent::RightButton:
      if ( isReadOnly() )
        menu.setItemEnabled( menu.idAt( 0 ), false );
      else {
        // TODO: settings need to be saved in view options
        dirsBorderedBG = KGlobal::instance()->dirs()->findDirs( "data", mBorderedBGDir );
        if ( dirsBorderedBG.count() > 0 ) {
          dir.setPath( dirsBorderedBG[ 0 ] );
          mBorders = dir.entryList( QDir::Files );
          for ( uint count = 0; count < mBorders.count(); ++count )
            mMenuBorderedBG->insertItem( mBorders[ count ], count );

          connect( mMenuBorderedBG, SIGNAL( activated( int ) ),
                   SLOT( slotBorderedBGSelected( int ) ) );
        } else
          menuBG->setItemEnabled( menuBG->idAt( 0 ), false );

        dirsTiledBG = KGlobal::instance()->dirs()->findDirs( "data", mTiledBGDir );
        if ( dirsTiledBG.count() > 0 ) {
          dir.setPath( dirsTiledBG[ 0 ] );
          mTiles = dir.entryList( QDir::Files );
          for ( uint count = 0; count < mTiles.count(); ++count )
            mMenuTiledBG->insertItem( mTiles[ count ], count );

          connect( mMenuTiledBG, SIGNAL( activated( int ) ),
                   SLOT( slotTiledBGSelected( int ) ) );
        } else
          menuBG->setItemEnabled( menuBG->idAt( 1 ), false );
      }

      mActionShowAddresses->plug( &menu );
      mActionShowEmails->plug( &menu );
      mActionShowPhones->plug( &menu );
      mActionShowURLs->plug( &menu );

      menu.exec( e->globalPos() );
      break;

    case QMouseEvent::LeftButton:
      // find whether the pointer touches an email address, URL,
      // talk address or telephone number:
      if ( ( rc = mPainter->hitsEmail( point ) ) != -1 ) {
        emit sendEmail( addressee().emails()[ rc ] );
        break;
      }
      if ( ( rc = mPainter->hitsURL( point ) ) != -1 ) {
        emit browse( addressee().url().prettyURL() );
        break;
      }
      if ( ( rc = mPainter->hitsPhone( point ) ) != -1 ) {
        // not implemented yet
        break;
      }
      if ( ( rc = mPainter->hitsTalk( point ) ) != -1 ) {
        // not implemented yet
        break;
      }
      break;
    default:
      break;
  }

  mMenuBorderedBG = 0;
  mMenuTiledBG = 0;
}

void KABDetailedView::setAddressee( const KABC::Addressee &addr )
{
  BackgroundStyle style = None;
  QString dir, file, styleSetting;
  KABBasicLook::setAddressee( addr );

  // TODO: preload path and styleSetting with possible preference values
  styleSetting = addressee().custom( "kab", "BackgroundStyle" );
  style = (BackgroundStyle)styleSetting.toInt();
  file = addressee().custom( "kab", "BackgroundImage" );
  if ( !file.isEmpty() ) {
    switch ( style ) {
      case Tiled:
        dir = mTiledBGDir;
        break;
      case Bordered:
        dir = mBorderedBGDir;
        break;
      case None:
      default:
        break;
    }

    QStringList dirs = KGlobal::instance()->dirs()->findDirs( "data", dir );
    mBackgroundStyle = None;
    if ( !dirs.isEmpty() ) {
      uint count = 0;
      for ( ; count < dirs.count(); ++count ) {
        QDir folder;
        folder.setPath( dirs[ count ] );
        file = folder.absPath() + "/" + file;
        if ( getBackground( file, mCurrentBackground ) ) {
          mBackgroundStyle = style;
          break;
        }
      }

      if ( count == dirs.count() ) {
        kdDebug(5720) << "KABDetailedView::setEntry: " << file
                      << " not locatable." << endl;
      }
    }
  } else { // no background here
    mBackgroundStyle = None;
    mCurrentBackground.resize( 0, 0 );
  }

  repaint( false );
}

void KABDetailedView::slotBorderedBGSelected( int index )
{
  if ( index >= 0 && (uint)index < mBorders.count() && !isReadOnly() ) {
    // get the selection and make it a full path
    QString path = mBorders[ index ];
    mBackgroundStyle = Bordered;
    addressee().insertCustom( "kab", "BackgroundStyle",
                              QString().setNum( mBackgroundStyle ) );
    addressee().insertCustom( "kab", "BackgroundImage", path );
    setAddressee( addressee() );
  }
}

void KABDetailedView::slotTiledBGSelected( int index )
{
  if ( index >= 0 && (uint)index < mTiles.count() && !isReadOnly() ) {
    QString path = mTiles[ index ];
    mBackgroundStyle = Tiled;
    addressee().insertCustom( "kab", "BackgroundStyle",
                              QString().setNum( mBackgroundStyle ) );
    addressee().insertCustom( "kab", "BackgroundImage", path );
    setAddressee( addressee() );
  }
}

void KABDetailedView::setReadOnly( bool state )
{
  KABBasicLook::setReadOnly( state );
  repaint( false );
}

void KABDetailedView::restoreSettings( KConfig *config )
{
  QFont general = KGlobalSettings::generalFont();
  QFont fixed = KGlobalSettings::fixedFont();
  QString gfont = general.family();
  QString ffont = fixed.family();

  int gpointsize = general.pixelSize();
  if ( gpointsize == -1 )
    gpointsize = general.pointSize();

  int fpointsize = fixed.pixelSize();
  if ( fpointsize == -1 )
    fpointsize = fixed.pointSize();

  config->setGroup( ConfigView );

  // load the default background image:
  QString bgImage;
  mUseDefaultBGImage = config->readBoolEntry( ConfigView_UseDefaultBackground, true );
  mDefaultBGColor = config->readColorEntry( ConfigView_DefaultBackgroundColor, &white );
  bgImage = config->readEntry( ConfigView_DefaultBackgroundImage, "konqueror/tiles/kenwimer.png" );

  if ( mUseDefaultBGImage ) {
    uint count = 0;
    QStringList dirs = KGlobal::instance()->dirs()->findDirs( "data", "/" );
    if ( !dirs.isEmpty() ) {
      for ( count = 0; count < dirs.count(); ++count )  {
        if ( getBackground( dirs[ count ] + "/" + bgImage, mDefaultBGImage ) )
          break;
      }
    }

    if ( count == dirs.count() ) {
      mUseDefaultBGImage = getBackground( bgImage, mDefaultBGImage );
      if ( !mUseDefaultBGImage )
        kdDebug(5720) << "KABDetailedView::configure: "
                      << "default BG image selected, but could not be loaded."
                      << endl;
    }
  }

  mDefaultBGColor = config->readColorEntry( ConfigView_DefaultBackgroundColor, &white );
  mHeadLineBGColor = config->readColorEntry( ConfigView_HeadlineBGColor, &darkBlue );
  mHeadLineTextColor = config->readColorEntry( ConfigView_HeadlineTextColor, &yellow );
  mUseHeadLineBGColor = config->readBoolEntry( ConfigView_UseHeadlineBGColor, true );

  if ( !mPainter )
    mPainter = new KABEntryPainter;

  mPainter->setForegroundColor( black );
  mPainter->setHeaderColor( mHeadLineTextColor );
  mPainter->setUseHeaderColor( mUseHeadLineBGColor );
  mPainter->setBackgroundColor( mHeadLineBGColor );

  mPainter->setHeaderFont( QFont( gfont, gpointsize + 4, QFont::Bold, true ) );
  mPainter->setHeadLineFont( QFont( gfont, gpointsize + 2, QFont::Bold, true ) );
  mPainter->setBodyFont( QFont( gfont, gpointsize, QFont::Normal, false ) );
  mPainter->setFixedFont( QFont( ffont, fpointsize, QFont::Normal, false ) );
  mPainter->setCommentFont( QFont( gfont, gpointsize, QFont::Normal, false ) );
}

#ifndef KAB_EMBEDDED_
#include "moc_look_details.cpp"
#endif //KAB_EMBEDDED