• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kaddressbook

contactlistview.cpp

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <Qt3Support/Q3Header>
00025 #include <QtGui/QBrush>
00026 #include <QtGui/QDropEvent>
00027 #include <QtGui/QMouseEvent>
00028 #include <QtGui/QPainter>
00029 #include <QtGui/QPixmap>
00030 
00031 #include <k3listview.h>
00032 #include <kabc/addressbook.h>
00033 #include <kabc/addressee.h>
00034 #include <kapplication.h>
00035 #include <kcolorscheme.h>
00036 #include <kconfig.h>
00037 #include <kdebug.h>
00038 #include <kimproxy.h>
00039 #include <klocale.h>
00040 #include <kurl.h>
00041 
00042 #include "kaddressbooktableview.h"
00043 
00044 #include "contactlistview.h"
00045 
00047 // DynamicTip Methods
00048 
00049 #if 0
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   Q3ListViewItem *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     //kDebug(5720) <<"Tip rec:" << r.x() <<"," << r.y() <<"," << r.width()
00080     //          << "," << r.height();
00081 
00082   KABC::Addressee a = plvi->addressee();
00083   if (a.isEmpty())
00084     return;
00085 
00086   s += i18nc("label: value", "%1: %2", a.formattedNameLabel(),
00087                                       a.formattedName());
00088 
00089   s += '\n';
00090   s += i18nc("label: value", "%1: %2", a.organizationLabel(),
00091                                         a.organization());
00092 
00093   QString notes = a.note().trimmed();
00094   if ( !notes.isEmpty() ) {
00095     notes += '\n';
00096     s += '\n' + i18nc("label: value", "%1: \n", 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 
00147 #endif
00148 
00150 // ContactListViewItem Methods
00151 
00152 ContactListViewItem::ContactListViewItem(const KABC::Addressee &a,
00153                                          ContactListView *parent,
00154                                          KABC::AddressBook *doc,
00155                                          const KABC::Field::List &fields,
00156                                          KIMProxy *proxy )
00157   : K3ListViewItem(parent), mAddressee(a), mFields( fields ),
00158     parentListView( parent ), mDocument(doc), mIMProxy( proxy )
00159 {
00160   if ( mIMProxy )
00161     mHasIM = mIMProxy->isPresent( mAddressee.uid() );
00162   else
00163     mHasIM = false;
00164   refresh();
00165 }
00166 
00167 QString ContactListViewItem::key(int column, bool ascending) const
00168 {
00169   // Preserve behaviour of QListViewItem::key(), otherwise we cause a crash if the column does not exist
00170   if ( column >= parentListView->columns() )
00171     return QString();
00172 
00173   Q_UNUSED( ascending )
00174   if ( parentListView->showIM() ) {
00175     // in this case, one column is reserved for IM presence
00176     // so we have to process it differently
00177     if ( column == parentListView->imColumn() ) {
00178       // increment by one before converting to string so that -1 is not greater than 1
00179       // create the sort key by taking the numeric status 0 low, 5 high, and subtracting it from 5
00180       // so that the default ascending gives online before offline, etc.
00181       QString key = QString::number( 5 - ( mIMProxy->presenceNumeric( mAddressee.uid() ) + 1 ) );
00182       return key;
00183     }
00184     else {
00185       return mFields[ column ]->sortKey( mAddressee );
00186     }
00187   }
00188   else
00189     return mFields[ column ]->sortKey( mAddressee );
00190 }
00191 
00192 void ContactListViewItem::paintCell(QPainter * p,
00193                                     const QColorGroup & cg,
00194                                     int column,
00195                                     int width,
00196                                     int align)
00197 {
00198   K3ListViewItem::paintCell(p, cg, column, width, align);
00199 
00200   if ( !p )
00201     return;
00202 
00203   if (parentListView->singleLine()) {
00204     p->setPen( parentListView->alternateColor() );
00205     p->drawLine( 0, height() - 1, width, height() - 1 );
00206   }
00207 }
00208 
00209 
00210 ContactListView *ContactListViewItem::parent()
00211 {
00212   return parentListView;
00213 }
00214 
00215 
00216 void ContactListViewItem::refresh()
00217 {
00218   // Update our addressee, since it may have changed else were
00219   mAddressee = mDocument->findByUid(mAddressee.uid());
00220   if (mAddressee.isEmpty())
00221     return;
00222 
00223   int i = 0;
00224   // don't show unknown presence, it's not interesting
00225   if ( mHasIM ) {
00226     if ( mIMProxy->presenceNumeric( mAddressee.uid() ) > 0 )
00227       setPixmap( parentListView->imColumn(), mIMProxy->presenceIcon( mAddressee.uid() ) );
00228     else
00229       setPixmap( parentListView->imColumn(), QPixmap() );
00230   }
00231 
00232   KABC::Field::List::ConstIterator it;
00233   for ( it = mFields.begin(); it != mFields.end(); ++it ) {
00234     if ( (*it)->label() == KABC::Addressee::birthdayLabel() ) {
00235       QDate date = mAddressee.birthday().date();
00236       if ( date.isValid() )
00237         setText( i++, KGlobal::locale()->formatDate( date, KLocale::ShortDate ) );
00238       else
00239         setText( i++, "" );
00240     } else
00241       setText( i++, (*it)->value( mAddressee ) );
00242   }
00243 }
00244 
00245 void ContactListViewItem::setHasIM( bool hasIM )
00246 {
00247   mHasIM = hasIM;
00248 }
00249 
00251 // ContactListView
00252 
00253 ContactListView::ContactListView(KAddressBookTableView *view,
00254                                  KABC::AddressBook* /* doc */,
00255                                  QWidget *parent,
00256                                  const char *name )
00257   : K3ListView( parent ),
00258     pabWidget( view ),
00259     oldColumn( 0 )
00260 {
00261   setObjectName(name);
00262   mABackground = true;
00263   mSingleLine = false;
00264   mToolTips = true;
00265   mShowIM = true;
00266   mAlternateColor = KColorScheme( QPalette::Active, KColorScheme::View ).background( KColorScheme::AlternateBackground ).color();
00267 
00268   setAlternateBackgroundEnabled(mABackground);
00269   setAcceptDrops( true );
00270   viewport()->setAcceptDrops( true );
00271   setAllColumnsShowFocus( true );
00272   setShowSortIndicator(true);
00273   setSelectionModeExt( K3ListView::Extended );
00274   setDropVisualizer(false);
00275 
00276   connect(this, SIGNAL(dropped(QDropEvent*)),
00277           this, SLOT(itemDropped(QDropEvent*)));
00278 /*
00279  * FIXME porting
00280   new DynamicTip( this );
00281   */
00282 }
00283 
00284 void ContactListView::paintEmptyArea( QPainter * p, const QRect & rect )
00285 {
00286   QBrush b = palette().brush(QPalette::Active, QPalette::Base);
00287 
00288   // Get the brush, which will have the background pixmap if there is one.
00289   if (!b.texture().isNull())
00290   {
00291     p->drawTiledPixmap( rect.left(), rect.top(), rect.width(), rect.height(),
00292       b.texture(),
00293       rect.left() + contentsX(),
00294       rect.top() + contentsY() );
00295   }
00296 
00297   else
00298   {
00299     // Do a normal paint
00300     K3ListView::paintEmptyArea(p, rect);
00301   }
00302 }
00303 
00304 void ContactListView::contentsMousePressEvent(QMouseEvent* e)
00305 {
00306   presspos = e->pos();
00307   K3ListView::contentsMousePressEvent(e);
00308 }
00309 
00310 
00311 // To initiate a drag operation
00312 void ContactListView::contentsMouseMoveEvent( QMouseEvent *e )
00313 {
00314   if ((e->buttons() & Qt::LeftButton) && (e->pos() - presspos).manhattanLength() > 4 ) {
00315     emit startAddresseeDrag();
00316   }
00317   else
00318     K3ListView::contentsMouseMoveEvent( e );
00319 }
00320 
00321 bool ContactListView::acceptDrag(QDropEvent *e) const
00322 {
00323   return e->mimeData()->hasText();
00324 }
00325 
00326 void ContactListView::itemDropped(QDropEvent *e)
00327 {
00328   contentsDropEvent(e);
00329 }
00330 
00331 void ContactListView::contentsDropEvent( QDropEvent *e )
00332 {
00333   emit addresseeDropped(e);
00334 }
00335 
00336 void ContactListView::setAlternateBackgroundEnabled(bool enabled)
00337 {
00338   mABackground = enabled;
00339 
00340   if (mABackground)
00341   {
00342     setAlternateBackground(mAlternateColor);
00343   }
00344   else
00345   {
00346     setAlternateBackground(QColor());
00347   }
00348 }
00349 
00350 void ContactListView::setBackgroundPixmap(const QString &filename)
00351 {
00352   if (filename.isEmpty())
00353   {
00354     setPalette( QPalette() );
00355   }
00356   else
00357   {
00358     QPalette palette;
00359     palette.setBrush( backgroundRole(), QBrush(QPixmap(filename) ));
00360     setPalette( palette );
00361   }
00362 }
00363 
00364 void ContactListView::setShowIM( bool enabled )
00365 {
00366   mShowIM = enabled;
00367 }
00368 
00369 bool ContactListView::showIM()
00370 {
00371   return mShowIM;
00372 }
00373 
00374 void ContactListView::setIMColumn( int column )
00375 {
00376   mInstantMsgColumn = column;
00377 }
00378 
00379 int ContactListView::imColumn()
00380 {
00381   return mInstantMsgColumn;
00382 }
00383 
00384 #include "contactlistview.moc"

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal