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., 675 Mass Ave, Cambridge, MA 02139, 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 
00043 #include "kaddressbooktableview.h"
00044 
00045 #include "contactlistview.h"
00046 
00048 // DynamicTip Methods
00049 
00050 DynamicTip::DynamicTip( ContactListView *parent)
00051   : QToolTip( parent )
00052 {
00053 }
00054 
00055 void DynamicTip::maybeTip( const QPoint &pos )
00056 {
00057   if (!parentWidget()->inherits( "ContactListView" ))
00058     return;
00059 
00060   ContactListView *plv = (ContactListView*)parentWidget();
00061   if (!plv->tooltips())
00062     return;
00063 
00064   QPoint posVp = plv->viewport()->pos();
00065 
00066   QListViewItem *lvi = plv->itemAt( pos - posVp );
00067   if (!lvi)
00068     return;
00069 
00070   ContactListViewItem *plvi = dynamic_cast< ContactListViewItem* >(lvi);
00071   if (!plvi)
00072     return;
00073 
00074   QString s;
00075   QRect r = plv->itemRect( lvi );
00076   r.moveBy( posVp.x(), posVp.y() );
00077 
00078     //kdDebug() << "Tip rec: " << r.x() << "," << r.y() << "," << r.width()
00079     //          << "," << r.height() << endl;
00080 
00081   KABC::Addressee a = plvi->addressee();
00082   if (a.isEmpty())
00083     return;
00084 
00085   s += i18n("label: value", "%1: %2").arg(a.formattedNameLabel())
00086                                      .arg(a.formattedName());
00087 
00088   s += '\n';
00089   s += i18n("label: value", "%1: %2").arg(a.organizationLabel())
00090                                        .arg(a.organization());
00091 
00092   QString notes = a.note().stripWhiteSpace();
00093   if ( !notes.isEmpty() ) {
00094     notes += '\n';
00095     s += '\n' + i18n("label: value", "%1: \n").arg(a.noteLabel());
00096     QFontMetrics fm( font() );
00097 
00098     // Begin word wrap code based on QMultiLineEdit code
00099     int i = 0;
00100     bool doBreak = false;
00101     int linew = 0;
00102     int lastSpace = -1;
00103     int a = 0;
00104     int lastw = 0;
00105 
00106     while ( i < int(notes.length()) ) {
00107       doBreak = FALSE;
00108       if ( notes[i] != '\n' )
00109         linew += fm.width( notes[i] );
00110 
00111       if ( lastSpace >= a && notes[i] != '\n' )
00112         if  (linew >= parentWidget()->width()) {
00113           doBreak = TRUE;
00114           if ( lastSpace > a ) {
00115             i = lastSpace;
00116             linew = lastw;
00117           }
00118           else
00119             i = QMAX( a, i-1 );
00120         }
00121 
00122       if ( notes[i] == '\n' || doBreak ) {
00123         s += notes.mid( a, i - a + (doBreak?1:0) ) +"\n";
00124 
00125         a = i + 1;
00126         lastSpace = a;
00127         linew = 0;
00128       }
00129 
00130       if ( notes[i].isSpace() ) {
00131         lastSpace = i;
00132         lastw = linew;
00133       }
00134 
00135       if ( lastSpace <= a ) {
00136         lastw = linew;
00137       }
00138 
00139       ++i;
00140     }
00141   }
00142 
00143   tip( r, s );
00144 }
00145 
00147 // ContactListViewItem Methods
00148 
00149 ContactListViewItem::ContactListViewItem(const KABC::Addressee &a,
00150                                          ContactListView *parent,
00151                                          KABC::AddressBook *doc,
00152                                          const KABC::Field::List &fields )
00153   : KListViewItem(parent), mAddressee(a), mFields( fields ),
00154     parentListView( parent ), mDocument(doc)
00155 {
00156   refresh();
00157 }
00158 
00159 QString ContactListViewItem::key(int column, bool ascending) const
00160 {
00161   return QListViewItem::key(column, ascending).lower();
00162 }
00163 
00164 void ContactListViewItem::paintCell(QPainter * p,
00165                                                     const QColorGroup & cg,
00166                                     int column,
00167                                     int width,
00168                                     int align)
00169 {
00170   KListViewItem::paintCell(p, cg, column, width, align);
00171 
00172   if ( !p )
00173     return;
00174 
00175   if (parentListView->singleLine()) {
00176     p->setPen( parentListView->alternateColor() );
00177     p->drawLine( 0, height() - 1, width, height() - 1 );
00178   }
00179 }
00180 
00181 
00182 ContactListView *ContactListViewItem::parent()
00183 {
00184   return parentListView;
00185 }
00186 
00187 
00188 void ContactListViewItem::refresh()
00189 {
00190   // Update our addressee, since it may have changed else were
00191   mAddressee = mDocument->findByUid(mAddressee.uid());
00192   if (mAddressee.isEmpty())
00193     return;
00194 
00195   int i = 0;
00196   KABC::Field::List::ConstIterator it;
00197   for( it = mFields.begin(); it != mFields.end(); ++it ) {
00198     setText( i++, (*it)->value( mAddressee ) );
00199   }
00200 }
00201 
00203 // ContactListView
00204 
00205 ContactListView::ContactListView(KAddressBookTableView *view,
00206                                  KABC::AddressBook* /* doc */,
00207                                  QWidget *parent,
00208                                  const char *name )
00209   : KListView( parent, name ),
00210     pabWidget( view ),
00211     oldColumn( 0 )
00212 {
00213   mABackground = true;
00214   mSingleLine = false;
00215   mToolTips = true;
00216   mAlternateColor = KGlobalSettings::alternateBackgroundColor();
00217 
00218   setAlternateBackgroundEnabled(mABackground);
00219   setAcceptDrops( true );
00220   viewport()->setAcceptDrops( true );
00221   setAllColumnsShowFocus( true );
00222   setShowSortIndicator(true);
00223   setSelectionModeExt( KListView::Extended );
00224   setDropVisualizer(false);
00225 
00226   connect(this, SIGNAL(dropped(QDropEvent*)),
00227           this, SLOT(itemDropped(QDropEvent*)));
00228 
00229   new DynamicTip( this );
00230 }
00231 
00232 void ContactListView::paintEmptyArea( QPainter * p, const QRect & rect )
00233 {
00234   QBrush b = palette().brush(QPalette::Active, QColorGroup::Base);
00235 
00236   // Get the brush, which will have the background pixmap if there is one.
00237   if (b.pixmap())
00238   {
00239     p->drawTiledPixmap( rect.left(), rect.top(), rect.width(), rect.height(),
00240       *(b.pixmap()),
00241       rect.left() + contentsX(),
00242       rect.top() + contentsY() );
00243   }
00244 
00245   else
00246   {
00247     // Do a normal paint
00248     KListView::paintEmptyArea(p, rect);
00249   }
00250 }
00251 
00252 void ContactListView::contentsMousePressEvent(QMouseEvent* e)
00253 {
00254   presspos = e->pos();
00255   KListView::contentsMousePressEvent(e);
00256 }
00257 
00258 
00259 // To initiate a drag operation
00260 void ContactListView::contentsMouseMoveEvent( QMouseEvent *e )
00261 {
00262   if ((e->state() & LeftButton) && (e->pos() - presspos).manhattanLength() > 4 ) {
00263     emit startAddresseeDrag();
00264   }
00265   else
00266     KListView::contentsMouseMoveEvent( e );
00267 }
00268 
00269 bool ContactListView::acceptDrag(QDropEvent *e) const
00270 {
00271   return QTextDrag::canDecode(e);
00272 }
00273 
00274 void ContactListView::itemDropped(QDropEvent *e)
00275 {
00276   contentsDropEvent(e);
00277 }
00278 
00279 void ContactListView::contentsDropEvent( QDropEvent *e )
00280 {
00281   emit addresseeDropped(e);
00282 }
00283 
00284 void ContactListView::setAlternateBackgroundEnabled(bool enabled)
00285 {
00286   mABackground = enabled;
00287 
00288   if (mABackground)
00289   {
00290     setAlternateBackground(mAlternateColor);
00291   }
00292   else
00293   {
00294     setAlternateBackground(QColor());
00295   }
00296 }
00297 
00298 void ContactListView::setBackgroundPixmap(const QString &filename)
00299 {
00300   if (filename.isEmpty())
00301   {
00302     unsetPalette();
00303   }
00304   else
00305   {
00306     setPaletteBackgroundPixmap(QPixmap(filename));
00307   }
00308 }
00309 
00310 #include "contactlistview.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:09 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001