kaddressbook Library API Documentation

addresseditwidget.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 
00025 #include <qbuttongroup.h>
00026 #include <qlayout.h>
00027 #include <qlabel.h>
00028 #include <qlistbox.h>
00029 #include <qlistview.h>
00030 #include <qtoolbutton.h>
00031 #include <qtooltip.h>
00032 #include <qtextedit.h>
00033 #include <qpushbutton.h>
00034 #include <qcheckbox.h>
00035 #include <qsignal.h>
00036 #include <qstring.h>
00037 #include <qhbox.h>
00038 
00039 #include <kaccelmanager.h>
00040 #include <kapplication.h>
00041 #include <kbuttonbox.h>
00042 #include <kconfig.h>
00043 #include <klineedit.h>
00044 #include <klistview.h>
00045 #include <kcombobox.h>
00046 #include <klocale.h>
00047 #include <kdebug.h>
00048 #include <kiconloader.h>
00049 #include <kmessagebox.h>
00050 #include <kdialog.h>
00051 #include <kseparator.h>
00052 
00053 #include "addresseditwidget.h"
00054 
00055 
00056 AddressEditWidget::AddressEditWidget( QWidget *parent, const char *name )
00057   : QWidget( parent, name )
00058 {
00059   QBoxLayout *layout = new QVBoxLayout( this, 4, 2 );
00060   layout->setSpacing( KDialog::spacingHint() );
00061 
00062   mTypeCombo = new AddressTypeCombo( mAddressList, this );
00063   connect( mTypeCombo, SIGNAL( activated( int ) ),
00064            SLOT( updateAddressEdit() ) );
00065   layout->addWidget( mTypeCombo );
00066 
00067   mAddressTextEdit = new QTextEdit( this );
00068   mAddressTextEdit->setReadOnly( true );
00069   mAddressTextEdit->setMinimumHeight( 20 );
00070   layout->addWidget( mAddressTextEdit );
00071 
00072   QPushButton *editButton = new QPushButton( i18n( "&Edit Addresses..." ),
00073                                              this );
00074   connect( editButton, SIGNAL( clicked() ), SLOT( edit() ) );
00075   layout->addWidget( editButton );
00076 }
00077 
00078 AddressEditWidget::~AddressEditWidget()
00079 {
00080 }
00081 
00082 KABC::Address::List AddressEditWidget::addresses()
00083 {
00084   KABC::Address::List retList;
00085 
00086   KABC::Address::List::Iterator it;
00087   for ( it = mAddressList.begin(); it != mAddressList.end(); ++it )
00088     if ( !(*it).isEmpty() )
00089       retList.append( *it );
00090 
00091   return retList;
00092 }
00093 
00094 void AddressEditWidget::setAddresses(const KABC::Address::List &list)
00095 {
00096   mAddressList.clear();
00097 
00098   // Insert types for existing numbers.
00099   mTypeCombo->insertTypeList( list );
00100 
00101   QValueList<int> defaultTypes;
00102   defaultTypes << KABC::Address::Home;
00103   defaultTypes << KABC::Address::Work;
00104 
00105   // Insert default types.
00106   // Doing this for mPrefCombo is enough because the list is shared by all
00107   // combos.
00108   QValueList<int>::ConstIterator it;
00109   for( it = defaultTypes.begin(); it != defaultTypes.end(); ++it ) {
00110     if ( !mTypeCombo->hasType( *it ) )
00111       mTypeCombo->insertType( list, *it, Address( *it ) );
00112   }
00113 
00114   mTypeCombo->updateTypes();
00115 
00116   // find preferred address which will be shown
00117   int preferred = KABC::Address::Home;  // default if no preferred address set
00118   uint i;
00119   for (i = 0; i < list.count(); i++)
00120     if ( list[i].type() & KABC::Address::Pref ) {
00121       preferred = list[i].type();
00122       break;
00123     }
00124 
00125   mTypeCombo->selectType( preferred );
00126 
00127   updateAddressEdit();
00128 }
00129 
00130 void AddressEditWidget::edit()
00131 {
00132   AddressEditDialog dialog( mAddressList, mTypeCombo->currentItem(), this );
00133   if ( dialog.exec() ) {
00134     if ( dialog.changed() ) {
00135       mAddressList = dialog.addresses();
00136       mTypeCombo->updateTypes();
00137       updateAddressEdit();
00138       emit modified();
00139     }
00140   }
00141 }
00142 
00143 void AddressEditWidget::updateAddressEdit()
00144 {
00145   KABC::Address::List::Iterator it = mTypeCombo->selectedElement();
00146 
00147   bool block = signalsBlocked();
00148   blockSignals( true );
00149 
00150   mAddressTextEdit->setText( "" );
00151 
00152   if ( it != mAddressList.end() ) {
00153     KABC::Address a = *it;
00154 
00155     if ( !a.isEmpty() ) {
00156       QString text;
00157       if ( !a.street().isEmpty() )
00158         text += a.street() + "\n";
00159 
00160       if ( !a.postOfficeBox().isEmpty() )
00161         text += a.postOfficeBox() + "\n";
00162 
00163       text += a.locality() + QString(" ") + a.region();
00164 
00165       if ( !a.postalCode().isEmpty() )
00166         text += QString(", ") + a.postalCode();
00167 
00168       text += "\n";
00169 
00170       if ( !a.country().isEmpty() )
00171         text += a.country() + "\n";
00172 
00173       text += a.extended();
00174 
00175       mAddressTextEdit->setText(text);
00176     }
00177   }
00178 
00179   blockSignals( block );
00180 }
00181 
00182 
00183 AddressEditDialog::AddressEditDialog( const KABC::Address::List &list,
00184                                       int selected, QWidget *parent,
00185                                       const char *name )
00186   : KDialogBase( KDialogBase::Plain, i18n( "Edit Address" ),
00187                  KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok,
00188                  parent, name, true, true )
00189 {
00190   mAddressList = list;
00191 
00192   QWidget *page = plainPage();
00193   
00194   QGridLayout *topLayout = new QGridLayout(page, 8, 2);
00195   topLayout->setSpacing(spacingHint());
00196 
00197   mTypeCombo = new AddressTypeCombo( mAddressList, page );
00198   topLayout->addMultiCellWidget( mTypeCombo, 0, 0, 0, 1 );
00199 
00200   QLabel *label = new QLabel(i18n("Street:"), page);
00201   label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
00202   topLayout->addWidget(label, 1, 0);
00203   mStreetTextEdit = new QTextEdit(page, "mStreetTextEdit");
00204   label->setBuddy( mStreetTextEdit );
00205   topLayout->addWidget(mStreetTextEdit, 1, 1);
00206 
00207   label = new QLabel(i18n("Post office box:"), page);
00208   topLayout->addWidget(label, 2 , 0);
00209   mPOBoxEdit = new KLineEdit(page, "mPOBoxEdit");
00210   label->setBuddy( mPOBoxEdit );
00211   topLayout->addWidget(mPOBoxEdit, 2, 1);
00212 
00213   label = new QLabel(i18n("Locality:"), page);
00214   topLayout->addWidget(label, 3, 0);
00215   mLocalityEdit = new KLineEdit(page, "mLocalityEdit");
00216   label->setBuddy( mLocalityEdit );
00217   topLayout->addWidget(mLocalityEdit, 3, 1);
00218 
00219   label = new QLabel(i18n("Region:"), page);
00220   topLayout->addWidget(label, 4, 0);
00221   mRegionEdit = new KLineEdit(page, "mRegionEdit");
00222   label->setBuddy( mRegionEdit );
00223   topLayout->addWidget(mRegionEdit, 4, 1);
00224 
00225   label = new QLabel(i18n("Postal code:"), page);
00226   topLayout->addWidget(label, 5, 0);
00227   mPostalCodeEdit = new KLineEdit(page, "mPostalCodeEdit");
00228   label->setBuddy( mPostalCodeEdit );
00229   topLayout->addWidget(mPostalCodeEdit, 5, 1);
00230 
00231   label = new QLabel(i18n("Country:"), page);
00232   topLayout->addWidget(label, 6, 0);
00233   mCountryCombo = new KComboBox( true, page, "mCountryCombo" );
00234   mCountryCombo->setDuplicatesEnabled(false);
00235   mCountryCombo->setAutoCompletion(true);
00236   fillCountryCombo( mCountryCombo );
00237   label->setBuddy( mCountryCombo );
00238   topLayout->addWidget(mCountryCombo, 6, 1);
00239   
00240   mPreferredCheckBox = new QCheckBox( i18n( "This is the preferred address" ), page );
00241   topLayout->addMultiCellWidget( mPreferredCheckBox, 7, 7, 0, 1 );
00242 
00243   KSeparator *sep = new KSeparator( KSeparator::HLine, page );
00244   topLayout->addMultiCellWidget( sep, 8, 8, 0, 1 );
00245 
00246   QHBox *buttonBox = new QHBox( page );
00247   buttonBox->setSpacing( spacingHint() );
00248   topLayout->addMultiCellWidget( buttonBox, 9, 9, 0, 1 );
00249 
00250   QPushButton *addButton = new QPushButton( i18n( "&Add..." ), buttonBox );
00251   connect( addButton, SIGNAL( clicked() ), SLOT( addAddress() ) );
00252 
00253   removeButton = new QPushButton( i18n( "&Remove" ), buttonBox );
00254   connect( removeButton, SIGNAL( clicked() ), SLOT( removeAddress() ) );
00255 
00256   mTypeCombo->updateTypes();
00257   mTypeCombo->setCurrentItem( selected );
00258 
00259   updateAddressEdits();
00260 
00261   connect( mTypeCombo, SIGNAL( activated( int ) ),
00262            SLOT( updateAddressEdits() ) );
00263   connect( mStreetTextEdit, SIGNAL( textChanged() ), SLOT( modified() ) );
00264   connect( mPOBoxEdit, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00265   connect( mLocalityEdit, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00266   connect( mRegionEdit, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00267   connect( mPostalCodeEdit, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00268   connect( mCountryCombo, SIGNAL( textChanged( const QString& ) ), SLOT( modified() ) );
00269   connect( mPreferredCheckBox, SIGNAL( toggled( bool ) ), SLOT( modified() ) );
00270   connect( removeButton, SIGNAL( clicked() ), SLOT( modified() ) );
00271 
00272   KAcceleratorManager::manage( this );
00273 
00274   mChanged = false;
00275   removeButton->setEnabled( mAddressList.count() > 1 );
00276 }
00277 
00278 AddressEditDialog::~AddressEditDialog()
00279 {
00280 }
00281 
00282 void AddressEditDialog::updateAddressEdits()
00283 {
00284   KABC::Address::List::Iterator it = mTypeCombo->selectedElement();
00285   KABC::Address a = *it;
00286 
00287   bool tmp = mChanged;
00288 
00289   mStreetTextEdit->setText( a.street() );
00290   mRegionEdit->setText( a.region() );
00291   mLocalityEdit->setText( a.locality() );
00292   mPostalCodeEdit->setText( a.postalCode() );
00293   mPOBoxEdit->setText( a.postOfficeBox() );
00294   mCountryCombo->setCurrentText( a.country() );
00295 
00296   mPreferredCheckBox->setChecked( a.type() & KABC::Address::Pref );
00297 
00298   mStreetTextEdit->setFocus();
00299 
00300   mChanged = tmp;
00301 }
00302 
00303 KABC::Address::List AddressEditDialog::addresses()
00304 {
00305   saveAddress();
00306 
00307   return mAddressList;
00308 }
00309 
00310 void AddressEditDialog::saveAddress()
00311 {
00312   KABC::Address::List::Iterator a = mTypeCombo->selectedElement();
00313 
00314   (*a).setLocality( mLocalityEdit->text() );
00315   (*a).setRegion( mRegionEdit->text() );
00316   (*a).setPostalCode( mPostalCodeEdit->text() );
00317   (*a).setCountry( mCountryCombo->currentText() );
00318   (*a).setPostOfficeBox( mPOBoxEdit->text() );
00319   (*a).setStreet( mStreetTextEdit->text() );
00320 
00321   if ( mPreferredCheckBox->isChecked() )
00322     (*a).setType( (*a).type() | KABC::Address::Pref );
00323   else
00324     (*a).setType( (*a).type() & ~KABC::Address::Pref );
00325 }
00326 
00327 void AddressEditDialog::addAddress()
00328 {
00329   AddressTypeDialog dlg( mTypeCombo->selectedType(), this );
00330   if ( dlg.exec() ) {
00331     mAddressList.append( Address( dlg.type() ) );
00332 
00333     mTypeCombo->updateTypes();
00334     mTypeCombo->setCurrentItem( mTypeCombo->count() - 1 );
00335     updateAddressEdits();
00336 
00337     modified();
00338   }
00339   removeButton->setEnabled( true );
00340 }
00341 
00342 void AddressEditDialog::removeAddress()
00343 {
00344     if ( mAddressList.count()>1 )
00345     {
00346         mAddressList.remove( mTypeCombo->selectedElement() );
00347         mTypeCombo->updateTypes();
00348         updateAddressEdits();
00349     }
00350     removeButton->setEnabled( mAddressList.count()>1 );
00351 }
00352 
00353 void AddressEditDialog::fillCountryCombo(KComboBox *combo)
00354 {
00355   QString sCountry[] = {
00356     i18n( "Afghanistan" ), i18n( "Albania" ), i18n( "Algeria" ),
00357     i18n( "American Samoa" ), i18n( "Andorra" ), i18n( "Angola" ),
00358     i18n( "Anguilla" ), i18n( "Antarctica" ), i18n( "Antigua and Barbuda" ),
00359     i18n( "Argentina" ), i18n( "Armenia" ), i18n( "Aruba" ),
00360     i18n( "Ashmore and Cartier Islands" ), i18n( "Australia" ),
00361     i18n( "Austria" ), i18n( "Azerbaijan" ), i18n( "Bahamas" ),
00362     i18n( "Bahrain" ), i18n( "Bangladesh" ), i18n( "Barbados" ),
00363     i18n( "Belarus" ), i18n( "Belgium" ), i18n( "Belize" ),
00364     i18n( "Benin" ), i18n( "Bermuda" ), i18n( "Bhutan" ),
00365     i18n( "Bolivia" ), i18n( "Bosnia and Herzegovina" ), i18n( "Botswana" ),
00366     i18n( "Brazil" ), i18n( "Brunei" ), i18n( "Bulgaria" ),
00367     i18n( "Burkina Faso" ), i18n( "Burundi" ), i18n( "Cambodia" ),
00368     i18n( "Cameroon" ), i18n( "Canada" ), i18n( "Cape Verde" ),
00369     i18n( "Cayman Islands" ), i18n( "Central African Republic" ),
00370     i18n( "Chad" ), i18n( "Chile" ), i18n( "China" ), i18n( "Colombia" ),
00371     i18n( "Comoros" ), i18n( "Congo" ), i18n( "Congo, Dem. Rep." ),
00372     i18n( "Costa Rica" ), i18n( "Croatia" ),
00373     i18n( "Cuba" ), i18n( "Cyprus" ), i18n( "Czech Republic" ),
00374     i18n( "Denmark" ), i18n( "Djibouti" ),
00375     i18n( "Dominica" ), i18n( "Dominican Republic" ), i18n( "Ecuador" ),
00376     i18n( "Egypt" ), i18n( "El Salvador" ), i18n( "Equatorial Guinea" ),
00377     i18n( "Eritrea" ), i18n( "Estonia" ), i18n( "England" ),
00378     i18n( "Ethiopia" ), i18n( "European Union" ), i18n( "Faroe Islands" ),
00379     i18n( "Fiji" ), i18n( "Finland" ), i18n( "France" ),
00380     i18n( "French Polynesia" ), i18n( "Gabon" ), i18n( "Gambia" ),
00381     i18n( "Georgia" ), i18n( "Germany" ), i18n( "Ghana" ),
00382     i18n( "Greece" ), i18n( "Greenland" ), i18n( "Grenada" ),
00383     i18n( "Guam" ), i18n( "Guatemala" ), i18n( "Guinea" ),
00384     i18n( "Guinea-Bissau" ), i18n( "Guyana" ), i18n( "Haiti" ),
00385     i18n( "Honduras" ), i18n( "Hong Kong" ), i18n( "Hungary" ),
00386     i18n( "Iceland" ), i18n( "India" ), i18n( "Indonesia" ),
00387     i18n( "Iran" ), i18n( "Iraq" ), i18n( "Ireland" ),
00388     i18n( "Israel" ), i18n( "Italy" ), i18n( "Ivory Coast" ),
00389     i18n( "Jamaica" ), i18n( "Japan" ), i18n( "Jordan" ),
00390     i18n( "Kazakhstan" ), i18n( "Kenya" ), i18n( "Kiribati" ),
00391     i18n( "Korea, North" ), i18n( "Korea, South" ),
00392     i18n( "Kuwait" ), i18n( "Kyrgyzstan" ), i18n( "Laos" ),
00393     i18n( "Latvia" ), i18n( "Lebanon" ), i18n( "Lesotho" ),
00394     i18n( "Liberia" ), i18n( "Libya" ), i18n( "Liechtenstein" ),
00395     i18n( "Lithuania" ), i18n( "Luxembourg" ), i18n( "Macau" ),
00396     i18n( "Madagascar" ), i18n( "Malawi" ), i18n( "Malaysia" ),
00397     i18n( "Maldives" ), i18n( "Mali" ), i18n( "Malta" ),
00398     i18n( "Marshall Islands" ), i18n( "Martinique" ), i18n( "Mauritania" ),
00399     i18n( "Mauritius" ), i18n( "Mexico" ),
00400     i18n( "Micronesia, Federated States Of" ), i18n( "Moldova" ),
00401     i18n( "Monaco" ), i18n( "Mongolia" ), i18n( "Montserrat" ),
00402     i18n( "Morocco" ), i18n( "Mozambique" ), i18n( "Myanmar" ),
00403     i18n( "Namibia" ),
00404     i18n( "Nauru" ), i18n( "Nepal" ), i18n( "Netherlands" ),
00405     i18n( "Netherlands Antilles" ), i18n( "New Caledonia" ),
00406     i18n( "New Zealand" ), i18n( "Nicaragua" ), i18n( "Niger" ),
00407     i18n( "Nigeria" ), i18n( "Niue" ), i18n( "North Korea" ),
00408     i18n( "Northern Ireland" ), i18n( "Northern Mariana Islands" ),
00409     i18n( "Norway" ), i18n( "Oman" ), i18n( "Pakistan" ), i18n( "Palau" ),
00410     i18n( "Palestinian" ), i18n( "Panama" ), i18n( "Papua New Guinea" ),
00411     i18n( "Paraguay" ), i18n( "Peru" ), i18n( "Philippines" ),
00412     i18n( "Poland" ), i18n( "Portugal" ), i18n( "Puerto Rico" ),
00413     i18n( "Qatar" ), i18n( "Romania" ), i18n( "Russia" ), i18n( "Rwanda" ),
00414     i18n( "St. Kitts and Nevis" ), i18n( "St. Lucia" ),
00415     i18n( "St. Vincent and the Grenadines" ), i18n( "San Marino" ),
00416     i18n( "Sao Tome and Principe" ), i18n( "Saudi Arabia" ),
00417     i18n( "Senegal" ), i18n( "Serbia & Montenegro" ), i18n( "Seychelles" ),
00418     i18n( "Sierra Leone" ), i18n( "Singapore" ), i18n( "Slovakia" ),
00419     i18n( "Slovenia" ), i18n( "Solomon Islands" ), i18n( "Somalia" ),
00420     i18n( "South Africa" ), i18n( "South Korea" ), i18n( "Spain" ),
00421     i18n( "Sri Lanka" ), i18n( "St. Kitts and Nevis" ), i18n( "Sudan" ),
00422     i18n( "Suriname" ), i18n( "Swaziland" ), i18n( "Sweden" ),
00423     i18n( "Switzerland" ), i18n( "Syria" ), i18n( "Taiwan" ),
00424     i18n( "Tajikistan" ), i18n( "Tanzania" ), i18n( "Thailand" ),
00425     i18n( "Tibet" ), i18n( "Togo" ), i18n( "Tonga" ),
00426     i18n( "Trinidad and Tobago" ), i18n( "Tunisia" ), i18n( "Turkey" ),
00427     i18n( "Turkmenistan" ), i18n( "Turks and Caicos Islands" ),
00428     i18n( "Tuvalu" ), i18n( "Uganda " ), i18n( "Ukraine" ),
00429     i18n( "United Arab Emirates" ), i18n( "United Kingdom" ),
00430     i18n( "United States" ), i18n( "Uruguay" ), i18n( "Uzbekistan" ),
00431     i18n( "Vanuatu" ), i18n( "Vatican City" ), i18n( "Venezuela" ),
00432     i18n( "Vietnam" ), i18n( "Western Samoa" ), i18n( "Yemen" ),
00433     i18n( "Yugoslavia" ), i18n( "Zaire" ), i18n( "Zambia" ),
00434     i18n( "Zimbabwe" ),
00435     ""
00436   };
00437   
00438   QStringList countries;
00439   for (int i =0; sCountry[i] != ""; ++i )
00440     countries.append( sCountry[i] );
00441 
00442   countries.sort();
00443   
00444   combo->insertStringList( countries );
00445 }
00446 
00447 bool AddressEditDialog::changed() const
00448 {
00449   return mChanged;
00450 }
00451 
00452 void AddressEditDialog::modified()
00453 {
00454   mChanged = true;
00455 }
00456 
00457 AddressTypeDialog::AddressTypeDialog( int type, QWidget *parent )
00458   : KDialogBase( KDialogBase::Plain, i18n( "Edit Address Type" ),
00459                  KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok,
00460                  parent, "AddressTypeDialog" )
00461 {
00462   QWidget *page = plainPage();
00463   QVBoxLayout *layout = new QVBoxLayout( page );
00464 
00465   mGroup = new QButtonGroup( 2, Horizontal, i18n( "Address Types" ), page );
00466   layout->addWidget( mGroup );
00467 
00468   mTypeList = KABC::Address::typeList();
00469   mTypeList.remove( KABC::Address::Pref );
00470 
00471   KABC::Address::TypeList::Iterator it;
00472   for ( it = mTypeList.begin(); it != mTypeList.end(); ++it )
00473     new QCheckBox( KABC::Address::typeLabel( *it ), mGroup );
00474 
00475   for ( int i = 0; i < mGroup->count(); ++i ) {
00476     QCheckBox *box = (QCheckBox*)mGroup->find( i );
00477     box->setChecked( type & mTypeList[ i ] );
00478   }
00479 }
00480 
00481 AddressTypeDialog::~AddressTypeDialog()
00482 {
00483 }
00484 
00485 int AddressTypeDialog::type()
00486 {
00487   int type = 0;
00488   for ( int i = 0; i < mGroup->count(); ++i ) {
00489     QCheckBox *box = (QCheckBox*)mGroup->find( i );
00490     if ( box->isChecked() )
00491       type += mTypeList[ i ];
00492   }
00493 
00494   return type;
00495 }
00496 
00497 #include "addresseditwidget.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:09 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001