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     int pos = edit->cursorPosition();
00234     edit->setText( (*it).number() );
00235     edit->setCursorPosition( pos );
00236   } else {
00237     kdDebug(5720) << "PhoneEditWidget::updateEdit(): no selected element" << endl;
00238   }
00239 }
00240 
00241 void PhoneEditWidget::slotPrefEditChanged()
00242 {
00243   updatePhoneNumber( mPrefCombo );
00244 }
00245 
00246 void PhoneEditWidget::slotSecondEditChanged()
00247 {
00248   updatePhoneNumber( mSecondCombo );
00249 }
00250 
00251 void PhoneEditWidget::slotThirdEditChanged()
00252 {
00253   updatePhoneNumber( mThirdCombo );
00254 }
00255 
00256 void PhoneEditWidget::slotFourthEditChanged()
00257 {
00258   updatePhoneNumber( mFourthCombo );
00259 }
00260 
00261 void PhoneEditWidget::updatePhoneNumber( PhoneTypeCombo *combo )
00262 {
00263   QLineEdit *edit = combo->lineEdit();
00264   if ( !edit ) return;
00265 
00266   PhoneNumber::List::Iterator it = combo->selectedElement();
00267   if ( it != mPhoneList.end() ) {
00268     (*it).setNumber( edit->text() );
00269   } else {
00270     kdDebug(5720) << "PhoneEditWidget::updatePhoneNumber(): no selected element"
00271               << endl;
00272   }
00273 
00274   updateOtherEdit( combo, mPrefCombo );
00275   updateOtherEdit( combo, mSecondCombo );
00276   updateOtherEdit( combo, mThirdCombo );
00277   updateOtherEdit( combo, mFourthCombo );
00278 
00279   if ( !mReadOnly )
00280     emit modified();
00281 }
00282 
00283 void PhoneEditWidget::updateOtherEdit( PhoneTypeCombo *combo, PhoneTypeCombo *otherCombo )
00284 {
00285   if ( combo == otherCombo ) return;
00286 
00287   if ( combo->currentItem() == otherCombo->currentItem() ) {
00288     updateEdit( otherCombo );
00289   }
00290 }
00291 
00293 // PhoneEditDialog
00294 
00295 class PhoneViewItem : public QListViewItem
00296 {
00297 public:
00298   PhoneViewItem( QListView *parent, const KABC::PhoneNumber &number );
00299 
00300   void setPhoneNumber( const KABC::PhoneNumber &number )
00301   {
00302     mPhoneNumber = number;
00303     makeText();
00304   }
00305 
00306   QString key() { return mPhoneNumber.id(); }
00307   QString country() { return ""; }
00308   QString region() { return ""; }
00309   QString number() { return ""; }
00310 
00311   KABC::PhoneNumber phoneNumber() { return mPhoneNumber; }
00312 
00313 private:
00314   void makeText();
00315 
00316   KABC::PhoneNumber mPhoneNumber;
00317 };
00318 
00319 PhoneViewItem::PhoneViewItem( QListView *parent, const KABC::PhoneNumber &number )
00320   : QListViewItem( parent ), mPhoneNumber( number )
00321 {
00322   makeText();
00323 }
00324 
00325 void PhoneViewItem::makeText()
00326 {
00336   setText( 0, mPhoneNumber.number() );
00337   setText( 1, mPhoneNumber.typeLabel() );
00338 }
00339 
00340 PhoneEditDialog::PhoneEditDialog( const KABC::PhoneNumber::List &list, QWidget *parent, const char *name )
00341   : KDialogBase( KDialogBase::Plain, i18n( "Edit Phone Numbers" ),
00342                  KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok,
00343                  parent, name, true)
00344 {
00345   mPhoneNumberList = list;
00346 
00347   QWidget *page = plainPage();
00348 
00349   QGridLayout *layout = new QGridLayout( page, 1, 2 );
00350   layout->setSpacing( spacingHint() );
00351 
00352   mListView = new KListView( page );
00353   mListView->setAllColumnsShowFocus( true );
00354   mListView->addColumn( i18n( "Number" ) );
00355   mListView->addColumn( i18n( "Type" ) );
00356 
00357   KButtonBox *buttonBox = new KButtonBox( page, Vertical );
00358 
00359   buttonBox->addButton( i18n( "&Add..." ), this, SLOT( slotAddPhoneNumber() ) );
00360   mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, SLOT( slotEditPhoneNumber() ) );
00361   mEditButton->setEnabled( false );
00362   mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, SLOT( slotRemovePhoneNumber() ) );
00363   mRemoveButton->setEnabled( false );
00364   buttonBox->layout();
00365 
00366   layout->addWidget( mListView, 0, 0 );
00367   layout->addWidget( buttonBox, 0, 1 );
00368 
00369   connect( mListView, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged()) );
00370   connect( mListView, SIGNAL(doubleClicked( QListViewItem *, const QPoint &, int  )), this, SLOT( slotEditPhoneNumber()));
00371 
00372   KABC::PhoneNumber::List::Iterator it;
00373   for ( it = mPhoneNumberList.begin(); it != mPhoneNumberList.end(); ++it )
00374     new PhoneViewItem( mListView, *it );
00375 
00376   mChanged = false;
00377 }
00378 
00379 PhoneEditDialog::~PhoneEditDialog()
00380 {
00381 }
00382 
00383 void PhoneEditDialog::slotAddPhoneNumber()
00384 {
00385   KABC::PhoneNumber tmp( "", 0 );
00386   PhoneTypeDialog dlg( tmp, this );
00387 
00388   if ( dlg.exec() ) {
00389     KABC::PhoneNumber phoneNumber = dlg.phoneNumber();
00390     mPhoneNumberList.append( phoneNumber );
00391     new PhoneViewItem( mListView, phoneNumber );
00392 
00393     mChanged = true;
00394   }
00395 }
00396 
00397 void PhoneEditDialog::slotRemovePhoneNumber()
00398 {
00399   PhoneViewItem *item = static_cast<PhoneViewItem*>( mListView->currentItem() );
00400   if ( !item )
00401     return;
00402 
00403   mPhoneNumberList.remove( item->phoneNumber() );
00404   QListViewItem *currItem = mListView->currentItem();
00405   mListView->takeItem( currItem );
00406   delete currItem;
00407 
00408   mChanged = true;
00409 }
00410 
00411 void PhoneEditDialog::slotEditPhoneNumber()
00412 {
00413   PhoneViewItem *item = static_cast<PhoneViewItem*>( mListView->currentItem() );
00414   if ( !item )
00415     return;
00416 
00417   PhoneTypeDialog dlg( item->phoneNumber(), this );
00418 
00419   if ( dlg.exec() ) {
00420     slotRemovePhoneNumber();
00421     KABC::PhoneNumber phoneNumber = dlg.phoneNumber();
00422     mPhoneNumberList.append( phoneNumber );
00423     new PhoneViewItem( mListView, phoneNumber );
00424 
00425     mChanged = true;
00426   }
00427 }
00428 
00429 void PhoneEditDialog::slotSelectionChanged()
00430 {
00431   bool state = ( mListView->currentItem() != 0 );
00432 
00433   mRemoveButton->setEnabled( state );
00434   mEditButton->setEnabled( state );
00435 }
00436 
00437 const KABC::PhoneNumber::List &PhoneEditDialog::phoneNumbers()
00438 {
00439   return mPhoneNumberList;
00440 }
00441 
00442 bool PhoneEditDialog::changed() const
00443 {
00444   return mChanged;
00445 }
00446 
00448 // PhoneTypeDialog
00449 PhoneTypeDialog::PhoneTypeDialog( const KABC::PhoneNumber &phoneNumber,
00450                                QWidget *parent, const char *name)
00451   : KDialogBase( KDialogBase::Plain, i18n( "Edit Phone Number" ),
00452                 KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok,
00453                 parent, name, true), mPhoneNumber( phoneNumber )
00454 {
00455   QWidget *page = plainPage();
00456   QLabel *label = 0;
00457   QGridLayout *layout = new QGridLayout( page, 3, 2, marginHint(), spacingHint() );
00458 
00459   label = new QLabel( i18n( "Number:" ), page );
00460   layout->addWidget( label, 0, 0 );
00461   mNumber = new KLineEdit( page );
00462   layout->addWidget( mNumber, 0, 1 );
00463 
00464   mPreferredBox = new QCheckBox( i18n( "This is the preferred phone number" ), page );
00465   layout->addMultiCellWidget( mPreferredBox, 1, 1, 0, 1 );
00466 
00467   mGroup = new QButtonGroup( 2, Horizontal, i18n( "Types" ), page );
00468   layout->addMultiCellWidget( mGroup, 2, 2, 0, 1 );
00469 
00470   // fill widgets
00471   mNumber->setText( mPhoneNumber.number() );
00472 
00473   mTypeList = KABC::PhoneNumber::typeList();
00474   mTypeList.remove( KABC::PhoneNumber::Pref );
00475 
00476   KABC::PhoneNumber::TypeList::Iterator it;
00477   for ( it = mTypeList.begin(); it != mTypeList.end(); ++it )
00478     new QCheckBox( KABC::PhoneNumber::typeLabel( *it ), mGroup );
00479 
00480   for ( int i = 0; i < mGroup->count(); ++i ) {
00481     int type = mPhoneNumber.type();
00482     QCheckBox *box = (QCheckBox*)mGroup->find( i );
00483     box->setChecked( type & mTypeList[ i ] );
00484   }
00485 
00486   mPreferredBox->setChecked( mPhoneNumber.type() & KABC::PhoneNumber::Pref );
00487 }
00488 
00489 KABC::PhoneNumber PhoneTypeDialog::phoneNumber()
00490 {
00491   mPhoneNumber.setNumber( mNumber->text() );
00492 
00493   int type = 0;
00494   for ( int i = 0; i < mGroup->count(); ++i ) {
00495     QCheckBox *box = (QCheckBox*)mGroup->find( i );
00496     if ( box->isChecked() )
00497       type += mTypeList[ i ];
00498   }
00499 
00500   if ( mPreferredBox->isChecked() )
00501     mPhoneNumber.setType( type | KABC::PhoneNumber::Pref );
00502   else
00503     mPhoneNumber.setType( type & ~KABC::PhoneNumber::Pref );
00504 
00505   return mPhoneNumber;
00506 }
00507 
00508 
00509 #include "phoneeditwidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Apr 4 04:47:51 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003