kaddressbook

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