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> {
60 class TabPressEater :
public QObject
72 if (event->
type() == QEvent::KeyPress) {
74 if (keyEvent->
key() == Qt::Key_Tab) {
91 class AddressTypeDialog :
public KDialog
94 AddressTypeDialog(KABC::Address::Type type,
QWidget *parent);
97 KABC::Address::Type type()
const;
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));
164 connect(
this, SIGNAL(activated(
int)),
165 this, SLOT(selected(
int)));
174 if (!mTypeList.
contains((
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()
223 mTypeList.
insert(mTypeList.
at(mTypeList.
count() - 1), mType);
226 setType(KABC::Address::Type(mTypeList.
at(mLastSelected)));
232 AddressEditWidget::AddressEditWidget(
QWidget *parent)
241 QLabel *label =
new QLabel(i18nc(
"@label:listbox type of address",
"Address type:"),
this);
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()));
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);
308 void AddressEditWidget::editAddress()
312 if (dialog->exec()) {
313 const KABC::Address address = dialog->address();
314 fixPreferredAddress(address);
315 mAddressList[mAddressSelectionWidget->currentIndex()] = 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());
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()) {
356 mAddressView->
setText(address.formattedAddress(mName));
360 void AddressEditWidget::updateButtons()
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();
375 for (
int i = 0; i < mAddressList.count(); ++i) {
376 if (mAddressList.at(i).type() &KABC::Address::Pref) {
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);
418 QLabel *label =
new QLabel(i18nc(
"@label:listbox type of address",
"Address type:"),
this);
425 label =
new QLabel(i18nc(
"<streetLabel>:",
"%1:", KABC::Address::streetLabel()), page);
428 mStreetTextEdit =
new KTextEdit(page);
429 mStreetTextEdit->setAcceptRichText(
false);
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);
438 mPOBoxEdit =
new KLineEdit(page);
439 mPOBoxEdit->setTrapReturnKey(
true);
443 label =
new QLabel(i18nc(
"<localityLabel>:",
"%1:", KABC::Address::localityLabel()), page);
445 mLocalityEdit =
new KLineEdit(page);
446 mLocalityEdit->setTrapReturnKey(
true);
448 topLayout->
addWidget(mLocalityEdit, 3, 1);
450 label =
new QLabel(i18nc(
"<regionLabel>:",
"%1:", KABC::Address::regionLabel()), page);
452 mRegionEdit =
new KLineEdit(page);
453 mRegionEdit->setTrapReturnKey(
true);
457 label =
new QLabel(i18nc(
"<postalCodeLabel>:",
"%1:", KABC::Address::postalCodeLabel()), page);
459 mPostalCodeEdit =
new KLineEdit(page);
460 mPostalCodeEdit->setTrapReturnKey(
true);
462 topLayout->
addWidget(mPostalCodeEdit, 5, 1);
464 label =
new QLabel(i18nc(
"<countryLabel>:",
"%1:", KABC::Address::countryLabel()), page);
466 mCountryCombo =
new KComboBox(page);
467 mCountryCombo->setEditable(
true);
468 mCountryCombo->setDuplicatesEnabled(
false);
471 topLayout->
addWidget(labelButton, 7, 0, 1, 2);
472 connect(labelButton, SIGNAL(clicked()), SLOT(editLabel()));
476 topLayout->
addWidget(mCountryCombo, 6, 1);
478 mPreferredCheckBox =
new QCheckBox(i18nc(
"street/postal",
"This is the preferred address"), page);
479 topLayout->
addWidget(mPreferredCheckBox, 8, 0, 1, 2);
481 KHBox *buttonBox =
new KHBox(page);
482 buttonBox->setSpacing(spacingHint());
483 topLayout->
addWidget(buttonBox, 9, 0, 1, 2);
485 KAcceleratorManager::manage(
this);
488 AddressEditDialog::~AddressEditDialog()
492 void AddressEditDialog::editLabel()
495 QString result = KInputDialog::getMultiLineText(KABC::Address::labelLabel(),
496 KABC::Address::labelLabel(),
503 void AddressEditDialog::setAddress(
const KABC::Address &address)
507 mTypeCombo->
setType(mAddress.type());
508 mStreetTextEdit->setPlainText(mAddress.street());
509 mRegionEdit->setText(mAddress.region());
510 mLocalityEdit->setText(mAddress.locality());
511 mPostalCodeEdit->setText(mAddress.postalCode());
512 mPOBoxEdit->setText(mAddress.postOfficeBox());
513 mLabel = mAddress.label();
514 mPreferredCheckBox->
setChecked(mAddress.type() &KABC::Address::Pref);
516 if (mAddress.isEmpty()) {
517 mCountryCombo->setItemText(mCountryCombo->currentIndex(),
518 KGlobal::locale()->countryCodeToName(KGlobal::locale()->country()));
520 mCountryCombo->setItemText(mCountryCombo->currentIndex(), mAddress.country());
523 mStreetTextEdit->setFocus();
526 KABC::Address AddressEditDialog::address()
const
528 KABC::Address address(mAddress);
530 address.setType(mTypeCombo->
type());
531 address.setLocality(mLocalityEdit->text());
532 address.setRegion(mRegionEdit->text());
533 address.setPostalCode(mPostalCodeEdit->text());
534 address.setCountry(mCountryCombo->currentText());
535 address.setPostOfficeBox(mPOBoxEdit->text());
536 address.setStreet(mStreetTextEdit->toPlainText());
537 address.setLabel(mLabel);
540 address.setType(address.type() | KABC::Address::Pref);
542 address.setType(address.type() &~(KABC::Address::Pref));
548 void AddressEditDialog::fillCountryCombo()
552 foreach (
const QString &cc, KGlobal::locale()->allCountriesList()) {
553 countries.
append(KGlobal::locale()->countryCodeToName(cc));
556 qSort(countries.
begin(), countries.
end(), LocaleAwareLessThan());
558 mCountryCombo->addItems(countries);
559 mCountryCombo->setAutoCompletion(
true);
560 mCountryCombo->completionObject()->setItems(countries);
561 mCountryCombo->completionObject()->setIgnoreCase(
true);
563 const QString currentCountry = KGlobal::locale()->countryCodeToName(KGlobal::locale()->country());
564 mCountryCombo->setCurrentIndex(mCountryCombo->findText(currentCountry));
567 AddressTypeDialog::AddressTypeDialog(KABC::Address::Type type,
QWidget *parent)
570 setCaption(i18nc(
"street/postal",
"Edit Address Type"));
571 setButtons(Ok | Cancel);
572 setDefaultButton(Ok);
583 mGroup->setExclusive(
false);
587 mTypeList = KABC::Address::typeList();
588 mTypeList.
removeAll(KABC::Address::Pref);
590 KABC::Address::TypeList::ConstIterator it;
601 mGroup->addButton(cb);
605 AddressTypeDialog::~AddressTypeDialog()
609 KABC::Address::Type AddressTypeDialog::type()
const
611 KABC::Address::Type type;
612 for (
int i = 0; i < mGroup->buttons().count(); ++i) {
615 type |= mTypeList[i];
void setType(KABC::Address::Type type)
Sets the type that shall be selected in the combobox.
int localeAwareCompare(const QString &other) const
const T & at(int i) const
void setAlignment(QFlags< Qt::AlignmentFlag >)
AddressTypeCombo(QWidget *parent=0)
Creates a new address type combo.
void setSpacing(int spacing)
int indexOf(const T &value, int from) const
void setBuddy(QWidget *buddy)
const char * name() const
int count(const T &value) const
void append(const T &value)
~AddressTypeCombo()
Destroys the address type combo.
void setObjectName(const QString &name)
int removeAll(const T &value)
bool sendEvent(QObject *receiver, QEvent *event)
virtual bool eventFilter(QObject *watched, QEvent *event)
void setText(const QString &)
void setMargin(int margin)
A QPointer which when destructed, deletes the object it points to.
bool contains(const T &value) const
KABC::Address::Type type() const
Returns the type that is currently selected.
void addLayout(QLayout *layout, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
void insert(int i, const T &value)
Dialog for editing address details.
const_iterator constEnd() const
const_iterator constBegin() const
void setSpacing(int spacing)