kaddressbook Library API Documentation

kaddressbookcardview.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 "kaddressbookcardview.h"
00025 
00026 #include <qevent.h>
00027 #include <qdragobject.h>
00028 #include <qlayout.h>
00029 #include <qiconview.h>
00030 #include <qstringlist.h>
00031 
00032 #include <kconfig.h>
00033 #include <kdebug.h>
00034 #include <kabc/addressbook.h>
00035 #include <kabc/addressee.h>
00036 
00037 #include "kabprefs.h"
00038 
00040 // AddresseeCardViewItem  (internal class)
00041 class AddresseeCardViewItem : public CardViewItem
00042 {
00043   public:
00044     AddresseeCardViewItem(const KABC::Field::List &fields,
00045                           bool showEmptyFields,
00046                           KABC::AddressBook *doc, const KABC::Addressee &a,
00047                           CardView *parent)
00048       : CardViewItem(parent, a.formattedName()),
00049         mFields( fields ), mShowEmptyFields(showEmptyFields),
00050         mDocument(doc), mAddressee(a)
00051       {
00052           if ( mFields.isEmpty() ) {
00053             mFields = KABC::Field::defaultFields();
00054           }
00055           refresh();
00056       }
00057 
00058     const KABC::Addressee &addressee() const { return mAddressee; }
00059 
00060     void refresh()
00061     {
00062         // Update our addressee, since it may have changed elsewhere
00063         mAddressee = mDocument->findByUid(mAddressee.uid());
00064 
00065         if (!mAddressee.isEmpty())
00066         {
00067           clearFields();
00068 
00069           // Try all the selected fields until we find one with text.
00070           // This will limit the number of unlabeled icons in the view
00071           KABC::Field::List::Iterator iter;
00072           for (iter = mFields.begin(); iter != mFields.end(); ++iter)
00073           {
00074             if (mShowEmptyFields || !(*iter)->value( mAddressee ).isEmpty())
00075               insertField((*iter)->label(), (*iter)->value( mAddressee ));
00076           }
00077 
00078           // We might want to make this the first field. hmm... -mpilone
00079           setCaption( mAddressee.realName() );
00080         }
00081     }
00082 
00083   private:
00084     KABC::Field::List mFields;
00085     bool mShowEmptyFields;
00086     KABC::AddressBook *mDocument;
00087     KABC::Addressee mAddressee;
00088 };
00089 
00091 // AddresseeCardView
00092 
00093 AddresseeCardView::AddresseeCardView(QWidget *parent, const char *name)
00094   : CardView(parent, name)
00095 {
00096   setAcceptDrops(true);
00097 }
00098 
00099 AddresseeCardView::~AddresseeCardView()
00100 {
00101 }
00102 
00103 void AddresseeCardView::dragEnterEvent(QDragEnterEvent *e)
00104 {
00105   if (QTextDrag::canDecode(e))
00106     e->accept();
00107 }
00108 
00109 void AddresseeCardView::dropEvent(QDropEvent *e)
00110 {
00111   emit addresseeDropped(e);
00112 }
00113 
00114 void AddresseeCardView::startDrag()
00115 {
00116   emit startAddresseeDrag();
00117 }
00118 
00119 
00121 // KAddressBookCardView
00122 
00123 KAddressBookCardView::KAddressBookCardView(KABC::AddressBook *doc,
00124                                            QWidget *parent,
00125                                            const char *name)
00126     : KAddressBookView(doc, parent, name)
00127 {
00128     mShowEmptyFields = true;
00129 
00130     // Init the GUI
00131     QVBoxLayout *layout = new QVBoxLayout(viewWidget());
00132 
00133     mCardView = new AddresseeCardView(viewWidget(), "mCardView");
00134     mCardView->setSelectionMode(CardView::Extended);
00135     layout->addWidget(mCardView);
00136 
00137     // Connect up the signals
00138     connect(mCardView, SIGNAL(executed(CardViewItem *)),
00139             this, SLOT(addresseeExecuted(CardViewItem *)));
00140     connect(mCardView, SIGNAL(selectionChanged()),
00141             this, SLOT(addresseeSelected()));
00142     connect(mCardView, SIGNAL(addresseeDropped(QDropEvent*)),
00143             this, SIGNAL(dropped(QDropEvent*)));
00144     connect(mCardView, SIGNAL(startAddresseeDrag()),
00145             this, SIGNAL(startDrag()));
00146 }
00147 
00148 KAddressBookCardView::~KAddressBookCardView()
00149 {
00150 }
00151 
00152 void KAddressBookCardView::readConfig(KConfig *config)
00153 {
00154   KAddressBookView::readConfig(config);
00155 
00156   mCardView->setDrawCardBorder(config->readBoolEntry("DrawBorder", true));
00157   mCardView->setDrawColSeparators(config->readBoolEntry("DrawSeparators",
00158                                                         true));
00159   mCardView->setDrawFieldLabels(config->readBoolEntry("DrawFieldLabels",true));
00160   mShowEmptyFields = config->readBoolEntry("ShowEmptyFields", true);
00161 
00162   disconnect(mCardView, SIGNAL(executed(CardViewItem *)),
00163             this, SLOT(addresseeExecuted(CardViewItem *)));
00164 
00165   if (KABPrefs::instance()->mHonorSingleClick)
00166     connect(mCardView, SIGNAL(executed(CardViewItem *)),
00167             this, SLOT(addresseeExecuted(CardViewItem *)));
00168   else
00169     connect(mCardView, SIGNAL(doubleClicked(CardViewItem *)),
00170             this, SLOT(addresseeExecuted(CardViewItem *)));
00171 }
00172 
00173 QStringList KAddressBookCardView::selectedUids()
00174 {
00175     QStringList uidList;
00176     CardViewItem *item;
00177     AddresseeCardViewItem *aItem;
00178 
00179     for (item = mCardView->firstItem(); item; item = item->nextItem())
00180     {
00181         if (item->isSelected())
00182         {
00183             aItem = dynamic_cast<AddresseeCardViewItem*>(item);
00184             if (aItem)
00185                 uidList << aItem->addressee().uid();
00186         }
00187     }
00188 
00189     return uidList;
00190 }
00191 
00192 void KAddressBookCardView::refresh(QString uid)
00193 {
00194     CardViewItem *item;
00195     AddresseeCardViewItem *aItem;
00196 
00197     if (uid == QString::null)
00198     {
00199         // Rebuild the view
00200         mCardView->viewport()->setUpdatesEnabled( false );
00201         mCardView->clear();
00202 
00203         KABC::Addressee::List addresseeList = addressees();
00204         KABC::Addressee::List::Iterator iter;
00205         for (iter = addresseeList.begin(); iter != addresseeList.end(); ++iter)
00206         {
00207             aItem = new AddresseeCardViewItem(fields(), mShowEmptyFields,
00208                                               addressBook(), *iter, mCardView);
00209         }
00210         mCardView->viewport()->setUpdatesEnabled( true );
00211         mCardView->viewport()->update();
00212 
00213         // by default nothing is selected
00214         emit selected(QString::null);
00215     }
00216     else
00217     {
00218         // Try to find the one to refresh
00219         bool found = false;
00220         for (item = mCardView->firstItem(); item && !found;
00221              item = item->nextItem())
00222         {
00223             aItem = dynamic_cast<AddresseeCardViewItem*>(item);
00224             if ((aItem) && (aItem->addressee().uid() == uid))
00225             {
00226                 aItem->refresh();
00227                 found = true;
00228             }
00229         }
00230     }
00231 }
00232 
00233 void KAddressBookCardView::setSelected(QString uid, bool selected)
00234 {
00235     CardViewItem *item;
00236     AddresseeCardViewItem *aItem;
00237 
00238     if (uid == QString::null)
00239     {
00240         mCardView->selectAll(selected);
00241     }
00242     else
00243     {
00244         bool found = false;
00245         for (item = mCardView->firstItem(); item && !found;
00246              item = item->nextItem())
00247          {
00248              aItem = dynamic_cast<AddresseeCardViewItem*>(item);
00249 
00250              if ((aItem) && (aItem->addressee().uid() == uid))
00251              {
00252                  mCardView->setSelected(aItem, selected);
00253                  found = true;
00254              }
00255          }
00256     }
00257 }
00258 
00259 void KAddressBookCardView::addresseeExecuted(CardViewItem *item)
00260 {
00261     AddresseeCardViewItem *aItem = dynamic_cast<AddresseeCardViewItem*>(item);
00262 
00263     if (aItem)
00264     {
00265       emit executed(aItem->addressee().uid());
00266     }
00267 }
00268 
00269 void KAddressBookCardView::addresseeSelected()
00270 {
00271     CardViewItem *item;
00272     AddresseeCardViewItem *aItem;
00273 
00274     bool found = false;
00275     for (item = mCardView->firstItem(); item && !found;
00276          item = item->nextItem())
00277     {
00278         if (item->isSelected())
00279         {
00280             aItem = dynamic_cast<AddresseeCardViewItem*>(item);
00281             if ( aItem )
00282             {
00283                 emit selected(aItem->addressee().uid());
00284                 found = true;
00285             }
00286         }
00287     }
00288 
00289     if (!found)
00290         emit selected(QString::null);
00291 
00292 }
00293 
00294 void KAddressBookCardView::incrementalSearch(const QString &value,
00295                                              KABC::Field *field)
00296 {
00297   CardViewItem *item = mCardView->findItem(value, field->label());
00298 
00299   if (item)
00300   {
00301     // avoid flickering in details page
00302     bool blocked = signalsBlocked();
00303     blockSignals( true );
00304     for ( CardViewItem *it = mCardView->firstItem(); it; it = it->nextItem() )
00305       if ( it != item )
00306         mCardView->setSelected( it, false );
00307     blockSignals( blocked );
00308 
00309     mCardView->setSelected(item, true);
00310     mCardView->ensureItemVisible(item);
00311   } else {
00312     for ( item = mCardView->firstItem(); item; item = item->nextItem() )
00313       mCardView->setSelected( item, false );
00314   }
00315 }
00316 
00317 
00318 #include "kaddressbookcardview.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