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., 675 Mass Ave, Cambridge, MA 02139, 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 "kaddressbookiconview.h"
00025 
00026 #include <qlayout.h>
00027 #include <qiconview.h>
00028 #include <qstringlist.h>
00029 
00030 #include <kconfig.h>
00031 #include <kiconloader.h>
00032 #include <kglobal.h>
00033 #include <kdebug.h>
00034 #include <kabc/addressbook.h>
00035 #include <kabc/addressee.h>
00036 
00037 #include "kabprefs.h"
00038 
00040 // AddresseeIconView (internal class)
00041 AddresseeIconView::AddresseeIconView(QWidget *parent, const char *name)
00042   : KIconView(parent, name)
00043 {
00044   setSelectionMode(QIconView::Extended);
00045   setResizeMode(QIconView::Adjust);
00046   setWordWrapIconText(true);
00047   setGridX( 100 );
00048   setItemsMovable(false);
00049   setSorting(true, true);
00050   setMode( KIconView::Select );
00051 
00052   connect(this, SIGNAL(dropped(QDropEvent*, const QValueList<QIconDragItem>&)),
00053           this, SLOT(itemDropped(QDropEvent*, const QValueList<QIconDragItem>&)));
00054 }
00055 
00056 AddresseeIconView::~AddresseeIconView()
00057 {
00058 }
00059 
00060 void AddresseeIconView::itemDropped(QDropEvent *e, 
00061                                     const QValueList<QIconDragItem> &)
00062 {
00063   emit addresseeDropped(e);
00064 }
00065 
00066 QDragObject *AddresseeIconView::dragObject()
00067 {
00068   emit startAddresseeDrag();
00069   
00070   // We never want IconView to start the drag
00071   return 0;
00072 }
00074 // AddresseeIconViewItem  (internal class)
00075 class AddresseeIconViewItem : public KIconViewItem
00076 {
00077   public:
00078     AddresseeIconViewItem(const KABC::Field::List &fields,
00079                           KABC::AddressBook *doc, const KABC::Addressee &a, 
00080                           QIconView *parent)
00081       : KIconViewItem(parent), mFields( fields ), mDocument(doc), mAddressee(a)
00082       {
00083           if ( mFields.isEmpty() ) {
00084             mFields = KABC::Field::defaultFields();
00085           }
00086           refresh();
00087       }
00088       
00089     const KABC::Addressee &addressee() const { return mAddressee; }
00090     
00091     void refresh()
00092     {
00093         // Update our addressee, since it may have changed elsewhere
00094         mAddressee = mDocument->findByUid(mAddressee.uid());
00095         
00096         if (!mAddressee.isEmpty())
00097           setText( mAddressee.givenName() + " " + mAddressee.familyName() );
00098     }
00099     
00100   private:
00101     KABC::Field::List mFields;
00102     KABC::AddressBook *mDocument;
00103     KABC::Addressee mAddressee;
00104 };
00105 
00107 // KAddressBookView
00108 
00109 KAddressBookIconView::KAddressBookIconView(KABC::AddressBook *doc, 
00110                                            QWidget *parent,
00111                                            const char *name)
00112     : KAddressBookView(doc, parent, name)
00113 {
00114     // Init the GUI
00115     QVBoxLayout *layout = new QVBoxLayout(viewWidget());
00116     
00117     mIconView = new AddresseeIconView(viewWidget(), "mIconView");
00118     layout->addWidget(mIconView);
00119     
00120     // Connect up the signals
00121     connect(mIconView, SIGNAL(executed(QIconViewItem *)),
00122             this, SLOT(addresseeExecuted(QIconViewItem *)));
00123     connect(mIconView, SIGNAL(selectionChanged()),
00124             this, SLOT(addresseeSelected()));
00125     connect(mIconView, SIGNAL(addresseeDropped(QDropEvent*)),
00126             this, SIGNAL(dropped(QDropEvent*)));
00127     connect(mIconView, SIGNAL(startAddresseeDrag()),
00128             this, SIGNAL(startDrag()));
00129 }
00130 
00131 KAddressBookIconView::~KAddressBookIconView()
00132 {
00133 }
00134 
00135 void KAddressBookIconView::readConfig(KConfig *config)
00136 {
00137   KAddressBookView::readConfig(config);
00138   
00139   disconnect(mIconView, SIGNAL(executed(QIconViewItem *)),
00140              this, SLOT(addresseeExecuted(QIconViewItem *)));
00141              
00142   if (KABPrefs::instance()->mHonorSingleClick)
00143     connect(mIconView, SIGNAL(executed(QIconViewItem *)),
00144             this, SLOT(addresseeExecuted(QIconViewItem *)));
00145   else
00146     connect(mIconView, SIGNAL(doubleClicked(QIconViewItem *)),
00147             this, SLOT(addresseeExecuted(QIconViewItem *)));
00148 }
00149 
00150 QStringList KAddressBookIconView::selectedUids()
00151 {
00152     QStringList uidList;
00153     QIconViewItem *item;
00154     AddresseeIconViewItem *aItem;
00155     
00156     for (item = mIconView->firstItem(); item; item = item->nextItem())
00157     {
00158         if (item->isSelected())
00159         {
00160             aItem = dynamic_cast<AddresseeIconViewItem*>(item);
00161             if (aItem)
00162                 uidList << aItem->addressee().uid();
00163         }
00164     }
00165     
00166     return uidList;
00167 }
00168     
00169 void KAddressBookIconView::refresh(QString uid)
00170 {
00171   QIconViewItem *item;
00172   AddresseeIconViewItem *aItem;
00173     
00174   if ( uid == QString::null ) {
00175     // Rebuild the view
00176     mIconView->clear();
00177     mIconList.clear();
00178         
00179     QPixmap icon( KGlobal::iconLoader()->loadIcon( "vcard", KIcon::Desktop) );
00180     KABC::Addressee::List addresseeList = addressees();
00181     KABC::Addressee::List::Iterator iter;
00182     for ( iter = addresseeList.begin(); iter != addresseeList.end(); ++iter ) {
00183       aItem = new AddresseeIconViewItem( fields(), addressBook(), *iter, mIconView );
00184       aItem->setPixmap(icon);
00185     }
00186     mIconView->arrangeItemsInGrid( true );
00187 
00188     for ( item = mIconView->firstItem(); item; item = item->nextItem() )
00189       mIconList.append( dynamic_cast<AddresseeIconViewItem*>( item ) );
00190 
00191   } else {
00192     // Try to find the one to refresh
00193     for ( item = mIconView->firstItem(); item; item = item->nextItem() ) {
00194       aItem = dynamic_cast<AddresseeIconViewItem*>(item);
00195       if ((aItem) && (aItem->addressee().uid() == uid)) {
00196         aItem->refresh();
00197         mIconView->arrangeItemsInGrid( true );
00198         return;
00199       }
00200     }
00201     refresh( QString::null );
00202   }
00203 }
00204 
00205 void KAddressBookIconView::setSelected(QString uid, bool selected)
00206 {
00207     QIconViewItem *item;
00208     AddresseeIconViewItem *aItem;
00209     
00210     if (uid == QString::null)
00211     {
00212         mIconView->selectAll(selected);
00213     }
00214     else
00215     {
00216         bool found = false;
00217         for (item = mIconView->firstItem(); item && !found; 
00218              item = item->nextItem())
00219          {
00220              aItem = dynamic_cast<AddresseeIconViewItem*>(item);
00221              
00222              if ((aItem) && (aItem->addressee().uid() == uid))
00223              {
00224                  mIconView->setSelected(aItem, selected);
00225                  found = true;
00226              }
00227          }
00228     }
00229 }
00230    
00231 void KAddressBookIconView::addresseeExecuted(QIconViewItem *item)
00232 {
00233     AddresseeIconViewItem *aItem = dynamic_cast<AddresseeIconViewItem*>(item);
00234     
00235     if (aItem)
00236         emit executed(aItem->addressee().uid());
00237 }
00238 
00239 void KAddressBookIconView::addresseeSelected()
00240 {
00241     QIconViewItem *item;
00242     AddresseeIconViewItem *aItem;
00243     
00244     bool found = false;
00245     for (item = mIconView->firstItem(); item && !found; 
00246          item = item->nextItem())
00247     {
00248         if (item->isSelected())
00249         {
00250             aItem = dynamic_cast<AddresseeIconViewItem*>(item);
00251             emit selected(aItem->addressee().uid()); 
00252             found = true;
00253         }
00254     }
00255     
00256     if (!found)
00257         emit selected(QString::null);
00258 }
00259 
00260 void KAddressBookIconView::incrementalSearch(const QString &value, 
00261                                              KABC::Field *field)
00262 {
00263   if ( value.isEmpty() ) {
00264     mIconView->selectAll( false );
00265     return;
00266   }
00267 
00268   AddresseeIconViewItem *item;
00269   for ( item = mIconList.first(); item; item = mIconList.next() ) {
00270     if ( field->value( item->addressee() ).startsWith( value ) ) {
00271       mIconView->setSelected( item, true, false );
00272       mIconView->ensureItemVisible( item );
00273       return;
00274     }
00275   }
00276 }
00277 
00278 #include "kaddressbookiconview.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:09 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001