22 #include "addresseditwidget.h"
24 #include "autoqpointer_p.h"
26 #include <QtCore/QEvent>
27 #include <QtCore/QList>
28 #include <QApplication>
29 #include <QButtonGroup>
32 #include <QGridLayout>
36 #include <QPushButton>
38 #include <kacceleratormanager.h>
39 #include <kcombobox.h>
42 #include <kinputdialog.h>
43 #include <klineedit.h>
45 #include <klocalizedstring.h>
47 #include <kmessagebox.h>
48 #include <kseparator.h>
49 #include <ktextedit.h>
53 struct LocaleAwareLessThan : std::binary_function<QString,QString,bool> {
54 bool operator()(
const QString &s1,
const QString &s2 )
const
56 return QString::localeAwareCompare( s1, s2 ) < 0 ;
60 class TabPressEater :
public QObject
63 TabPressEater( QObject *parent )
66 setObjectName( QLatin1String(
"TabPressEater" ) );
70 bool eventFilter( QObject*, QEvent *event )
72 if ( event->type() == QEvent::KeyPress ) {
73 QKeyEvent *keyEvent = (QKeyEvent*)event;
74 if ( keyEvent->key() == Qt::Key_Tab ) {
75 QApplication::sendEvent( parent(), event );
90 class AddressTypeDialog :
public KDialog
93 AddressTypeDialog( KABC::Address::Type type, QWidget *parent );
96 KABC::Address::Type type()
const;
101 KABC::Address::TypeList mTypeList;
106 : KComboBox( parent )
108 connect(
this, SIGNAL(activated(
int)), SLOT(selected(
int)) );
117 mAddresses = addresses;
123 const int index = mAddresses.indexOf( address );
125 setCurrentIndex( index );
131 if ( currentIndex() != -1 && currentIndex() < mAddresses.count() ) {
132 return mAddresses.at( currentIndex() );
134 return KABC::Address();
138 void AddressSelectionWidget::selected(
int index )
140 Q_ASSERT( index != -1 && index < mAddresses.count() );
144 void AddressSelectionWidget::updateView()
147 for (
int i = 0; i < mAddresses.count(); ++i ) {
148 addItem( KABC::Address::typeLabel( mAddresses.at( i ).type() ) );
155 : KComboBox( parent ),
156 mType( KABC::Address::Home ),
159 for (
int i = 0; i < KABC::Address::typeList().count(); ++i ) {
160 mTypeList.append( KABC::Address::typeList().at( i ) );
162 mTypeList.append( -1 );
166 connect(
this, SIGNAL(activated(
int)),
167 this, SLOT(selected(
int)) );
176 if ( !mTypeList.contains( (
int)type ) ) {
178 mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), (
int)
type );
190 void AddressTypeCombo::update()
192 bool blocked = signalsBlocked();
193 blockSignals(
true );
196 for (
int i = 0; i < mTypeList.count(); ++i ) {
197 if ( mTypeList.at( i ) == -1 ) {
198 addItem( i18nc(
"@item:inlistbox Category of contact info field",
"Other..." ) );
200 addItem( KABC::Address::typeLabel( KABC::Address::Type( mTypeList.at( i ) ) ) );
204 setCurrentIndex( mLastSelected = mTypeList.indexOf( mType ) );
206 blockSignals( blocked );
209 void AddressTypeCombo::selected(
int pos )
211 if ( mTypeList.at( pos ) == -1 ) {
214 mType = KABC::Address::Type( mTypeList.at( pos ) );
219 void AddressTypeCombo::otherSelected()
224 if ( !mTypeList.contains( mType ) ) {
225 mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), mType );
228 setType( KABC::Address::Type( mTypeList.at( mLastSelected ) ) );
235 AddressEditWidget::AddressEditWidget( QWidget *parent )
236 : QWidget( parent ), mReadOnly( false )
238 QGridLayout *layout =
new QGridLayout(
this );
239 layout->setSpacing( KDialog::spacingHint() );
240 layout->setMargin( 0 );
243 connect( mAddressSelectionWidget, SIGNAL(selectionChanged(KABC::Address)),
244 SLOT(updateAddressView()) );
245 layout->addWidget( mAddressSelectionWidget, 0, 0, 1, 3 );
247 mAddressView =
new QLabel(
this );
248 mAddressView->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
249 mAddressView->setMinimumHeight( 20 );
250 mAddressView->setAlignment( Qt::AlignTop );
251 mAddressView->setTextFormat( Qt::PlainText );
252 mAddressView->setTextInteractionFlags( Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse );
253 layout->addWidget( mAddressView, 1, 0, 1, 3 );
255 mCreateButton =
new QPushButton( i18nc(
"street/postal",
"New..." ),
this );
256 connect( mCreateButton, SIGNAL(clicked()),
this, SLOT(createAddress()) );
257 mEditButton =
new QPushButton( i18nc(
"street/postal",
"Edit..." ),
this );
258 connect( mEditButton, SIGNAL(clicked()),
this, SLOT(editAddress()) );
259 mDeleteButton =
new QPushButton( i18nc(
"street/postal",
"Delete" ),
this );
260 connect( mDeleteButton, SIGNAL(clicked()),
this, SLOT(deleteAddress()) );
262 layout->addWidget( mCreateButton, 2, 0 );
263 layout->addWidget( mEditButton, 2, 1 );
264 layout->addWidget( mDeleteButton, 2, 2 );
269 AddressEditWidget::~AddressEditWidget()
273 void AddressEditWidget::setReadOnly(
bool readOnly )
275 mReadOnly = readOnly;
279 void AddressEditWidget::updateName(
const QString &name )
285 void AddressEditWidget::createAddress()
288 if ( dialog->exec() ) {
289 const KABC::Address address = dialog->address();
290 fixPreferredAddress( address );
291 mAddressList.append( address );
300 void AddressEditWidget::editAddress()
304 if ( dialog->exec() ) {
305 const KABC::Address address = dialog->address();
306 fixPreferredAddress( address );
307 mAddressList[ mAddressSelectionWidget->currentIndex() ] = address;
315 void AddressEditWidget::deleteAddress()
317 const int result = KMessageBox::questionYesNo(
this, i18n(
"Do you really want to delete this address?" ) );
319 if ( result != KMessageBox::Yes ) {
323 mAddressList.removeAt( mAddressSelectionWidget->currentIndex() );
329 void AddressEditWidget::fixPreferredAddress(
const KABC::Address &preferredAddress )
333 if ( preferredAddress.type() & KABC::Address::Pref ) {
334 for (
int i = 0; i < mAddressList.count(); ++i ) {
335 KABC::Address &address = mAddressList[ i ];
336 address.setType( address.type() & ~KABC::Address::Pref );
341 void AddressEditWidget::updateAddressView()
343 const KABC::Address address = mAddressSelectionWidget->
currentAddress();
345 if ( address.isEmpty() ) {
346 mAddressView->setText( QString() );
348 mAddressView->setText( address.formattedAddress( mName ) );
352 void AddressEditWidget::updateButtons()
354 mCreateButton->setEnabled( !mReadOnly );
355 mEditButton->setEnabled( !mReadOnly && ( mAddressList.count() > 0 ) );
356 mDeleteButton->setEnabled( !mReadOnly && ( mAddressList.count() > 0 ) );
359 void AddressEditWidget::loadContact(
const KABC::Addressee &contact )
361 mName = contact.realName();
362 mAddressList = contact.addresses();
367 for (
int i = 0; i < mAddressList.count(); ++i ) {
368 if ( mAddressList.at( i ).type() & KABC::Address::Pref ) {
378 void AddressEditWidget::storeContact( KABC::Addressee &contact )
const
381 const KABC::Address::List oldAddresses = contact.addresses();
382 for (
int i = 0; i < oldAddresses.count(); ++i ) {
383 contact.removeAddress( oldAddresses.at( i ) );
387 for (
int i = 0; i < mAddressList.count(); ++i ) {
388 const KABC::Address address( mAddressList.at( i ) );
389 if ( !address.isEmpty() ) {
390 contact.insertAddress( address );
396 AddressEditDialog::AddressEditDialog( QWidget *parent )
399 setCaption( i18nc(
"street/postal",
"Edit Address" ) );
400 setButtons( Ok | Cancel );
401 setDefaultButton( Ok );
402 showButtonSeparator(
true );
404 QWidget *page =
new QWidget(
this );
405 setMainWidget( page );
407 QGridLayout *topLayout =
new QGridLayout( page );
408 topLayout->setSpacing( spacingHint() );
409 topLayout->setMargin( 0 );
412 topLayout->addWidget( mTypeCombo, 0, 0, 1, 2 );
414 QLabel *label =
new QLabel( i18nc(
"<streetLabel>:",
"%1:", KABC::Address::streetLabel() ), page );
415 label->setAlignment( Qt::AlignTop | Qt::AlignLeft );
416 topLayout->addWidget( label, 1, 0 );
417 mStreetTextEdit =
new KTextEdit( page );
418 mStreetTextEdit->setAcceptRichText(
false );
419 label->setBuddy( mStreetTextEdit );
420 topLayout->addWidget( mStreetTextEdit, 1, 1 );
422 TabPressEater *eater =
new TabPressEater(
this );
423 mStreetTextEdit->installEventFilter( eater );
425 label =
new QLabel( i18nc(
"<postOfficeBoxLabel>:",
"%1:", KABC::Address::postOfficeBoxLabel() ), page );
426 topLayout->addWidget( label, 2 , 0 );
427 mPOBoxEdit =
new KLineEdit( page );
428 label->setBuddy( mPOBoxEdit );
429 topLayout->addWidget( mPOBoxEdit, 2, 1 );
431 label =
new QLabel( i18nc(
"<localityLabel>:",
"%1:", KABC::Address::localityLabel() ), page );
432 topLayout->addWidget( label, 3, 0 );
433 mLocalityEdit =
new KLineEdit( page );
434 label->setBuddy( mLocalityEdit );
435 topLayout->addWidget( mLocalityEdit, 3, 1 );
437 label =
new QLabel( i18nc(
"<regionLabel>:",
"%1:", KABC::Address::regionLabel() ), page );
438 topLayout->addWidget( label, 4, 0 );
439 mRegionEdit =
new KLineEdit( page );
440 label->setBuddy( mRegionEdit );
441 topLayout->addWidget( mRegionEdit, 4, 1 );
443 label =
new QLabel( i18nc(
"<postalCodeLabel>:",
"%1:", KABC::Address::postalCodeLabel() ), page );
444 topLayout->addWidget( label, 5, 0 );
445 mPostalCodeEdit =
new KLineEdit( page );
446 label->setBuddy( mPostalCodeEdit );
447 topLayout->addWidget( mPostalCodeEdit, 5, 1 );
449 label =
new QLabel( i18nc(
"<countryLabel>:",
"%1:", KABC::Address::countryLabel() ), page );
450 topLayout->addWidget( label, 6, 0 );
451 mCountryCombo =
new KComboBox( page );
452 mCountryCombo->setEditable(
true );
453 mCountryCombo->setDuplicatesEnabled(
false );
455 QPushButton *labelButton =
new QPushButton( i18n(
"Edit Label..." ), page );
456 topLayout->addWidget( labelButton, 7, 0, 1, 2 );
457 connect( labelButton, SIGNAL(clicked()), SLOT(editLabel()) );
460 label->setBuddy( mCountryCombo );
461 topLayout->addWidget( mCountryCombo, 6, 1 );
463 mPreferredCheckBox =
new QCheckBox( i18nc(
"street/postal",
"This is the preferred address" ), page );
464 topLayout->addWidget( mPreferredCheckBox, 8, 0, 1, 2 );
466 KHBox *buttonBox =
new KHBox( page );
467 buttonBox->setSpacing( spacingHint() );
468 topLayout->addWidget( buttonBox, 9, 0, 1, 2 );
470 KAcceleratorManager::manage(
this );
473 AddressEditDialog::~AddressEditDialog()
477 void AddressEditDialog::editLabel()
480 QString result = KInputDialog::getMultiLineText( KABC::Address::labelLabel(),
481 KABC::Address::labelLabel(),
488 void AddressEditDialog::setAddress(
const KABC::Address &address )
492 mTypeCombo->
setType( mAddress.type() );
493 mStreetTextEdit->setPlainText( mAddress.street() );
494 mRegionEdit->setText( mAddress.region() );
495 mLocalityEdit->setText( mAddress.locality() );
496 mPostalCodeEdit->setText( mAddress.postalCode() );
497 mPOBoxEdit->setText( mAddress.postOfficeBox() );
498 mLabel = mAddress.label();
499 mPreferredCheckBox->setChecked( mAddress.type() & KABC::Address::Pref );
501 if ( mAddress.isEmpty() ) {
502 mCountryCombo->setItemText( mCountryCombo->currentIndex(),
503 KGlobal::locale()->countryCodeToName( KGlobal::locale()->country() ) );
505 mCountryCombo->setItemText( mCountryCombo->currentIndex(), mAddress.country() );
508 mStreetTextEdit->setFocus();
511 KABC::Address AddressEditDialog::address()
const
513 KABC::Address address( mAddress );
515 address.setType( mTypeCombo->
type() );
516 address.setLocality( mLocalityEdit->text() );
517 address.setRegion( mRegionEdit->text() );
518 address.setPostalCode( mPostalCodeEdit->text() );
519 address.setCountry( mCountryCombo->currentText() );
520 address.setPostOfficeBox( mPOBoxEdit->text() );
521 address.setStreet( mStreetTextEdit->toPlainText() );
522 address.setLabel( mLabel );
524 if ( mPreferredCheckBox->isChecked() ) {
525 address.setType( address.type() | KABC::Address::Pref );
527 address.setType( address.type() & ~( KABC::Address::Pref ) );
533 void AddressEditDialog::fillCountryCombo()
535 QStringList countries;
537 foreach (
const QString &cc, KGlobal::locale()->allCountriesList() ) {
538 countries.append( KGlobal::locale()->countryCodeToName( cc ) );
541 qSort( countries.begin(), countries.end(), LocaleAwareLessThan() );
543 mCountryCombo->addItems( countries );
544 mCountryCombo->setAutoCompletion(
true );
545 mCountryCombo->completionObject()->setItems( countries );
546 mCountryCombo->completionObject()->setIgnoreCase(
true );
548 const QString currentCountry = KGlobal::locale()->countryCodeToName( KGlobal::locale()->country() );
549 mCountryCombo->setCurrentIndex( mCountryCombo->findText( currentCountry ) );
553 AddressTypeDialog::AddressTypeDialog( KABC::Address::Type type, QWidget *parent )
556 setCaption( i18nc(
"street/postal",
"Edit Address Type" ) );
557 setButtons( Ok | Cancel );
558 setDefaultButton( Ok );
560 QWidget *page =
new QWidget(
this );
561 setMainWidget( page );
562 QVBoxLayout *layout =
new QVBoxLayout( page );
563 layout->setSpacing( KDialog::spacingHint() );
564 layout->setMargin( 0 );
566 QGroupBox *box =
new QGroupBox( i18nc(
"street/postal",
"Address Types" ), page );
567 layout->addWidget( box );
568 mGroup =
new QButtonGroup( box );
569 mGroup->setExclusive (
false );
571 QGridLayout *buttonLayout =
new QGridLayout( box );
573 mTypeList = KABC::Address::typeList();
574 mTypeList.removeAll( KABC::Address::Pref );
576 KABC::Address::TypeList::ConstIterator it;
579 for ( it = mTypeList.constBegin(); it != mTypeList.constEnd(); ++it, ++i ) {
580 QCheckBox *cb =
new QCheckBox( KABC::Address::typeLabel( *it ), box );
581 cb->setChecked( type & mTypeList[ i ] );
582 buttonLayout->addWidget( cb, row, i%3 );
587 mGroup->addButton( cb );
591 AddressTypeDialog::~AddressTypeDialog()
595 KABC::Address::Type AddressTypeDialog::type()
const
597 KABC::Address::Type type;
598 for (
int i = 0; i < mGroup->buttons().count(); ++i ) {
599 QCheckBox *box =
dynamic_cast<QCheckBox*
>( mGroup->buttons().at( i ) );
600 if ( box && box->isChecked() ) {
601 type |= mTypeList[ i ];
void setType(KABC::Address::Type type)
Sets the type that shall be selected in the combobox.
AddressTypeCombo(QWidget *parent=0)
Creates a new address type combo.
~AddressTypeCombo()
Destroys the address type combo.
A QPointer which when destructed, deletes the object it points to.
KABC::Address::Type type() const
Returns the type that is currently selected.
Dialog for editing address details.