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   mAddButton->setEnabled( !readOnly );
00199   mRemoveButton->setEnabled( !readOnly && mPhoneNumberList.count() > 3 );
00200 
00201   QPtrListIterator<PhoneNumberWidget> it( mWidgets );
00202   while ( it.current() ) {
00203     it.current()->setReadOnly( readOnly );
00204     ++it;
00205   }
00206 }
00207 
00208 void PhoneEditWidget::setPhoneNumbers( const KABC::PhoneNumber::List &list )
00209 {
00210   mPhoneNumberList = list;
00211 
00212   KABC::PhoneNumber::TypeList types;
00213   types << KABC::PhoneNumber::Home;
00214   types << KABC::PhoneNumber::Work;
00215   types << KABC::PhoneNumber::Cell;
00216 
00217   // add an empty entry per default
00218   if ( mPhoneNumberList.count() < 3 )
00219     for ( int i = mPhoneNumberList.count(); i < 3; ++i )
00220       mPhoneNumberList.append( KABC::PhoneNumber( "", types[ i ] ) );
00221 
00222   recreateNumberWidgets();
00223 }
00224 
00225 KABC::PhoneNumber::List PhoneEditWidget::phoneNumbers() const
00226 {
00227   KABC::PhoneNumber::List list;
00228 
00229   KABC::PhoneNumber::List::ConstIterator it;
00230   for ( it = mPhoneNumberList.begin(); it != mPhoneNumberList.end(); ++it )
00231     if ( !(*it).number().isEmpty() )
00232       list.append( *it );
00233 
00234   return list;
00235 }
00236 
00237 void PhoneEditWidget::changed()
00238 {
00239   if ( !mReadOnly )
00240     emit modified();
00241 }
00242 
00243 void PhoneEditWidget::add()
00244 {
00245   mPhoneNumberList.append( KABC::PhoneNumber() );
00246 
00247   recreateNumberWidgets();
00248 }
00249 
00250 void PhoneEditWidget::remove()
00251 {
00252   mPhoneNumberList.remove( mPhoneNumberList.last() );
00253   changed();
00254 
00255   recreateNumberWidgets();
00256 }
00257 
00258 void PhoneEditWidget::recreateNumberWidgets()
00259 {
00260   for ( QWidget *w = mWidgets.first(); w; w = mWidgets.next() ) {
00261     mWidgetLayout->remove( w );
00262     w->deleteLater();
00263   }
00264   mWidgets.clear();
00265 
00266   KABC::PhoneNumber::List::ConstIterator it;
00267   int counter = 0;
00268   for ( it = mPhoneNumberList.begin(); it != mPhoneNumberList.end(); ++it ) {
00269     PhoneNumberWidget *wdg = new PhoneNumberWidget( this );
00270     wdg->setNumber( *it );
00271 
00272     mMapper->setMapping( wdg, counter );
00273     connect( wdg, SIGNAL( modified() ), mMapper, SLOT( map() ) );
00274 
00275     mWidgetLayout->addWidget( wdg );
00276     mWidgets.append( wdg );
00277     wdg->show();
00278 
00279     ++counter;
00280   }
00281   setReadOnly(mReadOnly);
00282 }
00283 
00284 void PhoneEditWidget::changed( int pos )
00285 {
00286   mPhoneNumberList[ pos ] = mWidgets.at( pos )->number();
00287   changed();
00288 }
00289 
00291 // PhoneTypeDialog
00292 PhoneTypeDialog::PhoneTypeDialog( int type, QWidget *parent )
00293   : KDialogBase( Plain, i18n( "Edit Phone Number" ), Ok | Cancel, Ok,
00294                  parent, "PhoneTypeDialog", true ),
00295     mType( type )
00296 {
00297   QWidget *page = plainPage();
00298 
00299   QVBoxLayout *layout = new QVBoxLayout( page, spacingHint() );
00300 
00301   mPreferredBox = new QCheckBox( i18n( "This is the preferred phone number" ), page );
00302   layout->addWidget( mPreferredBox );
00303 
00304   mGroup = new QButtonGroup( 2, Horizontal, i18n( "Types" ), page );
00305   layout->addWidget( mGroup );
00306 
00307   // fill widgets
00308   mTypeList = KABC::PhoneNumber::typeList();
00309   mTypeList.remove( KABC::PhoneNumber::Pref );
00310 
00311   KABC::PhoneNumber::TypeList::ConstIterator it;
00312   for ( it = mTypeList.begin(); it != mTypeList.end(); ++it )
00313     new QCheckBox( KABC::PhoneNumber::typeLabel( *it ), mGroup );
00314 
00315   for ( int i = 0; i < mGroup->count(); ++i ) {
00316     QCheckBox *box = (QCheckBox*)mGroup->find( i );
00317     box->setChecked( mType & mTypeList[ i ] );
00318   }
00319 
00320   mPreferredBox->setChecked( mType & KABC::PhoneNumber::Pref );
00321 }
00322 
00323 int PhoneTypeDialog::type() const
00324 {
00325   int type = 0;
00326 
00327   for ( int i = 0; i < mGroup->count(); ++i ) {
00328     QCheckBox *box = (QCheckBox*)mGroup->find( i );
00329     if ( box->isChecked() )
00330       type += mTypeList[ i ];
00331   }
00332 
00333   if ( mPreferredBox->isChecked() )
00334     type = type | KABC::PhoneNumber::Pref;
00335   else
00336     type = type & ~KABC::PhoneNumber::Pref;
00337 
00338   return type;
00339 }
00340 
00341 
00342 #include "phoneeditwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys