kaddressbook Library API Documentation

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., 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 <qlabel.h>
00026 #include <qtooltip.h>
00027 #include <qpushbutton.h>
00028 #include <qcheckbox.h>
00029 #include <qstring.h>
00030 #include <qlistbox.h>
00031 #include <qlistview.h>
00032 #include <qbuttongroup.h>
00033 
00034 #include <kbuttonbox.h>
00035 #include <klistview.h>
00036 #include <kapplication.h>
00037 #include <kconfig.h>
00038 #include <klineedit.h>
00039 #include <kcombobox.h>
00040 #include <klocale.h>
00041 #include <kdebug.h>
00042 #include <kiconloader.h>
00043 
00044 #include <kabc/phonenumber.h>
00045 
00046 #include "typecombo.h"
00047 
00048 #include "phoneeditwidget.h"
00049 
00050 PhoneEditWidget::PhoneEditWidget( QWidget *parent, const char *name )
00051   : QWidget( parent, name ), mReadOnly(false)
00052 {
00053   QGridLayout *layout = new QGridLayout( this, 5, 2 );
00054   layout->setSpacing( KDialog::spacingHint() );
00055 
00056   mPrefCombo = new PhoneTypeCombo( mPhoneList, this );
00057   mPrefEdit = new KLineEdit( this );
00058   mPrefEdit->setMinimumWidth( int(mPrefEdit->sizeHint().width() * 1.5) );
00059   mPrefCombo->setLineEdit( mPrefEdit );
00060   layout->addWidget( mPrefCombo, 0, 0 );
00061   layout->addWidget( mPrefEdit, 0, 1 );
00062 
00063   mSecondCombo = new PhoneTypeCombo( mPhoneList, this );
00064   mSecondEdit = new KLineEdit( this );
00065   mSecondCombo->setLineEdit( mSecondEdit );
00066   layout->addWidget( mSecondCombo, 1, 0 );
00067   layout->addWidget( mSecondEdit, 1, 1 );
00068 
00069   mThirdCombo = new PhoneTypeCombo( mPhoneList, this );
00070   mThirdEdit = new KLineEdit( this );
00071   mThirdCombo->setLineEdit( mThirdEdit );
00072   layout->addWidget( mThirdCombo, 2, 0 );
00073   layout->addWidget( mThirdEdit, 2, 1 );
00074 
00075   mFourthCombo = new PhoneTypeCombo( mPhoneList, this );
00076   mFourthEdit = new KLineEdit( this );
00077   mFourthCombo->setLineEdit( mFourthEdit );
00078   layout->addWidget( mFourthCombo, 3, 0 );
00079   layout->addWidget( mFourthEdit, 3, 1 );
00080 
00081   // Four numbers don't fit in the current dialog
00082   mFourthCombo->hide();
00083   mFourthEdit->hide();
00084 
00085   mEditButton = new QPushButton( i18n( "Edit Phone Numbers..." ), this );
00086   layout->addMultiCellWidget( mEditButton, 4, 4, 0, 1 );
00087 
00088   connect( mPrefEdit, SIGNAL( textChanged( const QString& ) ),
00089            SLOT( slotPrefEditChanged() ) );
00090   connect( mSecondEdit, SIGNAL( textChanged( const QString& ) ),
00091            SLOT( slotSecondEditChanged() ) );
00092   connect( mThirdEdit, SIGNAL( textChanged( const QString& ) ),
00093            SLOT( slotThirdEditChanged() ) );
00094   connect( mFourthEdit, SIGNAL( textChanged( const QString& ) ),
00095            SLOT( slotFourthEditChanged() ) );
00096 
00097   connect( mEditButton, SIGNAL( clicked() ), SLOT( edit() ) );
00098 
00099   connect( mPrefCombo, SIGNAL( activated( int ) ),
00100            SLOT( updatePrefEdit() ) );
00101   connect( mSecondCombo, SIGNAL( activated( int ) ),
00102            SLOT( updateSecondEdit() ) );
00103   connect( mThirdCombo, SIGNAL( activated( int ) ),
00104            SLOT( updateThirdEdit() ) );
00105   connect( mFourthCombo, SIGNAL( activated( int ) ),
00106            SLOT( updateFourthEdit() ) );
00107 }
00108 
00109 PhoneEditWidget::~PhoneEditWidget()
00110 {
00111 }
00112 
00113 void PhoneEditWidget::setReadOnly( bool readOnly )
00114 {
00115   mReadOnly = readOnly;
00116 
00117   mPrefEdit->setReadOnly( mReadOnly );
00118   mSecondEdit->setReadOnly( mReadOnly );
00119   mThirdEdit->setReadOnly( mReadOnly );
00120   mFourthEdit->setReadOnly( mReadOnly );
00121   mEditButton->setEnabled( !mReadOnly );
00122 }
00123 
00124 void PhoneEditWidget::setPhoneNumbers( const KABC::PhoneNumber::List &list )
00125 {
00126   mPhoneList.clear();
00127 
00128   // Insert types for existing numbers.
00129   mPrefCombo->insertTypeList( list );
00130 
00131   QValueList<int> defaultTypes;
00132   defaultTypes << KABC::PhoneNumber::Home;
00133   defaultTypes << KABC::PhoneNumber::Work;
00134   defaultTypes << KABC::PhoneNumber::Cell;
00135   defaultTypes << ( KABC::PhoneNumber::Work | KABC::PhoneNumber::Fax );
00136   defaultTypes << ( KABC::PhoneNumber::Home | KABC::PhoneNumber::Fax );
00137 
00138   // Insert default types.
00139   // Doing this for mPrefCombo is enough because the list is shared by all
00140   // combos.
00141   QValueList<int>::ConstIterator it;
00142   for( it = defaultTypes.begin(); it != defaultTypes.end(); ++it ) {
00143     if ( !mPrefCombo->hasType( *it ) )
00144       mPrefCombo->insertType( list, *it, PhoneNumber( "", *it ) );
00145   }
00146 
00147   updateCombos();
00148 
00149   mPrefCombo->selectType( defaultTypes[ 0 ] );
00150   mSecondCombo->selectType( defaultTypes[ 1 ] );
00151   mThirdCombo->selectType( defaultTypes[ 2 ] );
00152   mFourthCombo->selectType( defaultTypes[ 3 ] );
00153 
00154   updateLineEdits();
00155 }
00156 
00157 void PhoneEditWidget::updateLineEdits()
00158 {
00159   updatePrefEdit();
00160   updateSecondEdit();
00161   updateThirdEdit();
00162   updateFourthEdit();
00163 }
00164 
00165 void PhoneEditWidget::updateCombos()
00166 {
00167   mPrefCombo->updateTypes();
00168   mSecondCombo->updateTypes();
00169   mThirdCombo->updateTypes();
00170   mFourthCombo->updateTypes();
00171 }
00172 
00173 KABC::PhoneNumber::List PhoneEditWidget::phoneNumbers()
00174 {
00175   KABC::PhoneNumber::List retList;
00176 
00177   KABC::PhoneNumber::List::Iterator it;
00178   for ( it = mPhoneList.begin(); it != mPhoneList.end(); ++it )
00179     if ( !(*it).number().isEmpty() )
00180       retList.append( *it );
00181 
00182   return retList;
00183 }
00184 
00185 void PhoneEditWidget::edit()
00186 {
00187   PhoneEditDialog dlg( mPhoneList, this );
00188 
00189   if ( dlg.exec() ) {
00190     if ( dlg.changed() ) {
00191       mPhoneList = dlg.phoneNumbers();
00192       updateCombos();
00193       emit modified();
00194     }
00195   }
00196 }
00197 
00198 void PhoneEditWidget::updatePrefEdit()
00199 {
00200   updateEdit( mPrefCombo );
00201 }
00202 
00203 void PhoneEditWidget::updateSecondEdit()
00204 {
00205   updateEdit( mSecondCombo );
00206 }
00207 
00208 void PhoneEditWidget::updateThirdEdit()
00209 {
00210   updateEdit( mThirdCombo );
00211 }
00212 
00213 void PhoneEditWidget::updateFourthEdit()
00214 {
00215   updateEdit( mFourthCombo );
00216 }
00217 
00218 void PhoneEditWidget::updateEdit( PhoneTypeCombo *combo )
00219 {
00220   QLineEdit *edit = combo->lineEdit();
00221   if ( !edit )
00222     return;
00223 
00224 #if 0
00225   if ( edit == mPrefEdit ) kdDebug(5720) << " prefEdit" << endl;
00226   if ( edit == mSecondEdit ) kdDebug(5720) << " secondEdit" << endl;
00227   if ( edit == mThirdEdit ) kdDebug(5720) << " thirdEdit" << endl;
00228   if ( edit == mFourthEdit ) kdDebug(5720) << " fourthEdit" << endl;
00229 #endif
00230 
00231   PhoneNumber::List::Iterator it = combo->selectedElement();
00232   if ( it != mPhoneList.end() ) {
00233     edit->setText( (*it).number() );
00234   } else {
00235     kdDebug(5720) << "PhoneEditWidget::updateEdit(): no selected element" << endl;
00236   }
00237 }
00238 
00239 void PhoneEditWidget::slotPrefEditChanged()
00240 {
00241   updatePhoneNumber( mPrefCombo );
00242 }
00243 
00244 void PhoneEditWidget::slotSecondEditChanged()
00245 {
00246   updatePhoneNumber( mSecondCombo );
00247 }
00248 
00249 void PhoneEditWidget::slotThirdEditChanged()
00250 {
00251   updatePhoneNumber( mThirdCombo );
00252 }
00253 
00254 void PhoneEditWidget::slotFourthEditChanged()
00255 {
00256   updatePhoneNumber( mFourthCombo );
00257 }
00258 
00259 void PhoneEditWidget::updatePhoneNumber( PhoneTypeCombo *combo )
00260 {
00261   QLineEdit *edit = combo->lineEdit();
00262   if ( !edit ) return;
00263 
00264   PhoneNumber::List::Iterator it = combo->selectedElement();
00265   if ( it != mPhoneList.end() ) {
00266     (*it).setNumber( edit->text() );
00267   } else {
00268     kdDebug(5720) << "PhoneEditWidget::updatePhoneNumber(): no selected element"
00269               << endl;
00270   }
00271 
00272   updateOtherEdit( combo, mPrefCombo );
00273   updateOtherEdit( combo, mSecondCombo );
00274   updateOtherEdit( combo, mThirdCombo );
00275   updateOtherEdit( combo, mFourthCombo );
00276 
00277   if ( !mReadOnly )
00278     emit modified();
00279 }
00280 
00281 void PhoneEditWidget::updateOtherEdit( PhoneTypeCombo *combo, PhoneTypeCombo *otherCombo )
00282 {
00283   if ( combo == otherCombo ) return;
00284 
00285   if ( combo->currentItem() == otherCombo->currentItem() ) {
00286     updateEdit( otherCombo );
00287   }
00288 }
00289 
00291 // PhoneEditDialog
00292 
00293 class PhoneViewItem : public QListViewItem
00294 {
00295 public:
00296   PhoneViewItem( QListView *parent, const KABC::PhoneNumber &number );
00297 
00298   void setPhoneNumber( const KABC::PhoneNumber &number )
00299   {
00300     mPhoneNumber = number;
00301     makeText();
00302   }
00303 
00304   QString key() { return mPhoneNumber.id(); }
00305   QString country() { return ""; }
00306   QString region() { return ""; }
00307   QString number() { return ""; }
00308 
00309   KABC::PhoneNumber phoneNumber() { return mPhoneNumber; }
00310 
00311 private:
00312   void makeText();
00313 
00314   KABC::PhoneNumber mPhoneNumber;
00315 };
00316 
00317 PhoneViewItem::PhoneViewItem( QListView *parent, const KABC::PhoneNumber &number )
00318   : QListViewItem( parent ), mPhoneNumber( number )
00319 {
00320   makeText();
00321 }
00322 
00323 void PhoneViewItem::makeText()
00324 {
00334   setText( 0, mPhoneNumber.number() );
00335   setText( 1, mPhoneNumber.typeLabel() );
00336 }
00337 
00338 PhoneEditDialog::PhoneEditDialog( const KABC::PhoneNumber::List &list, QWidget *parent, const char *name )
00339   : KDialogBase( KDialogBase::Plain, i18n( "Edit Phone Numbers" ),
00340                  KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok,
00341                  parent, name, true)
00342 {
00343   mPhoneNumberList = list;
00344 
00345   QWidget *page = plainPage();
00346 
00347   QGridLayout *layout = new QGridLayout( page, 1, 2 );
00348   layout->setSpacing( spacingHint() );
00349 
00350   mListView = new KListView( page );
00351   mListView->setAllColumnsShowFocus( true );
00352   mListView->addColumn( i18n( "Number" ) );
00353   mListView->addColumn( i18n( "Type" ) );
00354 
00355   KButtonBox *buttonBox = new KButtonBox( page, Vertical );
00356 
00357   buttonBox->addButton( i18n( "&Add..." ), this, SLOT( slotAddPhoneNumber() ) );
00358   mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, SLOT( slotEditPhoneNumber() ) );
00359   mEditButton->setEnabled( false );
00360   mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, SLOT( slotRemovePhoneNumber() ) );
00361   mRemoveButton->setEnabled( false );
00362   buttonBox->layout();
00363 
00364   layout->addWidget( mListView, 0, 0 );
00365   layout->addWidget( buttonBox, 0, 1 );
00366 
00367   connect( mListView, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged()) );
00368   connect( mListView, SIGNAL(doubleClicked( QListViewItem *, const QPoint &, int  )), this, SLOT( slotEditPhoneNumber()));
00369 
00370   KABC::PhoneNumber::List::Iterator it;
00371   for ( it = mPhoneNumberList.begin(); it != mPhoneNumberList.end(); ++it )
00372     new PhoneViewItem( mListView, *it );
00373 
00374   mChanged = false;
00375 }
00376 
00377 PhoneEditDialog::~PhoneEditDialog()
00378 {
00379 }
00380 
00381 void PhoneEditDialog::slotAddPhoneNumber()
00382 {
00383   KABC::PhoneNumber tmp( "", 0 );
00384   PhoneTypeDialog dlg( tmp, this );
00385 
00386   if ( dlg.exec() ) {
00387     KABC::PhoneNumber phoneNumber = dlg.phoneNumber();
00388     mPhoneNumberList.append( phoneNumber );
00389     new PhoneViewItem( mListView, phoneNumber );
00390 
00391     mChanged = true;
00392   }
00393 }
00394 
00395 void PhoneEditDialog::slotRemovePhoneNumber()
00396 {
00397   PhoneViewItem *item = static_cast<PhoneViewItem*>( mListView->currentItem() );
00398   if ( !item )
00399     return;
00400 
00401   mPhoneNumberList.remove( item->phoneNumber() );
00402   QListViewItem *currItem = mListView->currentItem();
00403   mListView->takeItem( currItem );
00404   delete currItem;
00405 
00406   mChanged = true;
00407 }
00408 
00409 void PhoneEditDialog::slotEditPhoneNumber()
00410 {
00411   PhoneViewItem *item = static_cast<PhoneViewItem*>( mListView->currentItem() );
00412   if ( !item )
00413     return;
00414 
00415   PhoneTypeDialog dlg( item->phoneNumber(), this );
00416 
00417   if ( dlg.exec() ) {
00418     slotRemovePhoneNumber();
00419     KABC::PhoneNumber phoneNumber = dlg.phoneNumber();
00420     mPhoneNumberList.append( phoneNumber );
00421     new PhoneViewItem( mListView, phoneNumber );
00422 
00423     mChanged = true;
00424   }
00425 }
00426 
00427 void PhoneEditDialog::slotSelectionChanged()
00428 {
00429   bool state = ( mListView->currentItem() != 0 );
00430 
00431   mRemoveButton->setEnabled( state );
00432   mEditButton->setEnabled( state );
00433 }
00434 
00435 const KABC::PhoneNumber::List &PhoneEditDialog::phoneNumbers()
00436 {
00437   return mPhoneNumberList;
00438 }
00439 
00440 bool PhoneEditDialog::changed() const
00441 {
00442   return mChanged;
00443 }
00444 
00446 // PhoneTypeDialog
00447 PhoneTypeDialog::PhoneTypeDialog( const KABC::PhoneNumber &phoneNumber,
00448                                QWidget *parent, const char *name)
00449   : KDialogBase( KDialogBase::Plain, i18n( "Edit Phone Number" ),
00450                 KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok,
00451                 parent, name, true), mPhoneNumber( phoneNumber )
00452 {
00453   QWidget *page = plainPage();
00454   QLabel *label = 0;
00455   QGridLayout *layout = new QGridLayout( page, 3, 2, marginHint(), spacingHint() );
00456 
00457   label = new QLabel( i18n( "Number:" ), page );
00458   layout->addWidget( label, 0, 0 );
00459   mNumber = new KLineEdit( page );
00460   layout->addWidget( mNumber, 0, 1 );
00461 
00462   mPreferredBox = new QCheckBox( i18n( "This is the preferred phone number" ), page );
00463   layout->addMultiCellWidget( mPreferredBox, 1, 1, 0, 1 );
00464 
00465   mGroup = new QButtonGroup( 2, Horizontal, i18n( "Types" ), page );
00466   layout->addMultiCellWidget( mGroup, 2, 2, 0, 1 );
00467 
00468   // fill widgets
00469   mNumber->setText( mPhoneNumber.number() );
00470 
00471   mTypeList = KABC::PhoneNumber::typeList();
00472   mTypeList.remove( KABC::PhoneNumber::Pref );
00473 
00474   KABC::PhoneNumber::TypeList::Iterator it;
00475   for ( it = mTypeList.begin(); it != mTypeList.end(); ++it )
00476     new QCheckBox( KABC::PhoneNumber::typeLabel( *it ), mGroup );
00477 
00478   for ( int i = 0; i < mGroup->count(); ++i ) {
00479     int type = mPhoneNumber.type();
00480     QCheckBox *box = (QCheckBox*)mGroup->find( i );
00481     box->setChecked( type & mTypeList[ i ] );
00482   }
00483 
00484   mPreferredBox->setChecked( mPhoneNumber.type() & KABC::PhoneNumber::Pref );
00485 }
00486 
00487 KABC::PhoneNumber PhoneTypeDialog::phoneNumber()
00488 {
00489   mPhoneNumber.setNumber( mNumber->text() );
00490 
00491   int type = 0;
00492   for ( int i = 0; i < mGroup->count(); ++i ) {
00493     QCheckBox *box = (QCheckBox*)mGroup->find( i );
00494     if ( box->isChecked() )
00495       type += mTypeList[ i ];
00496   }
00497 
00498   if ( mPreferredBox->isChecked() )
00499     mPhoneNumber.setType( type | KABC::PhoneNumber::Pref );
00500   else
00501     mPhoneNumber.setType( type & ~KABC::PhoneNumber::Pref );
00502 
00503   return mPhoneNumber;
00504 }
00505 
00506 
00507 #include "phoneeditwidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Mar 6 17:18:27 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003