Akonadi Contacts

addresslocationwidget.cpp
1/*
2 This file is part of Contact Editor.
3
4 SPDX-FileCopyrightText: 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
5 SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "addresslocationwidget.h"
11#include "selectaddresstypecombobox.h"
12
13#include <KComboBox>
14#include <KLineEdit>
15#include <KLocalizedString>
16#include <KMessageBox>
17
18#include <KCountry>
19#include <QCheckBox>
20#include <QLabel>
21#include <QPushButton>
22#include <QStackedWidget>
23#include <QVBoxLayout>
24
25using namespace Akonadi;
26
27AddressLocationWidget::AddressLocationWidget(QWidget *parent)
28 : QWidget(parent)
29 , mCurrentMode(CreateAddress)
30{
31 auto topLayout = new QVBoxLayout(this);
32
33 auto gridLayout = new QGridLayout;
34 topLayout->addLayout(gridLayout);
35
36 mTypeCombo = new SelectAddressTypeComboBox(this);
37 mTypeCombo->setObjectName(QLatin1StringView("typeaddress"));
38 connect(mTypeCombo, &SelectAddressTypeComboBox::textHighlighted, this, &AddressLocationWidget::slotChanged);
39 gridLayout->addWidget(mTypeCombo, 0, 0, 1, 2);
40
42 label->setObjectName(QLatin1StringView("streetlabel"));
43 gridLayout->addWidget(label, 1, 0);
44
45 mStreetEdit = new KLineEdit(this);
46 mStreetEdit->setPlaceholderText(i18n("Add Street"));
47 mStreetEdit->setObjectName(QLatin1StringView("streetlineedit"));
48 mStreetEdit->setTrapReturnKey(true);
49 connect(mStreetEdit, &KLineEdit::editingFinished, this, &AddressLocationWidget::slotChanged);
50 gridLayout->addWidget(mStreetEdit, 2, 0);
51
53 label->setObjectName(QLatin1StringView("postofficeboxlabel"));
54 gridLayout->addWidget(label, 1, 1);
55
56 mPOBoxEdit = new KLineEdit(this);
57 mPOBoxEdit->setPlaceholderText(i18n("Add Post Office Box"));
58 mPOBoxEdit->setObjectName(QLatin1StringView("postofficeboxlineedit"));
59 mPOBoxEdit->setTrapReturnKey(true);
60 connect(mPOBoxEdit, &KLineEdit::editingFinished, this, &AddressLocationWidget::slotChanged);
61 gridLayout->addWidget(mPOBoxEdit, 2, 1);
62
64 label->setObjectName(QLatin1StringView("postalcodelabel"));
65 gridLayout->addWidget(label, 3, 0);
66 mPostalCodeEdit = new KLineEdit(this);
67 mPostalCodeEdit->setPlaceholderText(i18n("Add Postal Code"));
68 mPostalCodeEdit->setObjectName(QLatin1StringView("postalcodelineedit"));
69 mPostalCodeEdit->setTrapReturnKey(true);
70 connect(mPostalCodeEdit, &KLineEdit::editingFinished, this, &AddressLocationWidget::slotChanged);
71 gridLayout->addWidget(mPostalCodeEdit, 4, 0);
72
74 label->setObjectName(QLatin1StringView("localitylabel"));
75 gridLayout->addWidget(label, 3, 1);
76 mLocalityEdit = new KLineEdit(this);
77 mLocalityEdit->setPlaceholderText(i18n("Add Locality"));
78 mLocalityEdit->setObjectName(QLatin1StringView("localitylineedit"));
79 mLocalityEdit->setTrapReturnKey(true);
80 connect(mLocalityEdit, &KLineEdit::editingFinished, this, &AddressLocationWidget::slotChanged);
81 gridLayout->addWidget(mLocalityEdit, 4, 1);
82
84 label->setObjectName(QLatin1StringView("regionlabel"));
85 gridLayout->addWidget(label, 5, 0);
86 mRegionEdit = new KLineEdit(this);
87 mRegionEdit->setPlaceholderText(i18n("Add Region"));
88 mRegionEdit->setObjectName(QLatin1StringView("regionlineedit"));
89 mRegionEdit->setTrapReturnKey(true);
90 connect(mRegionEdit, &KLineEdit::editingFinished, this, &AddressLocationWidget::slotChanged);
91 gridLayout->addWidget(mRegionEdit, 6, 0);
92
94 label->setObjectName(QLatin1StringView("countrylabel"));
95 gridLayout->addWidget(label, 5, 1);
96 mCountryCombo = new QComboBox(this);
97 mCountryCombo->setObjectName(QLatin1StringView("countrycombobox"));
98 mCountryCombo->setEditable(false);
99 mCountryCombo->setDuplicatesEnabled(false);
100 connect(mCountryCombo, &KComboBox::textHighlighted, this, &AddressLocationWidget::slotChanged);
101 gridLayout->addWidget(mCountryCombo, 6, 1);
102
103 mPreferredCheckBox = new QCheckBox(i18nc("street/postal", "This is the preferred address"), this);
104 mPreferredCheckBox->setObjectName(QLatin1StringView("preferredcheckbox"));
105 gridLayout->addWidget(mPreferredCheckBox, 7, 0, 1, 2);
106
107 mButtonStack = new QStackedWidget(this);
108 mButtonStack->setObjectName(QLatin1StringView("buttonstacked"));
109 topLayout->addWidget(mButtonStack);
110
111 auto addButtonWidget = new QWidget(this);
112 auto addButtonWidgetLayout = new QHBoxLayout(addButtonWidget);
113 addButtonWidgetLayout->setContentsMargins({});
114 mAddAddress = new QPushButton(i18n("Add Address"), this);
115 mAddAddress->setObjectName(QLatin1StringView("addbuttonaddress"));
116 connect(mAddAddress, &QPushButton::clicked, this, &AddressLocationWidget::slotAddAddress);
117 addButtonWidgetLayout->addWidget(mAddAddress);
118 addButtonWidgetLayout->addStretch(1);
119 mButtonStack->addWidget(addButtonWidget);
120
121 auto modifyButtonWidget = new QWidget(this);
122 auto modifyButtonWidgetLayout = new QHBoxLayout(modifyButtonWidget);
123 modifyButtonWidgetLayout->setContentsMargins({});
124 mButtonStack->addWidget(modifyButtonWidget);
125
126 mRemoveAddress = new QPushButton(i18n("Remove Address"), this);
127 mRemoveAddress->setObjectName(QLatin1StringView("removebuttonaddress"));
128 modifyButtonWidgetLayout->addWidget(mRemoveAddress);
129 connect(mRemoveAddress, &QPushButton::clicked, this, &AddressLocationWidget::slotRemoveAddress);
130
131 mModifyAddress = new QPushButton(i18n("Update Address"), this);
132 mModifyAddress->setObjectName(QLatin1StringView("modifybuttonaddress"));
133 modifyButtonWidgetLayout->addWidget(mModifyAddress);
134 connect(mModifyAddress, &QPushButton::clicked, this, &AddressLocationWidget::slotUpdateAddress);
135
136 mCancelAddress = new QPushButton(i18n("Cancel"), this);
137 mCancelAddress->setObjectName(QLatin1StringView("cancelbuttonaddress"));
138 connect(mCancelAddress, &QPushButton::clicked, this, &AddressLocationWidget::slotCancelModifyAddress);
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);
144
145 topLayout->addStretch(1);
146 fillCountryCombo();
147 switchMode();
148}
149
150AddressLocationWidget::~AddressLocationWidget() = default;
151
152void AddressLocationWidget::slotChanged()
153{
154 mWasChanged = true;
155}
156
157bool AddressLocationWidget::wasChanged() const
158{
159 return mWasChanged;
160}
161
162void AddressLocationWidget::setReadOnly(bool readOnly)
163{
164 mPreferredCheckBox->setEnabled(!readOnly);
165 mPOBoxEdit->setReadOnly(readOnly);
166 mLocalityEdit->setReadOnly(readOnly);
167 mRegionEdit->setReadOnly(readOnly);
168 mPostalCodeEdit->setReadOnly(readOnly);
169 mStreetEdit->setReadOnly(readOnly);
170 mCountryCombo->setEnabled(!readOnly);
171 mTypeCombo->setEnabled(!readOnly);
172
173 mAddAddress->setEnabled(!readOnly);
174 mModifyAddress->setEnabled(!readOnly);
175 mCancelAddress->setEnabled(!readOnly);
176}
177
178void AddressLocationWidget::fillCountryCombo()
179{
180 QStringList countries;
181 for (const auto &country : KCountry::allCountries()) {
182 countries.append(country.name());
183 }
184
185 mCountryCombo->addItems(countries);
186
187 const QString currentCountry = QLocale::territoryToString(QLocale().territory());
188 mCountryCombo->setCurrentIndex(mCountryCombo->findText(currentCountry));
189}
190
191void AddressLocationWidget::slotAddAddress()
192{
193 const KContacts::Address addr = address();
194 if (!mLocalityEdit->text().trimmed().isEmpty() || !mRegionEdit->text().trimmed().isEmpty() || !mPostalCodeEdit->text().trimmed().isEmpty()
195 || !mStreetEdit->text().trimmed().isEmpty() || !mPOBoxEdit->text().trimmed().isEmpty()) {
196 Q_EMIT addNewAddress(addr);
197 reset();
198 }
199}
200
201void AddressLocationWidget::setAddress(const KContacts::Address &address)
202{
203 mAddress = address;
204 mTypeCombo->setType(mAddress.type());
205 mStreetEdit->setText(mAddress.street());
206 mRegionEdit->setText(address.region());
207 mLocalityEdit->setText(address.locality());
208 mPostalCodeEdit->setText(address.postalCode());
209 mPOBoxEdit->setText(address.postOfficeBox());
210 mPreferredCheckBox->setChecked(address.type() & KContacts::Address::Pref);
211 if (address.isEmpty()) {
212 mCountryCombo->setItemText(mCountryCombo->currentIndex(), QLocale::territoryToString(QLocale().territory()));
213 } else {
214 mCountryCombo->setItemText(mCountryCombo->currentIndex(), mAddress.country());
215 }
216}
217
218KContacts::Address AddressLocationWidget::address() const
219{
220 KContacts::Address address(mAddress);
221
222 address.setType(mTypeCombo->type());
223 address.setLocality(mLocalityEdit->text());
224 address.setRegion(mRegionEdit->text());
225 address.setPostalCode(mPostalCodeEdit->text());
226 address.setCountry(mCountryCombo->currentText());
227 address.setPostOfficeBox(mPOBoxEdit->text());
228 address.setStreet(mStreetEdit->text());
229 if (mPreferredCheckBox->isChecked()) {
230 address.setType(address.type() | KContacts::Address::Pref);
231 } else {
232 address.setType(address.type() & ~(KContacts::Address::Pref));
233 }
234 return address;
235}
236
237void AddressLocationWidget::switchMode()
238{
239 switch (mCurrentMode) {
240 case CreateAddress:
241 mButtonStack->setCurrentIndex(0);
242 break;
243 case ModifyAddress:
244 mButtonStack->setCurrentIndex(1);
245 break;
246 }
247}
248
249void AddressLocationWidget::slotModifyAddress(const KContacts::Address &address, int currentIndex)
250{
251 setAddress(address);
252 mCurrentMode = ModifyAddress;
253 mCurrentAddress = currentIndex;
254 switchMode();
255}
256
257void AddressLocationWidget::clear()
258{
259 mCurrentMode = CreateAddress;
260 setAddress(KContacts::Address());
261 switchMode();
262}
263
264void AddressLocationWidget::slotUpdateAddress()
265{
266 if (mCurrentMode == ModifyAddress) {
267 Q_EMIT updateAddress(address(), mCurrentAddress);
268 reset();
269 }
270}
271
272void AddressLocationWidget::slotRemoveAddress()
273{
274 if (mCurrentMode == ModifyAddress) {
275 const auto result = KMessageBox::questionTwoActions(this,
276 i18n("Do you really want to delete this address?"),
277 QString(),
280 if (result == KMessageBox::ButtonCode::PrimaryAction) {
281 Q_EMIT removeAddress(mCurrentAddress);
282 reset();
283 }
284 }
285}
286
287void AddressLocationWidget::reset()
288{
289 Q_EMIT updateAddressCanceled();
290 mCurrentAddress = -1;
291 mWasChanged = false;
292 clear();
293}
294
295void AddressLocationWidget::slotCancelModifyAddress()
296{
297 reset();
298}
299
300#include "moc_addresslocationwidget.cpp"
A widget for selecting the type of an address.
void setType(KContacts::Address::Type type)
Sets the type that shall be selected in the combobox.
KContacts::Address::Type type() const
Returns the type that is currently selected.
QString street() const
static QString streetLabel()
static QString countryLabel()
QString country() const
Type type() const
static QString regionLabel()
static QString postalCodeLabel()
static QString postOfficeBoxLabel()
static QString localityLabel()
QString name() const
virtual void setReadOnly(bool)
virtual void setText(const QString &)
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.
PostalAddress address(const QVariant &location)
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
KGuiItem cancel()
KGuiItem del()
QString label(StandardShortcut id)
KI18NLOCALEDATA_EXPORT KCountry country(const char *ianaId)
void setChecked(bool)
void clicked(bool checked)
void addItems(const QStringList &texts)
void setCurrentIndex(int index)
int findText(const QString &text, Qt::MatchFlags flags) const const
void setItemText(int index, const QString &text)
void textHighlighted(const QString &text)
void addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment)
void editingFinished()
void append(QList< T > &&value)
QString territoryToString(Territory territory)
Q_EMITQ_EMIT
void setCurrentIndex(int index)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:20 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.