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 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 #if KDE_VERSION >= 319 00162 Q_UNUSED( ascending ) 00163 return mFields[ column ]->sortKey( mAddressee ); 00164 #else 00165 return QListViewItem::key( column, ascending ).lower(); 00166 #endif 00167 } 00168 00169 void ContactListViewItem::paintCell(QPainter * p, 00170 const QColorGroup & cg, 00171 int column, 00172 int width, 00173 int align) 00174 { 00175 KListViewItem::paintCell(p, cg, column, width, align); 00176 00177 if ( !p ) 00178 return; 00179 00180 if (parentListView->singleLine()) { 00181 p->setPen( parentListView->alternateColor() ); 00182 p->drawLine( 0, height() - 1, width, height() - 1 ); 00183 } 00184 } 00185 00186 00187 ContactListView *ContactListViewItem::parent() 00188 { 00189 return parentListView; 00190 } 00191 00192 00193 void ContactListViewItem::refresh() 00194 { 00195 // Update our addressee, since it may have changed else were 00196 mAddressee = mDocument->findByUid(mAddressee.uid()); 00197 if (mAddressee.isEmpty()) 00198 return; 00199 00200 int i = 0; 00201 KABC::Field::List::ConstIterator it; 00202 for( it = mFields.begin(); it != mFields.end(); ++it ) { 00203 if ( (*it)->label() == KABC::Addressee::birthdayLabel() ) { 00204 QDate date = mAddressee.birthday().date(); 00205 if ( date.isValid() ) 00206 setText( i++, KGlobal::locale()->formatDate( date, true ) ); 00207 else 00208 setText( i++, "" ); 00209 } else 00210 setText( i++, (*it)->value( mAddressee ) ); 00211 } 00212 } 00213 00215 // ContactListView 00216 00217 ContactListView::ContactListView(KAddressBookTableView *view, 00218 KABC::AddressBook* /* doc */, 00219 QWidget *parent, 00220 const char *name ) 00221 : KListView( parent, name ), 00222 pabWidget( view ), 00223 oldColumn( 0 ) 00224 { 00225 mABackground = true; 00226 mSingleLine = false; 00227 mToolTips = true; 00228 mAlternateColor = KGlobalSettings::alternateBackgroundColor(); 00229 00230 setAlternateBackgroundEnabled(mABackground); 00231 setAcceptDrops( true ); 00232 viewport()->setAcceptDrops( true ); 00233 setAllColumnsShowFocus( true ); 00234 setShowSortIndicator(true); 00235 setSelectionModeExt( KListView::Extended ); 00236 setDropVisualizer(false); 00237 00238 connect(this, SIGNAL(dropped(QDropEvent*)), 00239 this, SLOT(itemDropped(QDropEvent*))); 00240 00241 new DynamicTip( this ); 00242 } 00243 00244 void ContactListView::paintEmptyArea( QPainter * p, const QRect & rect ) 00245 { 00246 QBrush b = palette().brush(QPalette::Active, QColorGroup::Base); 00247 00248 // Get the brush, which will have the background pixmap if there is one. 00249 if (b.pixmap()) 00250 { 00251 p->drawTiledPixmap( rect.left(), rect.top(), rect.width(), rect.height(), 00252 *(b.pixmap()), 00253 rect.left() + contentsX(), 00254 rect.top() + contentsY() ); 00255 } 00256 00257 else 00258 { 00259 // Do a normal paint 00260 KListView::paintEmptyArea(p, rect); 00261 } 00262 } 00263 00264 void ContactListView::contentsMousePressEvent(QMouseEvent* e) 00265 { 00266 presspos = e->pos(); 00267 KListView::contentsMousePressEvent(e); 00268 } 00269 00270 00271 // To initiate a drag operation 00272 void ContactListView::contentsMouseMoveEvent( QMouseEvent *e ) 00273 { 00274 if ((e->state() & LeftButton) && (e->pos() - presspos).manhattanLength() > 4 ) { 00275 emit startAddresseeDrag(); 00276 } 00277 else 00278 KListView::contentsMouseMoveEvent( e ); 00279 } 00280 00281 bool ContactListView::acceptDrag(QDropEvent *e) const 00282 { 00283 return QTextDrag::canDecode(e); 00284 } 00285 00286 void ContactListView::itemDropped(QDropEvent *e) 00287 { 00288 contentsDropEvent(e); 00289 } 00290 00291 void ContactListView::contentsDropEvent( QDropEvent *e ) 00292 { 00293 emit addresseeDropped(e); 00294 } 00295 00296 void ContactListView::setAlternateBackgroundEnabled(bool enabled) 00297 { 00298 mABackground = enabled; 00299 00300 if (mABackground) 00301 { 00302 setAlternateBackground(mAlternateColor); 00303 } 00304 else 00305 { 00306 setAlternateBackground(QColor()); 00307 } 00308 } 00309 00310 void ContactListView::setBackgroundPixmap(const QString &filename) 00311 { 00312 if (filename.isEmpty()) 00313 { 00314 unsetPalette(); 00315 } 00316 else 00317 { 00318 setPaletteBackgroundPixmap(QPixmap(filename)); 00319 } 00320 } 00321 00322 #include "contactlistview.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 28 23:58:08 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003