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);
91 class AddressTypeDialog :
public KDialog
94 AddressTypeDialog(KABC::Address::Type type, QWidget *parent);
97 KABC::Address::Type type()
const;
100 QButtonGroup *mGroup;
102 KABC::Address::TypeList mTypeList;
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()));
154 , mType(
KABC::Address::Home)
157 for (
int i = 0; i < KABC::Address::typeList().count(); ++i) {
158 mTypeList.append(KABC::Address::typeList().at(i));
160 mTypeList.append(-1);
164 connect(
this, SIGNAL(activated(
int)),
165 this, SLOT(selected(
int)));
174 if (!mTypeList.contains((
int)type)) {
176 mTypeList.insert(mTypeList.at(mTypeList.count() - 1), (
int)
type);
188 void AddressTypeCombo::update()
190 bool blocked = signalsBlocked();
194 for (
int i = 0; i < mTypeList.count(); ++i) {
195 if (mTypeList.at(i) == -1) {
196 addItem(i18nc(
"@item:inlistbox Category of contact info field",
"Other..."));
198 addItem(KABC::Address::typeLabel(KABC::Address::Type(mTypeList.at(i))));
202 setCurrentIndex(mLastSelected = mTypeList.indexOf(mType));
204 blockSignals(blocked);
207 void AddressTypeCombo::selected(
int pos)
209 if (mTypeList.at(pos) == -1) {
212 mType = KABC::Address::Type(mTypeList.at(pos));
217 void AddressTypeCombo::otherSelected()
222 if (!mTypeList.contains(mType)) {
223 mTypeList.insert(mTypeList.at(mTypeList.count() - 1), mType);
226 setType(KABC::Address::Type(mTypeList.at(mLastSelected)));
232 AddressEditWidget::AddressEditWidget(QWidget *parent)
236 QGridLayout *layout =
new QGridLayout;
237 layout->setSpacing(KDialog::spacingHint());
238 layout->setMargin(0);
240 QHBoxLayout *hboxLayout =
new QHBoxLayout;
241 QLabel *label =
new QLabel(i18nc(
"@label:listbox type of address",
"Address type:"),
this);
242 hboxLayout->addWidget(label);
245 connect(mAddressSelectionWidget, SIGNAL(selectionChanged(KABC::Address)),
246 SLOT(updateAddressView()));
247 label->setBuddy(mAddressSelectionWidget);
248 hboxLayout->addWidget(mAddressSelectionWidget, 1);
249 layout->addLayout(hboxLayout, 0, 0, 1, 3);
251 mAddressView =
new QLabel(
this);
252 mAddressView->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
253 mAddressView->setMinimumHeight(20);
254 mAddressView->setAlignment(Qt::AlignTop);
255 mAddressView->setTextFormat(Qt::PlainText);
256 mAddressView->setTextInteractionFlags(Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse);
257 layout->addWidget(mAddressView, 1, 0, 1, 3);
259 mCreateButton =
new QPushButton(i18nc(
"@action:button street/postal",
"New..."),
this);
260 connect(mCreateButton, SIGNAL(clicked()),
this, SLOT(createAddress()));
261 mEditButton =
new QPushButton(i18nc(
"@action:button street/postal",
"Edit..."),
this);
262 connect(mEditButton, SIGNAL(clicked()),
this, SLOT(editAddress()));
263 mDeleteButton =
new QPushButton(i18nc(
"@action:button street/postal",
"Delete"),
this);
264 connect(mDeleteButton, SIGNAL(clicked()),
this, SLOT(deleteAddress()));
266 layout->addWidget(mCreateButton, 2, 0);
267 layout->addWidget(mEditButton, 2, 1);
268 layout->addWidget(mDeleteButton, 2, 2);
273 AddressEditWidget::~AddressEditWidget()
277 void AddressEditWidget::setReadOnly(
bool readOnly)
279 if (mReadOnly != readOnly) {
280 mReadOnly = readOnly;
285 void AddressEditWidget::updateName(
const QString &name)
293 void AddressEditWidget::createAddress()
296 if (dialog->exec()) {
297 const KABC::Address address = dialog->address();
298 fixPreferredAddress(address);
299 mAddressList.append(address);
300 mAddressSelectionWidget->setAddresses(mAddressList);
301 mAddressSelectionWidget->setCurrentAddress(address);
308 void AddressEditWidget::editAddress()
311 dialog->setAddress(mAddressSelectionWidget->currentAddress());
312 if (dialog->exec()) {
313 const KABC::Address address = dialog->address();
314 fixPreferredAddress(address);
315 mAddressList[mAddressSelectionWidget->currentIndex()] = address;
316 mAddressSelectionWidget->setAddresses(mAddressList);
317 mAddressSelectionWidget->setCurrentAddress(address);
323 void AddressEditWidget::deleteAddress()
325 const int result = KMessageBox::questionYesNo(
this, i18n(
"Do you really want to delete this address?"));
327 if (result != KMessageBox::Yes) {
331 mAddressList.removeAt(mAddressSelectionWidget->currentIndex());
332 mAddressSelectionWidget->setAddresses(mAddressList);
337 void AddressEditWidget::fixPreferredAddress(
const KABC::Address &preferredAddress)
341 if (preferredAddress.type() &KABC::Address::Pref) {
342 for (
int i = 0; i < mAddressList.count(); ++i) {
343 KABC::Address &address = mAddressList[i];
344 address.setType(address.type() &~
KABC::Address::Pref);
349 void AddressEditWidget::updateAddressView()
351 const KABC::Address address = mAddressSelectionWidget->currentAddress();
353 if (address.isEmpty()) {
354 mAddressView->setText(QString());
356 mAddressView->setText(address.formattedAddress(mName));
360 void AddressEditWidget::updateButtons()
362 mCreateButton->setEnabled(!mReadOnly);
363 mEditButton->setEnabled(!mReadOnly && (mAddressList.count() > 0));
364 mDeleteButton->setEnabled(!mReadOnly && (mAddressList.count() > 0));
367 void AddressEditWidget::loadContact(
const KABC::Addressee &contact)
369 mName = contact.realName();
370 mAddressList = contact.addresses();
372 mAddressSelectionWidget->setAddresses(mAddressList);
375 for (
int i = 0; i < mAddressList.count(); ++i) {
376 if (mAddressList.at(i).type() &KABC::Address::Pref) {
377 mAddressSelectionWidget->setCurrentAddress(mAddressList.at(i));
386 void AddressEditWidget::storeContact(KABC::Addressee &contact)
const 389 const KABC::Address::List oldAddresses = contact.addresses();
390 for (
int i = 0; i < oldAddresses.count(); ++i) {
391 contact.removeAddress(oldAddresses.at(i));
395 for (
int i = 0; i < mAddressList.count(); ++i) {
396 const KABC::Address address(mAddressList.at(i));
397 if (!address.isEmpty()) {
398 contact.insertAddress(address);
403 AddressEditDialog::AddressEditDialog(QWidget *parent)
406 setCaption(i18nc(
"street/postal",
"Edit Address"));
407 setButtons(Ok | Cancel);
408 setDefaultButton(Ok);
409 showButtonSeparator(
true);
411 QWidget *page =
new QWidget(
this);
414 QGridLayout *topLayout =
new QGridLayout(page);
415 topLayout->setSpacing(spacingHint());
416 topLayout->setMargin(0);
418 QLabel *label =
new QLabel(i18nc(
"@label:listbox type of address",
"Address type:"),
this);
419 topLayout->addWidget(label, 0, 0);
422 label->setBuddy(mTypeCombo);
423 topLayout->addWidget(mTypeCombo, 0, 1);
425 label =
new QLabel(i18nc(
"<streetLabel>:",
"%1:", KABC::Address::streetLabel()), page);
426 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
427 topLayout->addWidget(label, 1, 0);
428 mStreetTextEdit =
new KTextEdit(page);
429 mStreetTextEdit->setAcceptRichText(
false);
430 label->setBuddy(mStreetTextEdit);
431 topLayout->addWidget(mStreetTextEdit, 1, 1);
433 TabPressEater *eater =
new TabPressEater(
this);
434 mStreetTextEdit->installEventFilter(eater);
436 label =
new QLabel(i18nc(
"<postOfficeBoxLabel>:",
"%1:", KABC::Address::postOfficeBoxLabel()), page);
437 topLayout->addWidget(label, 2 , 0);
438 mPOBoxEdit =
new KLineEdit(page);
439 label->setBuddy(mPOBoxEdit);
440 topLayout->addWidget(mPOBoxEdit, 2, 1);
442 label =
new QLabel(i18nc(
"<localityLabel>:",
"%1:", KABC::Address::localityLabel()), page);
443 topLayout->addWidget(label, 3, 0);
444 mLocalityEdit =
new KLineEdit(page);
445 label->setBuddy(mLocalityEdit);
446 topLayout->addWidget(mLocalityEdit, 3, 1);
448 label =
new QLabel(i18nc(
"<regionLabel>:",
"%1:", KABC::Address::regionLabel()), page);
449 topLayout->addWidget(label, 4, 0);
450 mRegionEdit =
new KLineEdit(page);
451 label->setBuddy(mRegionEdit);
452 topLayout->addWidget(mRegionEdit, 4, 1);
454 label =
new QLabel(i18nc(
"<postalCodeLabel>:",
"%1:", KABC::Address::postalCodeLabel()), page);
455 topLayout->addWidget(label, 5, 0);
456 mPostalCodeEdit =
new KLineEdit(page);
457 label->setBuddy(mPostalCodeEdit);
458 topLayout->addWidget(mPostalCodeEdit, 5, 1);
460 label =
new QLabel(i18nc(
"<countryLabel>:",
"%1:", KABC::Address::countryLabel()), page);
461 topLayout->addWidget(label, 6, 0);
462 mCountryCombo =
new KComboBox(page);
463 mCountryCombo->setEditable(
true);
464 mCountryCombo->setDuplicatesEnabled(
false);
466 QPushButton *labelButton =
new QPushButton(i18n(
"Edit Label..."), page);
467 topLayout->addWidget(labelButton, 7, 0, 1, 2);
468 connect(labelButton, SIGNAL(clicked()), SLOT(editLabel()));
471 label->setBuddy(mCountryCombo);
472 topLayout->addWidget(mCountryCombo, 6, 1);
474 mPreferredCheckBox =
new QCheckBox(i18nc(
"street/postal",
"This is the preferred address"), page);
475 topLayout->addWidget(mPreferredCheckBox, 8, 0, 1, 2);
477 KHBox *buttonBox =
new KHBox(page);
478 buttonBox->setSpacing(spacingHint());
479 topLayout->addWidget(buttonBox, 9, 0, 1, 2);
481 KAcceleratorManager::manage(
this);
484 AddressEditDialog::~AddressEditDialog()
488 void AddressEditDialog::editLabel()
491 QString result = KInputDialog::getMultiLineText(KABC::Address::labelLabel(),
492 KABC::Address::labelLabel(),
499 void AddressEditDialog::setAddress(
const KABC::Address &address)
503 mTypeCombo->setType(mAddress.type());
504 mStreetTextEdit->setPlainText(mAddress.street());
505 mRegionEdit->setText(mAddress.region());
506 mLocalityEdit->setText(mAddress.locality());
507 mPostalCodeEdit->setText(mAddress.postalCode());
508 mPOBoxEdit->setText(mAddress.postOfficeBox());
509 mLabel = mAddress.label();
510 mPreferredCheckBox->setChecked(mAddress.type() &KABC::Address::Pref);
512 if (mAddress.isEmpty()) {
513 mCountryCombo->setItemText(mCountryCombo->currentIndex(),
514 KGlobal::locale()->countryCodeToName(KGlobal::locale()->country()));
516 mCountryCombo->setItemText(mCountryCombo->currentIndex(), mAddress.country());
519 mStreetTextEdit->setFocus();
522 KABC::Address AddressEditDialog::address()
const 524 KABC::Address address(mAddress);
526 address.setType(mTypeCombo->type());
527 address.setLocality(mLocalityEdit->text());
528 address.setRegion(mRegionEdit->text());
529 address.setPostalCode(mPostalCodeEdit->text());
530 address.setCountry(mCountryCombo->currentText());
531 address.setPostOfficeBox(mPOBoxEdit->text());
532 address.setStreet(mStreetTextEdit->toPlainText());
533 address.setLabel(mLabel);
535 if (mPreferredCheckBox->isChecked()) {
536 address.setType(address.type() | KABC::Address::Pref);
538 address.setType(address.type() &~(KABC::Address::Pref));
544 void AddressEditDialog::fillCountryCombo()
546 QStringList countries;
548 foreach (
const QString &cc, KGlobal::locale()->allCountriesList()) {
549 countries.append(KGlobal::locale()->countryCodeToName(cc));
552 qSort(countries.begin(), countries.end(), LocaleAwareLessThan());
554 mCountryCombo->addItems(countries);
555 mCountryCombo->setAutoCompletion(
true);
556 mCountryCombo->completionObject()->setItems(countries);
557 mCountryCombo->completionObject()->setIgnoreCase(
true);
559 const QString currentCountry = KGlobal::locale()->countryCodeToName(KGlobal::locale()->country());
560 mCountryCombo->setCurrentIndex(mCountryCombo->findText(currentCountry));
563 AddressTypeDialog::AddressTypeDialog(KABC::Address::Type
type, QWidget *parent)
566 setCaption(i18nc(
"street/postal",
"Edit Address Type"));
567 setButtons(Ok | Cancel);
568 setDefaultButton(Ok);
570 QWidget *page =
new QWidget(
this);
572 QVBoxLayout *layout =
new QVBoxLayout(page);
573 layout->setSpacing(KDialog::spacingHint());
574 layout->setMargin(0);
576 QGroupBox *box =
new QGroupBox(i18nc(
"street/postal",
"Address Types"), page);
577 layout->addWidget(box);
578 mGroup =
new QButtonGroup(box);
579 mGroup->setExclusive(
false);
581 QGridLayout *buttonLayout =
new QGridLayout(box);
583 mTypeList = KABC::Address::typeList();
584 mTypeList.removeAll(KABC::Address::Pref);
586 KABC::Address::TypeList::ConstIterator it;
589 for (it = mTypeList.constBegin(); it != mTypeList.constEnd(); ++it, ++i) {
590 QCheckBox *cb =
new QCheckBox(KABC::Address::typeLabel(*it), box);
591 cb->setChecked(type &mTypeList[i]);
592 buttonLayout->addWidget(cb, row, i % 3);
597 mGroup->addButton(cb);
601 AddressTypeDialog::~AddressTypeDialog()
605 KABC::Address::Type AddressTypeDialog::type()
const 607 KABC::Address::Type
type;
608 for (
int i = 0; i < mGroup->buttons().count(); ++i) {
609 QCheckBox *box =
dynamic_cast<QCheckBox *
>(mGroup->buttons().at(i));
610 if (box && box->isChecked()) {
611 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.
Dialog for editing address details.
KABC::Address::Type type() const
Returns the type that is currently selected.