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