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., 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 <qdragobject.h>
00025 #include <qevent.h>
00026 #include <qiconview.h>
00027 #include <qlayout.h>
00028 #include <qstringlist.h>
00029 
00030 #include <kabc/addressbook.h>
00031 #include <kabc/addressee.h>
00032 #include <kconfig.h>
00033 #include <kdebug.h>
00034 #include <klocale.h>
00035 
00036 #include "core.h"
00037 #include "configurecardviewdialog.h"
00038 #include "kabprefs.h"
00039 
00040 #include "kaddressbookcardview.h"
00041 
00042 class CardViewFactory : public ViewFactory
00043 {
00044   public:
00045     KAddressBookView *view( KAB::Core *core, QWidget *parent, const char *name )
00046     {
00047       return new KAddressBookCardView( core, parent, name );
00048     }
00049 
00050     QString type() const { return I18N_NOOP("Card"); }
00051     
00052     QString description() const { return i18n( "Rolodex style cards represent contacts." ); }
00053     
00054     ViewConfigureWidget *configureWidget( KABC::AddressBook *ab, QWidget *parent,
00055                                           const char *name = 0 )
00056     {
00057       return new ConfigureCardViewWidget( ab, parent, name );
00058     }
00059 };
00060 
00061 extern "C" {
00062   void *init_libkaddrbk_cardview()
00063   {
00064     return ( new CardViewFactory );
00065   }
00066 }
00067 
00069 // AddresseeCardViewItem  (internal class)
00070 class AddresseeCardViewItem : public CardViewItem
00071 {
00072   public:
00073     AddresseeCardViewItem(const KABC::Field::List &fields,
00074                           bool showEmptyFields,
00075                           KABC::AddressBook *doc, const KABC::Addressee &a,
00076                           CardView *parent)
00077       : CardViewItem(parent, a.formattedName()),
00078         mFields( fields ), mShowEmptyFields(showEmptyFields),
00079         mDocument(doc), mAddressee(a)
00080       {
00081           if ( mFields.isEmpty() ) {
00082             mFields = KABC::Field::defaultFields();
00083           }
00084           refresh();
00085       }
00086 
00087     const KABC::Addressee &addressee() const { return mAddressee; }
00088 
00089     void refresh()
00090     {
00091         // Update our addressee, since it may have changed elsewhere
00092         mAddressee = mDocument->findByUid(mAddressee.uid());
00093 
00094         if (!mAddressee.isEmpty())
00095         {
00096           clearFields();
00097 
00098           // Try all the selected fields until we find one with text.
00099           // This will limit the number of unlabeled icons in the view
00100           KABC::Field::List::Iterator iter;
00101           for (iter = mFields.begin(); iter != mFields.end(); ++iter)
00102           {
00103             // insert empty fields or not? not doing so saves a bit of memory and CPU
00104             // (during geometry calculations), but prevents having equally
00105             // wide label columns in all cards, unless CardViewItem/CardView search
00106             // globally for the widest label. (anders)
00107             //if (mShowEmptyFields || !(*iter)->value( mAddressee ).isEmpty())
00108               insertField((*iter)->label(), (*iter)->value( mAddressee ));
00109           }
00110 
00111           // We might want to make this the first field. hmm... -mpilone
00112           setCaption( mAddressee.realName() );
00113         }
00114     }
00115 
00116   private:
00117     KABC::Field::List mFields;
00118     bool mShowEmptyFields;
00119     KABC::AddressBook *mDocument;
00120     KABC::Addressee mAddressee;
00121 };
00122 
00124 // AddresseeCardView
00125 
00126 AddresseeCardView::AddresseeCardView(QWidget *parent, const char *name)
00127   : CardView(parent, name)
00128 {
00129   setAcceptDrops(true);
00130 }
00131 
00132 AddresseeCardView::~AddresseeCardView()
00133 {
00134 }
00135 
00136 void AddresseeCardView::dragEnterEvent(QDragEnterEvent *e)
00137 {
00138   if (QTextDrag::canDecode(e))
00139     e->accept();
00140 }
00141 
00142 void AddresseeCardView::dropEvent(QDropEvent *e)
00143 {
00144   emit addresseeDropped(e);
00145 }
00146 
00147 void AddresseeCardView::startDrag()
00148 {
00149   emit startAddresseeDrag();
00150 }
00151 
00152 
00154 // KAddressBookCardView
00155 
00156 KAddressBookCardView::KAddressBookCardView( KAB::Core *core,
00157                                             QWidget *parent, const char *name )
00158     : KAddressBookView( core, parent, name )
00159 {
00160     mShowEmptyFields = false;
00161 
00162     // Init the GUI
00163     QVBoxLayout *layout = new QVBoxLayout(viewWidget());
00164 
00165     mCardView = new AddresseeCardView(viewWidget(), "mCardView");
00166     mCardView->setSelectionMode(CardView::Extended);
00167     layout->addWidget(mCardView);
00168 
00169     // Connect up the signals
00170     connect(mCardView, SIGNAL(executed(CardViewItem *)),
00171             this, SLOT(addresseeExecuted(CardViewItem *)));
00172     connect(mCardView, SIGNAL(selectionChanged()),
00173             this, SLOT(addresseeSelected()));
00174     connect(mCardView, SIGNAL(addresseeDropped(QDropEvent*)),
00175             this, SIGNAL(dropped(QDropEvent*)));
00176     connect(mCardView, SIGNAL(startAddresseeDrag()),
00177             this, SIGNAL(startDrag()));
00178     connect( mCardView, SIGNAL( contextMenuRequested( CardViewItem*, const QPoint& ) ),
00179              this, SLOT( rmbClicked( CardViewItem*, const QPoint& ) ) );
00180 }
00181 
00182 KAddressBookCardView::~KAddressBookCardView()
00183 {
00184 }
00185 
00186 KABC::Field *KAddressBookCardView::sortField() const
00187 {
00188   // we have hardcoded sorting, so we have to return a hardcoded field :(
00189   return KABC::Field::allFields()[ 0 ];
00190 }
00191 
00192 void KAddressBookCardView::readConfig(KConfig *config)
00193 {
00194   KAddressBookView::readConfig(config);
00195   
00196   // costum colors?
00197   if ( config->readBoolEntry( "EnableCustomColors", false ) )
00198   {
00199     QPalette p( mCardView->palette() );
00200     QColor c = p.color(QPalette::Normal, QColorGroup::Base );
00201     p.setColor( QPalette::Normal, QColorGroup::Base, config->readColorEntry( "BackgroundColor", &c ) );
00202     c = p.color(QPalette::Normal, QColorGroup::Text );
00203     p.setColor( QPalette::Normal, QColorGroup::Text, config->readColorEntry( "TextColor", &c ) );
00204     c = p.color(QPalette::Normal, QColorGroup::Button );
00205     p.setColor( QPalette::Normal, QColorGroup::Button, config->readColorEntry( "HeaderColor", &c ) );
00206     c = p.color(QPalette::Normal, QColorGroup::ButtonText );
00207     p.setColor( QPalette::Normal, QColorGroup::ButtonText, config->readColorEntry( "HeaderTextColor", &c ) );
00208     c = p.color(QPalette::Normal, QColorGroup::Highlight );
00209     p.setColor( QPalette::Normal, QColorGroup::Highlight, config->readColorEntry( "HighlightColor", &c ) );
00210     c = p.color(QPalette::Normal, QColorGroup::HighlightedText );
00211     p.setColor( QPalette::Normal, QColorGroup::HighlightedText, config->readColorEntry( "HighlightedTextColor", &c ) );
00212     mCardView->viewport()->setPalette( p );
00213   }
00214   else
00215   {
00216     // needed if turned off during a session.
00217     mCardView->viewport()->setPalette( mCardView->palette() );
00218   }
00219   
00220   //custom fonts?
00221   QFont f( font() );
00222   if ( config->readBoolEntry( "EnableCustomFonts", false ) )
00223   {
00224     mCardView->setFont( config->readFontEntry( "TextFont", &f) );
00225     f.setBold( true );
00226     mCardView->setHeaderFont( config->readFontEntry( "HeaderFont", &f ) );
00227   }
00228   else
00229   {
00230     mCardView->setFont( f );
00231     f.setBold( true );
00232     mCardView->setHeaderFont( f );
00233   }  
00234   
00235   mCardView->setDrawCardBorder(config->readBoolEntry("DrawBorder", true));
00236   mCardView->setDrawColSeparators(config->readBoolEntry("DrawSeparators",
00237                                                         true));
00238   mCardView->setDrawFieldLabels(config->readBoolEntry("DrawFieldLabels",false));
00239   mShowEmptyFields = config->readBoolEntry("ShowEmptyFields", false);
00240   
00241   mCardView->setShowEmptyFields( mShowEmptyFields );
00242   
00243   mCardView->setItemWidth( config->readNumEntry( "ItemWidth", 200 ) );
00244   mCardView->setItemMargin( config->readNumEntry( "ItemMargin", 0 ) );
00245   mCardView->setItemSpacing( config->readNumEntry( "ItemSpacing", 10 ) );
00246   mCardView->setSeparatorWidth( config->readNumEntry( "SeparatorWidth", 2 ) );
00247   
00248   disconnect(mCardView, SIGNAL(executed(CardViewItem *)),
00249             this, SLOT(addresseeExecuted(CardViewItem *)));
00250 
00251   if (KABPrefs::instance()->mHonorSingleClick)
00252     connect(mCardView, SIGNAL(executed(CardViewItem *)),
00253             this, SLOT(addresseeExecuted(CardViewItem *)));
00254   else
00255     connect(mCardView, SIGNAL(doubleClicked(CardViewItem *)),
00256             this, SLOT(addresseeExecuted(CardViewItem *)));
00257   
00258 }
00259 
00260 void KAddressBookCardView::writeConfig( KConfig *config )
00261 {
00262   config->writeEntry( "ItemWidth", mCardView->itemWidth() );
00263   KAddressBookView::writeConfig( config );
00264 }
00265   
00266 QStringList KAddressBookCardView::selectedUids()
00267 {
00268     QStringList uidList;
00269     CardViewItem *item;
00270     AddresseeCardViewItem *aItem;
00271 
00272     for (item = mCardView->firstItem(); item; item = item->nextItem())
00273     {
00274         if (item->isSelected())
00275         {
00276             aItem = dynamic_cast<AddresseeCardViewItem*>(item);
00277             if (aItem)
00278                 uidList << aItem->addressee().uid();
00279         }
00280     }
00281 
00282     return uidList;
00283 }
00284 
00285 void KAddressBookCardView::refresh(QString uid)
00286 {
00287     CardViewItem *item;
00288     AddresseeCardViewItem *aItem;
00289 
00290     if (uid.isNull())
00291     {
00292         // Rebuild the view
00293         mCardView->viewport()->setUpdatesEnabled( false );
00294         mCardView->clear();
00295 
00296         KABC::Addressee::List addresseeList = addressees();
00297         KABC::Addressee::List::Iterator iter;
00298         for (iter = addresseeList.begin(); iter != addresseeList.end(); ++iter)
00299         {
00300             aItem = new AddresseeCardViewItem(fields(), mShowEmptyFields,
00301                                               core()->addressBook(), *iter, mCardView);
00302         }
00303         mCardView->viewport()->setUpdatesEnabled( true );
00304         mCardView->viewport()->update();
00305 
00306         // by default nothing is selected
00307         emit selected(QString::null);
00308     }
00309     else
00310     {
00311         // Try to find the one to refresh
00312         bool found = false;
00313         for (item = mCardView->firstItem(); item && !found;
00314              item = item->nextItem())
00315         {
00316             aItem = dynamic_cast<AddresseeCardViewItem*>(item);
00317             if ((aItem) && (aItem->addressee().uid() == uid))
00318             {
00319                 aItem->refresh();
00320                 found = true;
00321             }
00322         }
00323     }
00324 }
00325 
00326 void KAddressBookCardView::setSelected(QString uid, bool selected)
00327 {
00328     CardViewItem *item;
00329     AddresseeCardViewItem *aItem;
00330 
00331     if (uid.isNull())
00332     {
00333         mCardView->selectAll(selected);
00334     }
00335     else
00336     {
00337         bool found = false;
00338         for (item = mCardView->firstItem(); item && !found;
00339              item = item->nextItem())
00340          {
00341              aItem = dynamic_cast<AddresseeCardViewItem*>(item);
00342 
00343              if ((aItem) && (aItem->addressee().uid() == uid))
00344              {
00345                  mCardView->setSelected(aItem, selected);
00346                  mCardView->ensureItemVisible(item);
00347                  found = true;
00348              }
00349          }
00350     }
00351 }
00352 
00353 void KAddressBookCardView::addresseeExecuted(CardViewItem *item)
00354 {
00355     AddresseeCardViewItem *aItem = dynamic_cast<AddresseeCardViewItem*>(item);
00356     if (aItem)
00357     {
00358       //kdDebug()<<"... even has a valid item:)"<<endl;
00359       emit executed(aItem->addressee().uid());
00360     }
00361 }
00362 
00363 void KAddressBookCardView::addresseeSelected()
00364 {
00365     CardViewItem *item;
00366     AddresseeCardViewItem *aItem;
00367 
00368     bool found = false;
00369     for (item = mCardView->firstItem(); item && !found;
00370          item = item->nextItem())
00371     {
00372         if (item->isSelected())
00373         {
00374             aItem = dynamic_cast<AddresseeCardViewItem*>(item);
00375             if ( aItem )
00376             {
00377                 emit selected(aItem->addressee().uid());
00378                 found = true;
00379             }
00380         }
00381     }
00382 
00383     if (!found)
00384         emit selected(QString::null);
00385 
00386 }
00387 
00388 void KAddressBookCardView::rmbClicked( CardViewItem*, const QPoint &point )
00389 {
00390   popup( point );
00391 }
00392 
00393 #include "kaddressbookcardview.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