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 TQt, and distribute the resulting executable,
00021     without including the source code for TQt in the source distribution.
00022 */
00023 
00024 #include <tqbuttongroup.h>
00025 #include <tqcheckbox.h>
00026 #include <tqlabel.h>
00027 #include <tqlayout.h>
00028 #include <tqlistbox.h>
00029 #include <tqlistview.h>
00030 #include <tqpushbutton.h>
00031 #include <tqsignalmapper.h>
00032 #include <tqstring.h>
00033 #include <tqtooltip.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( TQWidget *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, TQT_SIGNAL( activated( int ) ),
00060            this, TQT_SLOT( selected( int ) ) );
00061   connect( this, TQT_SIGNAL( activated( int ) ),
00062            this, TQT_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   TQValueList<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       KABC::PhoneNumber number;
00095       number.setType( *it );
00096       insertItem( number.typeLabel() );
00097     }
00098   }
00099 
00100   setCurrentItem( mLastSelected = mTypeList.findIndex( mType ) );
00101 
00102   blockSignals( blocked );
00103 }
00104 
00105 void PhoneTypeCombo::selected( int pos )
00106 {
00107   if ( mTypeList[ pos ] == -1 )
00108     otherSelected();
00109   else {
00110     mType = mTypeList[ pos ];
00111     mLastSelected = pos;
00112   }
00113 }
00114 
00115 void PhoneTypeCombo::otherSelected()
00116 {
00117   PhoneTypeDialog dlg( mType, this );
00118   if ( dlg.exec() ) {
00119     mType = dlg.type();
00120     if ( !mTypeList.contains( mType ) )
00121       mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), mType );
00122   } else {
00123     setType( mTypeList[ mLastSelected ] );
00124   }
00125 
00126   update();
00127 }
00128 
00129 PhoneNumberWidget::PhoneNumberWidget( TQWidget *parent )
00130   : TQWidget( parent )
00131 {
00132   TQHBoxLayout *layout = new TQHBoxLayout( this, 6, 11 );
00133 
00134   mTypeCombo = new PhoneTypeCombo( this );
00135   mNumberEdit = new KLineEdit( this );
00136 
00137   layout->addWidget( mTypeCombo );
00138   layout->addWidget( mNumberEdit );
00139 
00140   connect( mTypeCombo, TQT_SIGNAL( modified() ), TQT_SIGNAL( modified() ) );
00141   connect( mNumberEdit, TQT_SIGNAL( textChanged( const TQString& ) ), TQT_SIGNAL( modified() ) );
00142 }
00143 
00144 void PhoneNumberWidget::setNumber( const KABC::PhoneNumber &number )
00145 {
00146   mNumber = number;
00147 
00148   mTypeCombo->setType( number.type() );
00149   mNumberEdit->setText( number.number() );
00150 }
00151 
00152 KABC::PhoneNumber PhoneNumberWidget::number() const
00153 {
00154   KABC::PhoneNumber number( mNumber );
00155 
00156   number.setType( mTypeCombo->type() );
00157   number.setNumber( mNumberEdit->text() );
00158 
00159   return number;
00160 }
00161 
00162 void PhoneNumberWidget::setReadOnly( bool readOnly )
00163 {
00164   mTypeCombo->setEnabled( !readOnly );
00165   mNumberEdit->setReadOnly( readOnly );
00166 }
00167 
00168 
00169 PhoneEditWidget::PhoneEditWidget( TQWidget *parent, const char *name )
00170   : TQWidget( parent, name ), mReadOnly( false )
00171 {
00172   TQGridLayout *layout = new TQGridLayout( this, 2, 2 );
00173   layout->setSpacing( KDialog::spacingHint() );
00174 
00175   mWidgetLayout = new TQVBoxLayout( layout );
00176   layout->addMultiCellLayout( mWidgetLayout, 0, 0, 0, 1 );
00177 
00178   mAddButton = new TQPushButton( i18n( "Add" ), this );
00179   mAddButton->setMaximumSize( mAddButton->sizeHint() );
00180   layout->addWidget( mAddButton, 1, 0, TQt::AlignRight );
00181 
00182   mRemoveButton = new TQPushButton( i18n( "Remove" ), this );
00183   mRemoveButton->setMaximumSize( mRemoveButton->sizeHint() );
00184   layout->addWidget( mRemoveButton, 1, 1 );
00185 
00186   mMapper = new TQSignalMapper( TQT_TQOBJECT(this) );
00187   connect( mMapper, TQT_SIGNAL( mapped( int ) ), TQT_SLOT( changed( int ) ) );
00188 
00189   connect( mAddButton, TQT_SIGNAL( clicked() ), TQT_SLOT( add() ) );
00190   connect( mRemoveButton, TQT_SIGNAL( clicked() ), TQT_SLOT( remove() ) );
00191 }
00192 
00193 PhoneEditWidget::~PhoneEditWidget()
00194 {
00195 }
00196 
00197 void PhoneEditWidget::setReadOnly( bool readOnly )
00198 {
00199   mReadOnly = readOnly;
00200   mAddButton->setEnabled( !readOnly );
00201   mRemoveButton->setEnabled( !readOnly && mPhoneNumberList.count() > 3 );
00202 
00203   TQPtrListIterator<PhoneNumberWidget> it( mWidgets );
00204   while ( it.current() ) {
00205     it.current()->setReadOnly( readOnly );
00206     ++it;
00207   }
00208 }
00209 
00210 void PhoneEditWidget::setPhoneNumbers( const KABC::PhoneNumber::List &list )
00211 {
00212   mPhoneNumberList = list;
00213 
00214   KABC::PhoneNumber::TypeList types;
00215   types << KABC::PhoneNumber::Home;
00216   types << KABC::PhoneNumber::Work;
00217   types << KABC::PhoneNumber::Cell;
00218 
00219   // add an empty entry per default
00220   if ( mPhoneNumberList.count() < 3 )
00221     for ( int i = mPhoneNumberList.count(); i < 3; ++i )
00222       mPhoneNumberList.append( KABC::PhoneNumber( "", types[ i ] ) );
00223 
00224   recreateNumberWidgets();
00225 }
00226 
00227 KABC::PhoneNumber::List PhoneEditWidget::phoneNumbers() const
00228 {
00229   KABC::PhoneNumber::List list;
00230 
00231   KABC::PhoneNumber::List::ConstIterator it;
00232   for ( it = mPhoneNumberList.begin(); it != mPhoneNumberList.end(); ++it )
00233     if ( !(*it).number().isEmpty() )
00234       list.append( *it );
00235 
00236   return list;
00237 }
00238 
00239 void PhoneEditWidget::changed()
00240 {
00241   if ( !mReadOnly )
00242     emit modified();
00243 }
00244 
00245 void PhoneEditWidget::add()
00246 {
00247   mPhoneNumberList.append( KABC::PhoneNumber() );
00248 
00249   recreateNumberWidgets();
00250 }
00251 
00252 void PhoneEditWidget::remove()
00253 {
00254   mPhoneNumberList.remove( mPhoneNumberList.last() );
00255   changed();
00256 
00257   recreateNumberWidgets();
00258 }
00259 
00260 void PhoneEditWidget::recreateNumberWidgets()
00261 {
00262   for ( TQWidget *w = mWidgets.first(); w; w = mWidgets.next() ) {
00263     mWidgetLayout->remove( w );
00264     w->deleteLater();
00265   }
00266   mWidgets.clear();
00267 
00268   KABC::PhoneNumber::List::ConstIterator it;
00269   int counter = 0;
00270   for ( it = mPhoneNumberList.begin(); it != mPhoneNumberList.end(); ++it ) {
00271     PhoneNumberWidget *wdg = new PhoneNumberWidget( this );
00272     wdg->setNumber( *it );
00273 
00274     mMapper->setMapping( TQT_TQOBJECT(wdg), counter );
00275     connect( wdg, TQT_SIGNAL( modified() ), mMapper, TQT_SLOT( map() ) );
00276 
00277     mWidgetLayout->addWidget( wdg );
00278     mWidgets.append( wdg );
00279     wdg->show();
00280 
00281     ++counter;
00282   }
00283   setReadOnly(mReadOnly);
00284 }
00285 
00286 void PhoneEditWidget::changed( int pos )
00287 {
00288   mPhoneNumberList[ pos ] = mWidgets.at( pos )->number();
00289   changed();
00290 }
00291 
00293 // PhoneTypeDialog
00294 PhoneTypeDialog::PhoneTypeDialog( int type, TQWidget *parent )
00295   : KDialogBase( Plain, i18n( "Edit Phone Number" ), Ok | Cancel, Ok,
00296                  parent, "PhoneTypeDialog", true ),
00297     mType( type )
00298 {
00299   TQWidget *page = plainPage();
00300 
00301   TQVBoxLayout *layout = new TQVBoxLayout( page, spacingHint() );
00302 
00303   mPreferredBox = new TQCheckBox( i18n( "This is the preferred phone number" ), page );
00304   layout->addWidget( mPreferredBox );
00305 
00306   mGroup = new TQButtonGroup( 2, Qt::Horizontal, i18n( "Types" ), page );
00307   layout->addWidget( mGroup );
00308 
00309   // fill widgets
00310   mTypeList = KABC::PhoneNumber::typeList();
00311   mTypeList.remove( KABC::PhoneNumber::Pref );
00312 
00313   KABC::PhoneNumber::TypeList::ConstIterator it;
00314   for ( it = mTypeList.begin(); it != mTypeList.end(); ++it )
00315     new TQCheckBox( KABC::PhoneNumber::typeLabel( *it ), mGroup );
00316 
00317   for ( int i = 0; i < mGroup->count(); ++i ) {
00318     TQCheckBox *box = (TQCheckBox*)mGroup->find( i );
00319     box->setChecked( mType & mTypeList[ i ] );
00320   }
00321 
00322   mPreferredBox->setChecked( mType & KABC::PhoneNumber::Pref );
00323 }
00324 
00325 int PhoneTypeDialog::type() const
00326 {
00327   int type = 0;
00328 
00329   for ( int i = 0; i < mGroup->count(); ++i ) {
00330     TQCheckBox *box = (TQCheckBox*)mGroup->find( i );
00331     if ( box->isChecked() )
00332       type += mTypeList[ i ];
00333   }
00334 
00335   if ( mPreferredBox->isChecked() )
00336     type = type | KABC::PhoneNumber::Pref;
00337   else
00338     type = type & ~KABC::PhoneNumber::Pref;
00339 
00340   return type;
00341 }
00342 
00343 
00344 #include "phoneeditwidget.moc"