Akonadi Contacts

namewidget.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 "namewidget.h"
11#include "nameeditdialog.h"
12#include <KLineEdit>
13#include <KLocalizedString>
14#include <QLabel>
15#include <QLineEdit>
16#include <QToolButton>
17#include <QVBoxLayout>
18
19using namespace Akonadi;
20
21NameWidget::NameWidget(QWidget *parent)
22 : QWidget(parent)
23 , mNameEdit(new KLineEdit(this))
24 , mButtonEdit(new QToolButton(this))
25{
26 auto topLayout = new QVBoxLayout(this);
27 topLayout->setContentsMargins({});
28 auto label = new QLabel(i18nc("@label:textbox", "Name"));
29 label->setObjectName(QLatin1StringView("namelabel"));
30 topLayout->addWidget(label);
31
32 auto lineLayout = new QHBoxLayout;
33 lineLayout->setContentsMargins({});
34 topLayout->addLayout(lineLayout);
35
36 mNameEdit->setTrapReturnKey(true);
37 lineLayout->addWidget(mNameEdit);
38 setFocusProxy(mNameEdit);
39 setFocusPolicy(Qt::StrongFocus);
40
41 mButtonEdit->setText(i18n("..."));
42 mButtonEdit->setToolTip(i18nc("@info:tooltip", "Edit Contact Name"));
43 lineLayout->addWidget(mButtonEdit);
44
45 connect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
46 connect(mButtonEdit, &QToolButton::clicked, this, &NameWidget::slotOpenNameEditDialog);
47}
48
49NameWidget::~NameWidget() = default;
50
51void NameWidget::setReadOnly(bool readOnly)
52{
53 mNameEdit->setReadOnly(readOnly);
54 mButtonEdit->setEnabled(!readOnly);
55}
56
57void NameWidget::setDisplayType(DisplayNameEditWidget::DisplayType type)
58{
59 mDisplayType = type;
60}
61
62DisplayNameEditWidget::DisplayType NameWidget::displayType() const
63{
64 return mDisplayType;
65}
66
67void NameWidget::loadContact(const KContacts::Addressee &contact)
68{
69 mContact = contact;
70
71 disconnect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
72 mNameEdit->setText(contact.assembledName());
73 connect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
74}
75
76void NameWidget::storeContact(KContacts::Addressee &contact) const
77{
78 contact.setPrefix(mContact.prefix());
79 contact.setGivenName(mContact.givenName());
80 contact.setAdditionalName(mContact.additionalName());
81 contact.setFamilyName(mContact.familyName());
82 contact.setSuffix(mContact.suffix());
83 contact.setFormattedName(mContact.formattedName());
84}
85
86void NameWidget::slotTextChanged(const QString &text)
87{
88 mContact.setNameFromString(text);
89
90 Q_EMIT nameChanged(mContact);
91}
92
93void NameWidget::slotOpenNameEditDialog()
94{
95 QPointer<NameEditDialog> dlg = new NameEditDialog(this);
96 dlg->loadContact(mContact);
97 dlg->setDisplayType(mDisplayType);
98
99 if (dlg->exec() == QDialog::Accepted) {
100 dlg->storeContact(mContact);
101 mDisplayType = dlg->displayType();
102 disconnect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
103 mNameEdit->setText(mContact.assembledName());
104 connect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
105
106 Q_EMIT nameChanged(mContact);
107 }
108
109 delete dlg;
110}
111
112#include "moc_namewidget.cpp"
void setNameFromString(const QString &s)
void setAdditionalName(const QString &additionalName)
QString familyName() const
void setPrefix(const QString &prefix)
QString prefix() const
void setSuffix(const QString &suffix)
QString suffix() const
void setGivenName(const QString &givenName)
QString additionalName() const
QString assembledName() const
QString formattedName() const
QString givenName() const
void setFormattedName(const QString &formattedName)
void setFamilyName(const QString &familyName)
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...)
Type type(const QSqlDatabase &db)
A widget for editing the display name of a contact.
QString label(StandardShortcut id)
void clicked(bool checked)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
StrongFocus
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 Mon Nov 18 2024 12:08:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.