Akonadi Contacts

customfieldeditorwidget.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 "customfieldeditorwidget.h"
11 #include "customfieldsmodel.h"
12 #include <KContacts/Addressee>
13 #include <KLocalizedString>
14 #include <QCheckBox>
15 #include <QComboBox>
16 #include <QLabel>
17 #include <QLineEdit>
18 #include <QPushButton>
19 #include <QUuid>
20 #include <QVBoxLayout>
21 
22 using namespace ContactEditor;
23 
24 CustomFieldEditorWidget::CustomFieldEditorWidget(QWidget *parent)
25  : QWidget(parent)
26 {
27  auto topLayout = new QVBoxLayout(this);
28  auto label = new QLabel(i18n("Custom Field Title"), this);
29  label->setObjectName(QStringLiteral("labeltitle"));
30 
31  topLayout->addWidget(label);
32 
33  auto fieldLayout = new QHBoxLayout;
34  topLayout->addLayout(fieldLayout);
35  mFieldName = new QLineEdit(this);
36  mFieldName->setPlaceholderText(i18n("Add name"));
37  mFieldName->setObjectName(QStringLiteral("fieldname"));
38  fieldLayout->addWidget(mFieldName);
39 
40  mFieldType = new QComboBox(this);
41  mFieldType->setObjectName(QStringLiteral("fieldtype"));
42  fieldLayout->addWidget(mFieldType);
43  fillTypes();
44 
45  mAddField = new QPushButton(i18n("Add Field"), this);
46  mAddField->setObjectName(QStringLiteral("addfield"));
47  fieldLayout->addWidget(mAddField);
48  mAddField->setEnabled(false); // Disable at the beginning
49  connect(mAddField, &QPushButton::clicked, this, &CustomFieldEditorWidget::slotAddField);
50  connect(mFieldName, &QLineEdit::textChanged, this, &CustomFieldEditorWidget::slotFielNameChanged);
51 
52  mUseAllContacts = new QCheckBox(i18n("Use field for all contacts"));
53  mUseAllContacts->setObjectName(QStringLiteral("useallcontact"));
54  topLayout->addWidget(mUseAllContacts);
55 }
56 
57 CustomFieldEditorWidget::~CustomFieldEditorWidget()
58 {
59 }
60 
61 void CustomFieldEditorWidget::fillTypes()
62 {
63  mFieldType->addItem(i18n("Text"), CustomField::TextType);
64  mFieldType->addItem(i18n("Numeric"), CustomField::NumericType);
65  mFieldType->addItem(i18n("Boolean"), CustomField::BooleanType);
66  mFieldType->addItem(i18n("Date"), CustomField::DateType);
67  mFieldType->addItem(i18n("Time"), CustomField::TimeType);
68  mFieldType->addItem(i18n("DateTime"), CustomField::DateTimeType);
69  mFieldType->addItem(i18n("Url"), CustomField::UrlType);
70 }
71 
72 void CustomFieldEditorWidget::setReadOnly(bool readOnly)
73 {
74  mFieldName->setReadOnly(readOnly);
75  mAddField->setEnabled(!readOnly && !mFieldName->text().trimmed().isEmpty());
76  mFieldType->setEnabled(!readOnly);
77  mUseAllContacts->setEnabled(!readOnly);
78 }
79 
80 void CustomFieldEditorWidget::slotFielNameChanged(const QString &text)
81 {
82  mAddField->setEnabled(!text.trimmed().isEmpty());
83 }
84 
85 void CustomFieldEditorWidget::slotAddField()
86 {
87  if (mFieldName->text().trimmed().isEmpty()) {
88  return;
89  }
90  CustomField field;
91  // We use a Uuid as default key, so we won't have any duplicated keys,
92  // the user can still change it to something else in the editor dialog.
93  // Since the key only allows [A-Za-z0-9\-]*, we have to remove the curly
94  // braces as well.
96  key.remove(QLatin1Char('{'));
97  key.remove(QLatin1Char('}'));
98 
99  field.setKey(key);
100  field.setTitle(mFieldName->text());
101  field.setType(static_cast<CustomField::Type>(mFieldType->itemData(mFieldType->currentIndex()).toInt()));
102 
103  // TODO verify it
104  if (field.scope() != CustomField::ExternalScope) {
105  // do not change the scope for externally defined custom fields
106  field.setScope(mUseAllContacts->isChecked() ? CustomField::GlobalScope : CustomField::LocalScope);
107  }
108  Q_EMIT addNewField(field);
109  mFieldName->clear();
110  mFieldType->setCurrentIndex(0);
111 }
void textChanged(const QString &text)
QString & remove(int position, int n)
QString label(StandardShortcut id)
bool isEmpty() const const
QString trimmed() const const
void clicked(bool checked)
QString i18n(const char *text, const TYPE &arg...)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toString() const const
QUuid createUuid()
Q_EMITQ_EMIT
void addLayout(QLayout *layout, int stretch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Jun 22 2021 23:08:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.