kaddressbook Library API Documentation

selectfieldswidget.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
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 <qlabel.h>
00025 #include <qlayout.h>
00026 #include <qlistbox.h>
00027 #include <qpushbutton.h>
00028 #include <qtoolbutton.h>
00029 
00030 #include <kcombobox.h>
00031 #include <kdebug.h>
00032 #include <kdialog.h>
00033 #include <kiconloader.h>
00034 #include <klocale.h>
00035 
00036 #include "selectfieldswidget.h"
00037 
00038 class FieldItem : public QListBoxText
00039 {
00040   public:
00041     FieldItem( QListBox *parent, KABC::Field *field )
00042       : QListBoxText( parent, field->label() ), mField( field ) {}
00043     
00044     FieldItem( QListBox *parent, KABC::Field *field, int index )
00045       : QListBoxText( parent, field->label(), parent->item( index ) ),
00046         mField( field ) {}
00047     
00048     KABC::Field *field() { return mField; }
00049     
00050   private:
00051     KABC::Field *mField;
00052 };
00053 
00055 // SelectFieldsWidget Methods
00056 
00057 SelectFieldsWidget::SelectFieldsWidget( KABC::AddressBook *doc,
00058                                         const KABC::Field::List &selectedFields,
00059                                         QWidget *parent, const char *name )
00060   : QWidget( parent, name )
00061 {
00062   initGUI( doc );
00063   setSelectedFields( selectedFields );
00064 }
00065 
00066 SelectFieldsWidget::SelectFieldsWidget( KABC::AddressBook *doc, QWidget *parent,
00067                                         const char *name )
00068   : QWidget( parent, name )
00069 {
00070   initGUI( doc );
00071 }
00072 
00073 void SelectFieldsWidget::setSelectedFields( const KABC::Field::List &selectedFields )
00074 {
00075   KABC::Field::List::ConstIterator it;
00076   for( it = selectedFields.begin(); it != selectedFields.end(); ++it )
00077     new FieldItem( mSelectedBox, *it );
00078 
00079   slotShowFields( mCategoryCombo->currentItem() );
00080 }
00081 
00082 void SelectFieldsWidget::slotShowFields( int index )
00083 {
00084   int currentPos = mUnSelectedBox->currentItem();
00085   mUnSelectedBox->clear();
00086 
00087   int category;
00088   if ( index == 0 ) category = KABC::Field::All;
00089   else category = 1 << ( index - 1 );
00090 
00091   KABC::Field::List allFields = mDoc->fields( category );
00092 
00093   KABC::Field::List::ConstIterator it;
00094   for ( it = allFields.begin(); it != allFields.end(); ++it ) {
00095     QListBoxItem *item = mSelectedBox->firstItem();
00096     while( item ) {
00097       FieldItem *fieldItem = static_cast<FieldItem *>( item );
00098       if ( (*it)->equals( fieldItem->field() ) )
00099         break;
00100       item = item->next();
00101     }
00102 
00103     if ( !item )
00104       new FieldItem( mUnSelectedBox, *it );
00105   }
00106 
00107   mUnSelectedBox->sort();
00108   mUnSelectedBox->setCurrentItem( currentPos );
00109 }
00110 
00111 void SelectFieldsWidget::slotSelect()
00112 {
00113   // insert selected items in the unselected list to the selected list,
00114   // directoy under the current item if selected, or at the bottonm if
00115   // nothing is selected in the selected list
00116   int where = mSelectedBox->currentItem();
00117   if ( !(where > -1 && mSelectedBox->item( where )->isSelected()) )
00118     where = mSelectedBox->count() - 1;
00119 
00120   for ( uint i = 0; i < mUnSelectedBox->count(); ++i )
00121     if ( mUnSelectedBox->isSelected( mUnSelectedBox->item( i ) ) ) {
00122       FieldItem *fieldItem = static_cast<FieldItem *>( mUnSelectedBox->item( i ) );
00123       new FieldItem( mSelectedBox, fieldItem->field(), where );
00124       where++;
00125     }
00126 
00127   slotShowFields( mCategoryCombo->currentItem() );
00128 }
00129 
00130 void SelectFieldsWidget::slotUnSelect()
00131 {
00132   for ( uint i = 0; i < mSelectedBox->count(); ++i )
00133     if ( mSelectedBox->isSelected( mSelectedBox->item( i ) ) ) {
00134       mSelectedBox->removeItem( i );
00135       --i;
00136     }
00137 
00138   slotShowFields( mCategoryCombo->currentItem() );
00139 }
00140 
00141 KABC::Field::List SelectFieldsWidget::selectedFields()
00142 {
00143   KABC::Field::List result;
00144 
00145   for( uint i = 0; i < mSelectedBox->count(); ++i ) {
00146     FieldItem *fieldItem = static_cast<FieldItem *>( mSelectedBox->item( i ) );
00147     result.append( fieldItem->field() );
00148   }
00149 
00150   return result;
00151 }
00152 
00153 void SelectFieldsWidget::slotButtonsEnabled()
00154 {
00155   bool state = false;
00156   // add button: enabled if any items are selected in the unselected list
00157   for( uint i = 0; i < mUnSelectedBox->count(); ++i )
00158     if ( mUnSelectedBox->item( i )->isSelected() ) {
00159       state = true;
00160       break;
00161     }
00162   mAddButton->setEnabled( state );
00163 
00164   int j = mSelectedBox->currentItem();
00165   state = ( j > -1 && mSelectedBox->isSelected( j ) );
00166 
00167   // up button: enabled if there is a current item > 0 and that is selected
00168   mUpButton->setEnabled( ( j > 0 && state ) );
00169 
00170   // down button: enabled if there is a current item < count - 2 and that is selected
00171   mDownButton->setEnabled( ( j > -1 && j < (int)mSelectedBox->count() - 1 && state ) );
00172 
00173   // remove button: enabled if any items are selected in the selected list
00174   state = false;
00175   for ( uint i = 0; i < mSelectedBox->count(); ++i )
00176     if ( mSelectedBox->item( i )->isSelected() ) {
00177       state = true;
00178       break;
00179     }
00180   mRemoveButton->setEnabled( state );
00181 }
00182 
00183 void SelectFieldsWidget::slotMoveUp()
00184 {
00185   int i = mSelectedBox->currentItem();
00186   if ( i > 0 ) {
00187     QListBoxItem *item = mSelectedBox->item( i );
00188     mSelectedBox->takeItem( item );
00189     mSelectedBox->insertItem( item, i - 1 );
00190     mSelectedBox->setCurrentItem( item );
00191     mSelectedBox->setSelected( i - 1, true );
00192   }
00193 }
00194 
00195 void SelectFieldsWidget::slotMoveDown()
00196 {
00197   int i = mSelectedBox->currentItem();
00198   if ( i > -1 && i < (int)mSelectedBox->count() - 1 ) {
00199     QListBoxItem *item = mSelectedBox->item( i );
00200     mSelectedBox->takeItem( item );
00201     mSelectedBox->insertItem( item, i + 1 );
00202     mSelectedBox->setCurrentItem( item );
00203     mSelectedBox->setSelected( i + 1, true );
00204   }
00205 }
00206 
00207 void SelectFieldsWidget::initGUI( KABC::AddressBook *doc )
00208 {
00209   mDoc = doc;
00210 
00211   setCaption( i18n("Select Fields to Display") );
00212 
00213   QGridLayout *gl = new QGridLayout( this , 6, 4, KDialog::spacingHint() );
00214 
00215   mCategoryCombo = new KComboBox( false, this );
00216   mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::All ) );
00217   mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::Frequent ) );
00218   mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::Address ) );
00219   mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::Email ) );
00220   mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::Personal ) );
00221   mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::Organization ) );
00222   mCategoryCombo->insertItem( KABC::Field::categoryLabel( KABC::Field::CustomCategory ) );
00223   connect( mCategoryCombo, SIGNAL( activated(int) ), SLOT( slotShowFields(int) ) );
00224   gl->addWidget( mCategoryCombo, 0, 0 );
00225 
00226   QLabel *label = new QLabel( i18n( "&Selected fields:" ), this );
00227   gl->addWidget( label, 0, 2 );
00228 
00229   mUnSelectedBox = new QListBox( this );
00230   mUnSelectedBox->setSelectionMode( QListBox::Extended );
00231   mUnSelectedBox->setMinimumHeight( 100 );
00232   gl->addWidget( mUnSelectedBox, 1, 0 );
00233 
00234   mSelectedBox = new QListBox( this );
00235   mSelectedBox->setSelectionMode( QListBox::Extended );
00236   label->setBuddy( mSelectedBox );
00237   gl->addWidget( mSelectedBox, 1, 2 );
00238 
00239   QBoxLayout *vb1 = new QBoxLayout( QBoxLayout::TopToBottom, KDialog::spacingHint() );
00240   vb1->addStretch();
00241 
00242   mAddButton = new QToolButton( this );
00243   mAddButton->setIconSet( SmallIconSet( "1rightarrow" ) );
00244   connect( mAddButton, SIGNAL( clicked() ), SLOT( slotSelect() ) );
00245   vb1->addWidget( mAddButton );
00246 
00247   mRemoveButton = new QToolButton( this );
00248   mRemoveButton->setIconSet( SmallIconSet( "1leftarrow" ) );
00249   connect( mRemoveButton, SIGNAL( clicked() ), SLOT( slotUnSelect() ) );
00250   vb1->addWidget( mRemoveButton );
00251 
00252   vb1->addStretch();
00253   gl->addLayout( vb1, 1, 1 );
00254 
00255   QBoxLayout *vb2 = new QBoxLayout( QBoxLayout::TopToBottom, KDialog::spacingHint() );
00256   vb2->addStretch();
00257 
00258   mUpButton = new QToolButton( this );
00259   mUpButton->setIconSet( SmallIconSet( "1uparrow" ) );
00260   connect( mUpButton, SIGNAL( clicked() ), SLOT( slotMoveUp() ) );
00261   vb2->addWidget( mUpButton );
00262 
00263   mDownButton = new QToolButton( this );
00264   mDownButton->setIconSet( SmallIconSet( "1downarrow" ) );
00265   connect( mDownButton, SIGNAL( clicked() ), SLOT( slotMoveDown() ) );
00266   vb2->addWidget( mDownButton );
00267 
00268   vb2->addStretch();
00269   gl->addLayout( vb2, 1, 3 );
00270 
00271   QSize sizeHint = mUnSelectedBox->sizeHint();
00272 
00273   // make sure we fill the list with all items, so that we can
00274   // get the maxItemWidth we need to not truncate the view
00275   slotShowFields( 0 );
00276 
00277   sizeHint = sizeHint.expandedTo( mSelectedBox->sizeHint() );
00278   sizeHint.setWidth( mUnSelectedBox->maxItemWidth() );
00279   mUnSelectedBox->setMinimumSize( sizeHint );
00280   mSelectedBox->setMinimumSize( sizeHint );
00281 
00282   gl->activate();
00283 
00284   connect( mUnSelectedBox, SIGNAL( selectionChanged() ), SLOT( slotButtonsEnabled() ) );
00285   connect( mSelectedBox, SIGNAL( selectionChanged() ), SLOT( slotButtonsEnabled() ) );
00286   connect( mSelectedBox, SIGNAL( currentChanged( QListBoxItem * ) ), SLOT( slotButtonsEnabled() ) );
00287 
00288   slotButtonsEnabled();
00289 }
00290 
00291 #include "selectfieldswidget.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:10 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001