kaddressbook Library API Documentation

kaddressbooktableview.cpp

00001 // $Id: kaddressbooktableview.cpp,v 1.1 2002/09/16 18:49:18 tokoe Exp $
00002 
00003 #include <qlayout.h>
00004 #include <qvbox.h>
00005 #include <qlistbox.h>
00006 #include <qwidget.h>
00007 #include <qfile.h>
00008 #include <qimage.h>
00009 #include <qcombobox.h>
00010 #include <qapplication.h>
00011 #include <qdragobject.h>
00012 #include <qevent.h>
00013 #include <qurl.h>
00014 
00015 #include <kurl.h>
00016 #include <kdebug.h>
00017 #include <klocale.h>
00018 #include <kconfig.h>
00019 #include <kapplication.h>
00020 #include <kcolorbutton.h>
00021 #include <kglobal.h>
00022 #include <kmessagebox.h>
00023 #include <kiconloader.h>
00024 #include <kurlrequester.h>
00025 #include <klineedit.h>
00026 #include <kabc/addressbook.h>
00027 
00028 #include "kaddressbooktableview.h"
00029 #include "undocmds.h"
00030 #include "contactlistview.h"
00031 #include "kabprefs.h"
00032 
00033 KAddressBookTableView::KAddressBookTableView( KABC::AddressBook *doc,
00034                       QWidget *parent,
00035                       const char *name )
00036   : KAddressBookView(doc, parent, name)
00037 {
00038   mainLayout = new QVBoxLayout( viewWidget(), 2 );
00039   
00040   // The list view will be created when the config is read.
00041   mListView = 0;
00042 }
00043 
00044 KAddressBookTableView::~KAddressBookTableView()
00045 {
00046 }
00047 
00048 void KAddressBookTableView::reconstructListView()
00049 {
00050     if (mListView)
00051     {
00052         disconnect(mListView, SIGNAL(selectionChanged()),
00053                    this, SLOT(addresseeSelected()));
00054         disconnect(mListView, SIGNAL(executed(QListViewItem*)),
00055                    this, SLOT(addresseeExecuted(QListViewItem*)));
00056         disconnect(mListView, SIGNAL(doubleClicked(QListViewItem*)),
00057                    this, SLOT(addresseeExecuted(QListViewItem*)));
00058         disconnect(mListView, SIGNAL(startAddresseeDrag()), this,
00059                    SIGNAL(startDrag()));
00060         disconnect(mListView, SIGNAL(addresseeDropped(QDropEvent*)), this,
00061                    SIGNAL(dropped(QDropEvent*)));
00062         delete mListView;
00063     }
00064 
00065   mListView = new ContactListView( this, addressBook(), viewWidget() );
00066   
00067   // Add the columns
00068   KABC::Field::List fieldList = fields();
00069   KABC::Field::List::ConstIterator it;
00070 
00071   int c = 0;
00072   for( it = fieldList.begin(); it != fieldList.end(); ++it ) {
00073       mListView->addColumn( (*it)->label() );
00074       mListView->setColumnWidthMode(c++, QListView::Manual);
00075   }
00076  
00077   connect(mListView, SIGNAL(selectionChanged()),
00078           this, SLOT(addresseeSelected()));
00079   connect(mListView, SIGNAL(startAddresseeDrag()), this,
00080           SIGNAL(startDrag()));
00081   connect(mListView, SIGNAL(addresseeDropped(QDropEvent*)), this,
00082           SIGNAL(dropped(QDropEvent*)));
00083   if (KABPrefs::instance()->mHonorSingleClick)
00084     connect(mListView, SIGNAL(executed(QListViewItem*)),
00085           this, SLOT(addresseeExecuted(QListViewItem*)));
00086   else
00087     connect(mListView, SIGNAL(doubleClicked(QListViewItem*)),
00088           this, SLOT(addresseeExecuted(QListViewItem*)));
00089           
00090   refresh();
00091   
00092   mListView->setSorting( 0, true );
00093   mainLayout->addWidget( mListView );
00094   mainLayout->activate();
00095   mListView->show();
00096 }
00097 
00098 void KAddressBookTableView::writeConfig(KConfig *config)
00099 {
00100   KAddressBookView::writeConfig(config);
00101 
00102   mListView->saveLayout(config, config->group());
00103 }
00104 
00105 void KAddressBookTableView::readConfig(KConfig *config)
00106 {
00107   QString group = config->group();
00108 
00109   KAddressBookView::readConfig(config);
00110   
00111   // The config could have changed the fields, so we need to reconstruct
00112   // the listview.
00113   reconstructListView();
00114 
00115   // reconstructListView modifies config-group
00116   config->setGroup( group );
00117   
00118   // Set the list view options
00119   mListView->setAlternateBackgroundEnabled(config->readBoolEntry("ABackground",
00120                                                                  true));
00121   mListView->setSingleLineEnabled(config->readBoolEntry("SingleLine", false));
00122   mListView->setToolTipsEnabled(config->readBoolEntry("ToolTips", true));
00123   
00124   if (config->readBoolEntry("Background", false))
00125     mListView->setBackgroundPixmap(config->readEntry("BackgroundName"));
00126   
00127   // Restore the layout of the listview
00128   mListView->restoreLayout(config, config->group());
00129 }
00130 
00131 void KAddressBookTableView::refresh(QString uid)
00132 {
00133   // For now just repopulate. In reality this method should 
00134   // check the value of uid, and if valid iterate through
00135   // the listview to find the entry, then tell it to refresh.
00136 
00137   if (uid == QString::null) {
00138     // Clear the list view
00139     mListView->clear();
00140     
00141     KABC::Addressee::List addresseeList = addressees();
00142     KABC::Addressee::List::Iterator it;
00143     for (it = addresseeList.begin(); it != addresseeList.end(); ++it ) {
00144       new ContactListViewItem(*it, mListView, addressBook(), fields());
00145     }
00146         
00147     // Sometimes the background pixmap gets messed up when we add lots
00148     // of items.
00149     mListView->repaint();
00150   } else {
00151     // Only need to update on entry. Iterate through and try to find it
00152     ContactListViewItem *ceItem;
00153     QListViewItemIterator it( mListView );
00154     while ( it.current() ) {
00155       ceItem = dynamic_cast<ContactListViewItem*>( it.current() );
00156       if ( ceItem && ceItem->addressee().uid() == uid ) {
00157         ceItem->refresh();
00158         return;
00159       }
00160       ++it;
00161     }
00162 
00163     refresh( QString::null );
00164   }
00165 }
00166 
00167 QStringList KAddressBookTableView::selectedUids()
00168 {
00169     QStringList uidList;
00170     QListViewItem *item;
00171     ContactListViewItem *ceItem;
00172     
00173     for(item = mListView->firstChild(); item; item = item->itemBelow()) 
00174     {
00175         if (mListView->isSelected( item ))
00176         {
00177             ceItem = dynamic_cast<ContactListViewItem*>(item);
00178             if (ceItem != 0L)
00179                 uidList << ceItem->addressee().uid();
00180         }
00181     }
00182     
00183     return uidList;
00184 }
00185 
00186 void KAddressBookTableView::setSelected(QString uid, bool selected)
00187 {
00188     QListViewItem *item;
00189     ContactListViewItem *ceItem;
00190     
00191     if (uid == QString::null)
00192     {
00193         mListView->selectAll(selected);
00194     }
00195     else
00196     {
00197         for(item = mListView->firstChild(); item; item = item->itemBelow())
00198         {
00199             ceItem = dynamic_cast<ContactListViewItem*>(item);
00200             if ((ceItem != 0L) && (ceItem->addressee().uid() == uid))
00201             {
00202                 mListView->setSelected(item, selected);
00203                 
00204                 if (selected)
00205                     mListView->ensureItemVisible(item);
00206             }
00207         }
00208     }
00209 }
00210 
00211 void KAddressBookTableView::incrementalSearch(const QString &value, 
00212                                               KABC::Field *field)
00213 {
00214     if ( value.isEmpty() ) {
00215       mListView->clearSelection();
00216       return;
00217     }
00218 
00219     KABC::Field::List fieldList = fields();
00220     KABC::Field::List::ConstIterator it;
00221     int column = 0;
00222     for( it = fieldList.begin(); it != fieldList.end(); ++it ) {
00223       if ( (*it)->equals( field ) ) break;
00224       ++column;
00225     }
00226     
00227     if ( it == fieldList.end() ) column = 0;
00228 
00229     // Now do the inc search
00230     mListView->setCurrentItem( mListView->firstChild() );
00231     QListViewItem *item = mListView->findItem(value, column, Qt::BeginsWith);
00232     
00233     if (item)
00234     {
00235         // We have a match. Deselect all the others and select this one
00236 
00237         // avoid flickering in details page
00238         bool blocked = signalsBlocked();
00239         blockSignals( true );
00240         mListView->clearSelection();
00241         blockSignals( blocked );
00242 
00243         mListView->setSelected(item, true);
00244         mListView->ensureItemVisible(item);
00245     }
00246 }
00247 
00248 void KAddressBookTableView::addresseeSelected()
00249 {
00250     // We need to try to find the first selected item. This might not be the
00251     // last selected item, but when QListView is in multiselection mode,
00252     // there is no way to figure out which one was
00253     // selected last.
00254     QListViewItem *item;
00255     bool found =false;
00256     for (item = mListView->firstChild(); item && !found; 
00257          item = item->nextSibling())
00258     {
00259         if (item->isSelected())
00260         {
00261             found = true;
00262             ContactListViewItem *ceItem 
00263                  = dynamic_cast<ContactListViewItem*>(item);
00264              emit selected(ceItem->addressee().uid());
00265         }
00266     }
00267     
00268     if (!found)
00269         emit selected(QString::null);
00270 }
00271 
00272 void KAddressBookTableView::addresseeExecuted(QListViewItem *item)
00273 {
00274     if (item)
00275     {
00276         ContactListViewItem *ceItem 
00277                = dynamic_cast<ContactListViewItem*>(item);
00278                
00279        if (ceItem)
00280        {
00281            emit executed(ceItem->addressee().uid());
00282        }
00283     }
00284     else
00285     {
00286         emit executed(QString::null);
00287     }
00288 }
00289 
00290 #include "kaddressbooktableview.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:37 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001