kaddressbook

phoneeditwidget.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., 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 <qbuttongroup.h>
00025 #include <qcheckbox.h>
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 #include <qlistbox.h>
00029 #include <qlistview.h>
00030 #include <qpushbutton.h>
00031 #include <qsignalmapper.h>
00032 #include <qstring.h>
00033 #include <qtooltip.h>
00034 
00035 #include <kapplication.h>
00036 #include <kbuttonbox.h>
00037 #include <kcombobox.h>
00038 #include <kconfig.h>
00039 #include <kdebug.h>
00040 #include <kiconloader.h>
00041 #include <klineedit.h>
00042 #include <klistview.h>
00043 #include <klocale.h>
00044 
00045 #include <kabc/phonenumber.h>
00046 
00047 #include "phoneeditwidget.h"
00048 
00049 PhoneTypeCombo::PhoneTypeCombo( QWidget *parent )
00050   : KComboBox( parent, "TypeCombo" ),
00051     mType( KABC::PhoneNumber::Home ),
00052     mLastSelected( 0 ),
00053     mTypeList( KABC::PhoneNumber::typeList() )
00054 {
00055   mTypeList.append( -1 ); // Others...
00056 
00057   update();
00058 
00059   connect( this, SIGNAL( activated( int ) ),
00060            this, SLOT( selected( int ) ) );
00061   connect( this, SIGNAL( activated( int ) ),
00062            this, SIGNAL( modified() ) );
00063 }
00064 
00065 PhoneTypeCombo::~PhoneTypeCombo()
00066 {
00067 }
00068 
00069 void PhoneTypeCombo::setType( int type )
00070 {
00071   if ( !mTypeList.contains( type ) )
00072     mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), type );
00073 
00074   mType = type;
00075   update();
00076 }
00077 
00078 int PhoneTypeCombo::type() const
00079 {
00080   return mType;
00081 }
00082 
00083 void PhoneTypeCombo::update()
00084 {
00085   bool blocked = signalsBlocked();
00086   blockSignals( true );
00087 
00088   clear();
00089   QValueList<int>::ConstIterator it;
00090   for ( it = mTypeList.begin(); it != mTypeList.end(); ++it ) {
00091     if ( *it == -1 ) { // "Other..." entry
00092       insertItem( i18n( "Other..." ) );
00093     } else {
00094       insertItem( KABC::PhoneNumber::typeLabel( *it ) );
00095     }
00096   }
00097 
00098   setCurrentItem( mTypeList.findIndex( mType ) );
00099 
00100   blockSignals( blocked );
00101 }
00102 
00103 void PhoneTypeCombo::selected( int pos )
00104 {
00105   if ( mTypeList[ pos ] == -1 )
00106     otherSelected();
00107   else {
00108     mType = mTypeList[ pos ];
00109     mLastSelected = pos;
00110   }
00111 }
00112 
00113 void PhoneTypeCombo::otherSelected()
00114 {
00115   PhoneTypeDialog dlg( mType, this );
00116   if ( dlg.exec() ) {
00117     mType = dlg.type();
00118     if ( !mTypeList.contains( mType ) )
00119       mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), mType );
00120   } else {
00121     setType( mTypeList[ mLastSelected ] );
00122   }
00123 
00124   update();
00125 }
00126 
00127 PhoneNumberWidget::PhoneNumberWidget( QWidget *parent )
00128   : QWidget( parent )
00129 {
00130   QHBoxLayout *layout = new QHBoxLayout( this, 6, 11 );
00131 
00132   mTypeCombo = new PhoneTypeCombo( this );
00133   mNumberEdit = new KLineEdit( this );
00134 
00135   layout->addWidget( mTypeCombo );
00136   layout->addWidget( mNumberEdit );
00137 
00138   connect( mTypeCombo, SIGNAL( modified() ), SIGNAL( modified() ) );
00139   connect( mNumberEdit, SIGNAL( textChanged( const QString& ) ), SIGNAL( modified() ) );
00140 }
00141 
00142 void PhoneNumberWidget::setNumber( const KABC::PhoneNumber &number )
00143 {
00144   mNumber = number;
00145 
00146   mTypeCombo->setType( number.type() );
00147   mNumberEdit->setText( number.number() );
00148 }
00149 
00150 KABC::PhoneNumber PhoneNumberWidget::number() const
00151 {
00152   KABC::PhoneNumber number( mNumber );
00153 
00154   number.setType( mTypeCombo->type() );
00155   number.setNumber( mNumberEdit->text() );
00156 
00157   return number;
00158 }
00159 
00160 void PhoneNumberWidget::setReadOnly( bool readOnly )
00161 {
00162   mTypeCombo->setEnabled( !readOnly );
00163   mNumberEdit->setReadOnly( readOnly );
00164 }
00165 
00166 
00167 PhoneEditWidget::PhoneEditWidget( QWidget *parent, const char *name )
00168   : QWidget( parent, name ), mReadOnly( false )
00169 {
00170   QGridLayout *layout = new QGridLayout( this, 2, 2 );
00171   layout->setSpacing( KDialog::spacingHint() );
00172 
00173   mWidgetLayout = new QVBoxLayout( layout );
00174   layout->addMultiCellLayout( mWidgetLayout, 0, 0, 0, 1 );
00175 
00176   mAddButton = new QPushButton( i18n( "Add" ), this );
00177   mAddButton->setMaximumSize( mAddButton->sizeHint() );
00178   layout->addWidget( mAddButton, 1, 0, Qt::AlignRight );
00179 
00180   mRemoveButton = new QPushButton( i18n( "Remove" ), this );
00181   mRemoveButton->setMaximumSize( mRemoveButton->sizeHint() );
00182   layout->addWidget( mRemoveButton, 1, 1 );
00183 
00184   mMapper = new QSignalMapper( this );
00185   connect( mMapper, SIGNAL( mapped( int ) ), SLOT( changed( int ) ) );
00186 
00187   connect( mAddButton, SIGNAL( clicked() ), SLOT( add() ) );
00188   connect( mRemoveButton, SIGNAL( clicked() ), SLOT( remove() ) );
00189 }
00190 
00191 PhoneEditWidget::~PhoneEditWidget()
00192 {
00193 }
00194 
00195 void PhoneEditWidget::setReadOnly( bool readOnly )
00196 {
00197   mReadOnly = readOnly;
00198 
00199   QPtrListIterator<PhoneNumberWidget> it( mWidgets );
00200   while ( it.current() ) {
00201     it.current()->setReadOnly( readOnly );
00202     ++it;
00203   }
00204 }
00205 
00206 void PhoneEditWidget::setPhoneNumbers( const KABC::PhoneNumber::List &list )
00207 {
00208   mPhoneNumberList = list;
00209 
00210   KABC::PhoneNumber::TypeList types;
00211   types << KABC::PhoneNumber::Home;
00212   types << KABC::PhoneNumber::Work;
00213   types << KABC::PhoneNumber::Cell;
00214 
00215   // add an empty entry per default
00216   if ( mPhoneNumberList.count() < 3 )
00217     for ( int i = mPhoneNumberList.count(); i < 3; ++i )
00218       mPhoneNumberList.append( KABC::PhoneNumber( "", types[ i ] ) );
00219 
00220   updateWidgets();
00221   updateButtons();
00222 }
00223 
00224 KABC::PhoneNumber::List PhoneEditWidget::phoneNumbers() const
00225 {
00226   KABC::PhoneNumber::List list;
00227 
00228   KABC::PhoneNumber::List::ConstIterator it;
00229   for ( it = mPhoneNumberList.begin(); it != mPhoneNumberList.end(); ++it )
00230     if ( !(*it).number().isEmpty() )
00231       list.append( *it );
00232 
00233   return list;
00234 }
00235 
00236 void PhoneEditWidget::changed()
00237 {
00238   if ( !mReadOnly )
00239     emit modified();
00240 }
00241 
00242 void PhoneEditWidget::add()
00243 {
00244   mPhoneNumberList.append( KABC::PhoneNumber() );
00245 
00246   updateWidgets();
00247   updateButtons();
00248 }
00249 
00250 void PhoneEditWidget::remove()
00251 {
00252   mPhoneNumberList.remove( mPhoneNumberList.last() );
00253   changed();
00254 
00255   updateWidgets();
00256   updateButtons();
00257 }
00258 
00259 void PhoneEditWidget::updateWidgets()
00260 {
00261   mWidgets.setAutoDelete( true );
00262   mWidgets.clear();
00263   mWidgets.setAutoDelete( false );
00264 
00265   KABC::PhoneNumber::List::ConstIterator it;
00266   int counter = 0;
00267   for ( it = mPhoneNumberList.begin(); it != mPhoneNumberList.end(); ++it ) {
00268     PhoneNumberWidget *wdg = new PhoneNumberWidget( this );
00269     wdg->setNumber( *it );
00270 
00271     mMapper->setMapping( wdg, counter );
00272     connect( wdg, SIGNAL( modified() ), mMapper, SLOT( map() ) );
00273 
00274     mWidgetLayout->addWidget( wdg );
00275     mWidgets.append( wdg );
00276     wdg->show();
00277 
00278     ++counter;
00279   }
00280 }
00281 
00282 void PhoneEditWidget::updateButtons()
00283 {
00284   mRemoveButton->setEnabled( mPhoneNumberList.count() > 3 );
00285 }
00286 
00287 void PhoneEditWidget::changed( int pos )
00288 {
00289   mPhoneNumberList[ pos ] = mWidgets.at( pos )->number();
00290   changed();
00291 }
00292 
00294 // PhoneTypeDialog
00295 PhoneTypeDialog::PhoneTypeDialog( int type, QWidget *parent )
00296   : KDialogBase( Plain, i18n( "Edit Phone Number" ), Ok | Cancel, Ok,
00297                  parent, "PhoneTypeDialog", true ),
00298     mType( type )
00299 {
00300   QWidget *page = plainPage();
00301 
00302   QVBoxLayout *layout = new QVBoxLayout( page, spacingHint() );
00303 
00304   mPreferredBox = new QCheckBox( i18n( "This is the preferred phone number" ), page );
00305   layout->addWidget( mPreferredBox );
00306 
00307   mGroup = new QButtonGroup( 2, Horizontal, i18n( "Types" ), page );
00308   layout->addWidget( mGroup );
00309 
00310   // fill widgets
00311   mTypeList = KABC::PhoneNumber::typeList();
00312   mTypeList.remove( KABC::PhoneNumber::Pref );
00313 
00314   KABC::PhoneNumber::TypeList::ConstIterator it;
00315   for ( it = mTypeList.begin(); it != mTypeList.end(); ++it )
00316     new QCheckBox( KABC::PhoneNumber::typeLabel( *it ), mGroup );
00317 
00318   for ( int i = 0; i < mGroup->count(); ++i ) {
00319     QCheckBox *box = (QCheckBox*)mGroup->find( i );
00320     box->setChecked( mType & mTypeList[ i ] );
00321   }
00322 
00323   mPreferredBox->setChecked( mType & KABC::PhoneNumber::Pref );
00324 }
00325 
00326 int PhoneTypeDialog::type() const
00327 {
00328   int type = 0;
00329 
00330   for ( int i = 0; i < mGroup->count(); ++i ) {
00331     QCheckBox *box = (QCheckBox*)mGroup->find( i );
00332     if ( box->isChecked() )
00333       type += mTypeList[ i ];
00334   }
00335 
00336   if ( mPreferredBox->isChecked() )
00337     type = type | KABC::PhoneNumber::Pref;
00338   else
00339     type = type & ~KABC::PhoneNumber::Pref;
00340 
00341   return type;
00342 }
00343 
00344 
00345 #include "phoneeditwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys