kaddressbook Library API Documentation

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