00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <QtCore/QEvent>
00025 #include <QtCore/QStringList>
00026 #include <QtGui/QApplication>
00027 #include <QtGui/QDragEnterEvent>
00028 #include <QtGui/QDropEvent>
00029 #include <QtGui/QKeyEvent>
00030 #include <QtGui/QVBoxLayout>
00031
00032 #include <kabc/addressbook.h>
00033 #include <kabc/addressee.h>
00034 #include <kconfig.h>
00035 #include <kconfiggroup.h>
00036 #include <kdebug.h>
00037 #include <klocale.h>
00038
00039 #include "core.h"
00040 #include "configurecardviewdialog.h"
00041 #include "kabprefs.h"
00042
00043 #include "kaddressbookcardview.h"
00044
00045 class CardViewFactory : public ViewFactory
00046 {
00047 public:
00048 KAddressBookView *view( KAB::Core *core, QWidget *parent )
00049 {
00050 return new KAddressBookCardView( core, parent );
00051 }
00052
00053 QString type() const { return I18N_NOOP("Card"); }
00054
00055 QString description() const { return i18n( "Rolodex style cards represent contacts." ); }
00056
00057 ViewConfigureWidget *configureWidget( KABC::AddressBook *ab, QWidget *parent )
00058 {
00059 return new ConfigureCardViewWidget( ab, parent );
00060 }
00061 };
00062
00063 K_EXPORT_PLUGIN(CardViewFactory)
00064
00065 class AddresseeCardViewItem : public CardViewItem
00066 {
00067 public:
00068 AddresseeCardViewItem( const KABC::Field::List &fields,
00069 bool showEmptyFields,
00070 KABC::AddressBook *doc, const KABC::Addressee &addr,
00071 CardView *parent )
00072 : CardViewItem( parent, addr.realName() ),
00073 mFields( fields ), mShowEmptyFields( showEmptyFields ),
00074 mDocument( doc ), mAddressee( addr )
00075 {
00076 if ( mFields.isEmpty() )
00077 mFields = KABC::Field::defaultFields();
00078
00079 refresh();
00080 }
00081
00082 const KABC::Addressee &addressee() const { return mAddressee; }
00083
00084 void refresh()
00085 {
00086 mAddressee = mDocument->findByUid( mAddressee.uid() );
00087
00088 if ( !mAddressee.isEmpty() ) {
00089 clearFields();
00090
00091 KABC::Field::List::ConstIterator it( mFields.begin() );
00092 const KABC::Field::List::ConstIterator endIt( mFields.end() );
00093 for ( ; it != endIt; ++it ) {
00094
00095
00096
00097
00098
00099
00100 insertField( (*it)->label(), (*it)->value( mAddressee ) );
00101 }
00102
00103 setCaption( mAddressee.realName() );
00104 }
00105 }
00106
00107 private:
00108 KABC::Field::List mFields;
00109 bool mShowEmptyFields;
00110 KABC::AddressBook *mDocument;
00111 KABC::Addressee mAddressee;
00112 };
00113
00114
00115 AddresseeCardView::AddresseeCardView( QWidget *parent, const char *name )
00116 : CardView( parent, name )
00117 {
00118 setAcceptDrops( true );
00119 }
00120
00121 AddresseeCardView::~AddresseeCardView()
00122 {
00123 }
00124
00125 void AddresseeCardView::dragEnterEvent( QDragEnterEvent *event )
00126 {
00127 const QMimeData *md = event->mimeData();
00128 if ( md->hasText() )
00129 event->accept();
00130 }
00131
00132 void AddresseeCardView::dropEvent( QDropEvent *event )
00133 {
00134 emit addresseeDropped( event );
00135 }
00136
00137 void AddresseeCardView::startDrag()
00138 {
00139 emit startAddresseeDrag();
00140 }
00141
00142
00143 KAddressBookCardView::KAddressBookCardView( KAB::Core *core,
00144 QWidget *parent )
00145 : KAddressBookView( core, parent )
00146 {
00147 mShowEmptyFields = false;
00148
00149 QVBoxLayout *layout = new QVBoxLayout( viewWidget() );
00150 layout->setMargin( 0 );
00151
00152 mCardView = new AddresseeCardView( viewWidget(), "mCardView" );
00153 mCardView->setSelectionMode( CardView::Extended );
00154 layout->addWidget( mCardView );
00155
00156
00157 connect( mCardView, SIGNAL( executed( CardViewItem* ) ),
00158 this, SLOT( addresseeExecuted( CardViewItem* ) ) );
00159 connect( mCardView, SIGNAL( selectionChanged() ),
00160 this, SLOT( addresseeSelected() ) );
00161 connect( mCardView, SIGNAL( addresseeDropped( QDropEvent* ) ),
00162 this, SIGNAL( dropped( QDropEvent* ) ) );
00163 connect( mCardView, SIGNAL( startAddresseeDrag() ),
00164 this, SIGNAL( startDrag() ) );
00165 connect( mCardView, SIGNAL( contextMenuRequested( CardViewItem*, const QPoint& ) ),
00166 this, SLOT( rmbClicked( CardViewItem*, const QPoint& ) ) );
00167 }
00168
00169 KAddressBookCardView::~KAddressBookCardView()
00170 {
00171 }
00172
00173 KABC::Field *KAddressBookCardView::sortField() const
00174 {
00175
00176 return KABC::Field::allFields()[ 0 ];
00177 }
00178
00179 void KAddressBookCardView::readConfig( KConfigGroup &cfg )
00180 {
00181 KAddressBookView::readConfig( cfg );
00182
00183
00184 if ( cfg.readEntry( "EnableCustomColors", false ) ) {
00185 QPalette p( mCardView->palette() );
00186 QColor c = p.color( QPalette::Normal, QPalette::Base );
00187 p.setColor( QPalette::Normal, QPalette::Base, cfg.readEntry( "BackgroundColor", c ) );
00188 c = p.color( QPalette::Normal, QPalette::Text );
00189 p.setColor( QPalette::Normal, QPalette::Text, cfg.readEntry( "TextColor", c ) );
00190 c = p.color( QPalette::Normal, QPalette::Button );
00191 p.setColor( QPalette::Normal, QPalette::Button, cfg.readEntry( "HeaderColor", c ) );
00192 c = p.color( QPalette::Normal, QPalette::ButtonText );
00193 p.setColor( QPalette::Normal, QPalette::ButtonText, cfg.readEntry( "HeaderTextColor", c ) );
00194 c = p.color( QPalette::Normal, QPalette::Highlight );
00195 p.setColor( QPalette::Normal, QPalette::Highlight, cfg.readEntry( "HighlightColor", c ) );
00196 c = p.color( QPalette::Normal, QPalette::HighlightedText );
00197 p.setColor( QPalette::Normal, QPalette::HighlightedText, cfg.readEntry( "HighlightedTextColor", c ) );
00198 mCardView->viewport()->setPalette( p );
00199 } else {
00200
00201 mCardView->viewport()->setPalette( mCardView->palette() );
00202 }
00203
00204
00205 QFont f( font() );
00206 if ( cfg.readEntry( "EnableCustomFonts", false ) ) {
00207 mCardView->setFont( cfg.readEntry( "TextFont", f ) );
00208 f.setBold( true );
00209 mCardView->setHeaderFont( cfg.readEntry( "HeaderFont", f ) );
00210 } else {
00211 mCardView->setFont( f );
00212 f.setBold( true );
00213 mCardView->setHeaderFont( f );
00214 }
00215
00216 mCardView->setDrawCardBorder( cfg.readEntry( "DrawBorder", true ) );
00217 mCardView->setDrawColSeparators( cfg.readEntry( "DrawSeparators", true ) );
00218 mCardView->setDrawFieldLabels( cfg.readEntry( "DrawFieldLabels", false ) );
00219 mShowEmptyFields = cfg.readEntry( "ShowEmptyFields", false );
00220
00221 mCardView->setShowEmptyFields( mShowEmptyFields );
00222
00223 mCardView->setItemWidth( cfg.readEntry( "ItemWidth", 200 ) );
00224 mCardView->setItemMargin( cfg.readEntry( "ItemMargin", 0 ) );
00225 mCardView->setItemSpacing( cfg.readEntry( "ItemSpacing", 10 ) );
00226 mCardView->setSeparatorWidth( cfg.readEntry( "SeparatorWidth", 2 ) );
00227
00228 disconnect( mCardView, SIGNAL( executed( CardViewItem* ) ),
00229 this, SLOT( addresseeExecuted( CardViewItem* ) ) );
00230
00231 if ( KABPrefs::instance()->honorSingleClick() )
00232 connect( mCardView, SIGNAL( executed( CardViewItem* ) ),
00233 this, SLOT( addresseeExecuted( CardViewItem* ) ) );
00234 else
00235 connect( mCardView, SIGNAL( doubleClicked( CardViewItem* ) ),
00236 this, SLOT( addresseeExecuted( CardViewItem* ) ) );
00237 }
00238
00239 void KAddressBookCardView::writeConfig( KConfigGroup &cfg )
00240 {
00241 cfg.writeEntry( "ItemWidth", mCardView->itemWidth() );
00242 KAddressBookView::writeConfig( cfg );
00243 }
00244
00245 QStringList KAddressBookCardView::selectedUids()
00246 {
00247 QStringList uidList;
00248 CardViewItem *item;
00249 AddresseeCardViewItem *aItem;
00250
00251 for ( item = mCardView->firstItem(); item; item = item->nextItem() ) {
00252 if ( item->isSelected() ) {
00253 aItem = dynamic_cast<AddresseeCardViewItem*>( item );
00254 if ( aItem )
00255 uidList << aItem->addressee().uid();
00256 }
00257 }
00258
00259 return uidList;
00260 }
00261
00262 void KAddressBookCardView::refresh( const QString &uid )
00263 {
00264 CardViewItem *item;
00265 AddresseeCardViewItem *aItem;
00266
00267 if ( uid.isEmpty() ) {
00268
00269 mCardView->viewport()->setUpdatesEnabled( false );
00270 mCardView->clear();
00271
00272 const KABC::Addressee::List addresseeList( addressees() );
00273 KABC::Addressee::List::ConstIterator it( addresseeList.begin() );
00274 const KABC::Addressee::List::ConstIterator endIt( addresseeList.end() );
00275 for ( ; it != endIt; ++it ) {
00276 aItem = new AddresseeCardViewItem( fields(), mShowEmptyFields,
00277 core()->addressBook(), *it, mCardView );
00278 }
00279 mCardView->viewport()->setUpdatesEnabled( true );
00280 mCardView->viewport()->update();
00281
00282
00283 emit selected( QString() );
00284 } else {
00285
00286 bool found = false;
00287 for ( item = mCardView->firstItem(); item && !found; item = item->nextItem() ) {
00288 aItem = dynamic_cast<AddresseeCardViewItem*>( item );
00289 if ( aItem && (aItem->addressee().uid() == uid) ) {
00290 aItem->refresh();
00291 found = true;
00292 }
00293 }
00294 }
00295 }
00296
00297 void KAddressBookCardView::setSelected( const QString &uid, bool selected )
00298 {
00299 CardViewItem *item;
00300 AddresseeCardViewItem *aItem;
00301
00302 if ( uid.isEmpty() ) {
00303 mCardView->selectAll( selected );
00304 } else {
00305 bool found = false;
00306 for ( item = mCardView->firstItem(); item && !found; item = item->nextItem() ) {
00307 aItem = dynamic_cast<AddresseeCardViewItem*>( item );
00308
00309 if ( aItem && (aItem->addressee().uid() == uid) ) {
00310 mCardView->setSelected( aItem, selected );
00311 mCardView->ensureItemVisible( item );
00312 found = true;
00313 }
00314 }
00315 }
00316 }
00317
00318 void KAddressBookCardView::setFirstSelected( bool selected )
00319 {
00320 if ( mCardView->firstItem() ) {
00321 mCardView->setSelected( mCardView->firstItem(), selected );
00322 mCardView->ensureItemVisible( mCardView->firstItem() );
00323 }
00324 }
00325
00326 void KAddressBookCardView::addresseeExecuted( CardViewItem *item )
00327 {
00328 AddresseeCardViewItem *aItem = dynamic_cast<AddresseeCardViewItem*>( item );
00329 if ( aItem )
00330 emit executed( aItem->addressee().uid() );
00331 }
00332
00333 void KAddressBookCardView::addresseeSelected()
00334 {
00335 CardViewItem *item;
00336 AddresseeCardViewItem *aItem;
00337
00338 bool found = false;
00339 for ( item = mCardView->firstItem(); item && !found; item = item->nextItem() ) {
00340 if ( item->isSelected() ) {
00341 aItem = dynamic_cast<AddresseeCardViewItem*>( item );
00342 if ( aItem ) {
00343 emit selected( aItem->addressee().uid() );
00344 found = true;
00345 }
00346 }
00347 }
00348
00349 if ( !found )
00350 emit selected( QString() );
00351 }
00352
00353 void KAddressBookCardView::rmbClicked( CardViewItem*, const QPoint &point )
00354 {
00355 popup( point );
00356 }
00357
00358 void KAddressBookCardView::scrollUp()
00359 {
00360 QApplication::postEvent( mCardView, new QKeyEvent( QEvent::KeyPress, Qt::Key_Up, 0 ) );
00361 }
00362
00363 void KAddressBookCardView::scrollDown()
00364 {
00365 QApplication::postEvent( mCardView, new QKeyEvent( QEvent::KeyPress, Qt::Key_Down, 0 ) );
00366 }
00367
00368 #include "kaddressbookcardview.moc"