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

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 "namewidget.h"
0011 #include "nameeditdialog.h"
0012 #include <KLineEdit>
0013 #include <KLocalizedString>
0014 #include <QLabel>
0015 #include <QLineEdit>
0016 #include <QToolButton>
0017 #include <QVBoxLayout>
0018 
0019 using namespace Akonadi;
0020 
0021 NameWidget::NameWidget(QWidget *parent)
0022     : QWidget(parent)
0023     , mNameEdit(new KLineEdit(this))
0024     , mButtonEdit(new QToolButton(this))
0025 {
0026     auto topLayout = new QVBoxLayout(this);
0027     topLayout->setContentsMargins({});
0028     auto label = new QLabel(i18n("Name"));
0029     label->setObjectName(QLatin1StringView("namelabel"));
0030     topLayout->addWidget(label);
0031 
0032     auto lineLayout = new QHBoxLayout;
0033     lineLayout->setContentsMargins({});
0034     topLayout->addLayout(lineLayout);
0035 
0036     mNameEdit->setTrapReturnKey(true);
0037     lineLayout->addWidget(mNameEdit);
0038     setFocusProxy(mNameEdit);
0039     setFocusPolicy(Qt::StrongFocus);
0040 
0041     mButtonEdit->setText(i18n("..."));
0042     mButtonEdit->setToolTip(i18n("Edit Contact Name"));
0043     lineLayout->addWidget(mButtonEdit);
0044 
0045     connect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
0046     connect(mButtonEdit, &QToolButton::clicked, this, &NameWidget::slotOpenNameEditDialog);
0047 }
0048 
0049 NameWidget::~NameWidget() = default;
0050 
0051 void NameWidget::setReadOnly(bool readOnly)
0052 {
0053     mNameEdit->setReadOnly(readOnly);
0054     mButtonEdit->setEnabled(!readOnly);
0055 }
0056 
0057 void NameWidget::setDisplayType(DisplayNameEditWidget::DisplayType type)
0058 {
0059     mDisplayType = type;
0060 }
0061 
0062 DisplayNameEditWidget::DisplayType NameWidget::displayType() const
0063 {
0064     return mDisplayType;
0065 }
0066 
0067 void NameWidget::loadContact(const KContacts::Addressee &contact)
0068 {
0069     mContact = contact;
0070 
0071     disconnect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
0072     mNameEdit->setText(contact.assembledName());
0073     connect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
0074 }
0075 
0076 void NameWidget::storeContact(KContacts::Addressee &contact) const
0077 {
0078     contact.setPrefix(mContact.prefix());
0079     contact.setGivenName(mContact.givenName());
0080     contact.setAdditionalName(mContact.additionalName());
0081     contact.setFamilyName(mContact.familyName());
0082     contact.setSuffix(mContact.suffix());
0083     contact.setFormattedName(mContact.formattedName());
0084 }
0085 
0086 void NameWidget::slotTextChanged(const QString &text)
0087 {
0088     mContact.setNameFromString(text);
0089 
0090     Q_EMIT nameChanged(mContact);
0091 }
0092 
0093 void NameWidget::slotOpenNameEditDialog()
0094 {
0095     QPointer<NameEditDialog> dlg = new NameEditDialog(this);
0096     dlg->loadContact(mContact);
0097     dlg->setDisplayType(mDisplayType);
0098 
0099     if (dlg->exec() == QDialog::Accepted) {
0100         dlg->storeContact(mContact);
0101         mDisplayType = dlg->displayType();
0102         disconnect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
0103         mNameEdit->setText(mContact.assembledName());
0104         connect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
0105 
0106         Q_EMIT nameChanged(mContact);
0107     }
0108 
0109     delete dlg;
0110 }
0111 
0112 #include "moc_namewidget.cpp"