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, [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 "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 
19 using namespace ContactEditor;
20 
21 NameWidget::NameWidget(QWidget *parent)
22  : QWidget(parent)
23 {
24  auto topLayout = new QVBoxLayout(this);
25  topLayout->setContentsMargins(0, 0, 0, 0);
26  auto label = new QLabel(i18n("Name"));
27  label->setObjectName(QStringLiteral("namelabel"));
28  topLayout->addWidget(label);
29 
30  auto lineLayout = new QHBoxLayout;
31  lineLayout->setContentsMargins(0, 0, 0, 0);
32  topLayout->addLayout(lineLayout);
33 
34  mNameEdit = new KLineEdit(this);
35  mNameEdit->setTrapReturnKey(true);
36  lineLayout->addWidget(mNameEdit);
37  setFocusProxy(mNameEdit);
38  setFocusPolicy(Qt::StrongFocus);
39 
40  mButtonEdit = new QToolButton(this);
41  mButtonEdit->setText(i18n("..."));
42  mButtonEdit->setToolTip(i18n("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 
49 NameWidget::~NameWidget()
50 {
51 }
52 
53 void NameWidget::setReadOnly(bool readOnly)
54 {
55  mNameEdit->setReadOnly(readOnly);
56  mButtonEdit->setEnabled(!readOnly);
57 }
58 
59 void NameWidget::setDisplayType(DisplayNameEditWidget::DisplayType type)
60 {
61  mDisplayType = type;
62 }
63 
64 DisplayNameEditWidget::DisplayType NameWidget::displayType() const
65 {
66  return mDisplayType;
67 }
68 
69 void NameWidget::loadContact(const KContacts::Addressee &contact)
70 {
71  mContact = contact;
72 
73  disconnect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
74  mNameEdit->setText(contact.assembledName());
75  connect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
76 }
77 
78 void NameWidget::storeContact(KContacts::Addressee &contact) const
79 {
80  contact.setPrefix(mContact.prefix());
81  contact.setGivenName(mContact.givenName());
82  contact.setAdditionalName(mContact.additionalName());
83  contact.setFamilyName(mContact.familyName());
84  contact.setSuffix(mContact.suffix());
85  contact.setFormattedName(mContact.formattedName());
86 }
87 
88 void NameWidget::slotTextChanged(const QString &text)
89 {
90  mContact.setNameFromString(text);
91 
92  Q_EMIT nameChanged(mContact);
93 }
94 
95 void NameWidget::slotOpenNameEditDialog()
96 {
97  QPointer<NameEditDialog> dlg = new NameEditDialog(this);
98  dlg->loadContact(mContact);
99  dlg->setDisplayType(mDisplayType);
100 
101  if (dlg->exec() == QDialog::Accepted) {
102  dlg->storeContact(mContact);
103  mDisplayType = dlg->displayType();
104  disconnect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
105  mNameEdit->setText(mContact.assembledName());
106  connect(mNameEdit, &QLineEdit::textChanged, this, &NameWidget::slotTextChanged);
107 
108  Q_EMIT nameChanged(mContact);
109  }
110 
111  delete dlg;
112 }
void setFormattedName(const QString &formattedName)
StrongFocus
void setContentsMargins(int left, int top, int right, int bottom)
void setSuffix(const QString &suffix)
void setPrefix(const QString &prefix)
QString assembledName() const
void textChanged(const QString &text)
void setGivenName(const QString &givenName)
void setAdditionalName(const QString &additionalName)
DisplayType
Describes what the display name should look like.
QString label(StandardShortcut id)
void setFamilyName(const QString &familyName)
void clicked(bool checked)
QString i18n(const char *text, const TYPE &arg...)
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.