File indexing completed on 2024-11-24 04:39:31

0001 /*
0002     This file is part of Contact Editor.
0003 
0004     SPDX-FileCopyrightText: 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
0005     SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "phonewidget.h"
0011 #include "editor/widgets/preferredlineeditwidget.h"
0012 #include "phonecomboboxtype.h"
0013 
0014 #include <KLocalizedString>
0015 
0016 #include <QHBoxLayout>
0017 #include <QToolButton>
0018 
0019 using namespace Akonadi;
0020 PhoneWidget::PhoneWidget(QWidget *parent)
0021     : QWidget(parent)
0022     , mPhoneNumberEdit(new PreferredLineEditWidget(this))
0023     , mAddButton(new QToolButton(this))
0024     , mRemoveButton(new QToolButton(this))
0025     , mPhoneType(new PhoneComboBoxType(this))
0026 {
0027     auto layout = new QHBoxLayout(this);
0028     layout->setSpacing(0);
0029     layout->setContentsMargins({});
0030 
0031     mPhoneNumberEdit->setTrapReturnKey(true);
0032     mPhoneNumberEdit->setPlaceholderText(i18n("Add a Phone Number"));
0033     mPhoneNumberEdit->setObjectName(QLatin1StringView("phonenumber"));
0034     layout->addWidget(mPhoneNumberEdit);
0035     connect(mPhoneNumberEdit, &PreferredLineEditWidget::preferredChanged, this, &PhoneWidget::slotPreferredChanged);
0036 
0037     mPhoneType->setObjectName(QLatin1StringView("phonetype"));
0038     layout->addWidget(mPhoneType);
0039 
0040     mAddButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0041     mAddButton->setToolTip(i18n("Add a Phone Number"));
0042     mAddButton->setObjectName(QLatin1StringView("addbutton"));
0043     connect(mAddButton, &QToolButton::clicked, this, &PhoneWidget::slotAddPhone);
0044     layout->addWidget(mAddButton);
0045 
0046     mRemoveButton->setObjectName(QLatin1StringView("removebutton"));
0047     mRemoveButton->setToolTip(i18n("Remove Phone Number"));
0048     mRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
0049     connect(mRemoveButton, &QToolButton::clicked, this, &PhoneWidget::slotRemovePhone);
0050     layout->addWidget(mRemoveButton);
0051 }
0052 
0053 PhoneWidget::~PhoneWidget() = default;
0054 
0055 void PhoneWidget::updateAddRemoveButton(bool addButtonEnabled)
0056 {
0057     mAddButton->setEnabled(addButtonEnabled);
0058 }
0059 
0060 KContacts::PhoneNumber PhoneWidget::storePhone()
0061 {
0062     KContacts::PhoneNumber number;
0063     number.setNumber(mPhoneNumberEdit->text());
0064     KContacts::PhoneNumber::Type currentType = mPhoneType->type();
0065     if (mPhoneNumberEdit->preferred()) {
0066         currentType |= KContacts::PhoneNumber::Pref;
0067     }
0068     number.setType(currentType);
0069     return number;
0070 }
0071 
0072 void PhoneWidget::loadPhone(const KContacts::PhoneNumber &number)
0073 {
0074     mPhoneNumberEdit->setText(number.number());
0075     KContacts::PhoneNumber::Type currentType = number.type();
0076     mPhoneType->setType(number.type() & ~KContacts::PhoneNumber::Pref);
0077     mPhoneNumberEdit->setPreferred(currentType & KContacts::PhoneNumber::Pref);
0078 }
0079 
0080 void PhoneWidget::slotAddPhone()
0081 {
0082     Q_EMIT addWidget(this);
0083 }
0084 
0085 void PhoneWidget::slotRemovePhone()
0086 {
0087     Q_EMIT removeWidget(this);
0088 }
0089 
0090 void PhoneWidget::clearWidget()
0091 {
0092     mPhoneNumberEdit->clear();
0093     mPhoneType->resetToDefault();
0094 }
0095 
0096 void PhoneWidget::slotPreferredChanged()
0097 {
0098     Q_EMIT preferredChanged(this);
0099 }
0100 
0101 void PhoneWidget::setPreferred(bool b)
0102 {
0103     mPhoneNumberEdit->setPreferred(b);
0104 }
0105 
0106 void PhoneWidget::setReadOnly(bool readOnly)
0107 {
0108     mPhoneNumberEdit->setReadOnly(readOnly);
0109     mAddButton->setEnabled(!readOnly);
0110     mRemoveButton->setEnabled(!readOnly);
0111     mPhoneType->setEnabled(!readOnly);
0112 }
0113 
0114 #include "moc_phonewidget.cpp"