kaddressbook Library API Documentation

nameeditdialog.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 <qlistbox.h>
00027 #include <qlistview.h>
00028 #include <qtooltip.h>
00029 #include <qpushbutton.h>
00030 #include <qcheckbox.h>
00031 #include <qstring.h>
00032 
00033 #include <kaccelmanager.h>
00034 #include <kapplication.h>
00035 #include <kbuttonbox.h>
00036 #include <kconfig.h>
00037 #include <klineedit.h>
00038 #include <klistview.h>
00039 #include <kcombobox.h>
00040 #include <klocale.h>
00041 #include <kdebug.h>
00042 #include <kiconloader.h>
00043 #include <kmessagebox.h>
00044 
00045 #include "nameeditdialog.h"
00046 
00047 NameEditDialog::NameEditDialog( const KABC::Addressee &addr, int type,
00048                                 bool readOnly, QWidget *parent, const char *name )
00049   : KDialogBase( Plain, i18n( "Edit Contact Name" ), Help | Ok | Cancel,
00050                  Ok, parent, name, true )
00051 {
00052   QWidget *page = plainPage();
00053   QGridLayout *layout = new QGridLayout( page );
00054   layout->setSpacing( spacingHint() );
00055   layout->addColSpacing( 2, 100 );
00056   QLabel *label;
00057 
00058   label = new QLabel( i18n( "Honorific prefixes:" ), page );
00059   layout->addWidget( label, 0, 0 );
00060   mPrefixCombo = new KComboBox( page );
00061   mPrefixCombo->setDuplicatesEnabled( false );
00062   mPrefixCombo->setEditable( true );
00063   mPrefixCombo->setEnabled( !readOnly );
00064   label->setBuddy( mPrefixCombo );
00065   layout->addMultiCellWidget( mPrefixCombo, 0, 0, 1, 2 );
00066 
00067   label = new QLabel( i18n( "Given name:" ), page );
00068   layout->addWidget( label, 1, 0 );
00069   mGivenNameEdit = new KLineEdit( page );
00070   mGivenNameEdit->setReadOnly( readOnly );
00071   label->setBuddy( mGivenNameEdit );
00072   layout->addMultiCellWidget( mGivenNameEdit, 1, 1, 1, 2 );
00073 
00074   label = new QLabel( i18n( "Additional names:" ), page );
00075   layout->addWidget( label, 2, 0 );
00076   mAdditionalNameEdit = new KLineEdit( page );
00077   mAdditionalNameEdit->setReadOnly( readOnly );
00078   label->setBuddy( mAdditionalNameEdit );
00079   layout->addMultiCellWidget( mAdditionalNameEdit, 2, 2, 1, 2 );
00080 
00081   label = new QLabel( i18n( "Family names:" ), page );
00082   layout->addWidget( label, 3, 0 );
00083   mFamilyNameEdit = new KLineEdit( page );
00084   mFamilyNameEdit->setReadOnly( readOnly );
00085   label->setBuddy( mFamilyNameEdit );
00086   layout->addMultiCellWidget( mFamilyNameEdit, 3, 3, 1, 2 );
00087 
00088   label = new QLabel( i18n( "Honorific suffixes:" ), page );
00089   layout->addWidget( label, 4, 0 );
00090   mSuffixCombo = new KComboBox( page );
00091   mSuffixCombo->setDuplicatesEnabled( false );
00092   mSuffixCombo->setEditable( true );
00093   mSuffixCombo->setEnabled( !readOnly );
00094   label->setBuddy( mSuffixCombo );
00095   layout->addMultiCellWidget( mSuffixCombo, 4, 4, 1, 2 );
00096 
00097   mFormattedNameCombo = new KComboBox( page );
00098   mFormattedNameCombo->setEnabled( !readOnly );
00099   layout->addWidget( mFormattedNameCombo, 5, 0 );
00100   connect( mFormattedNameCombo, SIGNAL( activated( int ) ), SLOT( typeChanged( int ) ) );
00101 
00102   mFormattedNameEdit = new KLineEdit( page );
00103   mFormattedNameEdit->setEnabled( type == CustomName && !readOnly );
00104   layout->addWidget( mFormattedNameEdit, 5, 1 );
00105 
00106   mParseBox = new QCheckBox( i18n( "Parse name automatically" ), page );
00107   mParseBox->setEnabled( !readOnly );
00108   connect( mParseBox, SIGNAL( toggled(bool) ), SLOT( parseBoxChanged(bool) ) );
00109   connect( mParseBox, SIGNAL( toggled(bool) ), SLOT( modified() ) );
00110   layout->addMultiCellWidget( mParseBox, 6, 6, 0, 1 );
00111 
00112   // Fill in the values
00113   mFamilyNameEdit->setText( addr.familyName() );
00114   mGivenNameEdit->setText( addr.givenName() );
00115   mAdditionalNameEdit->setText( addr.additionalName() );
00116   mFormattedNameEdit->setText( addr.formattedName() );
00117 
00118   // Prefix and suffix combos
00119   KConfig config( "kabcrc" );
00120   config.setGroup( "General" );
00121 
00122   QStringList sTitle;
00123   sTitle += i18n( "Dr." );
00124   sTitle += i18n( "Miss" );
00125   sTitle += i18n( "Mr." );
00126   sTitle += i18n( "Mrs." );
00127   sTitle += i18n( "Ms." );
00128   sTitle += i18n( "Prof." );
00129   sTitle += config.readListEntry( "Prefixes" );
00130   sTitle.sort();
00131 
00132   QStringList sSuffix;
00133   sSuffix += i18n( "I" );
00134   sSuffix += i18n( "II" );
00135   sSuffix += i18n( "III" );
00136   sSuffix += i18n( "Jr." );
00137   sSuffix += i18n( "Sr." );
00138   sSuffix += config.readListEntry( "Suffixes" );
00139   sSuffix.sort();
00140 
00141   mPrefixCombo->insertStringList( sTitle );
00142   mSuffixCombo->insertStringList( sSuffix );
00143 
00144   mPrefixCombo->setCurrentText( addr.prefix() );
00145   mSuffixCombo->setCurrentText( addr.suffix() );
00146 
00147   mAddresseeConfig.setAddressee( addr );
00148   mParseBox->setChecked( mAddresseeConfig.automaticNameParsing() );
00149 
00150   KAcceleratorManager::manage( this );
00151 
00152   connect( mPrefixCombo, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00153   connect( mPrefixCombo, SIGNAL( textChanged( const QString& ) ), SLOT( updateTypeCombo() ) );
00154   connect( mGivenNameEdit, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00155   connect( mGivenNameEdit, SIGNAL( textChanged( const QString& ) ), SLOT( updateTypeCombo() ) );
00156   connect( mAdditionalNameEdit, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00157   connect( mAdditionalNameEdit, SIGNAL( textChanged( const QString& ) ), SLOT( updateTypeCombo() ) );
00158   connect( mFamilyNameEdit, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00159   connect( mFamilyNameEdit, SIGNAL( textChanged( const QString& ) ), SLOT( updateTypeCombo() ) );
00160   connect( mSuffixCombo, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00161   connect( mSuffixCombo, SIGNAL( textChanged( const QString& ) ), SLOT( updateTypeCombo() ) );
00162   connect( mFormattedNameCombo, SIGNAL( activated( int ) ), SLOT( modified() ) );
00163   connect( mFormattedNameEdit, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00164 
00165   updateTypeCombo();
00166   mFormattedNameCombo->setCurrentItem( type );
00167   mPrefixCombo->lineEdit()->setFocus();
00168   mChanged = false;
00169 }
00170 
00171 NameEditDialog::~NameEditDialog()
00172 {
00173 }
00174 
00175 QString NameEditDialog::familyName() const
00176 {
00177   return mFamilyNameEdit->text();
00178 }
00179 
00180 QString NameEditDialog::givenName() const
00181 {
00182   return mGivenNameEdit->text();
00183 }
00184 
00185 QString NameEditDialog::prefix() const
00186 {
00187   return mPrefixCombo->currentText();
00188 }
00189 
00190 QString NameEditDialog::suffix() const
00191 {
00192   return mSuffixCombo->currentText();
00193 }
00194 
00195 QString NameEditDialog::additionalName() const
00196 {
00197   return mAdditionalNameEdit->text();
00198 }
00199 
00200 QString NameEditDialog::customFormattedName() const
00201 {
00202   return mFormattedNameEdit->text();
00203 }
00204 
00205 int NameEditDialog::formattedNameType() const
00206 {
00207   return mFormattedNameCombo->currentItem();
00208 }
00209 
00210 bool NameEditDialog::changed() const
00211 {
00212   return mChanged;
00213 }
00214 
00215 QString NameEditDialog::formattedName( const KABC::Addressee &addr, int type )
00216 {
00217   QString name;
00218 
00219   switch ( type ) {
00220     case SimpleName:
00221       name = addr.givenName() + " " + addr.familyName();
00222       break;
00223     case FullName:
00224       name = addr.prefix() + " " + addr.givenName() + " " +
00225              addr.additionalName() + " " + addr.familyName() + " " +
00226              addr.suffix();
00227       break;
00228     case ReverseName:
00229       name = addr.familyName() + ", " + addr.givenName();
00230       break;
00231     default:
00232       name = "";
00233       break;
00234   }
00235 
00236   return name.simplifyWhiteSpace();
00237 }
00238 
00239 void NameEditDialog::parseBoxChanged( bool value )
00240 {
00241   mAddresseeConfig.setAutomaticNameParsing( value );
00242 }
00243 
00244 void NameEditDialog::typeChanged( int pos )
00245 {
00246   mFormattedNameEdit->setEnabled( pos == 0 );
00247 }
00248 
00249 void NameEditDialog::modified()
00250 {
00251   mChanged = true;
00252 }
00253 
00254 void NameEditDialog::updateTypeCombo()
00255 {
00256   KABC::Addressee addr;
00257   addr.setPrefix( mPrefixCombo->currentText() );
00258   addr.setGivenName( mGivenNameEdit->text() );
00259   addr.setAdditionalName( mAdditionalNameEdit->text() );
00260   addr.setFamilyName( mFamilyNameEdit->text() );
00261   addr.setSuffix( mSuffixCombo->currentText() );
00262 
00263   int pos = mFormattedNameCombo->currentItem();
00264 
00265   mFormattedNameCombo->clear();
00266   mFormattedNameCombo->insertItem( i18n( "Custom" ) );
00267   mFormattedNameCombo->insertItem( formattedName( addr, SimpleName ) );
00268   mFormattedNameCombo->insertItem( formattedName( addr, FullName ) );
00269   mFormattedNameCombo->insertItem( formattedName( addr, ReverseName ) );
00270 
00271   mFormattedNameCombo->setCurrentItem( pos );
00272 }
00273 
00274 void NameEditDialog::slotHelp()
00275 {
00276   kapp->invokeHelp( "managing-contacts-automatic-nameparsing" );
00277 }
00278 
00279 #include "nameeditdialog.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:52 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003