kaddressbook Library API Documentation

contactlistview.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2002 Mike Pilone <mpilone@slac.com>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qheader.h>
00025 #include <qiconset.h>
00026 #include <qimage.h>
00027 #include <qdragobject.h>
00028 #include <qcombobox.h>
00029 #include <qpainter.h>
00030 #include <qbrush.h>
00031 #include <qevent.h>
00032 
00033 #include <klocale.h>
00034 #include <kglobalsettings.h>
00035 #include <kiconloader.h>
00036 #include <kdebug.h>
00037 #include <kconfig.h>
00038 #include <kapplication.h>
00039 #include <kurl.h>
00040 #include <kabc/addressbook.h>
00041 #include <kabc/addressee.h>
00042 #include <libkdepim/kimproxy.h>
00043 
00044 #include "kaddressbooktableview.h"
00045 
00046 #include "contactlistview.h"
00047 
00049 // DynamicTip Methods
00050 
00051 DynamicTip::DynamicTip( ContactListView *parent)
00052   : QToolTip( parent )
00053 {
00054 }
00055 
00056 void DynamicTip::maybeTip( const QPoint &pos )
00057 {
00058   if (!parentWidget()->inherits( "ContactListView" ))
00059     return;
00060 
00061   ContactListView *plv = (ContactListView*)parentWidget();
00062   if (!plv->tooltips())
00063     return;
00064 
00065   QPoint posVp = plv->viewport()->pos();
00066 
00067   QListViewItem *lvi = plv->itemAt( pos - posVp );
00068   if (!lvi)
00069     return;
00070 
00071   ContactListViewItem *plvi = dynamic_cast< ContactListViewItem* >(lvi);
00072   if (!plvi)
00073     return;
00074 
00075   QString s;
00076   QRect r = plv->itemRect( lvi );
00077   r.moveBy( posVp.x(), posVp.y() );
00078 
00079     //kdDebug(5720) << "Tip rec: " << r.x() << "," << r.y() << "," << r.width()
00080     //          << "," << r.height() << endl;
00081 
00082   KABC::Addressee a = plvi->addressee();
00083   if (a.isEmpty())
00084     return;
00085 
00086   s += i18n("label: value", "%1: %2").arg(a.formattedNameLabel())
00087                                      .arg(a.formattedName());
00088 
00089   s += '\n';
00090   s += i18n("label: value", "%1: %2").arg(a.organizationLabel())
00091                                        .arg(a.organization());
00092 
00093   QString notes = a.note().stripWhiteSpace();
00094   if ( !notes.isEmpty() ) {
00095     notes += '\n';
00096     s += '\n' + i18n("label: value", "%1: \n").arg(a.noteLabel());
00097     QFontMetrics fm( font() );
00098 
00099     // Begin word wrap code based on QMultiLineEdit code
00100     int i = 0;
00101     bool doBreak = false;
00102     int linew = 0;
00103     int lastSpace = -1;
00104     int a = 0;
00105     int lastw = 0;
00106 
00107     while ( i < int(notes.length()) ) {
00108       doBreak = FALSE;
00109       if ( notes[i] != '\n' )
00110         linew += fm.width( notes[i] );
00111 
00112       if ( lastSpace >= a && notes[i] != '\n' )
00113         if  (linew >= parentWidget()->width()) {
00114           doBreak = TRUE;
00115           if ( lastSpace > a ) {
00116             i = lastSpace;
00117             linew = lastw;
00118           }
00119           else
00120             i = QMAX( a, i-1 );
00121         }
00122 
00123       if ( notes[i] == '\n' || doBreak ) {
00124         s += notes.mid( a, i - a + (doBreak?1:0) ) +"\n";
00125 
00126         a = i + 1;
00127         lastSpace = a;
00128         linew = 0;
00129       }
00130 
00131       if ( notes[i].isSpace() ) {
00132         lastSpace = i;
00133         lastw = linew;
00134       }
00135 
00136       if ( lastSpace <= a ) {
00137         lastw = linew;
00138       }
00139 
00140       ++i;
00141     }
00142   }
00143 
00144   tip( r, s );
00145 }
00146 
00148 // ContactListViewItem Methods
00149 
00150 ContactListViewItem::ContactListViewItem(const KABC::Addressee &a,
00151                                          ContactListView *parent,
00152                                          KABC::AddressBook *doc,
00153                                          const KABC::Field::List &fields,
00154                                          KIMProxy *proxy )
00155   : KListViewItem(parent), mAddressee(a), mFields( fields ),
00156     parentListView( parent ), mDocument(doc), mIMProxy( proxy )
00157 {
00158   if ( mIMProxy )
00159     mHasIM = ( !( mIMProxy->allContacts().find( mAddressee.uid() ) == mIMProxy->allContacts().end() ) );
00160   else 
00161     mHasIM = false;
00162   refresh();
00163 }
00164 
00165 QString ContactListViewItem::key(int column, bool ascending) const
00166 {
00167   // Preserve behaviour of QListViewItem::key(), otherwise we cause a crash if the column does not exist
00168   if ( column >= parentListView->columns() )
00169     return QString::null;
00170     
00171 #if KDE_VERSION >= 319
00172   Q_UNUSED( ascending )
00173   if ( parentListView->showIM() ) {
00174     // in this case, one column is reserved for IM presence
00175     // so we have to process it differently
00176     if ( column == parentListView->imColumn() ) {
00177       // increment by one before converting to string so that -1 is not greater than 1
00178       // create the sort key by taking the numeric status 0 low, 5 high, and subtracting it from 5
00179       // so that the default ascending gives online before offline, etc.
00180       QString key = QString::number( 5 - ( mIMProxy->presenceNumeric( mAddressee.uid() ) + 1 ) );
00181       return key;
00182     }
00183     else {
00184       return mFields[ column ]->sortKey( mAddressee );
00185     }
00186   }
00187   else 
00188     return mFields[ column ]->sortKey( mAddressee );
00189 #else
00190   return QListViewItem::key( column, ascending ).lower();
00191 #endif
00192 }
00193 
00194 void ContactListViewItem::paintCell(QPainter * p,
00195                                     const QColorGroup & cg,
00196                                     int column,
00197                                     int width,
00198                                     int align)
00199 {
00200   KListViewItem::paintCell(p, cg, column, width, align);
00201 
00202   if ( !p )
00203     return;
00204 
00205   if (parentListView->singleLine()) {
00206     p->setPen( parentListView->alternateColor() );
00207     p->drawLine( 0, height() - 1, width, height() - 1 );
00208   }
00209 }
00210 
00211 
00212 ContactListView *ContactListViewItem::parent()
00213 {
00214   return parentListView;
00215 }
00216 
00217 
00218 void ContactListViewItem::refresh()
00219 {
00220   // Update our addressee, since it may have changed else were
00221   mAddressee = mDocument->findByUid(mAddressee.uid());
00222   if (mAddressee.isEmpty())
00223     return;
00224 
00225   int i = 0;
00226   if ( mHasIM )
00227     setPixmap( parentListView->imColumn(), mIMProxy->presenceIcon( mAddressee.uid() ) );
00228   
00229   KABC::Field::List::ConstIterator it;
00230   for( it = mFields.begin(); it != mFields.end(); ++it ) {
00231     if ( (*it)->label() == KABC::Addressee::birthdayLabel() ) {
00232       QDate date = mAddressee.birthday().date();
00233       if ( date.isValid() )
00234         setText( i++, KGlobal::locale()->formatDate( date, true ) );
00235       else
00236         setText( i++, "" );
00237     } else
00238       setText( i++, (*it)->value( mAddressee ) );
00239   }
00240 }
00241 
00242 void ContactListViewItem::setHasIM( bool hasIM )
00243 {
00244   mHasIM = hasIM;
00245 }
00246 
00248 // ContactListView
00249 
00250 ContactListView::ContactListView(KAddressBookTableView *view,
00251                                  KABC::AddressBook* /* doc */,
00252                                  QWidget *parent,
00253                                  const char *name )
00254   : KListView( parent, name ),
00255     pabWidget( view ),
00256     oldColumn( 0 )
00257 {
00258   mABackground = true;
00259   mSingleLine = false;
00260   mToolTips = true;
00261   mShowIM = true;
00262   mAlternateColor = KGlobalSettings::alternateBackgroundColor();
00263   
00264   setAlternateBackgroundEnabled(mABackground);
00265   setAcceptDrops( true );
00266   viewport()->setAcceptDrops( true );
00267   setAllColumnsShowFocus( true );
00268   setShowSortIndicator(true);
00269   setSelectionModeExt( KListView::Extended );
00270   setDropVisualizer(false);
00271 
00272   connect(this, SIGNAL(dropped(QDropEvent*)),
00273           this, SLOT(itemDropped(QDropEvent*)));
00274 
00275   new DynamicTip( this );
00276 }
00277 
00278 void ContactListView::paintEmptyArea( QPainter * p, const QRect & rect )
00279 {
00280   QBrush b = palette().brush(QPalette::Active, QColorGroup::Base);
00281 
00282   // Get the brush, which will have the background pixmap if there is one.
00283   if (b.pixmap())
00284   {
00285     p->drawTiledPixmap( rect.left(), rect.top(), rect.width(), rect.height(),
00286       *(b.pixmap()),
00287       rect.left() + contentsX(),
00288       rect.top() + contentsY() );
00289   }
00290 
00291   else
00292   {
00293     // Do a normal paint
00294     KListView::paintEmptyArea(p, rect);
00295   }
00296 }
00297 
00298 void ContactListView::contentsMousePressEvent(QMouseEvent* e)
00299 {
00300   presspos = e->pos();
00301   KListView::contentsMousePressEvent(e);
00302 }
00303 
00304 
00305 // To initiate a drag operation
00306 void ContactListView::contentsMouseMoveEvent( QMouseEvent *e )
00307 {
00308   if ((e->state() & LeftButton) && (e->pos() - presspos).manhattanLength() > 4 ) {
00309     emit startAddresseeDrag();
00310   }
00311   else
00312     KListView::contentsMouseMoveEvent( e );
00313 }
00314 
00315 bool ContactListView::acceptDrag(QDropEvent *e) const
00316 {
00317   return QTextDrag::canDecode(e);
00318 }
00319 
00320 void ContactListView::itemDropped(QDropEvent *e)
00321 {
00322   contentsDropEvent(e);
00323 }
00324 
00325 void ContactListView::contentsDropEvent( QDropEvent *e )
00326 {
00327   emit addresseeDropped(e);
00328 }
00329 
00330 void ContactListView::setAlternateBackgroundEnabled(bool enabled)
00331 {
00332   mABackground = enabled;
00333 
00334   if (mABackground)
00335   {
00336     setAlternateBackground(mAlternateColor);
00337   }
00338   else
00339   {
00340     setAlternateBackground(QColor());
00341   }
00342 }
00343 
00344 void ContactListView::setBackgroundPixmap(const QString &filename)
00345 {
00346   if (filename.isEmpty())
00347   {
00348     unsetPalette();
00349   }
00350   else
00351   {
00352     setPaletteBackgroundPixmap(QPixmap(filename));
00353   }
00354 }
00355 
00356 void ContactListView::setShowIM( bool enabled )
00357 {
00358   mShowIM = enabled;
00359 }
00360 
00361 bool ContactListView::showIM()
00362 {
00363   return mShowIM;
00364 }
00365 
00366 void ContactListView::setIMColumn( int column )
00367 {
00368   mInstantMsgColumn = column;
00369 }
00370 
00371 int ContactListView::imColumn()
00372 {
00373   return mInstantMsgColumn;
00374 }
00375 
00376 #include "contactlistview.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Apr 4 04:47:50 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003