kaddressbook

kaddressbookiconview.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., 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 <qapplication.h>
00025 #include <qiconview.h>
00026 #include <qlayout.h>
00027 #include <qstringlist.h>
00028 
00029 #include <kabc/addressbook.h>
00030 #include <kabc/addressee.h>
00031 #include <kconfig.h>
00032 #include <kdebug.h>
00033 #include <kglobal.h>
00034 #include <kiconloader.h>
00035 #include <klocale.h>
00036 
00037 #include "core.h"
00038 #include "kabprefs.h"
00039 
00040 #include "kaddressbookiconview.h"
00041 
00042 class IconViewFactory : public ViewFactory
00043 {
00044   public:
00045     KAddressBookView *view( KAB::Core *core, QWidget *parent, const char *name )
00046     {
00047       return new KAddressBookIconView( core, parent, name );
00048     }
00049 
00050     QString type() const { return I18N_NOOP( "Icon" ); }
00051 
00052     QString description() const { return i18n( "Icons represent contacts. Very simple view." ); }
00053 };
00054 
00055 extern "C" {
00056   void *init_libkaddrbk_iconview()
00057   {
00058     return ( new IconViewFactory );
00059   }
00060 }
00061 
00062 AddresseeIconView::AddresseeIconView( QWidget *parent, const char *name )
00063   : KIconView( parent, name )
00064 {
00065   setSelectionMode( QIconView::Extended );
00066   setResizeMode( QIconView::Adjust );
00067   setWordWrapIconText( true );
00068   setGridX( 100 );
00069   setItemsMovable( false );
00070   setSorting( true, true );
00071   setMode( KIconView::Select );
00072 
00073   connect( this, SIGNAL( dropped( QDropEvent*, const QValueList<QIconDragItem>& ) ),
00074            this, SLOT( itemDropped( QDropEvent*, const QValueList<QIconDragItem>& ) ) );
00075 }
00076 
00077 AddresseeIconView::~AddresseeIconView()
00078 {
00079 }
00080 
00081 void AddresseeIconView::itemDropped( QDropEvent *event, const QValueList<QIconDragItem>& )
00082 {
00083   emit addresseeDropped( event );
00084 }
00085 
00086 QDragObject *AddresseeIconView::dragObject()
00087 {
00088   emit startAddresseeDrag();
00089 
00090   // We never want IconView to start the drag
00091   return 0;
00092 }
00093 
00094 
00095 class AddresseeIconViewItem : public KIconViewItem
00096 {
00097   public:
00098     AddresseeIconViewItem( const KABC::Field::List&, KABC::AddressBook *doc,
00099                            const KABC::Addressee &addr, QIconView *parent )
00100       : KIconViewItem( parent ), mDocument( doc ), mAddressee( addr )
00101       {
00102         refresh();
00103       }
00104 
00105     const KABC::Addressee &addressee() const { return mAddressee; }
00106 
00107     void refresh()
00108     {
00109       mAddressee = mDocument->findByUid( mAddressee.uid() );
00110 
00111       if ( !mAddressee.isEmpty() )
00112         setText( mAddressee.givenName() + " " + mAddressee.familyName() );
00113 
00114       QPixmap icon;
00115       QPixmap defaultIcon( KGlobal::iconLoader()->loadIcon( "vcard", KIcon::Desktop ) );
00116       KABC::Picture pic = mAddressee.photo();
00117       if ( pic.data().isNull() )
00118         pic = mAddressee.logo();
00119 
00120       if ( pic.isIntern() && !pic.data().isNull() ) {
00121         QImage img = pic.data();
00122         if ( img.width() > img.height() )
00123           icon = img.scaleWidth( 32 );
00124         else
00125           icon = img.scaleHeight( 32 );
00126       } else
00127         icon = defaultIcon;
00128 
00129       setPixmap( icon );
00130     }
00131 
00132   private:
00133     KABC::AddressBook *mDocument;
00134     KABC::Addressee mAddressee;
00135 };
00136 
00137 
00138 KAddressBookIconView::KAddressBookIconView( KAB::Core *core,
00139                                             QWidget *parent, const char *name)
00140   : KAddressBookView( core, parent, name )
00141 {
00142   QVBoxLayout *layout = new QVBoxLayout( viewWidget() );
00143 
00144   mIconView = new AddresseeIconView( viewWidget(), "mIconView" );
00145   layout->addWidget( mIconView );
00146 
00147   // Connect up the signals
00148   connect( mIconView, SIGNAL( executed( QIconViewItem* ) ),
00149            this, SLOT( addresseeExecuted( QIconViewItem* ) ) );
00150   connect( mIconView, SIGNAL( selectionChanged() ),
00151            this, SLOT( addresseeSelected() ) );
00152   connect( mIconView, SIGNAL( addresseeDropped( QDropEvent* ) ),
00153            this, SIGNAL( dropped( QDropEvent* ) ) );
00154   connect( mIconView, SIGNAL( startAddresseeDrag() ),
00155            this, SIGNAL( startDrag() ) );
00156   connect( mIconView, SIGNAL( contextMenuRequested( QIconViewItem*, const QPoint& ) ),
00157            this, SLOT( rmbClicked( QIconViewItem*, const QPoint& ) ) );
00158 }
00159 
00160 KAddressBookIconView::~KAddressBookIconView()
00161 {
00162 }
00163 
00164 KABC::Field *KAddressBookIconView::sortField() const
00165 {
00166   // we have hardcoded sorting, so we have to return a hardcoded field :(
00167   return KABC::Field::allFields()[ 2 ];
00168 }
00169 
00170 void KAddressBookIconView::readConfig( KConfig *config )
00171 {
00172   KAddressBookView::readConfig( config );
00173 
00174   disconnect( mIconView, SIGNAL( executed( QIconViewItem* ) ),
00175               this, SLOT( addresseeExecuted( QIconViewItem* ) ) );
00176 
00177   if ( KABPrefs::instance()->honorSingleClick() )
00178     connect( mIconView, SIGNAL( executed( QIconViewItem* ) ),
00179              this, SLOT( addresseeExecuted( QIconViewItem* ) ) );
00180   else
00181     connect( mIconView, SIGNAL( doubleClicked( QIconViewItem* ) ),
00182              this, SLOT( addresseeExecuted( QIconViewItem* ) ) );
00183 }
00184 
00185 QStringList KAddressBookIconView::selectedUids()
00186 {
00187   QStringList uidList;
00188   QIconViewItem *item;
00189   AddresseeIconViewItem *aItem;
00190 
00191   for ( item = mIconView->firstItem(); item; item = item->nextItem() ) {
00192     if ( item->isSelected() ) {
00193       aItem = dynamic_cast<AddresseeIconViewItem*>( item );
00194       if ( aItem )
00195         uidList << aItem->addressee().uid();
00196     }
00197   }
00198 
00199   return uidList;
00200 }
00201 
00202 void KAddressBookIconView::refresh( const QString &uid )
00203 {
00204   QIconViewItem *item;
00205   AddresseeIconViewItem *aItem;
00206 
00207   if ( uid.isEmpty() ) {
00208     // Rebuild the view
00209     mIconView->clear();
00210     mIconList.clear();
00211 
00212     const KABC::Addressee::List addresseeList( addressees() );
00213     KABC::Addressee::List::ConstIterator it( addresseeList.begin() );
00214     const KABC::Addressee::List::ConstIterator endIt( addresseeList.end() );
00215     for ( ; it != endIt; ++it )
00216       aItem = new AddresseeIconViewItem( fields(), core()->addressBook(), *it, mIconView );
00217 
00218     mIconView->arrangeItemsInGrid( true );
00219 
00220     for ( item = mIconView->firstItem(); item; item = item->nextItem() ) {
00221       AddresseeIconViewItem* aivi = dynamic_cast<AddresseeIconViewItem*>( item );
00222       mIconList.append( aivi );
00223     }
00224 
00225   } else {
00226     // Try to find the one to refresh
00227     for ( item = mIconView->firstItem(); item; item = item->nextItem() ) {
00228       aItem = dynamic_cast<AddresseeIconViewItem*>( item );
00229       if ( aItem && (aItem->addressee().uid() == uid) ) {
00230         aItem->refresh();
00231         mIconView->arrangeItemsInGrid( true );
00232         return;
00233       }
00234     }
00235 
00236     refresh( QString::null );
00237   }
00238 }
00239 
00240 void KAddressBookIconView::setSelected( const QString &uid, bool selected )
00241 {
00242   QIconViewItem *item;
00243   AddresseeIconViewItem *aItem;
00244 
00245   if ( uid.isEmpty() ) {
00246     mIconView->selectAll( selected );
00247   } else {
00248     bool found = false;
00249     for ( item = mIconView->firstItem(); item && !found; item = item->nextItem() ) {
00250 
00251       aItem = dynamic_cast<AddresseeIconViewItem*>( item );
00252       if ( aItem && (aItem->addressee().uid() == uid) ) {
00253         mIconView->setSelected( aItem, selected );
00254         mIconView->ensureItemVisible( aItem );
00255         found = true;
00256       }
00257     }
00258   }
00259 }
00260 
00261 void KAddressBookIconView::setFirstSelected( bool selected )
00262 {
00263   if ( mIconView->firstItem() ) {
00264     mIconView->setSelected( mIconView->firstItem(), selected );
00265     mIconView->ensureItemVisible( mIconView->firstItem() );
00266   }
00267 }
00268 
00269 void KAddressBookIconView::addresseeExecuted( QIconViewItem *item )
00270 {
00271   AddresseeIconViewItem *aItem = dynamic_cast<AddresseeIconViewItem*>( item );
00272 
00273   if ( aItem )
00274     emit executed( aItem->addressee().uid() );
00275 }
00276 
00277 void KAddressBookIconView::addresseeSelected()
00278 {
00279   QIconViewItem *item;
00280   AddresseeIconViewItem *aItem;
00281 
00282   bool found = false;
00283   for ( item = mIconView->firstItem(); item && !found; item = item->nextItem() ) {
00284     if ( item->isSelected() ) {
00285       aItem = dynamic_cast<AddresseeIconViewItem*>( item );
00286       if ( aItem ) {
00287         emit selected( aItem->addressee().uid() );
00288         found = true;
00289       }
00290     }
00291   }
00292 
00293   if ( !found )
00294     emit selected( QString::null );
00295 }
00296 
00297 void KAddressBookIconView::rmbClicked( QIconViewItem*, const QPoint &point )
00298 {
00299   popup( point );
00300 }
00301 
00302 void KAddressBookIconView::scrollUp()
00303 {
00304   QApplication::postEvent( mIconView, new QKeyEvent( QEvent::KeyPress, Qt::Key_Up, 0, 0 ) );
00305 }
00306 
00307 void KAddressBookIconView::scrollDown()
00308 {
00309   QApplication::postEvent( mIconView, new QKeyEvent( QEvent::KeyPress, Qt::Key_Down, 0, 0 ) );
00310 }
00311 
00312 #include "kaddressbookiconview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys