kaddressbook Library API Documentation

kaddressbooktableview.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 <qlayout.h>
00025 #include <qheader.h>
00026 #include <qvbox.h>
00027 #include <qlistbox.h>
00028 #include <qwidget.h>
00029 #include <qfile.h>
00030 #include <qimage.h>
00031 #include <qcombobox.h>
00032 #include <qapplication.h>
00033 #include <qdragobject.h>
00034 #include <qevent.h>
00035 #include <qurl.h>
00036 #include <qpixmap.h>
00037 
00038 #include <kabc/addressbook.h>
00039 #include <kapplication.h>
00040 #include <kconfig.h>
00041 #include <kcolorbutton.h>
00042 #include <kdebug.h>
00043 #include <kglobal.h>
00044 #include <kiconloader.h>
00045 #include <klineedit.h>
00046 #include <klocale.h>
00047 #include <kmessagebox.h>
00048 #include <kurl.h>
00049 #include <kurlrequester.h>
00050 
00051 #include "configuretableviewdialog.h"
00052 #include "contactlistview.h"
00053 #include "core.h"
00054 #include "kabprefs.h"
00055 #include "undocmds.h"
00056 
00057 #include "kaddressbooktableview.h"
00058 
00059 class TableViewFactory : public ViewFactory
00060 {
00061   public:
00062     KAddressBookView *view( KAB::Core *core, QWidget *parent, const char *name )
00063     {
00064       return new KAddressBookTableView( core, parent, name );
00065     }
00066 
00067     QString type() const { return I18N_NOOP("Table"); }
00068 
00069     QString description() const { return i18n( "A listing of contacts in a table. Each cell of "
00070                                   "the table holds a field of the contact." ); }
00071 
00072     ViewConfigureWidget *configureWidget( KABC::AddressBook *ab, QWidget *parent,
00073                                           const char *name = 0 )
00074     {
00075       return new ConfigureTableViewWidget( ab, parent, name );
00076     }
00077 };
00078 
00079 extern "C" {
00080   void *init_libkaddrbk_tableview()
00081   {
00082     return ( new TableViewFactory );
00083   }
00084 }
00085 
00086 KAddressBookTableView::KAddressBookTableView( KAB::Core *core,
00087                                               QWidget *parent, const char *name )
00088   : KAddressBookView( core, parent, name )
00089 {
00090   mainLayout = new QVBoxLayout( viewWidget(), 2 );
00091 
00092   // The list view will be created when the config is read.
00093   mListView = 0;
00094 }
00095 
00096 KAddressBookTableView::~KAddressBookTableView()
00097 {
00098 }
00099 
00100 void KAddressBookTableView::reconstructListView()
00101 {
00102     if (mListView)
00103     {
00104         disconnect(mListView, SIGNAL(selectionChanged()),
00105                    this, SLOT(addresseeSelected()));
00106         disconnect(mListView, SIGNAL(executed(QListViewItem*)),
00107                    this, SLOT(addresseeExecuted(QListViewItem*)));
00108         disconnect(mListView, SIGNAL(doubleClicked(QListViewItem*)),
00109                    this, SLOT(addresseeExecuted(QListViewItem*)));
00110         disconnect(mListView, SIGNAL(startAddresseeDrag()), this,
00111                    SIGNAL(startDrag()));
00112         disconnect(mListView, SIGNAL(addresseeDropped(QDropEvent*)), this,
00113                    SIGNAL(dropped(QDropEvent*)));
00114         delete mListView;
00115     }
00116 
00117   mListView = new ContactListView( this, core()->addressBook(), viewWidget() );
00118   mListView->setFullWidth( true );
00119 
00120   // Add the columns
00121   KABC::Field::List fieldList = fields();
00122   KABC::Field::List::ConstIterator it;
00123 
00124   int c = 0;
00125   for( it = fieldList.begin(); it != fieldList.end(); ++it ) {
00126       mListView->addColumn( (*it)->label() );
00127       mListView->setColumnWidthMode(c++, QListView::Manual);
00128   }
00129 
00130   connect(mListView, SIGNAL(selectionChanged()),
00131           this, SLOT(addresseeSelected()));
00132   connect(mListView, SIGNAL(startAddresseeDrag()), this,
00133           SIGNAL(startDrag()));
00134   connect(mListView, SIGNAL(addresseeDropped(QDropEvent*)), this,
00135           SIGNAL(dropped(QDropEvent*)));
00136   connect( mListView, SIGNAL( contextMenu( KListView*, QListViewItem*, const QPoint& ) ),
00137            this, SLOT( rmbClicked( KListView*, QListViewItem*, const QPoint& ) ) );
00138   connect( mListView->header(), SIGNAL( clicked(int) ),
00139            SIGNAL( sortFieldChanged() ) );
00140 
00141   if (KABPrefs::instance()->mHonorSingleClick)
00142     connect(mListView, SIGNAL(executed(QListViewItem*)),
00143           this, SLOT(addresseeExecuted(QListViewItem*)));
00144   else
00145     connect(mListView, SIGNAL(doubleClicked(QListViewItem*)),
00146           this, SLOT(addresseeExecuted(QListViewItem*)));
00147 
00148   refresh();
00149 
00150   mListView->setSorting( 0, true );
00151   mainLayout->addWidget( mListView );
00152   mainLayout->activate();
00153   mListView->show();
00154 }
00155 
00156 KABC::Field *KAddressBookTableView::sortField() const
00157 {
00158   // we have hardcoded sorting, so we have to return a hardcoded field :(
00159   return ( mListView->sortColumn() == -1 ? fields()[ 0 ] : fields()[ mListView->sortColumn() ] );
00160 }
00161 
00162 void KAddressBookTableView::writeConfig(KConfig *config)
00163 {
00164   KAddressBookView::writeConfig(config);
00165 
00166   mListView->saveLayout(config, config->group());
00167 }
00168 
00169 void KAddressBookTableView::readConfig(KConfig *config)
00170 {
00171   KAddressBookView::readConfig( config );
00172 
00173   // The config could have changed the fields, so we need to reconstruct
00174   // the listview.
00175   reconstructListView();
00176 
00177   // Set the list view options
00178   mListView->setAlternateBackgroundEnabled(config->readBoolEntry("ABackground",
00179                                                                  true));
00180   mListView->setSingleLineEnabled(config->readBoolEntry("SingleLine", false));
00181   mListView->setToolTipsEnabled(config->readBoolEntry("ToolTips", true));
00182 
00183   if (config->readBoolEntry("Background", false))
00184     mListView->setBackgroundPixmap(config->readPathEntry("BackgroundName"));
00185 
00186   // Restore the layout of the listview
00187   mListView->restoreLayout(config, config->group());
00188 }
00189 
00190 void KAddressBookTableView::refresh(QString uid)
00191 {
00192   // For now just repopulate. In reality this method should
00193   // check the value of uid, and if valid iterate through
00194   // the listview to find the entry, then tell it to refresh.
00195 
00196   if (uid.isNull()) {
00197     // Clear the list view
00198     QString currentUID, nextUID;
00199     ContactListViewItem *currentItem = dynamic_cast<ContactListViewItem*>( mListView->currentItem() );
00200     if ( currentItem ) {
00201       ContactListViewItem *nextItem = dynamic_cast<ContactListViewItem*>( currentItem->itemBelow() );
00202       if ( nextItem )
00203         nextUID = nextItem->addressee().uid();
00204       currentUID = currentItem->addressee().uid();
00205     }
00206 
00207     mListView->clear();
00208 
00209     currentItem = 0;
00210     KABC::Addressee::List addresseeList = addressees();
00211     KABC::Addressee::List::Iterator it;
00212     for (it = addresseeList.begin(); it != addresseeList.end(); ++it ) {
00213       ContactListViewItem *item = new ContactListViewItem(*it, mListView, 
00214                                         core()->addressBook(), fields());
00215       if ( (*it).uid() == currentUID )
00216         currentItem = item;
00217       else if ( (*it).uid() == nextUID && !currentItem )
00218         currentItem = item;
00219     }
00220 
00221     // Sometimes the background pixmap gets messed up when we add lots
00222     // of items.
00223     mListView->repaint();
00224 
00225     if ( currentItem ) {
00226       mListView->setCurrentItem( currentItem );
00227       mListView->ensureItemVisible( currentItem );
00228     }
00229   } else {
00230     // Only need to update on entry. Iterate through and try to find it
00231     ContactListViewItem *ceItem;
00232     QListViewItemIterator it( mListView );
00233     while ( it.current() ) {
00234       ceItem = dynamic_cast<ContactListViewItem*>( it.current() );
00235       if ( ceItem && ceItem->addressee().uid() == uid ) {
00236         ceItem->refresh();
00237         return;
00238       }
00239       ++it;
00240     }
00241 
00242     refresh( QString::null );
00243   }
00244 }
00245 
00246 QStringList KAddressBookTableView::selectedUids()
00247 {
00248     QStringList uidList;
00249     QListViewItem *item;
00250     ContactListViewItem *ceItem;
00251 
00252     for(item = mListView->firstChild(); item; item = item->itemBelow())
00253     {
00254         if (mListView->isSelected( item ))
00255         {
00256             ceItem = dynamic_cast<ContactListViewItem*>(item);
00257             if (ceItem != 0L)
00258                 uidList << ceItem->addressee().uid();
00259         }
00260     }
00261 
00262     return uidList;
00263 }
00264 
00265 void KAddressBookTableView::setSelected(QString uid, bool selected)
00266 {
00267     QListViewItem *item;
00268     ContactListViewItem *ceItem;
00269 
00270     if (uid.isNull())
00271     {
00272         mListView->selectAll(selected);
00273     }
00274     else
00275     {
00276         for(item = mListView->firstChild(); item; item = item->itemBelow())
00277         {
00278             ceItem = dynamic_cast<ContactListViewItem*>(item);
00279             if ((ceItem != 0L) && (ceItem->addressee().uid() == uid))
00280             {
00281                 mListView->setSelected(item, selected);
00282 
00283                 if (selected)
00284                     mListView->ensureItemVisible(item);
00285             }
00286         }
00287     }
00288 }
00289 
00290 void KAddressBookTableView::addresseeSelected()
00291 {
00292     // We need to try to find the first selected item. This might not be the
00293     // last selected item, but when QListView is in multiselection mode,
00294     // there is no way to figure out which one was
00295     // selected last.
00296     QListViewItem *item;
00297     bool found =false;
00298     for (item = mListView->firstChild(); item && !found;
00299          item = item->nextSibling())
00300     {
00301         if (item->isSelected())
00302         {
00303             found = true;
00304             ContactListViewItem *ceItem
00305                  = dynamic_cast<ContactListViewItem*>(item);
00306             if ( ceItem ) emit selected(ceItem->addressee().uid());
00307         }
00308     }
00309 
00310     if (!found)
00311         emit selected(QString::null);
00312 }
00313 
00314 void KAddressBookTableView::addresseeExecuted(QListViewItem *item)
00315 {
00316     if (item)
00317     {
00318         ContactListViewItem *ceItem
00319                = dynamic_cast<ContactListViewItem*>(item);
00320 
00321        if (ceItem)
00322        {
00323            emit executed(ceItem->addressee().uid());
00324        }
00325     }
00326     else
00327     {
00328         emit executed(QString::null);
00329     }
00330 }
00331 
00332 void KAddressBookTableView::rmbClicked( KListView*, QListViewItem*, const QPoint &point )
00333 {
00334   popup( point );
00335 }
00336 
00337 #include "kaddressbooktableview.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