10#include "addresslocationwidget.h"
11#include "selectaddresstypecombobox.h"
15#include <KLocalizedString>
22#include <QStackedWidget>
27AddressLocationWidget::AddressLocationWidget(
QWidget *parent)
29 , mCurrentMode(CreateAddress)
39 gridLayout->addWidget(mTypeCombo, 0, 0, 1, 2);
43 gridLayout->addWidget(label, 1, 0);
46 mStreetEdit->setPlaceholderText(
i18nc(
"@info:placeholder",
"Add Street"));
48 mStreetEdit->setTrapReturnKey(
true);
50 gridLayout->addWidget(mStreetEdit, 2, 0);
54 gridLayout->addWidget(label, 1, 1);
57 mPOBoxEdit->setPlaceholderText(
i18nc(
"@info:placeholder",
"Add Post Office Box"));
59 mPOBoxEdit->setTrapReturnKey(
true);
61 gridLayout->addWidget(mPOBoxEdit, 2, 1);
65 gridLayout->addWidget(label, 3, 0);
67 mPostalCodeEdit->setPlaceholderText(
i18nc(
"@info:placeholder",
"Add Postal Code"));
69 mPostalCodeEdit->setTrapReturnKey(
true);
71 gridLayout->addWidget(mPostalCodeEdit, 4, 0);
75 gridLayout->addWidget(label, 3, 1);
77 mLocalityEdit->setPlaceholderText(
i18nc(
"@info:placeholder",
"Add Locality"));
79 mLocalityEdit->setTrapReturnKey(
true);
81 gridLayout->addWidget(mLocalityEdit, 4, 1);
85 gridLayout->addWidget(label, 5, 0);
87 mRegionEdit->setPlaceholderText(
i18nc(
"@info:placeholder",
"Add Region"));
89 mRegionEdit->setTrapReturnKey(
true);
91 gridLayout->addWidget(mRegionEdit, 6, 0);
95 gridLayout->addWidget(label, 5, 1);
98 mCountryCombo->setEditable(
false);
99 mCountryCombo->setDuplicatesEnabled(
false);
101 gridLayout->addWidget(mCountryCombo, 6, 1);
103 mPreferredCheckBox =
new QCheckBox(
i18nc(
"street/postal",
"This is the preferred address"),
this);
105 gridLayout->addWidget(mPreferredCheckBox, 7, 0, 1, 2);
109 topLayout->addWidget(mButtonStack);
111 auto addButtonWidget =
new QWidget(
this);
112 auto addButtonWidgetLayout =
new QHBoxLayout(addButtonWidget);
113 addButtonWidgetLayout->setContentsMargins({});
114 mAddAddress =
new QPushButton(
i18nc(
"@action:button",
"Add Address"),
this);
117 addButtonWidgetLayout->addWidget(mAddAddress);
118 addButtonWidgetLayout->addStretch(1);
119 mButtonStack->addWidget(addButtonWidget);
121 auto modifyButtonWidget =
new QWidget(
this);
122 auto modifyButtonWidgetLayout =
new QHBoxLayout(modifyButtonWidget);
123 modifyButtonWidgetLayout->setContentsMargins({});
124 mButtonStack->addWidget(modifyButtonWidget);
126 mRemoveAddress =
new QPushButton(
i18nc(
"@action:button",
"Remove Address"),
this);
128 modifyButtonWidgetLayout->addWidget(mRemoveAddress);
131 mModifyAddress =
new QPushButton(
i18nc(
"@action:button",
"Update Address"),
this);
133 modifyButtonWidgetLayout->addWidget(mModifyAddress);
139 modifyButtonWidgetLayout->addWidget(mCancelAddress);
140 modifyButtonWidgetLayout->addStretch(1);
141 const int buttonWidth = qMax(mCancelAddress->width(), mModifyAddress->width());
142 mModifyAddress->setMinimumWidth(buttonWidth);
143 mCancelAddress->setMinimumWidth(buttonWidth);
145 topLayout->addStretch(1);
151AddressLocationWidget::~AddressLocationWidget() =
default;
153void AddressLocationWidget::slotChanged()
158bool AddressLocationWidget::wasChanged()
const
163void AddressLocationWidget::setReadOnly(
bool readOnly)
165 mPreferredCheckBox->setEnabled(!readOnly);
166 mPOBoxEdit->setReadOnly(readOnly);
167 mLocalityEdit->setReadOnly(readOnly);
168 mRegionEdit->setReadOnly(readOnly);
169 mPostalCodeEdit->setReadOnly(readOnly);
170 mStreetEdit->setReadOnly(readOnly);
171 mCountryCombo->setEnabled(!readOnly);
172 mTypeCombo->setEnabled(!readOnly);
174 mAddAddress->setEnabled(!readOnly);
175 mModifyAddress->setEnabled(!readOnly);
176 mCancelAddress->setEnabled(!readOnly);
179void AddressLocationWidget::fillCountryCombo()
181 QStringList countries;
186 mCountryCombo->addItems(countries);
189 mCountryCombo->setCurrentIndex(mCountryCombo->findText(currentCountry));
192void AddressLocationWidget::slotAddAddress()
194 const KContacts::Address addr = address();
195 if (!mLocalityEdit->text().trimmed().isEmpty() || !mRegionEdit->text().trimmed().isEmpty() || !mPostalCodeEdit->text().trimmed().isEmpty()
196 || !mStreetEdit->text().trimmed().isEmpty() || !mPOBoxEdit->text().trimmed().isEmpty()) {
197 Q_EMIT addNewAddress(addr);
202void AddressLocationWidget::setAddress(
const KContacts::Address &address)
205 mTypeCombo->setType(mAddress.type());
206 mStreetEdit->setText(mAddress.street());
207 mRegionEdit->setText(address.region());
208 mLocalityEdit->setText(address.locality());
209 mPostalCodeEdit->setText(address.postalCode());
210 mPOBoxEdit->setText(address.postOfficeBox());
212 if (address.isEmpty()) {
215 mCountryCombo->setItemText(mCountryCombo->currentIndex(), mAddress.country());
219KContacts::Address AddressLocationWidget::address()
const
221 KContacts::Address address(mAddress);
223 address.setType(mTypeCombo->type());
224 address.setLocality(mLocalityEdit->text());
225 address.setRegion(mRegionEdit->text());
226 address.setPostalCode(mPostalCodeEdit->text());
227 address.setCountry(mCountryCombo->currentText());
228 address.setPostOfficeBox(mPOBoxEdit->text());
229 address.setStreet(mStreetEdit->text());
230 if (mPreferredCheckBox->isChecked()) {
238void AddressLocationWidget::switchMode()
240 switch (mCurrentMode) {
242 mButtonStack->setCurrentIndex(0);
245 mButtonStack->setCurrentIndex(1);
250void AddressLocationWidget::slotModifyAddress(
const KContacts::Address &address,
int currentIndex)
253 mCurrentMode = ModifyAddress;
254 mCurrentAddress = currentIndex;
258void AddressLocationWidget::clear()
260 mCurrentMode = CreateAddress;
261 setAddress(KContacts::Address());
265void AddressLocationWidget::slotUpdateAddress()
267 if (mCurrentMode == ModifyAddress) {
268 Q_EMIT updateAddress(address(), mCurrentAddress);
273void AddressLocationWidget::slotRemoveAddress()
275 if (mCurrentMode == ModifyAddress) {
277 i18n(
"Do you really want to delete this address?"),
281 if (result == KMessageBox::ButtonCode::PrimaryAction) {
282 Q_EMIT removeAddress(mCurrentAddress);
288void AddressLocationWidget::reset()
290 Q_EMIT updateAddressCanceled();
291 mCurrentAddress = -1;
296void AddressLocationWidget::slotCancelModifyAddress()
301#include "moc_addresslocationwidget.cpp"
A widget for selecting the type of an address.
static QList< KCountry > allCountries()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
QStringView country(QStringView ifopt)
QString label(StandardShortcut id)
void textHighlighted(const QString &text)
void addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment)
void append(QList< T > &&value)
QString territoryToString(Territory territory)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)