Akonadi Contacts

phonewidget.cpp
1 /*
2  This file is part of Contact Editor.
3 
4  SPDX-FileCopyrightText: 2016 eyeOS S.L.U., a Telefonica company, [email protected]
5  SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel.org>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "phonewidget.h"
11 #include "editor/widgets/preferredlineeditwidget.h"
12 #include "phonecomboboxtype.h"
13 
14 #include <KLocalizedString>
15 
16 #include <QHBoxLayout>
17 #include <QToolButton>
18 
19 using namespace ContactEditor;
20 PhoneWidget::PhoneWidget(QWidget *parent)
21  : QWidget(parent)
22 {
23  auto layout = new QHBoxLayout(this);
24  layout->setSpacing(0);
25  layout->setContentsMargins(0, 0, 0, 0);
26 
27  mPhoneNumberEdit = new PreferredLineEditWidget(this);
28  mPhoneNumberEdit->setTrapReturnKey(true);
29  mPhoneNumberEdit->setPlaceholderText(i18n("Add a Phone Number"));
30  mPhoneNumberEdit->setObjectName(QStringLiteral("phonenumber"));
31  layout->addWidget(mPhoneNumberEdit);
32  connect(mPhoneNumberEdit, &PreferredLineEditWidget::preferredChanged, this, &PhoneWidget::slotPreferredChanged);
33 
34  mPhoneType = new PhoneComboBoxType(this);
35  mPhoneType->setObjectName(QStringLiteral("phonetype"));
36  layout->addWidget(mPhoneType);
37 
38  mAddButton = new QToolButton(this);
39  mAddButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
40  mAddButton->setToolTip(i18n("Add a Phone Number"));
41  mAddButton->setObjectName(QStringLiteral("addbutton"));
42  connect(mAddButton, &QToolButton::clicked, this, &PhoneWidget::slotAddPhone);
43  layout->addWidget(mAddButton);
44 
45  mRemoveButton = new QToolButton(this);
46  mRemoveButton->setObjectName(QStringLiteral("removebutton"));
47  mRemoveButton->setToolTip(i18n("Remove Phone Number"));
48  mRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
49  connect(mRemoveButton, &QToolButton::clicked, this, &PhoneWidget::slotRemovePhone);
50  layout->addWidget(mRemoveButton);
51 }
52 
53 PhoneWidget::~PhoneWidget()
54 {
55 }
56 
57 void PhoneWidget::updateAddRemoveButton(bool addButtonEnabled)
58 {
59  mAddButton->setEnabled(addButtonEnabled);
60 }
61 
62 KContacts::PhoneNumber PhoneWidget::storePhone()
63 {
65  number.setNumber(mPhoneNumberEdit->text());
66  KContacts::PhoneNumber::Type currentType = mPhoneType->type();
67  if (mPhoneNumberEdit->preferred()) {
68  currentType |= KContacts::PhoneNumber::Pref;
69  }
70  number.setType(currentType);
71  return number;
72 }
73 
74 void PhoneWidget::loadPhone(const KContacts::PhoneNumber &number)
75 {
76  mPhoneNumberEdit->setText(number.number());
77  KContacts::PhoneNumber::Type currentType = number.type();
78  mPhoneType->setType(number.type() & ~KContacts::PhoneNumber::Pref);
79  mPhoneNumberEdit->setPreferred(currentType & KContacts::PhoneNumber::Pref);
80 }
81 
82 void PhoneWidget::slotAddPhone()
83 {
84  Q_EMIT addWidget(this);
85 }
86 
87 void PhoneWidget::slotRemovePhone()
88 {
89  Q_EMIT removeWidget(this);
90 }
91 
92 void PhoneWidget::clearWidget()
93 {
94  mPhoneNumberEdit->clear();
95  mPhoneType->resetToDefault();
96 }
97 
98 void PhoneWidget::slotPreferredChanged()
99 {
100  Q_EMIT preferredChanged(this);
101 }
102 
103 void PhoneWidget::setPreferred(bool b)
104 {
105  mPhoneNumberEdit->setPreferred(b);
106 }
107 
108 void PhoneWidget::setReadOnly(bool readOnly)
109 {
110  mPhoneNumberEdit->setReadOnly(readOnly);
111  mAddButton->setEnabled(!readOnly);
112  mRemoveButton->setEnabled(!readOnly);
113  mPhoneType->setEnabled(!readOnly);
114 }
QLayout * layout() const const
void setNumber(const QString &number)
void setContentsMargins(int left, int top, int right, int bottom)
void setType(Type type)
QString number() const
KIOCORE_EXPORT QString number(KIO::filesize_t size)
void clicked(bool checked)
void addWidget(QWidget *w)
A combobox to select a phone number type.
QString i18n(const char *text, const TYPE &arg...)
void setSpacing(int)
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.